<template>
  <div>
    <ShrinkBox>
      <span slot="title">付款准备明细</span>

      <div class="footer-btns">
        <Button
          v-show="showAudit"
          :loading="auditLoad"
          type="info"
          :disabled="auditStatus"
          @click="audit()"
        >
          复核
        </Button>
        <Button
          v-show="showRefuse"
          :loading="refuseLoad"
          type="info"
          :disabled="refuseStatus"
          @click="refusalPay()"
        >
          拒付
        </Button>
        <Button
          v-show="showPay"
          :loading="payLoad"
          type="info"
          :disabled="payStatus"
          @click="payMent()"
        >
          支付
        </Button>
        <Button
          v-show="showCannelAudit"
          :loading="cannelAuditLoad"
          type="info"
          :disabled="cannelAuditStatus"
          @click="cannelAudit()"
        >
          撤销复核
        </Button>
        <Button
          v-show="showCannel"
          :loading="cannelLoad"
          type="info"
          :disabled="cannelStatus"
          @click="cannelPrepPayment()"
        >
          撤销
        </Button>
        <Button
          v-show="showReset"
          :loading="resetLoad"
          type="info"
          :disabled="resetStatus"
          @click="resetPaymentprepare()"
        >
          重做付款准备
        </Button>
        <Button
          v-show="true"
          type="primary"
          :disabled="true"
          @click="addOrUpdateDetail()"
        >
          查看附件
        </Button>
        <Button
          v-show="cashier"
          :loading="cashierLoad"
          type="primary"
          :disabled="cashierStatus"
          @click="noticeCashier()"
        >
          通知本地出纳
        </Button>
        <Button
          v-show="paySuccessBtn"
          :loading="paySuccessLoad"
          type="primary"
          :disabled="paySuccessStatus"
          @click="paySuccess()"
        >
          支付成功
        </Button>
        <Button
          v-show="payFailBtn"
          :loading="payFailLoad"
          type="primary"
          :disabled="payFailStatus"
          @click="payFail()"
        >
          支付失败
        </Button>
        <Button
          v-show="false"
          :loading="offlineLoad"
          type="primary"
          :disabled="offlineStatus"
          @click="offlineFail()"
        >
          线下退票
        </Button>
      </div>
      <Table
        :columns="columns"
        :data="tableData"
        stripe
        @on-expand="clickExpand"
      ></Table>
    </ShrinkBox>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import Server from '../server.js';
import PayPrepareExpand from './PayPrepareExpand';
import RefuseContent from './RefuseContent.vue';
import { Cashier } from '@paymentPlatformComps/index.js';
let vm;
const server = new Server();

export default {
  components: {
  },
  props: {
    applyPrepParams: {
      type: Object,
      default() {
        return {};
      }
    },
    batchId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      // 因为表格数据改变的时候回重新渲染表格
      // 所以用另外一个数据来修改值  达到不重新渲染表格的目的
      // disabled: false,
      currentInfo: {},
      cashierLoad: false,
      paySuccessLoad: false,
      payFailLoad: false,
      offlineLoad: false,
      auditStatus: true,
      refuseStatus: true,
      payStatus: true,
      cannelAuditStatus: true,
      cannelStatus: true,
      resetStatus: true,
      cashierStatus: true,
      paySuccessStatus: true,
      payFailStatus: true,
      offlineStatus: true,
      auditLoad: false,
      refuseLoad: false,
      payLoad: false,
      cannelAuditLoad: false,
      cannelLoad: false,
      resetLoad: false,
      server: server,
      applyAmount: '',
      columns: [
        {
          type: 'expand',
          width: 30,
          align: 'left',
          render: (h, params) => {
            return h(PayPrepareExpand, {
              props: {
                row: params.row,
                value: vm.tableData[params.index].subPayLineDetails,
                // columnsList: vm.expandColumns,
                editIncell: true,
                hoverShow: true,
                server: vm.server
              }
            });
          }
        },
        {
          title: '付款通道',
          align: 'left',
          width: 140,
          key: 'payWay',
          render: (h, params) => {
            const val = params.row.payWay;
            const list =
              vm.$store.state.createPaymentPreparation.valueSet.payWay || [];
            let text = '';
            list.forEach(function(item) {
              if (item.value == val) {
                text = item.label;
                return false;
              }
            });
            return h('div', {}, text);
          }
        },
        {
          title: '付款公司',
          align: 'left',
          width: 170,
          key: 'companyName'
        },
        {
          title: '银行账户名称',
          align: 'left',
          width: 200,
          key: 'accountName'
        },
        {
          title: '银行账号',
          align: 'left',
          width: 200,
          key: 'accountNo'
        },
        {
          title: '状态',
          align: 'left',
          minWidth: 100,
          key: 'statusName'
        },
        {
          title: '匹配付款总额',
          align: 'left',
          minWidth: 100,
          key: 'payAmount'
        },
        {
          title: '实际付款总额',
          align: 'left',
          minWidth: 100,
          key: 'actualPayAmount'
        },
        {
          title: '传送ERP总额',
          align: 'left',
          minWidth: 100,
          key: 'transErpAmount'
        }
      ]
    };
  },
  computed: {
    ...mapState('createPaymentPreparation', {
      tableData: state => state.paymentPreparationInfo.preparationInfo,
      applyPayExpandList: state =>
        state.paymentPreparationInfo.preparationInfo.applyPayExpandList,
      selections: state => state.paymentSelects.selections,
      basicInfoComponent: state => state.basicInfoComponent,
      paymentApplyInfoComponent: state => state.paymentApplyInfoComponent,
      paymentPreparationInfoComponent: state =>
        state.paymentPreparationInfoComponent
    }),
    // 复核按钮
    showAudit() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == '准备付款') {
          let amountFlag = false;
          for (let i = 0; i < this.selections.length; i++) {
            if (this.selections[i].payAmount < 0) {
              amountFlag = true;
              break;
            }
          }
          if (amountFlag) {
            this.auditStatus = true;
            return true;
          } else {
            this.auditStatus = false;
            return true;
          }
        } else {
          this.auditStatus = true;
          return true;
        }
      } else {
        this.auditStatus = true;
        return true;
      }
    },
    // 拒付按钮
    showRefuse() {
      if (this.selections.length > 0) {
        const refuseFlag = false;
        switch (this.selections[0].statusName) {
          case '复核':
            this.refuseStatus = false;
            // return true;
            break;
          case '外部系统支付失败':
            this.refuseStatus = false;
            break;
          case '已退票':
            if (this.selections[0].payWay == '1' || this.selections[0].payWay == '3' || this.selections[0].payWay == '5') {
              this.refuseStatus = false;
              // return true;
            } else {
              this.refuseStatus = true;
              // return true;
            }
            break;
        }
        // if (
        //   this.selections[0].statusName == "复核" ||
        //   this.selections[0].statusName == "外部系统支付失败" ||
        //   this.selections[0].statusName == "已退票"
        // ) {
        //   this.refuseStatus = false;
        //   return true;
        // } else {
        //   this.refuseStatus = true;
        //   return true;
        // }
        return true;
      } else {
        this.refuseStatus = true;
        return true;
      }
    },
    // 本地出纳cashier
    cashier() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == '复核') {
          let cashierFlag = false;
          for (let i = 0; i < this.selections.length; i++) {
            if (this.selections[i].payWay != '5') {
              cashierFlag = true;
              break;
            }
          }
          if (cashierFlag) {
            this.cashierStatus = true;
            return true;
          } else {
            this.cashierStatus = false;
            return true;
          }
        } else {
          this.cashierStatus = true;
          return true;
        }
      } else {
        this.cashierStatus = true;
        return true;
      }
    },
    // 支付按钮
    showPay() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == '复核') {
          let payFlag = false;
          for (let i = 0; i < this.selections.length; i++) {
            if (this.selections[i].payWay == '5') {
              payFlag = true;
              break;
            }
          }
          if (payFlag) {
            this.payStatus = true;
            return true;
          } else {
            this.payStatus = false;
            return true;
          }
        } else {
          this.payStatus = true;
          return true;
        }
      } else {
        this.payStatus = true;
        return true;
      }
    },
    // 撤销复核按钮
    showCannelAudit() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == '复核') {
          this.cannelAuditStatus = false;
          return true;
        } else {
          this.cannelAuditStatus = true;
          return true;
        }
      } else {
        this.cannelAuditStatus = true;
        return true;
      }
    },
    // 撤销按钮
    showCannel() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == '准备付款') {
          this.cannelStatus = false;
          return true;
        } else {
          this.cannelStatus = true;
          return true;
        }
      } else {
        this.cannelStatus = true;
        return true;
      }
    },
    // 重新付款按钮
    showReset() {
      if (this.selections.length > 0) {
        let resetFlag = false;
        switch (this.selections[0].statusName) {
          case '外部系统支付失败':
            this.resetStatus = false;
            break;
          case '提交支付失败':
            for (let i = 0; i < this.selections.length; i++) {
              if (this.selections[i].payWay == '5' || this.selections[0].payWay == '11') {
                resetFlag = true;
                break;
              }
            }
            if (resetFlag) {
              this.resetStatus = true;
            } else {
              this.resetStatus = false;
            }
            break;
          case '资金计划预占失败':
            this.resetStatus = false;
            break;
          case '已退票':
            for (let i = 0; i < this.selections.length; i++) {
              if (this.selections[0].payWay == '11') {
                resetFlag = true;
                break;
              }
            }
            if (resetFlag) {
              this.resetStatus = true;
            } else {
              this.resetStatus = false;
            }
            break;
        }
        // if (
        //   this.selections[0].statusName == "外部系统支付失败" ||
        //   this.selections[0].statusName == "提交支付失败" ||
        //   this.selections[0].statusName == "资金计划预占失败" ||
        //   this.selections[0].statusName == "已退票"
        // ) {
        //   this.resetStatus = false;
        //   return true;
        // } else {
        //   this.resetStatus = true;
        //   return true;
        // }
        return true;
      } else {
        this.resetStatus = true;
        return true;
      }
    },
    // 支付成功
    paySuccessBtn() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == '支付中') {
          let flag = false;
          for (let i = 0; i < this.selections.length; i++) {
            if (this.selections[i].payWay == '5' && this.selections[i].selectedCashier != vm.currentInfo.data.userCode) {
              flag = true;
              break;
            } else if (this.selections[i].payWay == '1' || this.selections[i].payWay == '3') {
              flag = true;
              break;
            }
          }
          if (flag) {
            this.paySuccessStatus = true;
            return true;
          } else {
            this.paySuccessStatus = false;
            return true;
          }
        } else {
          this.paySuccessStatus = true;
          return true;
        }
      } else {
        this.paySuccessStatus = true;
        return true;
      }
    },
    // 支付失败
    payFailBtn() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == '支付中') {
          let flag = false;
          for (let i = 0; i < this.selections.length; i++) {
            if (this.selections[i].payWay == '5' && this.selections[i].selectedCashier != vm.currentInfo.data.userCode) {
              flag = true;
              break;
            } else if (this.selections[i].payWay == '1' || this.selections[i].payWay == '3') {
              flag = true;
              break;
            }
          }
          if (flag) {
            this.payFailStatus = true;
            return true;
          } else {
            this.payFailStatus = false;
            return true;
          }
        } else {
          this.payFailStatus = true;
          return true;
        }
      } else {
        this.payFailStatus = true;
        return true;
      }
    },
    // 线下退票按钮
    offlineBtn() {
      if (this.selections.length > 0) {
        if (this.selections[0].statusName == 'ERP支付成功') {
          let flag = false;
          for (let i = 0; i < this.selections.length; i++) {
            if (this.selections[i].payWay != '11') {
              flag = true;
              break;
            }
          }
          if (flag) {
            this.offlineStatus = true;
            return true;
          } else {
            this.offlineStatus = false;
            return true;
          }
        } else {
          this.offlineStatus = true;
          return true;
        }
      } else {
        this.offlineStatus = true;
        return true;
      }
    }
  },
  created: function() {
    vm = this;
  },
  beforeMount: function() {},
  mounted: function() {
    window.bb = this;
    vm = this;
    vm.server.getCurrentEmployeeInfo().then(response => {
      if (response.statusCode == '-1') {
        vm.$Message.error(response.msg);
      } else {
        vm.currentInfo = response;
      }
    });
  },
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
    ...mapMutations('createPaymentPreparation', ['setPaymentPreparationInfo']),
    init() {
      const self = this;
      self.server.preparePayment({ batchId: self.batchId }).then(response => {
        if (response.stateCode == -1) {
          this.$Message.warning(response.msg);
        } else {
          const obj = {};
          obj.preparationInfo = response;
          self.setPaymentPreparationInfo(obj);
        }
      });
    },
    addOrUpdateDetail(obj) {
      // return;
      // if (this.reimburseInfo.claimId) {
      //   new this.$pageModal(
      //     AddOrUpReimDet,
      //     {
      //       props: {
      //         obj,
      //         $store: this.$store
      //       }
      //     },
      //     data => {
      //       if (data.action != 'cancel') {
      //         this.queryReimDetList();
      //         $store.dispatch(
      //           'supplierFeeAdvanceCharge/GetClaimHeadAndTravelInfo',
      //           { claimId: this.reimburseInfo.claimId }
      //         );
      //         this.disabled = true;
      //       }
      //     }
      //   );
      // } else {
      //   this.$Message.warning({
      //     content: '请先保存表单信息！',
      //     duration: 3
      //   });
      //   return;
    }
  },
  // 点击展开
  clickExpand(row, state) {
    if (state) {
      // const self = this;
    }
  },
  // 复核
  audit() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定要复核吗？', this).then(() => {
        const payLineDetailIds = [];
        for (let i = 0; i < vm.selections.length; i++) {
          payLineDetailIds.push(vm.selections[i].payLineDetailId);
        }
        self.auditLoad = true;
        vm.server
          .reviewPreparation({
            payLineDetailIds: payLineDetailIds,
            payBatchId: this.batchId
          })
          .then(response => {
            self.auditLoad = false;
            if (response.statusCode == '-1') {
              self.$Message.info(response.msg);
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            } else {
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.$Message.success('操作成功');
              self.selections = [];
            }
          });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 撤销复核
  cannelAudit() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定要撤销复核吗？', this).then(() => {
        const payLineDetailIds = [];
        for (let i = 0; i < vm.selections.length; i++) {
          payLineDetailIds.push(vm.selections[i].payLineDetailId);
        }
        self.cannelAuditLoad = true;
        vm.server
          .cancelReviewPreparation({
            payBatchId: this.batchId,
            payLineDetailIds: payLineDetailIds
          })
          .then(response => {
            self.cannelAuditLoad = false;
            if (response.statusCode == '-1') {
              self.$Message.info(response.msg);
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            } else {
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
              self.$Message.success('操作成功');
            }
          });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 撤销准备付款
  cannelPrepPayment() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定要撤销吗？', this).then(() => {
        const payLineDetailIds = [];
        for (let i = 0; i < self.selections.length; i++) {
          payLineDetailIds.push(self.selections[i].payLineDetailId);
        }
        self.cannelLoad = true;
        self.server
          .cannelPaymentPreparation({
            payBatchId: self.batchId,
            payLineDetailIds: payLineDetailIds
          })
          .then(response => {
            self.cannelLoad = false;
            if (response.statusCode == '-1') {
              self.$Message.info(response.msg);
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            } else {
              vm.selections = [];
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              vm.$Message.success('操作成功');
            }
          });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 重做付款准备
  resetPaymentprepare() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定要重做付款准备吗？', this).then(() => {
        const payLineDetailIds = [];
        for (let i = 0; i < self.selections.length; i++) {
          payLineDetailIds.push(self.selections[i].payLineDetailId);
        }
        self.resetLoad = true;
        self.server
          .reDoPaymentPreparation(payLineDetailIds)
          .then(response => {
            self.resetLoad = false;
            if (response.statusCode == '-1') {
              self.$Message.info(response.msg);
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            } else {
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.$Message.success('操作成功');
              self.selections = [];
            }
          });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 拒付
  refusalPay() {
    const self = this;
    if (self.selections.length > 0) {
      self.refuseLoad = true;
      let payLineDetailIds = '';
      for (let i = 0; i < vm.selections.length; i++) {
        if (i == 0) {
          payLineDetailIds = vm.selections[i].payLineDetailId;
        } else {
          payLineDetailIds += '-' + vm.selections[i].payLineDetailId;
        }
      }
      new this.$pageModal(
        RefuseContent,
        {
          props: {
            payHeadId: self.batchId,
            payLineDetailIds: payLineDetailIds,
            refuseDescr: '',
            server: self.server
          }
        },
        data => {
          if (data) {
            vm.basicInfoComponent.init();
            vm.paymentApplyInfoComponent.init();
            vm.paymentPreparationInfoComponent.init();
            self.selections = [];
            self.refuseLoad = false;
          }
        }
      );
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 支付
  payMent() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定要支付吗？', this).then(() => {
        const payLineDetailIds = [];
        for (let i = 0; i < vm.selections.length; i++) {
          payLineDetailIds.push(vm.selections[i].payLineDetailId);
        }
        self.payLoad = true;
        self.server
          .payPreparation({
            payLineDetailIds: payLineDetailIds
          })
          .then(response => {
            self.payLoad = false;
            if (response.statusCode == '-1') {
              self.$Message.info(response.msg);
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            } else {
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.$Message.success(response.msg);
              self.selections = [];
            }
          });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 通知本地出纳
  noticeCashier() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定要通知本地出纳吗？', this).then(() => {
        const cashierParam = self.selections;
        let cashierFlag = false;
        for (let i = 0; i < self.selections.length; i++) {
          if (
            self.selections[i].companyCode != self.selections[0].companyCode
          ) {
            cashierFlag = true;
            break;
          }
        }
        if (cashierFlag) {
          self.$Message.info('请保持付款公司一致');
          return;
        }
        new this.$pageModal(Cashier, {
          props: {
            title: '选择本地出纳',
            cashierParam: cashierParam
          },
          on: {
            handleSelect: () => {
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            }
          }
        });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 支付成功
  paySuccess() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定支付成功吗？', this).then(() => {
        const payLineDetailIds = [];
        for (let i = 0; i < vm.selections.length; i++) {
          payLineDetailIds.push(self.selections[i].payLineDetailId);
        }
        self.paySuccessLoad = true;
        self.server
          .paySuccess({
            payLineDetailIds: payLineDetailIds
          })
          .then(response => {
            self.paySuccessLoad = false;
            if (response.statusCode == '-1') {
              self.$Message.info(response.msg);
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            } else {
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.$Message.success(response.msg);
              self.selections = [];
            }
          });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 支付失败
  payFail() {
    const self = this;
    if (self.selections.length > 0) {
      new $confirm('你确定支付失败吗？', this).then(() => {
        const payLineDetailIds = [];
        for (let i = 0; i < vm.selections.length; i++) {
          payLineDetailIds.push(self.selections[i].payLineDetailId);
        }
        self.payFailLoad = true;
        self.server
          .payFail({
            payLineDetailIds: payLineDetailIds
          })
          .then(response => {
            self.payFailLoad = false;
            if (response.statusCode == '-1') {
              self.$Message.info(response.msg);
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.selections = [];
            } else {
              vm.basicInfoComponent.init();
              vm.paymentApplyInfoComponent.init();
              vm.paymentPreparationInfoComponent.init();
              self.$Message.success(response.msg);
              self.selections = [];
            }
          });
      });
    } else {
      self.$Message.info('至少选择一条付款准备明细行');
    }
  },
  // 线下退票
  offlineFail() {

  }

};
</script>
