<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item label="日期时间">
            <el-date-picker
                v-model="dateRange"
                type="datetimerange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd HH:mm:ss" style="width: 400px"
                :default-time="['00:00:00', '23:59:59']"
                :picker-options="pickerOptions" >
            </el-date-picker>
        </el-form-item>
        <el-form-item label="时间类型" prop="timeType">
            <el-select v-model="queryParams.timeType" style="width: 240px" clearable> 
                <el-option label="下单时间" :value="1"></el-option>
                <el-option label="支付时间" :value="2"></el-option> 
            </el-select>   
        </el-form-item>
        <el-form-item label="关键字"> 
            <el-input v-model="queryParams.keyword" placeholder="请输入关键字" maxlength="20" clearable style="width: 240px"/>
        </el-form-item> 
        <br>
        <el-form-item label="所属组织" prop="orgIdList">  
            <el-cascader v-model="queryParams.orgIdList"
            :options="deptOptions" :filterable="true" style="width: 240px" :show-all-levels="false"
            :props="{ 
                multiple: true,
                emitPath: false,// 若设置 false，则只返回该节点的值，只返回最后选择的id
                checkStrictly: false,//来设置父子节点取消选中关联，从而达到选择任意一级选项的目的
                value:'id',label:'label' 
            }" clearable collapse-tags>
            </el-cascader> 
        </el-form-item>
        <el-form-item label="用户类别">
            <el-select v-model="queryParams.psnTypeList" multiple style="width: 220px" clearable> 
                <el-option
                    v-for="dict in dict.type.user_psn_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="Number(dict.value)"
                    />
            </el-select>   
        </el-form-item>
        <el-form-item label="交易类型" prop="flowTypeList">
            <el-select v-model="queryParams.flowTypeList" multiple style="width: 240px" clearable> 
                <el-option label="消费" :value="110"></el-option>
                <el-option label="消费补扣" :value="120"></el-option> 
                <el-option label="消费退款" :value="130"></el-option> 
            </el-select>   
        </el-form-item>
        <el-form-item label="钱包类型" prop="accWalletIdList">
            <el-select v-model="queryParams.accWalletIdList" multiple style="width: 240px" clearable> 
                <el-option label="个人钱包" value="1"></el-option>
                <el-option label="补贴钱包" value="2"></el-option>
                <el-option label="红包" value="4"></el-option> 
            </el-select>   
        </el-form-item>
        <br>
        <!-- minAmount maxAmount -->
        <el-form-item label="消费金额">
            <el-input v-model="queryParams.minAmount"  placeholder="最小金额" clearable style="width: 40%;" 
                @input="(v)=>(queryParams.minAmount=Number(v.replace(/[^\d.]/g,'')))">
                <template slot="append">元</template>
            </el-input>
            <span>至</span>
            <el-input v-model="queryParams.maxAmount"  placeholder="最大金额" clearable style="width: 40%;" 
                @input="(v)=>(queryParams.maxAmount=Number(v.replace(/[^\d.]/g,'')))">
                <template slot="append">元</template>
            </el-input>
        </el-form-item> 
        <el-form-item label="是否在线" prop="ifOnlineList">
            <el-select v-model="queryParams.ifOnlineList" multiple style="width: 240px" clearable> 
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option> 
            </el-select>   
        </el-form-item>
        

        <!-- <el-form-item label="操作员">
            <el-input v-model="queryParams.crBy" placeholder="请输入操作员" maxlength="20" clearable style="width: 240px"/>
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="tableListData" height="800" ref="multipleTable" :row-key="(row)=>{return row.orderId}"> 
        <el-table-column label="序号" align="center" width="80" type="index" fixed="left">
                <template slot-scope="scope">
                    <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
                </template>
        </el-table-column>
        <el-table-column label="账户交易号" align="center" prop="flowId" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="用户编号" align="center" prop="custNum" :show-overflow-tooltip="true" width="100"/>
        <el-table-column label="用户姓名" align="center" prop="custName" :show-overflow-tooltip="true" width="100"/>
        <el-table-column label="用户手机号" align="center" prop="mobile" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="所属组织" align="center" prop="orgFullName" :show-overflow-tooltip="true" width="120"/> 
        <el-table-column label="用户类别" align="center" prop="psnTypeName" :show-overflow-tooltip="true" width="100"/>  
        <el-table-column label="支付时间" align="center" prop="payTime" :show-overflow-tooltip="true" width="100"/>
        <el-table-column label="下单时间" align="center" prop="ordTime" :show-overflow-tooltip="true" width="100"/>
        <el-table-column label="交易类型" align="center" prop="walletFlowType" :show-overflow-tooltip="true" width="100">
            <template slot-scope="scope">
                <span v-if="scope.row.walletFlowType==110">消费</span>
                <span v-if="scope.row.walletFlowType==120">消费补扣</span> 
                <span v-if="scope.row.walletFlowType==130">消费退款</span>
            </template> 
            
        </el-table-column>
        <el-table-column label="钱包类型" align="center" prop="walletId" :show-overflow-tooltip="true" width="100">
            <template slot-scope="scope">
                <span v-if="scope.row.walletId==1">个人钱包</span>
                <span v-if="scope.row.walletId==2">补贴钱包</span>
                <span v-if="scope.row.walletId==4">红包</span>
            </template> 
        </el-table-column>
        <el-table-column label="变动金额" align="center" prop="amount" :show-overflow-tooltip="true" width="120">
            <template slot-scope="scope">
                <span>{{ (scope.row.amount/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="可用余额" align="center" prop="walletBal" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.walletBal/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="冻结余额" align="center" prop="frozenBalance" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.frozenBalance/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="对应钱包余额" align="center" prop="walletBal" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.walletBal/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="交易地点" align="center" prop="address" :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" width="150"/>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      /> 
    </div>
  </template>
  
  <script>
  import { deptTreeSelect } from '@/api/system/user' 
  import { accountFlowPageApi } from "@/api/accountCenter/consume";
  export default {
    name: "",
    dicts: ['user_psn_type'], 
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        //表格数据
        tableListData: [], 
        // 是否显示弹出层
        open: false, 
        dateRange:this.defaultDateRange(),
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date(new Date().toLocaleDateString());
              end.setTime(end.getTime() + 24 * 60 * 60 * 1000 - 1);
              const start = new Date((new Date().toLocaleDateString()));
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
 
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date(new Date().toLocaleDateString());
              end.setTime(end.getTime() + 24 * 60 * 60 * 1000 - 1);
              const start = new Date((new Date().toLocaleDateString()));
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date(new Date().toLocaleDateString());
              end.setTime(end.getTime() + 24 * 60 * 60 * 1000 - 1);
              const start = new Date((new Date().toLocaleDateString()));
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        deptOptions:[],//组织树 
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          timeType:2,  
          keyword:null, 
          orgIdList:[],
          psnTypeList:[], 
          flowTypeList:[], 
          accWalletIdList:[], 
          ifOnlineList:[], 
          minAmount:null, 
          maxAmount:null,
        },
      };
    },
    created() {  
        this.getDeptTree(); 
        this.getList()
    },
    methods: { 
        /** 查询部门下拉树结构 */
        getDeptTree() {
            deptTreeSelect().then((response) => {
                this.deptOptions = this.filterTree(response.data)
            })
        },
        filterTree(nodes) {
            return nodes
                .map((node) => {
                    if (node.children) {
                        node.children = this.filterTree(node.children)
                    }
                    return node
                })
                .filter((node) => node.status !== '1')
        }, 
 
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
            pageNum: 1,
            pageSize: 10,
            timeType:2,  
            keyword:null, 
            orgIdList:[],
            psnTypeList:[], 
            flowTypeList:[], 
            accWalletIdList:[], 
            ifOnlineList:[], 
            minAmount:null, 
            maxAmount:null
            }
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 查询列表 */
        getList() {
            this.loading = true;
            console.log(this.dateRange) 
            let param = { 
                ...this.queryParams,
                "page":{
                    "current": this.queryParams.pageNum, 
                    "size": this.queryParams.pageSize, 
                },
                "onlyConsume":1,
                "beginTime":this.formatDate(this.dateRange[0]),
                "endTime":this.formatDate(this.dateRange[1])
            }
            accountFlowPageApi(param).then(response => {
                this.tableListData = response.resultPage.records;
                this.total = Number(response.resultPage.total);
                this.loading = false;
            });
        },
        defaultDateRange() {
          const end = new Date(new Date().toLocaleDateString());
          end.setTime(end.getTime() + 24 * 60 * 60 * 1000 - 1);
          const start = new Date((new Date().toLocaleDateString()));
          start.setTime(start.getTime());
  
          this.start = parseInt(start.getTime() / 1000)
          this.end = parseInt(end.getTime() / 1000)
          return [start, end]
        }, 
        //日期
        formatDate(date) {
            // 格式化为 YYYY-MM-DD
            date = new Date(date)
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
            const day = String(date.getDate()).padStart(2, '0');
            const hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
            const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
            const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        } 
    }
  };
  </script>
  <style scoped>
    .remind-question{
      width: 100%;
      height: 50px;
      font-size: 22px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .remind-title{
      width: 100%;
      height: 40px;
      font-size: 16px;
      font-weight: bold;
      color: #000;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  </style>
  