<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :span="8">
            <a-form-item label="登记人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['registerPerson', validatorRules.registerPerson]"
                :trigger-change="true"
                org-fields="realname,depart_name"
                dest-fields="registerPerson,registerDept"
                code="user_dept"
                @callback="popupCallback"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="登记部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input  :disabled="true" v-decorator="[ 'registerDept', validatorRules.registerDept]" placeholder="请输入登记部门"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="登记日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择登记日期" v-decorator="[ 'registerDate', validatorRules.registerDate]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="预算年度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-date-picker
                mode="year"
                v-decorator="['budgetYear', validatorRules.budgetYear]"
                placeholder="请输入年份"
                format="YYYY"
                style="width: 200px"
                :open="yearShowOne"
                @openChange="openChangeOne"
                @panelChange="panelChangeOne"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="预算总额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'budgetTotal', validatorRules.budgetTotal]" placeholder="请输入预算总额"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['file', validatorRules.file]" :trigger-change="true"></j-upload>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="预算批复管理子表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="budgetReplyDetailTable.loading"
            :columns="budgetReplyDetailTable.columns"
            :dataSource="budgetReplyDetailTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>

      </a-tabs>

    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'

  export default {
    name: 'BudgetReplyModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JUpload,
    },
    data() {
      return {
        yearShowOne: false,
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          registerPerson: {rules: [
            {required: true, message: '请输入登记人!'},
          ]},
          registerDept: {rules: [
          ]},
          registerDate: {rules: [
            {required: true, message: '请输入登记日期!'},
          ]},
          budgetYear: {rules: [
            {required: true, message: '请输入预算年度!'},
          ]},
          budgetTotal: {rules: [
          ]},
          file: {rules: [
          ]},
        },
        refKeys: ['budgetReplyDetail', ],
        tableKeys:['budgetReplyDetail', ],
        activeKey: 'budgetReplyDetail',
        // 预算批复管理子表
        budgetReplyDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '部门',
              key: 'department',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '费用资金类别',
              key: 'costFundType',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '预算资金类别',
              key: 'budgetFundType',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '预算资金名称',
              key: 'budgetFundName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '预算资金构成',
              key: 'budgetFundPose',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '预算资金来源',
              key: 'budgetFundSource',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '明细内容',
              key: 'detailContent',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '明细金额',
              key: 'detailMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '拨付方式',
              key: 'paymentMethod',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '采购方式',
              key: 'purchaseMethod',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '采购编号',
              key: 'purchaseNum',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/oa/budgetReply/add",
          edit: "/oa/budgetReply/edit",
          budgetReplyDetail: {
            list: '/oa/budgetReply/queryBudgetReplyDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        if(this.model.budgetYear){
          this.model.budgetYear =  moment(this.model.budgetYear);
        }
        let fieldval = pick(this.model,'registerPerson','registerDept','registerDate','budgetYear','budgetTotal','file')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.budgetReplyDetail.list, params, this.budgetReplyDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          budgetReplyDetailList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'registerPerson','registerDept','registerDate','budgetYear','budgetTotal','file'))
     },
      // 弹出日历和关闭日历的回调
      openChangeOne(status) {
        if (status) {
          this.yearShowOne = true;
        }
      },
      // 得到年份选择器的值
      panelChangeOne(value) {
        this.yearShowOne = false;
        this.$nextTick(() => {
          this.form.setFieldsValue({
            budgetYear: value,
          });
        });
      },
    }
  }
</script>

<style scoped>
</style>