<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>汇付账号管理
    </h2>
    <div class="topWrap">
      <div class="newBand"
           v-permission="'adapay/createMember'"
           @click="onOperationAccount('create')">创建账号</div>
    </div>
    <div class="m-core">
      <save-form :inline="true"
               class="searchForm"
               @submit="search"
               :model="searchForm">
        <el-form-item>
          <el-input v-model.trim="searchForm.memberId" clearable
                    placeholder="请输入商户号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model.trim="searchForm.name" clearable
                    placeholder="请输入公司名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model.trim="searchForm.status"
                     filterable
                     clearable
                     placeholder="请选择审核状态">
            <el-option label="处理中"
                       value="pending"></el-option>
            <el-option label="成功"
                       value="succeeded"></el-option>
            <el-option label="失败"
                       value="failed"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="danger">搜索</el-button>
        </el-form-item>
      </save-form>
      <div class="tableWrap">
        <el-table :data="tableList"
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column label="商户号"
                           align="center"
                           prop="memberId"></el-table-column>
          <el-table-column label="法人"
                           align="center"
                           prop="legalPerson"></el-table-column>
          <el-table-column label="法人手机号"
                           align="center"
                           prop="legalMp"></el-table-column>
          <el-table-column label="法人身份证号"
                           align="center"
                           prop="legalCertId"></el-table-column>
          <el-table-column label="公司名称"
                           align="center"
                           prop="name"></el-table-column>
          <el-table-column label="统一社会信用码"
                           align="center"
                           prop="socialCreditCode"></el-table-column>
          <el-table-column label="银行卡号"
                           align="center"
                           prop="cardNo"></el-table-column>
          <el-table-column label="审核状态"
                           align="center"
                           prop="status">
            <template slot-scope="scope">
              {{ scope.row.status | formatStatus }}
            </template>
          </el-table-column>
          <el-table-column label="备注"
                           align="center"
                           prop="memo"></el-table-column>
          <el-table-column label="操作"
                           width="250px"
                           align="center"
                           fixed="right">
            <template slot-scope="scope">
              <el-button v-if="scope.row.status === 'failed'"
                         v-permission="'adapay/updateMember'"
                         @click="onOperationAccount('update', scope.row)"
                         type="text">修改</el-button>
              <el-button v-if="scope.row.status === 'succeeded'"
                         v-permission="'adapay/createSettleAccount'"
                         @click="onUpdateAccount(scope.row)"
                         type="text">修改结算账户</el-button>
              <el-button v-if="scope.row.status === 'succeeded'"
                         v-permission="'adapay/updateMonthMaxReceipt'"
                         @click="onMoneyAccount(scope.row)"
                         type="text">设置金额</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination sync :total.sync="rules.total"
                    :page.sync="rules.page"
                    :limit.sync="rules.limit"
                    :page-sizes="rules.page_size"
                    @pagination="getList" />
      </div>
    </div>

    <el-dialog title="绑定结算账户"
               :visible.sync="zeroVisible"
               width="400px"
               @close="onFormClose('maskForm')">
      <el-form :model='maskForm'
               :rules="maskRules"
               label-width="100px"
               label-position="right"
               ref="maskForm">
        <el-form-item label="银行卡号"
                      prop="cardNo">
          <el-input v-model="maskForm.cardNo"
                    placeholder="请输入银行卡号"></el-input>
        </el-form-item>
        <el-form-item label="所属银行"
                      prop="bankCode">
          <el-select v-model="maskForm.bankCode"
                     style="width: 100% !important"
                     placeholder="请选择所属银行">
            <el-option v-for="(item, index) in backList"
                       :key="index"
                       :label="item.title"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="zeroVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addZero('maskForm')">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="设置金额"
               :visible.sync="moneyVisible"
               width="500px"
               @close="onFormClose('moneyForm')">
      <el-form :model='moneyForm'
               :rules="moneyRules"
               label-width="160px"
               label-position="right"
               ref="moneyForm">
        <el-form-item label="每日最大收款金额"
                      prop="monthMaxReceipt">
          <template #label>
            每日最大收款金额
            <el-tooltip placement="top"
                    popper-class="mTooltip">
            <div slot="content">
              每日最大收款金额，0不限制
            </div>
            <i class="el-icon-question micon el-tooltip"
              style="font-size: 18px; color: #F56C6C"
              v-permission="'export/teacherSalary'"></i>
          </el-tooltip>
          </template>
          <el-input v-model="moneyForm.monthMaxReceipt" type="number"
                    placeholder="请输入每日最大收款金额">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="满额转到收款账户"
                      prop="organId">
          <el-select v-model="moneyForm.organId"
                     style="width: 100% !important"
                     clearable
                     filterable
                     @change="onBranchChange"
                     placeholder="请选择满额转到收款账户">
            <el-option v-for="(item, index) in calcBranchList"
                       :key="index"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="满额转到收款账户" prop="routeMemberId" v-show="false">
          <el-input v-model="moneyForm.routeMemberId" disabled
                    placeholder="请输入满额转到收款账户"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="moneyVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addMaxMoney('moneyForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import pagination from "@/components/Pagination/index";
import AccountForm from './form'
import { queryPageList, createSettleAccount, updateMonthMaxReceipt } from './api.js'
import { getPaymentConfigs } from "../chargeManager/api";
import { cityJson } from './cityJson'
import { bankJson } from './bankJson'
import cleanDeep from 'clean-deep'
export default {
  components: {
    pagination,
    AccountForm
  },
  data () {
    return {
      zeroVisible: false,
      backList: bankJson,
      rules: {
        // 分页规则
        limit: 20, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 50, 100] // 选择限制显示条数
      },
      tableList: [],
      searchForm: {},
      maskRules: {
        cardNo: [{ required: true, message: '请输入银行卡号', trigger: 'blur' }],
        bankCode: [{ required: true, message: '请选择所属银行', trigger: 'change' }]
      },
      maskForm: {
        memberId: null,
        cardNo: null,
        bankCode: null
      },
      moneyVisible: false,
      moneyForm: {
        monthMaxReceipt: null,
        organId: null,
        routeMemberId: null
      },
      moneyRules: {
        monthMaxReceipt: [{ required: true, message: '请输入每日最大收款金额', trigger: 'blur' }],
        organId: [{ required: true, message: '请选择满额转分部', trigger: 'change' }]
      },
      calcBranchList: []
    };
  },
  mounted () {
    this.getList()
  },
  methods: {
    async getList () {
      try {
        let obj = this.searchForm
        obj.page = this.rules.page
        obj.rows = this.rules.limit
        await queryPageList(cleanDeep(obj)).then(res => {
          if (res.code == 200) {
            this.tableList = res.data.rows
            this.rules.total = res.data.total
          }
        })
      } catch {
        //
      }
    },
    async getCalcBranchList() {
      await getPaymentConfigs({ payType: 'ADAPAY' }).then(res => {
        this.calcBranchList = []
        if (res.code == 200 && res.data) {
          res.data.forEach(item => {
            this.calcBranchList.push({
              label: item.hfMerNo,
              value: item.organId,
              hfMerNo: item.hfMerNo
            });
          });
        }
      });
    },
    search() {
      this.rules.page = 1
      this.getList()
    },
    onFormClose (formName) {
      this.$refs[formName].resetFields()
    },
    onOperationAccount (type, row) {
      let tagTitle = '创建'
      if(type == 'update') {
        tagTitle = '修改'
      }
      this.$router.push({
        path: '/sysBasics/adapayOperation',
        query: {
          type: type,
          row: JSON.stringify(row)
        }
      }, (route) => {
        route.meta.title = tagTitle + '汇付账号'
      })
    },
    onUpdateAccount (row) {
      this.zeroVisible = true
      this.$nextTick(() => {
        this.maskForm = {
          memberId: row.memberId,
          cardNo: row.cardNo,
          bankCode: row.bankCode
        }
      })
    },
    addZero (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          createSettleAccount(cleanDeep(this.maskForm)).then(res => {
            if (res.code == 200) {
              this.$message.success('修改结算账户成功')
              this.zeroVisible = false
              this.getList()
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          return false;
        }
      });
    },
    onBranchChange(val) {
      console.log(val)
      this.calcBranchList.forEach(item => {
        if(item.value == val) {
          this.moneyForm.routeMemberId = item.hfMerNo
        }
      })
    },
    onMoneyAccount(row) {
      this.moneyVisible = true
      this.$nextTick(() => {
        this.moneyForm = {
          id: row.id,
          organId: row.routeOrganId || null,
          monthMaxReceipt: row.monthMaxReceipt,
          routeMemberId: row.routeMemberId
        }
      })
      this.getCalcBranchList()
    },
    addMaxMoney(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          await updateMonthMaxReceipt(cleanDeep(this.moneyForm)).then(res => {
            if (res.code == 200) {
              this.$message.success('设置金额成功')
              this.moneyVisible = false
              this.getList()
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          return false;
        }
      });
    }
  },
  filters: {
    formatStatus (val) {
      const templateList = {
        pending: "处理中",
        succeeded: "成功",
        failed: "失败"
      }
      return templateList[val]
    }
  }
};
</script>
<style lang='scss' scoped>
</style>