<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="领用单位号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collectingUnitNumber">
              <j-select-depart v-model="model.collectingUnitNumber" placeholder="请输入领用单位号"
                :multi="false"></j-select-depart>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="资产编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="intangibleNumber">
              <a-input v-model="model.intangibleNumber" placeholder="请输入资产编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="资产名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="intangibleName">
              <a-input v-model="model.intangibleName" placeholder="请输入资产名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分类号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <a-input v-model="model.categoryId" placeholder="请输入分类号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitPrice">
              <a-input v-model="model.unitPrice" placeholder="请输入单价"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="购置日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseDate">
              <j-date placeholder="请选择购置日期" v-model="model.purchaseDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="专利申请号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="patentApplicationNumber">
              <a-input v-model="model.patentApplicationNumber" placeholder="请输入专利申请号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发明人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inventor">
              <a-input v-model="model.inventor" placeholder="请输入发明人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发明名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inventionName">
              <a-input v-model="model.inventionName" placeholder="请输入发明名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="注册日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="registrationDate">
              <j-date placeholder="请选择注册日期" v-model="model.registrationDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="注册机关" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="registrationAuthority">
              <a-input v-model="model.registrationAuthority" placeholder="请输入注册机关"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="授权公告日" :labelCol="labelCol" :wrapperCol="wrapperCol"
              prop="authorizationAnnouncement">
              <j-date placeholder="请选择授权公告日" v-model="model.authorizationAnnouncement" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="批准文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalNumber">
              <a-input v-model="model.approvalNumber" placeholder="请输入批准文号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="领用人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recipient">
              <j-select-user-by-dep v-model="model.recipient" :multi="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="入库时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="entryDate">
              <j-date placeholder="请选择入库时间" v-model="model.entryDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentNumber">
              <a-input v-model="model.documentNumber" placeholder="请输入单据号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="存放地地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="locationId">
              <a-cascader :options="treeData" placeholder="请选择父级存放地址" @change="onChange"></a-cascader>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="note">
              <a-input v-model="model.note" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalStatus">
              <j-dict-select-tag type="radio" v-model="model.approvalStatus" dictCode="approval_status"
                placeholder="请选择审核状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="初审状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="initialAudit">
              <j-dict-select-tag type="radio" v-model="model.initialAudit" dictCode="initial_audit"
                placeholder="请选择初审状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'RmsIntangibleForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        approvalStatus: [
          { required: true, message: '请选择审核状态', trigger: 'change' },
        ],
        initialAudit: [
          { required: true, message: '请选择初审状态', trigger: 'change' },
        ],
        collectingUnitNumber: [
          { required: true, message: '请选择领用单位号', trigger: 'change' },
        ],
        intangibleNumber: [
          { required: true, message: '请输入资产编号', trigger: 'blur' },
        ],
        intangibleName: [
          { required: true, message: '请输入资产名称', trigger: 'blur' },
        ],
        categoryId: [
          { required: true, message: '请输入分类号', trigger: 'blur' },
        ],
        unitPrice: [
          { required: true, message: '请输入单价', trigger: 'blur' },
        ],
        purchaseDate: [
          { required: true, message: '请选择购置日期', trigger: 'change' },
        ],
        inventionName: [
          { required: true, message: '请输入发明名称', trigger: 'blur' },
        ],
        registrationDate: [
          { required: true, message: '请选择注册日期', trigger: 'change' },
        ],
        registrationAuthority: [
          { required: true, message: '请输入注册机关', trigger: 'blur' },
        ],
        authorizationAnnouncement: [
          { required: true, message: '请选择授权公告日', trigger: 'change' },
        ],
        approvalNumber: [
          { required: true, message: '请输入批准文号', trigger: 'blur' },
        ],
        recipient: [
          { required: true, message: '请选择领用人', trigger: 'change' },
        ],
        entryDate: [
          { required: true, message: '请选择入库时间', trigger: 'change' },
        ],
        documentNumber: [
          { required: true, message: '请输入单据号', trigger: 'blur' },
        ],
        locationId: [
          { required: true, message: '请选择存放地地址', trigger: 'change' },
        ],
      },
      url: {
        add: "/rms/rmsIntangible/add",
        edit: "/rms/rmsIntangible/edit",
        queryById: "/rms/rmsIntangible/queryById"
      },
      treeData: [],
      getTreeUrl: '/rms/rmsLocation/getCascader?type=1'

    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    this.getTreeData();
  },
  methods: {
    getTreeData() {
      getAction(this.getTreeUrl, {}).then(res => {
        if (res.success) {
          this.treeData = res.result;
        }
      });
    },
    onChange(value) {
      this.model.locationId = value[value.length - 1];
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>