<!-- @desc:其它支出单  @auth:付斌  @time:2024/02/20 15:15 -->
<template>
  <div class="main-div">
    <BaseIndexButtonGroup id="BaseIndexButtonGroup">
      <template #left>
        <BaseIndexButton ref="search" name="search"></BaseIndexButton>
        <BaseIndexButton ref="clear" name="clear"></BaseIndexButton>
        <BaseIndexButton ref="edit" name="edit"></BaseIndexButton>
        <BaseIndexButton ref="invalid" name="invalid"></BaseIndexButton>
        <!--打印-->
        <BaseIndexButton ref="print" name="print"></BaseIndexButton>
      </template>
      <template #right>
        <BaseIndexButton ref="add" name="add"></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="payableId"
                   @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 :type="$libaray.checkButtonHasRight('payable-edit')?'link':''" field="payableNo"
                           :tree-node="true" @on-link="edit"/>
            <DkTableColumn field="supName"/>
            <DkTableColumn field="sumAmtPayable" :digits="2" dataType="number" sum/>
            <DkTableColumn field="accDate" data-type="date"/>
            <DkTableColumn field="orgName"/>
            <DkTableColumn field="staffName"/>
            <DkTableColumn field="makeStaffName"/>
            <DkTableColumn field="makeTime" data-type="dateTime"/>
            <DkTableColumn field="flgValid" type="switch"/>
            <DkTableColumn field="remarks"/>
          </DkTable>
        </div>
        <div slot="bottom">
          <DkTabs v-model="detailModel" :options="detailOptions" @on-click="detailTabsClick"></DkTabs>
          <!--其他支出单明细-->
          <div v-show="visItemFlag">
            <DkTable :id="'table-'+$options.name" ref="detail-select" :data="tableDataDetail_otherPayableItem" showFooter
                     :height="this.tableHeight * (1-split)-62" primaryKey="itemId" name="table">
              <DkTableColumn field="payableTypeName"/>
              <DkTableColumn field="qtyPayable" dataType="number" sum/>
              <DkTableColumn field="amtPayable" :title="$t('shouRuAmt')" :digits="2" dataType="number" sum/>
              <DkTableColumn field="remarks"/>
            </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.outlay"
                :postName="'other_payable'"
                :id="$refs?.['table-select']?.batchRows?.[0]?.payableId||null"/>
  </div>
</template>

<script>

import {indexMixin} from '@/mixins'
import PrintModal from '_c/business/print/modal'

export default {
  name: 'payable',
  mixins: [indexMixin],
  components: {PrintModal},
  data() {
    let self = this
    return {
      tableData: [],
      tableDataDetail_otherPayableItem: [],  // 用于存放其他支出单明细数据
      tableDataDetail_files: null,
      detailModel: 'otherPayableDetail',
      detailOptions: [
        {label: self.$t('otherPayableDetail'), name: 'otherPayableDetail'},//收款明细
        {label: this.$t('filesDetail'), name: 'filesDetail'}
      ],
      visItemFlag: true,
      visFilesDetailFlag: false,
      searchContent: [
        {
          itemCode: '其他支出单单号-文本',
          itemName: 'payableNo',
          valueFormat: {code: 'payableNo'},
          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: '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()
    },
    /**
     * @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-20 15:53
     */
    getData(params) {
      this.tableData = [];
      this.tableDataDetail_otherPayableItem = [];
      return this.excute(this.$service.otherPayableService, this.$service.otherPayableService.selectByCond, params)
    },

    /**
     * @desc   : 行改变事件
     * @author : 付斌
     * @date   : 2024-02-28 11:06
     */
    currentChange({row}) {
      this.loading = true
      this.excuteNoParam(this.$service.otherPayableService, this.$service.otherPayableService.selectOtherPayableInfoById, [row.payableId], false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.tableDataDetail_otherPayableItem = res.data.otherPayableItem;
        }
        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.otherPayableService, this.$service.otherPayableService.invalid, [rows[0].payableId], 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   : 2023/8/31 14:58
     */
    detailTabsClick(val) {
      if (val === 'otherPayableDetail') {
        this.visItemFlag = true
        this.visFilesDetailFlag = false
      } else if (val === 'filesDetail') {
        this.visItemFlag = false
        this.visFilesDetailFlag = true
      }
    },
    /**
     * @desc   :校验按钮
     * @author : 付斌
     * @date   : 2024-03-09 16:58
     */
    openValidData(btnName, rows) {
      if (btnName === 'edit') {
        if (rows && rows.length > 0) {
          if (rows[0].businessType === 0) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_121')}));
            return false;
          } else if (!rows[0].flgValid) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_122')}));
            return false;
          }
        }
      } else if (btnName === 'invalid') {
        if (rows && rows.length > 0) {
          if (rows[0].businessType === 0) {
            this.$Message.warning(this.$t('W_007', {'param': this.$t('W_121')}));
            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() {
    // console.log('this.$store.state.user.cpId',this.$store.state.user)
    this.primaryKey = 'payableId'  // 设置主键Id
    this.routeObjName = 'otherPayable'  // 设置路由名称
  },
}
</script>

