<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-col span="8">
          <a-form-item label="印章名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-popup
              v-decorator="['sealName', validatorRules.sealName]"
              :trigger-change="true"
              org-fields="seal_name,engraving_date,id"
              dest-fields="sealName,enableDate,sealId"
              code="seal_select"
              @callback="popupCallback"/>
          </a-form-item>
        </a-col>
        <a-col span="8">
          <a-form-item label="启用日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-date placeholder="请选择启用日期"  v-decorator="[ 'enableDate', validatorRules.enableDate]" :trigger-change="true" :read-only="true" style="width: 100%"/>
          </a-form-item>
        </a-col>
          <a-col span="8">
          <a-form-item label="停用日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-date placeholder="请选择停用日期" v-decorator="[ 'stopDate', validatorRules.stopDate]" :trigger-change="true" style="width: 100%"/>
          </a-form-item>
          </a-col>
          <a-col span="8">
            <a-form-item label="上缴日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择上缴日期" v-decorator="[ 'turnDate', validatorRules.turnDate]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col span="8">
            <a-form-item label="原保管人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['oldCustodian', validatorRules.oldCustodian]"
                :trigger-change="true"
                org-fields="realname,depart_name"
                dest-fields="oldCustodian,oldDept"
                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="[ 'oldDept', validatorRules.oldDept]" :disabled="true" placeholder="请输入原保管部门"></a-input>
            </a-form-item>
          </a-col>
          <a-col span="8">
            <a-form-item label="现上缴人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['nowTurnPerson', validatorRules.nowTurnPerson]"
                :trigger-change="true"
                org-fields="realname,depart_name"
                dest-fields="nowTurnPerson,nowTurnDept"
                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="[ 'nowTurnDept', validatorRules.nowTurnDept]" :disabled="true" placeholder="请输入现上缴部门"></a-input>
            </a-form-item>
          </a-col>
          <a-col span="8">
            <a-form-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['handlingPerson', validatorRules.handlingPerson]"
                :trigger-change="true"
                org-fields="realname,depart_name"
                dest-fields="handlingPerson,handlingDept"
                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="[ 'handlingDept', validatorRules.handlingDept]" placeholder="请输入经办部门" :disabled="true"></a-input>
            </a-form-item>
          </a-col>
          <a-col span="24">
            <a-form-item label="上缴原因" :labelCol="labelCol1" :wrapperCol="wrapperCol1">
              <a-textarea v-decorator="['turnResult', validatorRules.turnResult]" rows="1" placeholder="请输入上缴原因"/>
            </a-form-item>
          </a-col>
          <a-col span="24">
            <a-form-item label="附件" :labelCol="labelCol1" :wrapperCol="wrapperCol1">
              <j-upload v-decorator="['file', validatorRules.file]" :trigger-change="true"></j-upload>
            </a-form-item>
          </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'

  export default {
    name: "SealTurnModal",
    components: {
      JDate,
      JUpload,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:1200,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 },
        },
        labelCol1: {
          xs: { span: 24 },
          sm: { span: 2 },
        },
        wrapperCol1: {
          xs: { span: 24 },
          sm: { span: 21 },
        },
        sealId:'',
        confirmLoading: false,
        validatorRules: {
          sealName: {rules: [
            {required: true, message: '请选择印章名称!'},
          ]},
          enableDate: {rules: [
          ]},
          stopDate: {rules: [
            {required: true, message: '请选择停用日期!'},
          ]},
          turnDate: {rules: [
            {required: true, message: '请选择上缴日期!'},
          ]},
          oldCustodian: {rules: [
            {required: true, message: '请选择原保管人!'},
          ]},
          oldDept: {rules: [
          ]},
          nowTurnPerson: {rules: [
            {required: true, message: '请选择现上缴人!'},
          ]},
          nowTurnDept: {rules: [
          ]},
          handlingPerson: {rules: [
            {required: true, message: '请选择经办人!'},
          ]},
          handlingDept: {rules: [
          ]},
          turnResult: {rules: [
            {required: true, message: '请输入上缴原因!'},
          ]},
          file: {rules: [
          ]},
        },
        url: {
          add: "/sealTurn/sealTurn/add",
          edit: "/sealTurn/sealTurn/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'sealName','enableDate','stopDate','turnDate','oldCustodian','oldDept','nowTurnPerson','nowTurnDept','handlingPerson','handlingDept','turnResult','file'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData.sealId = this.sealId;
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        if(typeof(row.sealId)!="undefined"){
          this.sealId = row.sealId;
        }
        this.form.setFieldsValue(pick(row,'sealId','sealName','enableDate','stopDate','turnDate','oldCustodian','oldDept','nowTurnPerson','nowTurnDept','handlingPerson','handlingDept','turnResult','file'))
        // console.log(this.sealId);
      },
    }
  }
</script>