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

          <a-col :span="16">
            <a-form-item label="标题" :labelCol="{          span: 3        }" :wrapperCol="{          span: 21        }">
              <a-input v-decorator="[ 'title', validatorRules.title]" placeholder="请输入标题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="单据号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'inNo', validatorRules.inNo]" placeholder="请输入单据号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'creater', validatorRules.creater]" placeholder="请输入创建人" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="创建部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'createrDept', validatorRules.createrDept]" placeholder="请输入创建部门" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择创建时间" v-decorator="[ 'createDate']" :showTime="true" dateFormat="YYYY-MM-DD HH:mm:ss" :trigger-change="true" style="width: 100%" disabled/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="入库人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['operator', validatorRules.operator]"
                :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="8">
            <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="8">
            <a-form-item label="入库日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择入库日期" v-decorator="[ 'operatorDate', validatorRules.operatorDate]" :showTime="true" dateFormat="YYYY-MM-DD HH:mm:ss" :trigger-change="true" style="width: 100%"/>
            </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="assetInDetailTable.loading"
            :columns="assetInDetailTable.columns"
            :dataSource="assetInDetailTable.dataSource"
            :maxHeight="300"
            :rowNumber="false"
            :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 moment from 'moment'

  export default {
    name: 'AssetInModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          title: {rules: [
          ]},
          inNo: {rules: [
          ]},
          creater: {rules: [
          ]},
          createrDept: {rules: [
          ]},
          operator: {rules: [
            {required: true, message: '请选择入库人	!'},
          ]},
          operatorDept: {rules: [
          ]},
          operatorDate: {rules: [
            {required: true, message: '请选择入库日期	!'},
          ]},
        },
        refKeys: ['assetInDetail', ],
        tableKeys:['assetInDetail', ],
        activeKey: 'assetInDetail',
        // 固定资产入库明细
        assetInDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '资产编号',
              key: 'assetNumber',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '资产名称',
              key: 'assetName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '资产类别',
              key: 'assetsCategory',
              type: FormTypes.input,
              width:"200px",
              type: FormTypes.select,
              options: [ // 下拉选项  
                { title: '办公家具', value: '办公家具' },
                { title: '办公设备', value: '办公设备' },
                { title: '不动产', value: '不动产' },
                { title: '车辆', value: '车辆' },
                { title: '其他', value: '其他' },
              ],
            },
            {
              title: '品牌',
              key: 'brand',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '单位名称',
              key: 'unitName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '规格型号',
              key: 'model',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '计量单位',
              key: 'unit',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '数量',
              key: 'quantity',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '金额',
              key: 'amount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [
                { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入金额（保留两位小数）' },
              ]
            },
            {
              title: '使用年限',
              key: 'exp',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '购置日期',
              key: 'purhcaseDate',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '入库日期',
              key: 'inDate',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '存放地点',
              key: 'place',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '使用人',
              key: 'user',
              type: FormTypes.popup,
              popupCode:"user_dept",
              destFields:"user,userDept,state",
              orgFieldse:"realname,depart_name,state",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '使用部门',
              key: 'userDept',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              disabled:true
            },
            {
              title: '资产状态',
              key: 'state',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '闲置',
              disabled:true
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '附件',
              key: 'attachment',
              type: FormTypes.file,
              token:true,
              responseName:"message",
              width:"200px",
              placeholder: '请选择文件',
              defaultValue: '',
            },


          ]
        },
        url: {
          add: "/oa/assetIn/add",
          edit: "/oa/assetIn/edit",
          assetInDetail: {
            list: '/oa/assetIn/queryAssetInDetailByMainId'
          },
        }
      }
    },
    methods: {
      init(){
        this.$nextTick(() => {
          this.form.setFieldsValue({
              creater: this.$store.getters.userInfo.realname,
              createrDept: this.$store.getters.userInfo.deptName,
              createDate: moment().format('YYYY-MM-DD HH:mm:ss')
          })
        })
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'title','inNo','creater','createrDept','operator','operatorDept','operatorDate')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.assetInDetail.list, params, this.assetInDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          assetInDetailList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'title','inNo','creater','createrDept','operator','operatorDept','operatorDate'))
     },

    }
  }
</script>

<style scoped>
</style>