<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <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: 340px"
                :default-time="['00:00:00', '23:59:59']"
                :picker-options="pickerOptions" >
            </el-date-picker>
        </el-form-item>
        <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="关键字" prop="keyword">
            <el-input v-model="queryParams.keyword" placeholder="请输入用户姓名，编号，手机号" maxlength="20" clearable style="width: 240px"/>
        </el-form-item>
        <br>
        <el-form-item label="钱包类型" prop="walletType">
            <el-select v-model="queryParams.walletType" style="width: 240px" clearable> 
                <el-option label="账户总余额" value="0"></el-option>
                <el-option label="个人钱包" value="1"></el-option>
                <el-option label="补贴钱包" value="2"></el-option> 
                <el-option label="红包" value="3"></el-option> 
            </el-select>   
        </el-form-item>
        <el-form-item label="最小金额" prop="walletMinAmount">
            <el-input
                v-model="queryParams.walletMinAmount"
                placeholder="请输入最小金额" style="width: 240px"  @input="(v)=>(queryParams.walletMinAmount=v.replace(/[^\d.]/g,''))">
                <template slot="append">元</template>
            </el-input>
        </el-form-item>
        <el-form-item label="最大金额" prop="walletMaxAmount">
            <el-input
                v-model="queryParams.walletMaxAmount"
                placeholder="请输入最大金额" style="width: 240px"  @input="(v)=>(queryParams.walletMaxAmount=v.replace(/[^\d.]/g,''))">
                <template slot="append">元</template>
            </el-input>
        </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">
        <!-- <el-col :span="1.5">
          <el-button 
            size="mini"
            @click="handleBatchEdit"
          >批量修改</el-button>
        </el-col> -->
        
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row> 
      <el-table v-loading="loading" :data="tableListData" height="800" :span-method="objectSpanMethod">
        <el-table-column label="序号" align="center" width="80" type="index" fixed="left">
            <template scope="scope">
                <span v-if="!scope.row.custId">合计：</span>
                <span v-else>{{ (queryParams.pageNum - 1) * 10 + scope.$index }}</span>
            </template> 
        </el-table-column> 
        <el-table-column label="操作时间" align="center" prop="uptime" :show-overflow-tooltip="true" width="160"/>
        <el-table-column label="用户编号" align="center" prop="custNum" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="用户姓名" align="center" prop="custName" :show-overflow-tooltip="true" width="120"/>
        <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="120"/>
        <el-table-column label="账户总余额" align="center" prop="accBalTotal" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.accBalTotal/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="可用总余额" align="center" prop="accAvailableBal" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.accAvailableBal/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="subsidyBal" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.subsidyBal/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="红包余额" align="center" prop="redEnvelope" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.redEnvelope/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="冻结金额" align="center" prop="accFreezeBalTotal" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
                <span>{{ (scope.row.accFreezeBalTotal/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="个人钱包允许最低金额" align="center" prop="minWalletBalLimit" :show-overflow-tooltip="true" width="160">
          <template slot-scope="scope">
                <span>{{ (scope.row.minWalletBalLimit/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="补贴钱包允许最低金额" align="center" prop="minSubBalLimit" :show-overflow-tooltip="true" width="160">
          <template slot-scope="scope">
                <span>{{ (scope.row.minSubBalLimit/100).toFixed(2) }}</span>
            </template> 
        </el-table-column>
        <el-table-column label="账户状态" align="center" prop="accStatusName" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="账户有效期" align="center" prop="endDate" :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 { accInfoPageApi, accInfoSumPageApi } from "@/api/accountCenter/manager";
  
  export default {
    name: "",
    dicts: [],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        //表格数据
        tableListData: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false, 
        //下拉选数据
        deptOptions:[], 
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          accStatusList:[3], 
          orgIdList:[], 
          keyword:null,
          walletType:null,
          walletMinAmount:null, 
          walletMaxAmount:null,
        },
        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]);
            }
          }]
        },
         
        
      };
    },
    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.resetForm("queryForm");
            this.handleQuery();
        },
        /** 查询列表 */
        getList() {
            console.log(this.queryParams)
            this.loading = true;
            let param = {
              "current": this.queryParams.pageNum, 
              "size": this.queryParams.pageSize,
              "accStatusList": this.queryParams.accStatusList,
              "orgIdList": this.queryParams.orgIdList,
              "keyword": this.queryParams.keyword,
              "walletType": this.queryParams.walletType,
              "walletMinAmount": Number(this.queryParams.walletMinAmount)*100,
              "walletMaxAmount": Number(this.queryParams.walletMaxAmount)*100,
              "startDateTime":this.formatDate(this.dateRange[0]),
              "endDateTime":this.formatDate(this.dateRange[1])
            } 
            accInfoPageApi(param).then(response => {
                this.tableListData = response.data.pageVO.records;
                this.total = Number(response.data.pageVO.total);
                this.loading = false;
            });
            accInfoSumPageApi(param).then(response => {
                this.tableListData.push(response.data.sumVO)   
            });
        },
        // 合并单元格 rowIndex=行数  columnIndex=列数  
        // 这里是合并table的除表头外的第一行的第二列 + 除表头外的第二行的第二列 
        // 注意列数和行数从 0 开始
        objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
            // if (rowIndex === 0 && columnIndex == 0) {
            //     let rowspan = 0
            //     let colspan = 0
            //     return { rowspan, colspan }
            // }
            // 解决偏移的重要代码
            // 重点在 else if 判断必须加因为合并单元格数据会出现偏移、
            // 否则如果是 第二行 的 第二列 那么隐藏原有第二行 的 第二列的数据使他隐藏
            // else if (rowIndex === 0 && columnIndex < 5) {
            //     return {
            //         rowspan: 11,  
            //         colspan: 0  
            //     }
            // }
        },
        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
            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>
  