<!-- @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="cusPhone" :required="true">
            <SelectMagnifier v-model="formData.cusPhone" ref="cusPhone" :telephone="true"
                             primary-key="objectId" :multiple="false"
                             :displayText="formData.cusPhone"
                             :type="this.$config.MagnifierType.customer"
                             @on-input-blur="selectCustomer"
                             @ok="selectCustomer"/>
          </DkFormItem>
          <!--客户名称-->
          <DkFormItem prop="cusName">
            <InputPop ref="cusName" :disabled="true" v-model="formData.cusName"/>
          </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="sumAmtReceivable">
            <InputNumberPop ref="sumAmtReceivable" v-model="formData.sumAmtReceivable" :disabled="true"/>
          </DkFormItem>
          <!-- 合计收款 -->
          <DkFormItem prop="sumAmtRec">
            <InputNumberPop ref="sumAmtRec" v-model="formData.sumAmtRec" :digits="2" :disabled="true"
                            :min="-9999999999"/>
          </DkFormItem>
          <!--备注-->
          <DkFormItem prop="remarks">
            <InputPop ref="remarks" v-model="formData.remarks"
                      maxlength=500 textareaFlag/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
      <!--其它收入项目-->
      <DkPanel prop="customerReceipt">
        <div id="search-cond-div-receivable-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable ref="receivableTable" :data="formData.itemList" showFooter
                     major-field="receivableId"
                     :height="this.tableHeight/2"
                     :new-row-flag="true"
                     :columns="receivableColumns"
                     @on-choose="getReceivableTotals" @changeValue="getReceivableTotals"/>
        </div>
      </DkPanel>
      <!--客户收款-->
      <DkPanel prop="customerReceipt">
        <div id="search-cond-div-receipt-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable ref="receiptTable" :data="formData.receiptList" showFooter
                     major-field="rpId"
                     :height="this.tableHeight/2"
                     :new-row-flag="true"
                     :columns="receiptColumns"
                     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.income" :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,
      receivableTypeList: [],
      // 画面表单数据
      formData: {
        accDate: new Date(),
        objectId: null,//客户Id
        cusName: null,
        cusPhone: null,
        staffId: null,
        staffName: null,
        staffCode: null,
        orgId: null,
        orgName: null,
        orgCode: null,
        sumAmtReceivable: null,
        sumAmtRec: null,
        remarks: null,
        annexPaths: null,
        makeStaff: self.$store.state.user.id,
        cpId: self.$store.state.user.cpId,
        itemList: [],
        receiptList: [],
      },
      receivableColumns: [
        {
          field: 'receivableType',
          type: 'select',
          options: () => self.receivableTypeList,
          labelKey: 'dataValue',
          valueKey: 'dataId',
          searchDetailFlag: true,
          width: 'auto'
        },
        //收入数量
        {
          field: 'qtyReceivable',
          type: 'number',
          digits: 0,
          default: 1,
          min: -9999999999,
          sum: true,
        },
        // 收入金额
        {
          field: 'amtReceivable',
          type: 'number',
          title: this.$t('shouRuAmt'),
          digits: 2,
          min: -9999999999,
          sum: true,
        },
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
      receiptColumns: [
        {
          field: 'macName',
          controlId: 'macId',
          type: 'tableSelect',
          width: 200,
          dataType: self.$config.tableSelectType.moneyAccount,
          fieldUpdate: self.$updateColumns.receiptChooseMac,
          searchDetailFlag: true
        },
        //收款金额
        {
          field: 'amtRec',
          type: 'number',
          digits: 2,
          negative: () => self.negativeFlag,
          sum: true,
        },
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
    }
  },
  methods: {
    /**
     * @desc   : 加载数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    initData() {
      this.getDictionaryData();
    },
    /**
     * @desc   :获取收入类别
     * @author : 付斌
     * @date   : 2024-04-01 14:01
     */
    getDictionaryData() {
      let params = {
        'dictCode': this.$config.kindCode.basicIncome,
      }
      this.excute(this.$service.commonService, this.$service.commonService.getDictionaryData, params, false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.receivableTypeList = res.data
        }
      })
    },
    /**
     *   @desc   : 获取客户资料并且监听每一次的客户修改
     *   @date   : 2022/5/24 15:04
     *   @author : 付斌
     */
    selectCustomer(val) {
      if (val && val.length > 0) {
        this.formData.objectId = val[0].cusId
        this.formData.cusPhone = val[0].cusPhone
        this.formData.cusName = val[0].cusName
        this.formData.orgId = val[0].orgId
        this.formData.orgName = val[0].orgName
        this.formData.staffId = val[0].staffId
        this.formData.staffName = val[0].staffName
      }
    },
    /**
     *   @desc   : 计算合计
     *   @date   : 2022/5/30 15:31
     *   @author : 付斌
     */
    getReceivableTotals(field, row, rowIndex) {
      let table = this.$refs['receivableTable'].tableData
      this.formData.sumAmtReceivable = table.sum('amtReceivable');
    },
    /**
     *   @desc   : 收款计算合计
     *   @date   : 2022/5/30 15:31
     *   @author : 付斌
     */
    getTotals() {
      let table = this.$refs['receiptTable'].tableData
      if (table && table.length > 0) {
        this.formData.sumAmtRec = table.sum('amtRec');
      }
    },
    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2022/03/04 13:17
     */
    detail(id) {
      return this.excuteNoParam(this.$service.otherReceivableService, this.$service.otherReceivableService.getOtherReceivableForUpdate, [id]);
    },
    /**
     * @desc   : 编辑页面赋值
     * @author : 付斌
     * @date   : 2024-03-03 9:33
     */
    setValuesByEdit(data) {
      if (data) {
        this.formData = data["data"];
        this.formData.itemList = data["dataItem"];
        this.formData.receiptList = data["receiptItem"];
      }
    },
    /**
     * @desc   : 校验数据
     * @author : 付斌
     * @date   : 2024-03-11 14:37
     */
    validData() {
      let itemTable = this.$refs['receivableTable'].getTableDataFilter();
      if (!itemTable || itemTable.length == 0) {
        this.$Message.warning(this.$t('W_012'))
        return false;
      }
      for (let it of itemTable) {
        if (!it.amtReceivable || it.amtReceivable === '') {
          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['receivableTable'].getTableDataFilter();
      this.formData.itemList = itemTable
      // 修改新增的表数据
      let receiptTable = this.$refs['receiptTable'].getTableDataFilter();
      this.formData.receiptList = receiptTable
      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.otherReceivableService, this.$service.otherReceivableService.insert, this.params);
      } else if (this.type === this.$config.formMode.edit) {
        return this.excute(this.$service.otherReceivableService, this.$service.otherReceivableService.update, this.params)
      }
    },
    /**
     * @desc   :新建完毕后清空窗体
     * @author : 付斌
     * @date   : 2024-02-27 10:33
     */
    clear() {
      let self = this
      this.formData = {
        accDate: new Date(),
        objectId: null,//客户Id
        cusName: null,
        cusPhone: null,
        staffId: null,
        staffName: null,
        staffCode: null,
        orgId: null,
        orgName: null,
        orgCode: null,
        sumAmtReceivable: null,
        sumAmtRec: null,
        remarks: null,
        annexPaths: null,
        makeStaff: self.$store.state.user.id,
        cpId: self.$store.state.user.cpId,
        itemList: [],
        receiptList: [],
      }
    },
  }
  ,
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 付斌
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.focusItem = 'receivableId';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'receivableId'  // 设置主键Id
  }
}
</script>
