<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,seal_material,seal_size,id"
              dest-fields="sealName,sealMaterial,sealSize,sealId"
              code="seal_select"
              @callback="popupCallback"/>
          </a-form-item>
        </a-col>
        <a-col span="8">
          <a-form-item label="印制材质" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="[ 'sealMaterial', validatorRules.sealMaterial]" :disabled="true" placeholder="请输入印制材质"></a-input>
          </a-form-item>
        </a-col>
        <a-col span="8">
          <a-form-item label="印章尺寸" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="[ 'sealSize', validatorRules.sealSize]" :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="[ 'destroyDate', validatorRules.destroyDate]" :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="['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]" :disabled="true" placeholder="请输入经办部门"></a-input>
          </a-form-item>
        </a-col>
        <a-col span="24">
          <a-form-item label="销毁原因" :labelCol="labelCol1" :wrapperCol="wrapperCol1">
            <a-textarea v-decorator="['destroyResult', validatorRules.destroyResult]" rows="1" placeholder="请输入销毁原因"/>
          </a-form-item>
        </a-col>
        <a-col span="24">
          <a-form-item label="备注" :labelCol="labelCol1" :wrapperCol="wrapperCol1">
            <a-textarea  v-decorator="['remark', validatorRules.remark]" 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: "SealDestroyModal",
    components: {
      JDate,
      JUpload,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:1150,
        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: '请输入印章名称!'},
          ]},
          sealMaterial: {rules: [
          ]},
          sealSize: {rules: [
          ]},
          destroyDate: {rules: [
            {required: true, message: '请输入销毁日期!'},
          ]},
          handlingPerson: {rules: [
            {required: true, message: '请输入经办人!'},
          ]},
          handlingDept: {rules: [
          ]},
          destroyResult: {rules: [
            {required: true, message: '请输入销毁原因!'},
          ]},
          remark: {rules: [
            {required: true, message: '请输入备注!'},
          ]},
          file: {rules: [
          ]},
        },
        url: {
          add: "/sealDestroy/sealDestroy/add",
          edit: "/sealDestroy/sealDestroy/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','sealMaterial','sealSize','destroyDate','handlingPerson','handlingDept','destroyResult','remark','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){
        console.log(row.sealId)
        if(typeof(row.sealId)!="undefined"){
          this.sealId = row.sealId;
        }
        this.form.setFieldsValue(pick(row,'sealName','sealMaterial','sealSize','destroyDate','handlingPerson','handlingDept','destroyResult','remark','file'))
        console.log(this.sealId)
      },


    }
  }
</script>