<!-- @desc:付款单  @auth:付斌  @time:2024/2/21 14:06 -->
<template>
  <div class="main-div" ref="mainDiv">
    <loading :loading="loading" v-if="!modalVisible"></loading>
    <!--按钮区-->
    <BaseIndexButtonGroup ref="BaseIndexButtonGroup" id="BaseIndexButtonGroup">
      <template #left>
        <BaseIndexButton ref="search" name="search"></BaseIndexButton>
        <BaseIndexButton ref="clear" name="clear"></BaseIndexButton>
        <BaseIndexButton ref="payment" name="payment" :isMustChooseFlag="false"></BaseIndexButton>
        <BaseIndexButton ref="refund" name="refund" :isMustChooseFlag="false"></BaseIndexButton>
        <BaseIndexButton ref="invalid" name="invalid"></BaseIndexButton>
        <!--打印-->
        <BaseIndexButton ref="print" name="print"></BaseIndexButton>
      </template>
    </BaseIndexButtonGroup>

    <!--  卡片区  -->
    <div class="index-card">

      <!--  查询条件区域  -->
      <div id="search-cond-div" ref="search-cond-div" class="search-cond-class">
        <SearchCond ref="searchCond" v-model="searchCond"
                    @collapse-change="collapseChange" :setFlag="false"
                    :searchContent="searchContent"
        ></SearchCond>
      </div>

      <!--  表格部分  -->
      <DkSplit mode="vertical" v-model="split" :height="tableHeight">
        <div slot="top">
          <DkTable slot="top"
                   :id="'table-'+$options.name"
                   ref="table-select"
                   :data="tableData"
                   showFooter
                   :height="this.tableHeight * split - 36"
                   primaryKey="rpId"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :current-page="pageInfo.currentPage"
                   :choose-flag="false"
                   @current-change="currentChange($event)"
                   :page-size-opts="pageSizeOpts"
                   :page-size="pageInfo.pageSize"
                   name="table">
            <DkTableColumn field="rpNo" :title="$t('paymentNo')"/>
            <DkTableColumn field="biznisNo"/>
            <DkTableColumn field="rpTypeName" :title="$t('paymentTypeName')"/>
            <DkTableColumn field="supName"/>
            <DkTableColumn field="sumAmtPay" :digits="2" dataType="number" sum/>
            <DkTableColumn field="sumShouldHandle" :digits="2" dataType="number" sum/>
            <DkTableColumn field="sumUsePaymentResidue" :digits="2" dataType="number" sum/>
            <DkTableColumn field="sumAmtPayableHandle" :digits="2" dataType="number" sum/>
            <DkTableColumn field="accDate" :title="$t('paymentDate')"/>
            <DkTableColumn field="orgName"/>
            <DkTableColumn field="staffName"/>
            <DkTableColumn field="makeStaffName"/>
            <DkTableColumn field="makeTime" data-type="dateTime"/>
            <DkTableColumn field="remarks"/>
          </DkTable>
        </div>
        <div slot="bottom">
          <DkTabs v-model="detailModel" :options="detailOptions" @on-click="detailTabsClick"></DkTabs>
          <!--付款明细-->
          <div v-show="visRpItemFlag">
            <DkTable :id="'table-'+$options.name" ref="rpItem-select" :data="tableDataDetail_rpItem" showFooter
                     :height="this.tableHeight * (1-split)-62" primaryKey="itemId" name="tableRpItem">
              <DkTableColumn field="macName"/>
              <DkTableColumn field="amtPay" :digits="2" dataType="number" sum/>
              <DkTableColumn field="remarks"/>
            </DkTable>
          </div>
          <div v-show="visRecPayHandleItemFlag">
            <DkTable :id="'table-'+$options.name" ref="recPayHandleItem-select" :data="tableDataDetail_recPayHandleItem"
                     showFooter
                     :height="this.tableHeight * (1-split)-62" primaryKey="itemId" name="tableRecPayHandleItem">
              <DkTableColumn field="biznisNo"/>
              <DkTableColumn field="accDate"/>
              <DkTableColumn field="amtShould" :title="$t('amtShouldPayable')" :digits="2" dataType="number" sum/>
              <DkTableColumn field="amtPayableHandle" :digits="2" dataType="number" sum/>
              <DkTableColumn field="amtWaive" :title="$t('amtWaiveHandle')" :digits="2" dataType="number" sum/>
            </DkTable>
          </div>
          <!--附件-->
          <div v-show="visFilesDetailFlag">
            <DkTable :pageFlag="false" :pageTotalFlag="false" :id="'table-'+$options.name"
                     name="table" ref="file-select" :data="tableDataDetail_files" :height="this.tableHeight * (1-split)-62">
              <DkTableColumn field="name" :title="$t('fileName')"/>
              <DkTableColumn field="path" :imageUrl="$config.imgUrl" type="image" :title="$t('yuLan')"/>
            </DkTable>
          </div>
        </div>
      </DkSplit>
    </div>

    <PrintModal ref="printModal" :doc-name="$config.docCode.payment"
                :postName="'payment'"
                :id="$refs?.['table-select']?.batchRows?.[0]?.rpId||null"/>
  </div>
</template>

<script>
import {indexMixin} from "@/mixins";
import PrintModal from '_c/business/print/modal'

export default {
  name: 'payment',
  mixins: [indexMixin],
  components: {PrintModal},
  data() {
    let self = this
    return {
      tableData: [],
      tableDataDetail_rpItem: [],
      tableDataDetail_recPayHandleItem: [],
      tableDataDetail_files: null,
      detailModel: 'collectionDetail',
      detailOptions: [
        {label: self.$t('collectionDetail'), name: 'collectionDetail'},//付款明细
        {label: self.$t('payHandleDetail'), name: 'payHandleDetail'}, //抵扣明细
        {label: this.$t('filesDetail'), name: 'filesDetail'},
      ],
      visRpItemFlag: true,
      visRecPayHandleItemFlag: false,
      visFilesDetailFlag: false,
      searchContent: [
        {
          itemCode: '付款单号-文本',
          itemName: 'paymentNo',
          valueFormat: {code: 'rpNo'},
          valueKind: 'STR'
        },
        {
          itemCode: '供应商名称-文本',
          itemName: 'supName',
          valueFormat: {code: 'supName'},
          valueKind: 'STR'
        },
        {
          itemCode: '业务部门-文本',
          itemName: 'orgId',
          valueFormat: {
            code: 'orgIdList',
            data: [],
            valueKey: 'orgId',
            labelKey: 'orgName',
          },
          valueKind: 'M-CHOICE'
        },
        {
          itemCode: '业务员-文本',
          itemName: 'staffId',
          valueFormat: {
            code: 'staffIdList',
            data: [],
            valueKey: 'staffId',
            labelKey: 'staffCodeName',
          },
          valueKind: 'M-CHOICE'
        },
        {
          itemCode: '付款类型-下拉',
          itemName: 'paymentType',
          valueFormat: {
            code: 'rpTypeList',
            data: [
              {rpType: '收付款类型-付款', rpTypeName: '付款'},
              {rpType: '收付款类型-退付款', rpTypeName: '退款'}],
            valueKey: 'rpType',
            labelKey: 'rpTypeName',
            default: []
          },
          valueKind: 'M-CHOICE'
        },
        // {
        //   itemCode: '资金账户-文本',
        //   itemName: 'paymentMac',
        //   valueFormat: {
        //     code: 'macList',
        //     data: [],
        //     valueKey: 'macId',
        //     labelKey: 'macName',
        //   },
        //   valueKind: 'M-CHOICE'
        // },
        {
          itemCode: '制单时间-时间',
          itemName: 'makeTime',
          valueFormat: {
            code: 'makeTime',
            default: [new Date().addMonths(-1).format('yyyy-MM-dd'), new Date().format('yyyy-MM-dd')]
          },
          valueKind: 'DATE_RANGE',
          // required: true,
        },
        self.$config.flgValidSearch
      ],
    }
  },
  methods: {
    /**
     * @desc   : 选择模板、打印机
     * @author : 洪旭东
     * @date   : 2023-04-10 13:45
     */
    print() {
      this.$refs.printModal.showChooseTemplate = true
    },

    /**
     * @desc   : 加载数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    initData() {
      //查询业务部门
      this.getOrg()
      //查询业务员
      this.getStaff()
      // //查询资金账户
      // this.getMac()
    },
    /**
     * @desc   : 查询业务部门
     * @author : 付斌
     * @date   : 2024-02-26 14:27
     */
    getOrg() {
      this.excute(this.$service.commonService, this.$service.commonService.getOrg, {}).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          let list = this.searchContent.filter(it => it.valueFormat.code == 'orgIdList')
          if (list.length > 0) {
            list[0].valueFormat.data = res.data
          }
        }
      })
    },
    /**
     * @desc   : 查询业务员
     * @author : 付斌
     * @date   : 2024-02-26 14:27
     */
    getStaff() {
      this.excute(this.$service.commonService, this.$service.commonService.getStaff, {}).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          let list = this.searchContent.filter(it => it.valueFormat.code == 'staffIdList')
          if (list.length > 0) {
            list[0].valueFormat.data = res.data
          }
        }
      })
    },
    // /**
    //  * @desc   : 查询资金账户
    //  * @author : 付斌
    //  * @date   : 2024-02-26 14:27
    //  */
    // getMac() {
    //   this.excute(this.$service.commonService, this.$service.commonService.getMac, {}).then(res => {
    //     if (res.code === this.$config.SUCCESS_CODE) {
    //       let list = this.searchContent.filter(it => it.valueFormat.code == 'macList')
    //       if (list.length > 0) {
    //         list[0].valueFormat.data = res.data
    //       }
    //     }
    //   })
    // },
    /**
     * @desc   : 查询数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    getData(params) {
      this.tableData = [];
      this.tableDataDetail_rpItem = null;
      this.tableDataDetail_recPayHandleItem = null;
      return this.excute(this.$service.paymentService, this.$service.paymentService.selectByCond, params)
    },
    /**
     * @desc   : 行改变事件
     * @author : 付斌
     * @date   : 2024-02-28 11:06
     */
    currentChange({row}) {
      this.loading = true
      this.tableDataDetail_rpItem = null;
      this.tableDataDetail_recPayHandleItem = null;
      this.excuteNoParam(this.$service.paymentService, this.$service.paymentService.selectRpInfoById, [row.rpId], false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableDataDetail_rpItem = res.data.recPayItem;
          this.tableDataDetail_recPayHandleItem = res.data.recPayHandleItem;
        }
        this.loading = false
      })
      this.tableDataDetail_files = row.annexPaths
    },
    /**
     * @desc   : 作废
     * @author : 付斌
     * @date   : 2024-03-05 8:56
     */
    invalid(rows) {
      if (rows && rows.length > 0) {
        this.$IBMessage({content: this.$t('Q_002', {'param': this.$v('invalid')}), title: this.$t('systemQuestion')},
          {
            ok: () => {
              this.loading = true
              this.excuteNoParam(this.$service.paymentService, this.$service.paymentService.invalid, [rows[0].rpId], false).then(res => {
                if (res.code === this.$config.SUCCESS_CODE) {
                  this.$Message.success(this.$t('I_001', {'param': this.$t('invalid')}))
                } else {
                  this.$Message.warning(res.message)
                }
                this.loading = false
              })
            },
          })
      }
    },
    /**
     * @desc   : 点击Tab进行切换
     * @author : 付斌
     * @date   : 2024-02-19 11:33
     */
    detailTabsClick(val) {
      if (val === 'payHandleDetail') {
        this.visRecPayHandleItemFlag = true
        this.visRpItemFlag = false
        this.visFilesDetailFlag = false
      } else if (val === 'collectionDetail') {
        this.visRecPayHandleItemFlag = false
        this.visRpItemFlag = true
        this.visFilesDetailFlag = false
      } else if (val === 'filesDetail') {
        this.visRecPayHandleItemFlag = false
        this.visRpItemFlag = false
        this.visFilesDetailFlag = true
      }
    },
    /**
     * @desc   :校验按钮
     * @author : 付斌
     * @date   : 2024-03-09 16:58
     */
    openValidData(btnName, rows) {
      if (btnName === 'invalid') {
        if (rows && rows.length > 0) {
          if (rows[0].biznisId) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_124')}));
            return false;
          } else if (!rows[0].flgValid) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_122')}));
            return false;
          }
        }
      }
      return true;
    },
  },
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 付斌
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.primaryKey = 'rpId'  // 设置主键Id
    this.routeObjName = 'payment'  // 设置路由名称
  },
}
</script>

<style scoped>

</style>
