<template>
  <div>
    <div class="page-search">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">

        <el-form-item label="身份证号">
          <el-input placeholder="身份证号" v-model="formInline.certNo"></el-input>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input placeholder="手机号" v-model="formInline.clientCell"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.status" placeholder="全部">
            <el-option v-for="item in this.$enumUtils.toValue('LoanSnapStatusEnum')" :key="item.value"
                       :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <br/>
    </div>
    <br/><br/>
    <h3>当前白名单列表</h3><br/><br/>
    <div>
      <el-table :data="loanBackedList" stripe style="width: 100%">
        <el-table-column prop="clientName" label="客户姓名"></el-table-column>
        <el-table-column prop="clientCell" label="客户手机号"></el-table-column>
        <el-table-column prop="certNo" label="身份证号"></el-table-column>
        <el-table-column prop="clientNo" label="客户编号"></el-table-column>
        <el-table-column prop="loanAmt" label="借款金额"></el-table-column>
        <el-table-column prop="raiseNo" label="放款流水号"></el-table-column>
        <el-table-column prop="loanNo" label="借款编号"></el-table-column>
        <el-table-column prop="raiseAmt" label="放款金额"></el-table-column>
        <el-table-column prop="cardOwnerName" label="持卡人姓名"></el-table-column>
        <el-table-column prop="cardOwnerAccount" label="收款银行卡"></el-table-column>
        <el-table-column prop="openBankName" label="银行名"></el-table-column>
        <el-table-column prop="statusStr" label="借款单状态"></el-table-column>
        <el-table-column prop="gmtCreated" label="借款时间" :formatter="dateFormat"></el-table-column>
        <el-table-column prop="memo" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div v-if="scope.row.status ==='SUBMITED' ">
              <el-button type="text" size="big" @click="passDerate(scope.row)">放款</el-button>
              <el-button type="text" size="big" @click="voidDerate(scope.row)">作废</el-button>
              <el-button type="text" size="big" @click="deleteDerate(scope.row)">移除</el-button>
              <el-button type="text" size="big" @click="autoDerate(scope.row)">自动放款</el-button>
            </div>
            <div v-else>
              <el-button type="text" size="small" disabled>已放款</el-button>
              <el-button type="text" size="small" disabled>已作废</el-button>
              <el-button type="text" size="small" disabled>已移除</el-button>
              <el-button type="text" size="small" disabled>已自动放款</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页-->
    <div class="page">
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :current-page.sync="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>


  </div>
</template>

<script>
import * as moment from "moment";

export default {
  name: "ByhLoanBackedList",
  data() {
    return {
      flag: false,
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      loginName: '',
      loanBackedList: [],
      formInline: {
        clientNo: '',
        clientCell: '',
        status: '',
        certNo: ''
      },
    }
  },

  created() {
    if (this.$route != null && this.$route.query != null && this.$route.query.flag) {
      this.flag = this.$route.query.flag;
      this.formInline.clientCell = this.$route.query.clientCell;
    }
  },

  mounted() {
    //还款日志
    this.loanBackedList = this.backedData();
    this.loginName = localStorage.getItem("loginName");
  },

  methods: {
    /** 加载数据 */
    backedData(params) {

      this.$$post('/raise/whiteLoan', this.buildParamPass()).then(res => {
        if (res.data.code != 200) {
          this.$message.error(res.data.message);
          return
        }
        this.loanBackedList = res.data.result.data;
        this.page.total = res.data.result.total;
      })


    },
    /*提醒期数*/
    planIndexFormat: function (row, column) {
      var planIndex = row[column.property]
      if (planIndex == undefined) {
        return ''
      }
      return planIndex + '/12'
    },

    passDerate(row) {
      const {loanRaiseNo} = row;
      this.$confirm('确认要放款吗？')
        .then(_ => {


          this.$$get('/raise/hangRaise?raiseNo=' + loanRaiseNo).then(res => {
            if (res.data.code != 200) {
              this.isShow = false;
              this.$message.error(res.data.message);
              return
            } else {
              // 修改成功后再次加载数据
              this.$message({message: '操作成功', type: 'success'});
              this.loanBackedList = this.backedData();
            }

          }).catch(error => {
            this.isShow = false;
            this.$Message.error(error || '网络异常，请稍后。。。');
          });

        })
        .catch(_ => {
        });

    },
    // 借款废除

    voidDerate(row) {
      const {loanRaiseNo} = row;
      this.$confirm('确认要作废吗？')
        .then(_ => {

          this.$$get('/loan/cancelWhite?raiseNo=' + loanRaiseNo + "&creater=" + localStorage.getItem("userName")).then(res => {
            if (res.data.code != 200) {
              this.$message.error(res.data.message);
              return
            } else {
              // 修改成功后再次加载数据
              this.$message({message: '作废成功', type: 'success'});

              this.loanBackedList = this.backedData();
            }

          }).catch(error => {
            this.$Message.error(error || '网络异常，请稍后。。。');
          });

        })
        .catch(_ => {
        });

    },
    // 移除白名单

    deleteDerate(row) {
      const {loanRaiseNo} = row;
      this.$confirm('确认要移除吗？')
        .then(_ => {

          this.$$get('/loan/removeWhite?raiseNo=' + loanRaiseNo + "&creater=" + localStorage.getItem("userName")).then(res => {
            if (res.data.code != 200) {
              this.$message.error(res.data.message);
              return
            } else {
              // 修改成功后再次加载数据
              this.$message({message: '移除成功', type: 'success'});
              this.loanBackedList = this.backedData();
            }

          }).catch(error => {
            this.$Message.error(error || '网络异常，请稍后。。。');
          });

        })
        .catch(_ => {
        });

    },
    // 自动放款autoDerate
    autoDerate(row) {
      const {loanRaiseNo} = row;
      this.$confirm('确认要放款吗？')
        .then(_ => {

          this.$$get('/raise/autoRaise?raiseNo=' + loanRaiseNo + "&creater=" + localStorage.getItem("userName")).then(res => {
            if (res.data.code != 200) {
              this.$message.error(res.data.message);
              return
            } else {
              // 修改成功后再次加载数据
              this.$message({message: '自动放款成功', type: 'success'});
              this.loanBackedList = this.backedData();
            }

          }).catch(error => {
            this.$Message.error(error || '网络异常，请稍后。。。');
          });

        })
        .catch(_ => {
        });

    },


    /*搜索条件*/
    onSubmit() {
      this.backedData();
    },
    /*重置*/
    reset() {
      this.formInline.status = '';
    },


    dateFormatText: function (param) {
      return moment(param).format("YYYY-MM-DD");
    },

    getDate(param, num) {
      if (param && param[num] !== '') {
        return moment(param[num]).format("YYYY-MM-DD")
      }
      return ""
    },

    mergeDate(param1, param2) {
      if (param1 == "") {
        return ""
      }
      var array = new Array();
      array[0] = new Date(param1)
      array[1] = new Date(param2)
      return array;
    },

    /*日期*/
    dateFormat: function (row, column) {
      var date = row[column.property]
      if (date == undefined) {
        return ''
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss")
    },

    //还款类型
    typyFormat: function (row, column) {
      var status = row['backedType'];
      return this.$enumUtils.toMsg('BackedTransferTypeEnum', status);
    },
    //还款单状态
    statusFormat: function (row, column) {
      var status = row['status'];
      return this.$enumUtils.toMsg('XmRaiseBackedStatus', status);
    },
    //结果状态
    resultFormat: function (row, column) {
      var result = row['result'];
      if (result != null) {
        return this.$enumUtils.toMsg('SuccessAndFail', result);
      } else {
        return '未处理';
      }

    },

    /** 处理分页操作*/
    handleCurrentChange() {
      this.loanBackedList = this.backedData()
    },

    /** 关闭处理 */
    handleClose() {
      this.visible = false;
      setTimeout(() => {
        this.$emit('update:dialogVisible', false)
      }, 300);
    },
    handleSizeChange(row) {
      this.page.size = row
      this.page.current = 1;
      this.backedData();
    },

    //参数

    buildParamPass() {
      var return_hash = {
        size: this.page.size,
        current: this.page.current,
        sort: 'id desc',
      };
      if (this.formInline.status != '') {
        return_hash['status'] = this.formInline.status
      }

      if (this.formInline.certNo != '') {
        return_hash['certNo'] = this.formInline.certNo
      }

      if (this.formInline.clientCell != '') {
        return_hash['clientCell'] = this.formInline.clientCell
      }

      return return_hash;
    },
    buildParamVoid(loanRaiseNo) {
      var return_hash = {
        creater: localStorage.getItem("userName"),
      };
      if (loanRaiseNo != '') {
        return_hash['raiseNo'] = loanRaiseNo
      }

      return return_hash;
    },

  }
}


</script>

<style scoped>
.page-search {
  margin-right: 20%;
  float: right;
}
</style>
