<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="12">
            <a-form-item label="入库人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['operator', validatorRules.operator]"
                placeholder="请选择入库人"
                :trigger-change="true"
                org-fields="realname,depart_name"
                dest-fields="operator,operatorDept"
                code="user_dept"
                @callback="popupCallback"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="入库部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'operatorDept', validatorRules.operatorDept]" placeholder="请输入入库部门" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="入库日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择入库日期" v-decorator="[ 'operateDate', validatorRules.operateDate]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['remark', validatorRules.remark]" rows="4" placeholder="请输入备注"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['attachment', validatorRules.attachment]" :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="materialInDetailTable.loading"
            :columns="materialInDetailTable.columns"
            :dataSource="materialInDetailTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

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

<script>

  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: 'MaterialInModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JUpload,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          operator: {rules: [
            {required: true, message: '请选择入库人	!'},
            
          ]},
          operatorDept: {rules: [
          ]},
          operateDate: {rules: [
            {required: true, message: '请选择入库日期	!'},
          ]},
          remark: {rules: [
          ]},
          attachment: {rules: [
          ]},
        },
        refKeys: ['materialInDetail', ],
        tableKeys:['materialInDetail', ],
        activeKey: 'materialInDetail',
        // 物料入库明细
        materialInDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物料名称',
              key: 'materialName',
              type: FormTypes.popup,
              popupCode:"sel_material",
              destFields:"materialName,brand,category,model,unit,cdMaterialId",
              orgFieldse:"material_name,brand,category,model,unit,id",
              width:"200px",
              placeholder: '请选择物料',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }]
            },
            {
              title: '品牌',
              key: 'brand',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              disabled: true
            },
            {
              title: '类别',
              key: 'category',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              disabled: true
            },
            {
              title: '规格型号',
              key: 'model',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              disabled: true
            },
            {
              title: '计量单位',
              key: 'unit',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              disabled: true
            },
            {
              title: '入库数量',
              key: 'quantity',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: 'ID',
              key: 'cdMaterialId',
              type: FormTypes.hidden,
              width:"200px",
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/oa/materialIn/add",
          edit: "/oa/materialIn/edit",
          materialInDetail: {
            list: '/oa/materialIn/queryMaterialInDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'operator','operatorDept','operateDate','remark','attachment')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.materialInDetail.list, params, this.materialInDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          materialInDetailList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'operator','operatorDept','operateDate','remark','attachment'))
     },

    }
  }
</script>

<style scoped>
</style>