<!-- @desc:其它支出新建  @auth:付斌  @time:2024/02/20 15:15 -->
<template>

  <div class="main-div" style="position: relative;">
    <DkCollapse @on-change="changeCollapse" ref="collapse">
      <DkPanel prop="essentialInformation">
        <!--  单据信息  -->
        <DkForm slot="content" ref="formInline" v-model="formData" :labelMaxWords="6" style="width:100%">
          <!--供应商-->
          <DkFormItem prop="objectId" :label="$t('supName')" :required="true">
            <SelectMagnifier v-model="formData.objectId" :display-text="formData.supName"
                             :type="this.$config.MagnifierType.supplier"
                             :multiple="false"
                             @ok="supplierOk"/>
          </DkFormItem>
          <!--业务部门-->
          <DkFormItem prop="orgId" :label="$t('orgId')">
            <SelectMagnifier v-model="formData.orgId" :display-text="formData.orgName"
                             :type="this.$config.MagnifierType.org"
                             :multiple="false"/>
          </DkFormItem>
          <!--业务员-->
          <DkFormItem prop="staffId" :label="$t('staffId')">
            <SelectMagnifier v-model="formData.staffId" :display-text="formData.staffName"
                             :type="this.$config.MagnifierType.staff"
                             :multiple="false"/>
          </DkFormItem>
          <!--账务日期-->
          <DkFormItem prop="accDate" :data-type="$config.dataType.date">
            <DatePickerPop v-model="formData.accDate"
                           :short-cut-flag="true"/>
          </DkFormItem>
          <!--合计支出-->
          <DkFormItem prop="sumAmtPayable">
            <InputNumberPop ref="sumAmtPayable" v-model="formData.sumAmtPayable" :disabled="true"/>
          </DkFormItem>
          <!-- 合计收款 -->
          <DkFormItem prop="sumAmtPay">
            <InputNumberPop ref="sumAmtPay" v-model="formData.sumAmtPay" :digits="2" :disabled="true"
                            :min="-9999999999"/>
          </DkFormItem>
          <!--备注-->
          <DkFormItem prop="remarks" :span="12">
            <InputPop ref="remarks" v-model="formData.remarks"
                      maxlength=500 textareaFlag/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
      <!--其它支出项目-->
      <DkPanel prop="customerReceipt">
        <div id="search-cond-div-payable-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable ref="payableTable" :data="formData.itemList" showFooter
                     major-field="payableId"
                     :height="this.tableHeight/2"
                     :new-row-flag="true"
                     :columns="payableColumns"
                     @on-choose="getPayableTotals" @changeValue="getPayableTotals"/>
        </div>
      </DkPanel>
      <!--供应商付款-->
      <DkPanel prop="supplierPayment">
        <div id="search-cond-div-payment-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable ref="paymentTable" :data="formData.paymentList" showFooter
                     major-field="rpId"
                     :height="this.tableHeight/2"
                     :new-row-flag="true"
                     :columns="paymentColumns"
                     controlId="macId" @on-choose="getTotals" @changeValue="getTotals"/>
        </div>
      </DkPanel>
      <!--附件-->
      <DkPanel prop="files">
        <div id="search-cond-div-files-info" ref="search-cond-div-files-info" slot="content">
          <DkPicWall v-model="formData.annexPaths"
                     :table="$config.tables.outlay" :accept="$config.uploadFileConfig.acceptPicType"
                     :format="['jpg','jpeg','png']"></DkPicWall>
        </div>
      </DkPanel>
    </DkCollapse>
    <!--      下部分按钮区域-->
    <DkSaveButton ref="saveButton" :loading="loading" @save="save(true)" @close="close"/>
  </div>
</template>

<script>
import {formMixin} from '@/mixins/form'

export default {
  components: {},
  mixins: [formMixin],
  data() {
    let self = this
    return {
      loading: false,
      payableTypeList: [],
      // 画面表单数据
      formData: {
        accDate: new Date(),
        objectId: null,//供应商Id
        supName: null,
        staffId: null,
        staffName: null,
        staffCode: null,
        orgId: null,
        orgName: null,
        orgCode: null,
        sumAmtPayable: null,
        sumAmtPay: null,
        remarks: null,
        annexPaths: null,
        makeStaff: self.$store.state.user.id,
        cpId: self.$store.state.user.cpId,
        itemList: [],
        paymentList: [],
      },
      paymentColumns: [
        {
          field: 'macName',
          controlId: 'macId',
          type: 'tableSelect',
          width: 200,
          dataType: self.$config.tableSelectType.moneyAccount,
          fieldUpdate: self.$updateColumns.paymentChooseMac,
          searchDetailFlag: true
        },
        //付款金额
        {
          field: 'amtPay',
          type: 'number',
          digits: 2,
          negative: () => self.negativeFlag,
          sum: true,
        },
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
      payableColumns: [
        {
          field: 'payableType',
          type: 'select',
          options: () => self.payableTypeList,
          labelKey: 'dataValue',
          valueKey: 'dataId',
          searchDetailFlag: true,
          width: 'auto'
        },
        //支出数量
        {
          field: 'qtyPayable',
          type: 'number',
          digits: 0,
          default: 1,
          sum: true,
        },
        // 支出金额
        {
          field: 'amtPayable',
          type: 'number',
          title: this.$t('zhiChuAmt'),
          digits: 2,
          sum: true,
        },
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
    }
  },
  methods: {
    /**
     * @desc   : 加载数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    initData() {
      this.getDictionaryData();

      this.formData.staffId = this.$store.state.user.id
      this.formData.staffName = this.$store.state.user.staffName
      this.formData.orgId = this.$store.state.user.orgId
      this.formData.orgName = this.$store.state.user.orgName
    },
    /**
     * @desc   :获取支出类别
     * @author : 付斌
     * @date   : 2024-04-01 14:01
     */
    getDictionaryData() {
      let params = {
        'dictCode': this.$config.kindCode.basicOutlay,
      }
      this.excute(this.$service.commonService, this.$service.commonService.getDictionaryData, params, false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.payableTypeList = res.data
        }
      })
    },
    /**
     *   @desc   : 获取供应商资料并且监听每一次的供应商修改
     *   @date   : 2022/5/24 15:04
     *   @author : 付斌
     */
    supplierOk(val) {
      if (val && val.length > 0) {
        this.formData.objectId = val[0].supplierId
        this.formData.supName = val[0].supplierName
      }
    },
    /**
     *   @desc   : 计算合计
     *   @date   : 2022/5/30 15:31
     *   @author : 付斌
     */
    getPayableTotals() {
      let table = this.$refs['payableTable'].tableData
      this.formData.sumAmtPayable = table.sum('amtPayable');
    },
    /**
     *   @desc   : 付款计算合计
     *   @date   : 2022/5/30 15:31
     *   @author : 付斌
     */
    getTotals() {
      let table = this.$refs['paymentTable'].tableData
      if (table && table.length > 0) {
        this.formData.sumAmtPay = table.sum('amtPay');
      }
    },
    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2022/03/04 13:17
     */
    detail(id) {
      return this.excuteNoParam(this.$service.otherPayableService, this.$service.otherPayableService.getOtherPayableForUpdate, [id]);
    },
    /**
     * @desc   : 编辑页面赋值
     * @author : 付斌
     * @date   : 2024-03-03 9:33
     */
    setValuesByEdit(data) {
      if (data) {
        this.formData = data["data"];
        this.formData.itemList = data["dataItem"];
        this.formData.paymentList = data["paymentItem"];
      }
    },
    /**
     * @desc   : 校验数据
     * @author : 付斌
     * @date   : 2024-03-11 14:37
     */
    validData() {
      let itemTable = this.$refs['payableTable'].getTableDataFilter();
      if (!itemTable || itemTable.length == 0) {
        this.$Message.warning(this.$t('W_012'))
        return false;
      }
      for (let it of itemTable) {
        if (!it.amtPayable || it.amtPayable === '') {
          this.$Message.warning(this.$t('W_042', {'param': '支出金额不能'}))
          return false;
        }
      }
      return true;
    },
    /**
     * @desc   : 给参数赋值
     * @author : 付斌
     * @date   : 2023/6/25 14:31
     */
    setParams(arg) {
      this.formData.accDate = new Date(this.formData.accDate).toDateStr();
      // 修改新增的表数据
      let itemTable = this.$refs['payableTable'].getTableDataFilter();
      this.formData.itemList = itemTable
      // 修改新增的表数据
      let paymentTable = this.$refs['paymentTable'].getTableDataFilter();
      this.formData.paymentList = paymentTable
      this.params = {...this.formData}
    },
    /**
     * @desc   : 保存数据
     * @author : 付斌
     * @date   : 2023/6/25 14:31
     */
    saveData() {
      if (this.type === this.$config.formMode.add) {
        return this.excute(this.$service.otherPayableService, this.$service.otherPayableService.insert, this.params);
      } else if (this.type === this.$config.formMode.edit) {
        return this.excute(this.$service.otherPayableService, this.$service.otherPayableService.update, this.params)
      }
    },
    /**
     * @desc   :新建完毕后清空窗体
     * @author : 付斌
     * @date   : 2024-02-27 10:33
     */
    clear() {
      let self = this
      this.formData = {
        accDate: new Date(),
        objectId: null,//供应商Id
        supName: null,
        staffId: null,
        staffName: null,
        staffCode: null,
        orgId: null,
        orgName: null,
        orgCode: null,
        sumAmtPayable: null,
        sumAmtPay: null,
        remarks: null,
        annexPaths: null,
        makeStaff: self.$store.state.user.id,
        cpId: self.$store.state.user.cpId,
        itemList: [],
        paymentList: [],
      }
    },
  }
  ,
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 付斌
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.focusItem = 'payableId';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'payableId'  // 设置主键Id
  }
}
</script>
