<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" :multi="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="仪器编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="instrumentNumber">
              <a-input v-model="model.instrumentNumber" placeholder="请输入仪器编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="仪器名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="instrumentName">
              <a-input v-model="model.instrumentName" 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="model">
              <a-input v-model="model.model" placeholder="请输入型号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="specifications">
              <a-input v-model="model.specifications" 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="manufactuer">
              <a-input v-model="model.manufactuer" 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="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: 'RmsInstrumentCollegeForm',
  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' },
        ],
        instrumentNumber: [
          { required: true, message: '请输入家具编号', trigger: 'blur' },
        ],
        instrumentName: [
          { required: true, message: '请输入家具名称', trigger: 'blur' },
        ],
        categoryId: [
          { required: true, message: '请输入分类号', trigger: 'blur' },
        ],
        unitPrice: [
          { required: true, message: '请输入单价', trigger: 'blur' },
        ],
        manufactuer: [
          { required: true, message: '请输入厂家', trigger: 'blur' },
        ],
        purchaseDate: [
          { required: true, message: '请选择购置日期', trigger: 'change' },
        ],
        recipient: [
          { required: true, message: '请选择领用人', trigger: 'change' },
        ],
        entryDate: [
          { required: true, message: '请选择入库时间', trigger: 'change' },
        ],
        documentNumber: [
          { required: true, message: '请输入单据号', trigger: 'blur' },
        ],
        locationId: [
          { required: true, message: '请选择存放地地址', trigger: 'blur' },
        ],
      },
      url: {
        add: "/rms/rmsInstrumentCollege/add",
        edit: "/rms/rmsInstrumentCollege/edit",
        queryById: "/rms/rmsInstrumentCollege/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>