<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="['applyPerson', validatorRules.applyPerson]"
                :trigger-change="true"
                org-fields="realname,depart_name"
                dest-fields="applyPerson,applyDept"
                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="[ 'applyDept', validatorRules.applyDept]" :disabled="true" 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="[ 'applyDate', validatorRules.applyDate]" :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="month"
                  v-decorator="[ 'dutyMonth', validatorRules.dutyMonth]"
                  placeholder="请选择值班月"
                  format="YYYY-MM"
                  :open="monthShowOne"
                  @openChange="openChangeOne"
                  @panelChange="panelChangeOne"/>
            </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="24">
            <a-form-item label="附件" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <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="museumDutyDetailTable.loading"
            :columns="museumDutyDetailTable.columns"
            :dataSource="museumDutyDetailTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true">
              <span slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>

              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </j-editable-table>
        </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'
  import moment from 'moment'

  export default {
    name: 'MuseumDutyModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JUpload,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 18
        },
        labelCol2: {
          span: 2
        },
        wrapperCol2: {
          span: 22
        },
        monthShowOne:false,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          applyPerson: {rules: [
            {required: true, message: '请输入申请人!'},
          ]},
          applyDept: {rules: [
          ]},
          applyDate: {rules: [
            {required: true, message: '请输入申请日期!'},
          ]},
          dutyMonth: {rules: [
            {required: true, message: '请输入值班月!'},
          ]},
          remark: {rules: [
          ]},
          file: {rules: [
          ]},
        },
        refKeys: ['museumDutyDetail', ],
        tableKeys:['museumDutyDetail', ],
        activeKey: 'museumDutyDetail',
        // 全馆值班登记子表
        museumDutyDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '值班日期',
              key: 'dutyDate',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '值班人',
              key: 'dutyPerson',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '值班岗位',
              key: 'dutyPost',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/oa/museumDuty/add",
          edit: "/oa/museumDuty/edit",
          museumDutyDetail: {
            list: '/oa/museumDuty/queryMuseumDutyDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        if(this.model.dutyMonth){
          this.model.dutyMonth =  moment(this.model.dutyMonth);
        }
        let fieldval = pick(this.model,'applyPerson','applyDept','applyDate','dutyMonth','remark','file')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.museumDutyDetail.list, params, this.museumDutyDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          museumDutyDetailList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'applyPerson','applyDept','applyDate','dutyMonth','remark','file'))
     },
      // 弹出日历和关闭日历的回调
      openChangeOne(status) {
        if (status) {
          this.monthShowOne = true;
        }
      },
      // 得到年份选择器的值
      panelChangeOne(value) {
        this.monthShowOne = false;
        this.$nextTick(() => {
          this.form.setFieldsValue({
            dutyMonth: value,
          });
        });
      },
    }
  }
</script>

<style scoped>
</style>