<template>
  <j-modal
    :title="title"
    :width="700"
    :visible="visible"
    :mask-closable="false"
    cancel-text="关闭"
    @close="close"
  >
    <a-form-model ref="form" :label-col="labelCol" :wrapper-col="wrapperCol" :rules="validatorRules" :model="model">
      <a-form-model-item label="车间" prop="deptId">
        <a-tree-select
          v-model="model.deptId"
          :show-search="true"
          allow-clear
          tree-default-expand-all
          style="width: 100%"
          :tree-data="treeData"
          tree-node-filter-prop="label"
          :replace-fields="{children:'children', title:'label', key:'id', value: 'id' }"
        />
      </a-form-model-item>
      <a-form-model-item label="存放处" prop="storePlace">
        <a-input
          v-model="model.storePlace"
        />
      </a-form-model-item>
      <a-form-model-item label="工具名称" prop="toolType">
        <j-dict-select-tag
          v-model="model.toolType"
          dict-code="tool_type"
          @toolChange="toolChange"
        />
      </a-form-model-item>
      <a-form-model-item label="编号" prop="toolCode">
        <a-input v-model="model.toolCode" />
      </a-form-model-item>
      <a-form-model-item label="电压等级" prop="elecLevel">
        <a-input v-model="model.elecLevel" />
      </a-form-model-item>
      <a-form-model-item label="计量单位" prop="unit">
        <a-input v-model="model.unit" :disabled="true" />
      </a-form-model-item>
      <a-form-model-item label="实验周期" prop="testCycle">
        <j-dict-select-tag v-model="model.testCycle" type="radioButton" dict-code="test_cycle" />
      </a-form-model-item>
      <a-form-model-item label="生产厂家" prop="produceFactory">
        <a-input v-model="model.produceFactory" />
      </a-form-model-item>
      <a-form-model-item label="生产日期" prop="produceDate">
        <a-date-picker
          v-model="model.produceDate"
          style="width: 100%"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          placeholder="请选择"
        />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark">
        <a-textarea v-model="model.remark" :max-length="300" :rows="4" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { httpAction, getAction } from '@/api/request'
import JModal from '@/components/JModal'

export default {
  name: 'CheckAndEditModel',
  components: {
    JModal
  },
  data() {
    return {
      treeData: [],
      labelCol: { span: 4 },
      wrapperCol: { span: 19 },
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        deptId: [{ required: true, message: '请选择' }],
        storePlace: [{ required: true, message: '请输入' }],
        toolType: [{ required: true, message: '请选择' }],
        toolCode: [{ required: true, message: '请输入' }],
        elecLevel: [{ required: true, message: '请输入' }],
        unit: [{ required: true, message: '请输入' }],
        testCycle: [{ required: true, message: '请选择' }],
        produceFactory: [{ required: true, message: '请输入' }],
        produceDate: [{ required: true, message: '请输入' }],
        remark: [{ required: true, message: '请输入' }]

      },
      url: {
        replace: '/business/safetool/base/safety/tool/replaceTool',
        tree: '/system/dept/treeSelect'
      }
    }
  },
  created() {
    this.loadTree()
  },
  methods: {
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    toolChange(data) {
      this.model.unit = data
    },
    edit(record) {
      this.model = Object.assign({}, record)
      // this.model = Object.assign({}, { toolId: record.toolId }, { experimentId: record.experimentId })
      this.visible = true
    },
    close(isSubmit) {
      if (isSubmit) {
        this.checkData()
      } else {
        this.visible = false
      }
    },
    checkData() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveData()
        } else {
          return false
        }
      })
    },
    saveData() {
      httpAction(this.url.replace, this.model, 'post').then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$emit('ok')
          this.visible = false
        } else {
          console.log(res)
        }
      })
    }
  }
}
</script>
