<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="所属组织" prop="deptIdList">  
            <el-cascader v-model="queryParams.deptIdList"
            :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="searchValue">
            <el-input v-model="queryParams.searchValue" 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" :span-method="objectSpanMethod">
        <el-table-column label="序号" align="center" width="80" type="index" fixed="left">
            <template scope="scope">
                <span v-if="!scope.row.userId">合计：</span>
                <span v-else>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template> 
        </el-table-column> 
        <el-table-column label="用户编号" align="center" prop="userId" :show-overflow-tooltip="true" width="120" fixed="left"/>
        <el-table-column label="用户姓名" align="center" prop="nickName" :show-overflow-tooltip="true" width="120" fixed="left"/>
        <el-table-column label="用户手机号" align="center" prop="phoneNumber" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="所属组织" align="center" prop="deptName" :show-overflow-tooltip="true"/>
        <el-table-column label="用户类别" align="center" prop="userType" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_type" :value="scope.row.userType"/>
          </template>
        </el-table-column>
        <el-table-column label="账户状态" align="center" prop="accountStatus" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.accountStatus==0">正常</span>
            <span v-if="scope.row.accountStatus==1">停用</span>
          </template>
        </el-table-column>
        <el-table-column label="账户总余额" align="center" prop="accBalTotal" :show-overflow-tooltip="true">
          <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">
          <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">
          <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">
          <template slot-scope="scope">
                <span>{{ (scope.row.subsidyBal/100).toFixed(2) }}</span>
            </template> 
        </el-table-column> 
        <el-table-column label="冻结金额" align="center" prop="accFreezeBalTotal" :show-overflow-tooltip="true">
          <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="endDate" :show-overflow-tooltip="true" width="150"/> --> 
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope" v-if="scope.row.userId">
            <el-button
              size="mini"
              type="text" 
              @click="handleCharge(scope.row)" 
            >充值</el-button>
            <el-button
              size="mini"
              type="text" 
              @click="handleTake(scope.row)" 
            >提现</el-button>
            <el-button
              size="mini"
              type="text" style="color: red;"
              @click="handleClear(scope.row)" 
            >钱包清空</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
   
        <!-- 充值对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <div style="width: 100%;height: 400px;overflow-y: auto;">
                <el-descriptions class="margin-top" title="账户信息" :column="2" size="small" border>  
                    <el-descriptions-item>
                        <template slot="label">用户编号</template>
                        {{form.userId}}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">用户姓名</template>
                        {{ form.nickName }}
                    </el-descriptions-item>

                    <el-descriptions-item>
                        <template slot="label">所属组织</template>
                        {{ form.deptName }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">用户手机号</template>
                        {{ form.phoneNumber }}
                    </el-descriptions-item>
                        
                    <el-descriptions-item>
                        <template slot="label">账户状态</template>
                        {{ form.accountStatusName }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">用户类别</template>
                        {{ form.userTypeName }}
                    </el-descriptions-item>

                    
                </el-descriptions>
                <el-descriptions class="margin-top" title="账户钱包" :column="2" size="small" border> 
                    <el-descriptions-item>
                        <template slot="label">账户总余额</template>
                        {{ (form.accBalTotal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">可用总余额</template>
                        {{ (form.accAvailableBal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">个人钱包余额</template>
                        {{ (form.walletBal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">补贴钱包余额</template>
                        {{ (form.subsidyBal/100).toFixed(2) }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">冻结金额</template>
                        {{ (form.accFreezeBalTotal/100).toFixed(2) }}
                    </el-descriptions-item>
                </el-descriptions>
                <div style="margin: 20px 0;font-weight: bold;font-size: 16px;color: #000;"></div>
                <el-form ref="form" :model="form" :rules="rules" label-width="100px"> 
                    <el-form-item label="充值金额" prop="amount">
                        <el-input  v-model="form.amount" placeholder="请输入充值金额" maxlength="9" clearable style="width: 300px;"
                            @input="(v)=>(form.amount=v.replace(/^(\d+)(\.\d{0,2})?.*$/, '$1$2'))">
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-form>
            </div> 
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 提现对话框 -->
        <el-dialog :title="title" :visible.sync="openTake" width="800px" append-to-body>
            <div style="width: 100%;height: 400px;overflow-y: auto;">
                <el-descriptions class="margin-top" title="账户信息" :column="2" size="small" border>  
                    <el-descriptions-item>
                        <template slot="label">用户编号</template>
                        {{form.userId}}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">用户姓名</template>
                        {{ form.nickName }}
                    </el-descriptions-item>

                    <el-descriptions-item>
                        <template slot="label">所属组织</template>
                        {{ form.deptName }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">用户手机号</template>
                        {{ form.phoneNumber }}
                    </el-descriptions-item>
                        
                    <el-descriptions-item>
                        <template slot="label">账户状态</template>
                        {{ form.accountStatusName }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">用户类别</template>
                        {{ form.userTypeName }}
                    </el-descriptions-item>

                    
                </el-descriptions>
                <el-descriptions class="margin-top" title="账户钱包" :column="2" size="small" border> 
                    <el-descriptions-item>
                        <template slot="label">账户总余额</template>
                        {{ (form.accBalTotal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">可用总余额</template>
                        {{ (form.accAvailableBal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">个人钱包余额</template>
                        {{ (form.walletBal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">补贴钱包余额</template>
                        {{ (form.subsidyBal/100).toFixed(2) }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">冻结金额</template>
                        {{ (form.accFreezeBalTotal/100).toFixed(2) }}
                    </el-descriptions-item>
                </el-descriptions>
                <div style="margin: 20px 0;font-weight: bold;font-size: 16px;color: #000;"></div>
                <el-form ref="form" :model="form" :rules="rules" label-width="100px"> 
                    <el-form-item label="提现金额" prop="amount">
                        <el-input  v-model="form.amount" placeholder="请输入提现金额" maxlength="9" clearable style="width: 300px;"
                            @input="(v)=>(form.amount=v.replace(/^(\d+)(\.\d{0,2})?.*$/, '$1$2'))">
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-form>
            </div> 
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitTakeForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 清空对话框 -->
        <el-dialog :title="title" :visible.sync="openClear" width="800px" append-to-body>
            <div style="width: 100%;height: 300px;overflow-y: auto;">
                <el-descriptions class="margin-top" title="账户信息" :column="2" size="small" border>  
                    <el-descriptions-item>
                        <template slot="label">用户编号</template>
                        {{form.userId}}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">用户姓名</template>
                        {{ form.nickName }}
                    </el-descriptions-item>

                    <el-descriptions-item>
                        <template slot="label">所属组织</template>
                        {{ form.deptName }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">用户手机号</template>
                        {{ form.phoneNumber }}
                    </el-descriptions-item>
                    
                    <el-descriptions-item>
                        <template slot="label">账户状态</template>
                        {{ form.accountStatusName }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">用户类别</template>
                        {{ form.userTypeName }}
                    </el-descriptions-item>

                
                </el-descriptions>
                <el-descriptions class="margin-top" title="账户钱包" :column="2" size="small" border> 
                    <el-descriptions-item>
                        <template slot="label">账户总余额</template>
                        {{ (form.accBalTotal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">可用总余额</template>
                        {{ (form.accAvailableBal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">个人钱包余额</template>
                        {{ (form.walletBal/100).toFixed(2) }}
                    </el-descriptions-item> 
                    <el-descriptions-item>
                        <template slot="label">补贴钱包余额</template>
                        {{ (form.subsidyBal/100).toFixed(2) }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                        <template slot="label">冻结金额</template>
                        {{ (form.accFreezeBalTotal/100).toFixed(2) }}
                    </el-descriptions-item>
                </el-descriptions> 
            </div> 
            <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitClearForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>




    </div>
  </template>
  
  <script>
  import { deptTreeSelect } from '@/api/system/user'
  import { accInfoPageApi, accInfoSumPageApi,rechargeAccWalletApi,withdrawAccWalletApi,clearAccWalletApi } from "@/api/accountCenter/manager";
  import { decryptWithSM4,encryptWithSM4 } from '@/utils/sm';
  export default {
    name: "",
    dicts: ['sys_user_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        //表格数据
        tableListData: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openClear:false,
        openTake:false,
        //下拉选数据
        deptOptions:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          accountStatusList:[], 
          deptIdList:[],   
          searchValue:null, 
        },
        cancelCount:0,
        expiredCount:0,
        // 表单参数
        form: {},
        pickerOptions:{                                 
            //禁用当前日期之前的日期
            disabledDate(time) {
            //Date.now()是javascript中的内置函数，它返回自1970年1月1日00:00:00 UTC以来经过的毫秒数。
               return time.getTime() < Date.now() - 8.64e7;
            }
        },
        // 表单校验
        rules: {
            amount: [
              { required: true, message: "金额不能为空", trigger: "blur" }
            ]
        }
      };
    },
    created() {
        this.getDeptTree()
        // this.getAccOperateCount()
        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')
        },
        // //注销过期用户数量
        // getAccOperateCount(){
        //   accInfoOperateCountApi().then(response => {
        //       this.cancelCount = response.data.cancelCount
        //       this.expiredCount = response.data.expiredCount
        //   });
        // },
        
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() { 
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 查询列表 */
        getList() {
            console.log(this.queryParams)
            this.loading = true;
            let param = {
              "pageNum": this.queryParams.pageNum, 
              "pageSize": this.queryParams.pageSize,
              "accountStatusList": this.queryParams.accountStatusList,
              "deptIdList": this.queryParams.deptIdList,  
              "searchValue": this.queryParams.searchValue
            } 
            accInfoPageApi(param).then(response => {
                this.tableListData = response.rows;
                this.total = Number(response.total);
                this.tableListData.forEach(item=>{
                    if(item.phoneNumber&&item.phoneNumber!=""){
                        this.$set(item,"phoneNumber",decryptWithSM4(item.phoneNumber))
                    }
                }) 
                accInfoSumPageApi(param).then(response => {
                  this.tableListData.push(response.data)   
                }); 
                this.loading = false;
            });
        },
        // 合并单元格 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  
            //     }
            // }
        }, 
         
        /** 充值按钮操作 */
        handleCharge(row) {
            this.reset();
            this.form = Object.assign({}, row) 
            this.$set(this.form,"amount",null)
            // this.$set(this.form,"subLimitBalance",(row.minSubBalLimit/100).toFixed(2))
            // this.$set(this.form,"walletLimitBalance",(row.minWalletBalLimit/100).toFixed(2))
            this.open = true;
            this.title = "个人钱包充值";
        },
        /** 提现按钮操作 */
        handleTake(row) {
            this.reset();
            this.form = Object.assign({}, row) 
            this.$set(this.form,"amount",null)
            // this.$set(this.form,"subLimitBalance",(row.minSubBalLimit/100).toFixed(2))
            // this.$set(this.form,"walletLimitBalance",(row.minWalletBalLimit/100).toFixed(2))
            this.openTake = true;
            this.title = "个人钱包提现";
        }, 
        /** 补贴清空按钮操作 */
        handleClear(row) {
            this.reset();
            this.form = Object.assign({}, row) 
            // this.$set(this.form,"clearType",1)
            // this.$set(this.form,"amount",null)
            this.openClear = true;
            this.title = "个人钱包清空";
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.openTake = false;
            this.openClear = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {};
            this.resetForm("form");
        }, 
        /** 充值提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    let param = {
                        userId:this.form.userId,
                        amount:Number(this.form.amount*100)
                    }
                    rechargeAccWalletApi(param).then(response => {
                            this.$modal.msgSuccess("操作成功");
                            this.open = false;
                            this.getList();
                    }); 
                }
            });
        },
        /** 提现提交按钮 */
        submitTakeForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    let param = {
                        userId:this.form.userId,
                        amount:Number(this.form.amount*100)
                    }
                    withdrawAccWalletApi(param).then(response => {
                            this.$modal.msgSuccess("操作成功");
                            this.openTake = false;
                            this.getList();
                    }); 
                }
            });
        },
        /** 清空提交按钮 */
        submitClearForm: function() { 
              let param = {
                userId:this.form.userId 
              }
              clearAccWalletApi(param).then(response => {
                    this.$modal.msgSuccess("操作成功");
                    this.openClear = false;
                    this.getList();
              });
        }, 
        
    }
  };
  </script>
  