<template>
    <div class="app-container">
      <van-nav-bar
        left-arrow
        fixed
        placeholder
        @click-left="goBack()"
        @click-right="goAdd()"
      >
        <template #title>
          <p style="font-weight: bold;">银行卡转账申请列表</p>
        </template>
        <template #right>
          <van-icon name="add" size="18"/>
        </template>
      </van-nav-bar>

      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getList">
        <van-swipe-cell v-for="(item,index) in applicationList" :key="index">
          <van-cell :title="item.payerAccount" center :to="{name:'approvalDetail', query: {id:item.id}}">
            <template #icon>
              <van-icon name="../../../../../static/images/onlineHome/icon_yn1.png" size="30" color="#FF4646" style="margin-right: 10px;" />
            </template>
            <template #label>
              <p><span><i>￥</i>{{stateFormat(item.expenditureAmount)}}</span><i style="margin-right: 30px;"></i>{{item.applyDate}}</p>
            </template>
            <template #default>
              <p style="width: 80px;display: inline-block">{{item.auditStatus}}</p>
              <p v-if="item.auditStatus === '通过' && item.paymentState === '待支付'" style="color: #606266;width: 80px;display: inline-block">{{item.paymentState}}</p>
              <p v-else-if="item.paymentState === '银行受理'" style="width: 80px;display: inline-block">{{item.paymentState}}</p>
              <p v-else-if="(item.paymentState === '支付失败' || item.paymentState === '部分失败') && item.downId == null" style="color: #F56C6C;width: 80px;display: inline-block">{{item.paymentState}}-未重新申请</p>
              <p v-else-if="(item.paymentState === '支付失败' || item.paymentState === '部分失败') && item.downId != null" style="color: #E6A23C;width: 80px;display: inline-block">{{item.paymentState}}-已重新申请</p>
              <p v-else-if="item.paymentState === '支付成功'" style="color: #67C23A;width: 80px;display: inline-block">{{item.paymentState}}</p>
            </template>
          </van-cell>
          <template #right>
            <van-row>
              <van-col>
                <van-button v-if="item.auditStatus=='草稿'||item.auditStatus=='驳回'" square text="修改" type="info" :to="{name:'approvalModify', query: {id:item.id}}" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="item.auditStatus=='草稿'||item.auditStatus=='驳回'" color="#FFA63E" square text="提交" type="info" @click="onSubmit(item.id)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="item.auditStatus=='草稿'||item.auditStatus=='驳回'" square text="删除" type="danger" @click="deleteList(item.id,index)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="item.auditStatus=='待审'" square text="撤回" type="danger" @click="cancelApply(item)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="item.paymentState === '银行受理'" square text="核实结果" color="#FFA63E" type="info" @click="transactionStatusQuery(item.id)" class="delete-button" />
              </van-col>
              <van-col>
                <van-button v-if="(item.paymentState === '支付失败' || item.paymentState === '部分失败') && item.downId == null" square text="重新申请" type="danger" @click="reapply(item.id)" class="delete-button" />
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
    </div>
</template>

<script>
import { listTransfer , customSubmit , delTransfer , updateTYinnongTransferFoStatus,transactionStatusQuery,reapply } from "@/api/onlineHome/bankAgriculture/paymentApproval";
export default {
  name: "approvalList",
  data() {
    return {
      applicationList:[],
      auditStatusOptions:[],
      paymentStateOptions:[],
      loading: false,
      finished: false,
      queryParams:{
        pageNum:1,
        pageSize:10,
        transferType:"1",
      },
      form:{}
    };
  },
  created() {
    this.getDicts("audit_status").then((response) => {
      this.auditStatusOptions = response.data;
    });
    this.getDicts("payment_state").then((response) => {
      this.paymentStateOptions = response.data;
    });
  },
  methods: {
    //金额千分符 会在整数后添加两个0
    stateFormat(cellValue) {
      if (cellValue) {
        return Number(cellValue)
          .toFixed(2)
          .replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
            return $1 + ",";
          })
          .replace(/\.$/, "") + "元";
      }
    },
    goAdd(){
      window.location = 'approvalAdd';
    },
    getList(){
      setTimeout(() => {
        listTransfer(this.queryParams).then(response => {
          console.log(response)
          for (var i = 0; i < response.rows.length; i++) {
            response.rows[i].auditStatus = this.selectDictLabel(this.auditStatusOptions, response.rows[i].auditStatus);
            response.rows[i].paymentState = this.selectDictLabel(this.paymentStateOptions, response.rows[i].paymentState);
            this.applicationList.push(response.rows[i]);
          }
          console.log(this.applicationList.length >= response.total)
          if(this.applicationList.length >= response.total){
            this.finished = true;
            return;
          }else{
            this.loading = false;
            this.queryParams.pageNum += 1 ;
          }
        });
      }, 1000);
    },
    reapply(id){
      this.$dialog.confirm({
        message: '您确认将单据中支付失败的记录重新申请？请先确保已根据详情中【交易结果描述】修改准确后，再执行此操作！',
      })
      .then(() => {
        // on confirm
        reapply(id).then(res => {
          if(res.code = 200){
            this.$toast.success('重新申请成功！');
          }
        });
      })
      .catch(() => {
        // on cancel
      });
    },
    transactionStatusQuery(id){
      this.$dialog.confirm({
        message: '您确认查询单据的转账结果？',
      })
      .then(() => {
        // on confirm
        transactionStatusQuery(id).then(res => {
          if(res.code = 200){
            this.$toast.success('查询成功');
          }
        });
      })
      .catch(() => {
        // on cancel
      });
    },
    deleteList(id,index){
      this.$dialog.confirm({
        message: '您确认删除草稿？',
      })
      .then(() => {
        // on confirm
        this.applicationList.splice(index,1)
        delTransfer(id).then(res => {
          if(res.code = 200){
            this.$toast.success('删除成功');
          }
        });
      })
      .catch(() => {
        // on cancel
      });
    },
    onSubmit(id){
      this.$dialog.confirm({
        message: '您确认提交草稿？',
      })
      .then(() => {
        customSubmit(id).then(res => {
          this.$toast.success('提交成功');
          setTimeout(function(){
            history.go(0)
          },2000)
        })
      })
      .catch(() => {
        // on cancel
      });
    },
    goBack(){
      this.$router.push({path:'/yinnong/workbench'})
    },
    cancelApply(row) {
      console.log(row)
      const instanceId = row.instanceId;
      this.form.id = row.id;
      this.form.auditStatus = "0";
      this.form.applyUserId = "";
      this.form.applyUserName = "";
      this.form.applyTime = "";
      this.form.instanceId = "";
      this.form.processKey = "";
      let that = this;
      this.$dialog.confirm({
        message: '您确认撤回待审数据？',
      })
        .then(function () {
          return updateTYinnongTransferFoStatus(that.form).then(response => {
            setTimeout(function(){
              history.go(0)
            },2000)
            that.$toast.success('撤回成功');
          });
        })
    },
  },
}
</script>

<style scoped lang="scss">
  .app-container {
    padding: 0.2rem 3%;
  }
  /deep/.van-cell__title{
    flex: 0.7;
  }
  /deep/.van-cell__title span{
    font-family: Arial;
    font-size: 0.4rem;
    font-weight: normal;

  }
  /deep/.van-cell__label span{
    color: #1D6FE9;
    font-weight: bold;
    i{
      font-size: 0.2rem;
    }
  }
  /deep/.van-cell__value{
    flex: 0.3;
    color: #1D6FE9;
    font-weight: bold;
  }
  /deep/.van-swipe-cell{
    margin-bottom: 0.2rem;
    border-radius: 0.2rem;
    overflow: hidden;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
  }
  /deep/van-ellipsis{
    font-weight: bold;
  }
  .van-row{
    height: 100%;
  }
  .van-col{
    height: 100%;
  }
  .delete-button {
    height: 100%;
  }
</style>
