<!-- @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" style="width:100%">
          <DkFormItem prop="trfNo">
            <InputPop v-model="formData.trfNo" :readonly="true"/>
          </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" :label="$t('trfDate')" :data-type="$config.dataType.date">
            <DatePickerPop v-model="formData.accDate" :short-cut-flag="true"/>
          </DkFormItem>
          <DkFormItem prop="remarks" :span="24">
            <InputPop v-model="formData.remarks" textareaFlag/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
      <!--资金转账-->
      <DkPanel prop="macTransfer">
        <div id="search-cond-div-transfer-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable ref="transferTable" :data="formData.itemList" showFooter
                     major-field="trfId"
                     :new-row-flag="true"
                     :columns="transferColumns"/>
        </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.macTransfer"
                     :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,
      macList: [],  // 资金账户
      formData: {
        trfNo: null,
        trfInMac: null,
        trfOutMac: null,
        amtTrf: null,
        remarks: null,
        annexPaths: null,
        accDate: new Date(),
        staffId: self.$store.state.user.id,
        staffName: self.$store.state.user.staffName,
        orgId: self.$store.state.user.orgId,
        orgName: self.$store.state.user.orgName,
        makeStaff: self.$store.state.user.id,
      },
      transferColumns: [
        {
          field: 'trfOutMac',
          type: 'select',
          options: () => self.macList,
          labelKey: 'macName',
          valueKey: 'macId',
          searchDetailFlag: true,
        },
        {
          field: 'trfInMac',
          type: 'select',
          options: () => self.macList,
          labelKey: 'macName',
          valueKey: 'macId',
          searchDetailFlag: true,
        },
        //转账金额
        {
          field: 'amtTrf',
          type: 'number',
          digits: 2,
          sum: true,
        },
        {field: 'remarks', type: 'text', equal: true},
      ],
    }
  },
  methods: {
    /**
     * @desc   : 加载数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    initData() {
      this.getMacData();
    },
    /**
     * @desc   :获取资金账户
     * @author : 付斌
     * @date   : 2024-04-01 14:01
     */
    getMacData() {
      let params = {
        'dictCode': this.$config.kindCode.basicIncome,
      }
      this.excute(this.$service.commonService, this.$service.commonService.getMac, params, false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.macList = res.data
        }
      })
    },
    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2022/03/04 13:17
     */
    detail(id) {
      return this.excuteNoParam(this.$service.moneyTransferService, this.$service.moneyTransferService.getMacTransferForUpdate, [id]);
    },
    /**
     * @desc   : 编辑页面赋值
     * @author : 付斌
     * @date   : 2024-03-03 9:33
     */
    setValuesByEdit(data) {
      if (data) {
        this.formData = data["data"];
        this.formData.itemList = data["dataItem"];
      }
    },
    /**
     * @desc   : 校验数据
     * @author : 付斌
     * @date   : 2024-03-11 14:37
     */
    validData() {
      let itemTable = this.$refs['transferTable'].getTableDataFilter();
      if (!itemTable || itemTable.length == 0) {
        this.$Message.warning(this.$t('W_012'))
        return false;
      }
      for (let it of itemTable) {
        if (!it.amtTrf || it.amtTrf === '') {
          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['transferTable'].getTableDataFilter();
      this.formData.itemList = itemTable
      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.moneyTransferService, this.$service.moneyTransferService.insert, this.params);
      } else if (this.type === this.$config.formMode.edit) {
        return this.excute(this.$service.moneyTransferService, this.$service.moneyTransferService.update, this.params)
      }
    },
    /**
     * @desc   :新建完毕后清空窗体
     * @author : 付斌
     * @date   : 2024-02-27 10:33
     */
    clear() {
      let self = this
      this.formData = {
        trfNo: null,
        trfInMac: null,
        trfOutMac: null,
        amtTrf: null,
        remarks: null,
        annexPaths: null,
        accDate: new Date(),
        staffId: self.$store.state.user.id,
        staffName: self.$store.state.user.staffName,
        orgId: self.$store.state.user.orgId,
        orgName: self.$store.state.user.orgName,
        makeStaff: self.$store.state.user.id,
      }
    },
  }
  ,
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 付斌
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.focusItem = 'trfId';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'trfId'  // 设置主键Id
  }
}
</script>
