<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="tradeNo">
        <el-input
          v-model="queryParams.tradeNo"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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="borrowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="订单编号" align="center" prop="tradeNo" />
      <el-table-column label="客户信息" align="center" prop="customerPhone" min-width="100">
        <template v-slot="scope">
          <span style="font-weight: bold">姓名:</span><span>{{scope.row.realName}}</span> <br>
          <span style="font-weight: bold">用户:</span><span>{{scope.row.customerLoginName}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="贷款信息" align="center" prop="customerPhone" min-width="100">
        <template v-slot="scope">
          <span style="font-weight: bold">总贷款额:</span><span>{{scope.row.totalLoanMoney}}</span> <br>
          <span style="font-weight: bold">还款月数:</span><span>{{scope.row.totalMonth}}</span><br>
          <span style="font-weight: bold">每月还款额:</span><span>{{scope.row.avgRepayment}}</span><br>
          <span style="font-weight: bold">月利率:</span><span>{{scope.row.loanMonthRate*100}}</span><br>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <el-table-column label="允许提现" align="center" prop="withdrawFlag">
        <template v-slot="scope">
          <dict-tag-v2 :options="booleanList" :value="scope.row.withdrawFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="是否打款" align="center" prop="remitFlag" >
        <template v-slot="scope">
          <dict-tag-v2 :options="yesOrNoList" :value="scope.row.remitFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="借款状态" align="center" prop="borrowName" >
        <template v-slot="scope">
          <span :style={color:scope.row.borrowNameStyle}>{{scope.row.borrowName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="借款说明" align="center" prop="borrowRemark" />
      <el-table-column label="资料" align="center" prop="borrowName" >
        <template v-slot="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleTransAccount(scope.row)"
          >转账记录</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleUpdateTransRemark(scope.row)"
          >修改转账备注</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleContract(scope.row)"
          >合同保单</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleContractTemplate(scope.row)"
          >合同</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStatus(scope.row)"
            v-hasPermi="['dk:borrow:edit']"
          >修改状态</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-bank-card"
              @click="handleBank(scope.row)"
              v-hasPermi="['dk:borrow:edit']"
          >修改银行</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-scissors"
              @click="handleLoan(scope.row)"
              v-hasPermi="['dk:borrow:edit']"
          >修改贷款</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleCustomerInfo(scope.row)"
              v-hasPermi="['dk:borrow:edit']"
          >客户资料</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"
    />

    <update-bank-dialog v-if="updateBankVisible" ref="updateBank" @refreshDataList="getList" />
    <update-loan-dialog v-if="updateLoanVisible" ref="updateLoan" @refreshDataList="getList" />
    <update-status-dialog v-if="updateStatusVisible" ref="updateStatus" @refreshDataList="getList" />
    <update-trans-remark-dialog v-if="updateTransRemarkVisible" ref="updateTransRemark" @refreshDataList="getList" />
    <customer-info-dialog v-if="infoVisible" ref="info"/>
    <trans-account-dialog v-if="transAccountVisible" ref="transAccount" />
  </div>
</template>

<script>
import { getBorrow, listBorrow } from '@/api/dk/borrow'
import UpdateBankDialog from './update-bank'
import UpdateLoanDialog from './update-loan'
import UpdateStatusDialog from './update-status'
import CustomerInfoDialog from '../dkCustomer/info'
import TransAccountDialog from '@/views/dk/borrow/trans-account'
import UpdateTransRemarkDialog from '@/views/dk/borrow/update-trans-remark'
import { booleanList, yesOrNoList } from '@/constant/statusMap'

export default {
  name: "Borrow",
  components: {
    UpdateBankDialog,UpdateLoanDialog,CustomerInfoDialog,UpdateStatusDialog,TransAccountDialog,UpdateTransRemarkDialog
  },
  data() {
    return {
      yesOrNoList,booleanList,
      infoVisible: false,
      updateBankVisible: false,
      updateLoanVisible: false,
      updateStatusVisible: false,
      transAccountVisible: false,
      updateTransRemarkVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 借款计划表格数据
      borrowList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tradeNo: undefined,
        realName: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleBank(row){
      this.updateBankVisible = true
      this.$nextTick(() => {
        this.$refs.updateBank.init(row.id)
      })
    },
    handleLoan(row){
      this.updateLoanVisible = true
      this.$nextTick(() => {
        this.$refs.updateLoan.init(row.id)
      })
    },
    handleStatus(row){
      this.updateStatusVisible = true
      this.$nextTick(() => {
        this.$refs.updateStatus.init(row.id)
      })
    },
    handleCustomerInfo(row){
      this.infoVisible = true
      this.$nextTick(() => {
        this.$refs.info.init(row.customerId,row.id)
      })
    },
    handleUpdateTransRemark(row){
      this.updateTransRemarkVisible = true
      this.$nextTick(() => {
        this.$refs.updateTransRemark.init(row.id)
      })
    },
    handleTransAccount(row){
      this.$router.push({ path: '/dk/transAccount/' + row.id })
    },
    handleContract(row){
      this.$router.push({ path: '/dk/contract/' + row.id })
    },
    handleContractTemplate(row){
      this.$router.push({ path: '/dk/contractTemplate/' + row.tradeNo })
    },
    /** 查询借款计划列表 */
    getList() {
      this.loading = true;
      listBorrow(this.queryParams).then(response => {
        this.borrowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getBorrow(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改借款计划";
      });
    },
  }
};
</script>
