<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="600">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100">
          <Form-item label="样品名称" prop="name">
            <Input v-model="formObj.name" name="name" placeholder="请输入或选择样品名称" @on-result-change="_judgeBasisChange" />
          </Form-item>
          <Form-item label="检定依据" prop="verification">
            <Input v-model="formObj.verification" name="verification" placeholder="请选择检定依据" />
          </Form-item>
          <Form-item label="型号规格" prop="spec">
            <Input v-model="formObj.spec" name="spec" placeholder="请输入型号规格" />
          </Form-item>
          <Form-item label="出厂编号" prop="factoryNumber">
            <Input v-model="formObj.factoryNumber" name="factoryNumber" placeholder="请输入出厂编号" />
          </Form-item>
          <Form-item label="数量" prop="quantity">
            <Input v-model="formObj.quantity" name="quantity" placeholder="请选择数量" />
          </Form-item>

          <Form-item label="检测类型" prop="type">
            <Input v-model="formObj.type" name="quantity" placeholder="请选择检测类型" />
          </Form-item>

          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" :rows="3" placeholder="请输入备注" type="textarea" name="remark"/>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" :footer="footerList" @on-result-change="_footerResult"></ModalFooter>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑环境检测项目包
 */
import ModalFooter from '../../../components/base/modalFooter'
import { meterAptitude } from '../../../api'

export default {
  components: {
    ModalFooter
    // autoComplete
  },
  data() {
    // const validateJudgeBasis = (rule, value, callback) => {
    //   if (this.formObj.code === '') {
    //     callback(new Error('判定依据不能为空！'))
    //   } else if (this.formObj.stdId === '') {
    //     this.formObj.code = ''
    //     callback(new Error('请选择已维护过的判定依据！'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      id: '',
      modalTitle: '',
      formObj: {
        verification: '',
        name: '',
        spec: '',
        factoryNumber: '',
        type: '',
        remark: '',
        quantity: 1,
        entrustId: ''
      },
      showModal: false,
      aptitudeData: [],
      ruleValidate: {
        name: [{ required: true, message: '样品名称不能为空', trigger: 'blur' }]
      },
      footerList: [
        { name: '取消', type: '' },
        { name: '保存', type: 'primary' }
      ],
      typeData: [],
      judgeBasisData: []
    }
  },
  methods: {
    _save: async function(data) {
      const result = await meterAptitude.save(data)
      console.log(result)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await meterAptitude.edit(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    },
    _open(entrustId) {
      this.formObj.entrustId = entrustId
      this._getJudgeBasisList()
      this.$refs.formObj.resetFields()
      this.id = ''
      this.modalTitle = '添加样品'
      this.showModal = true
    },
    _typeChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.testType = data.name
          break
      }
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          if (this.$string(this.id).isEmpty()) {
            // 添加
            this._save(data)
          } else {
            // 编辑
            this._edit({ id: this.formObj.id, obj: data })
          }
          // if (this.$string(this.id).isEmpty()) {
          //   // 添加
          //   this.$store.dispatch('FoodJudgeBasis/add', data).then(() => {
          //     this._resultChange('添加成功!')
          //   })
          // } else {
          //   // 编辑
          //   this.$store
          //     .dispatch('FoodJudgeBasis/edit', { id: this.id, obj: data })
          //     .then(() => {
          //       this._resultChange('编辑成功!')
          //     })
          // }
        } else {
          this.$refs.footerModal._hideLoading()
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _cancel() {
      this.showModal = false
    },
    _resultChange(msg) {
      this.showModal = false
      this.$refs.footerModal._hideLoading()
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this.$refs.footerModal._hideLoading()
    },
    // 关联
    _getJudgeBasisList(data) {
      const obj = { classify: 0 }
      if (data) {
        obj.stdNum = data
      }
      // this.$store.dispatch('StandardInfo/list', obj).then(() => {
      //   this.judgeBasisData = this.$store.state.StandardInfo.list
      // })
    },
    _judgeBasisChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.stdId = data.id
          this.formObj.code = data.stdNum
          this.formObj.name = data.name
          this.$refs.formObj.validateField('code')
          break
        case 'query':
          this.formObj.stdId = ''
          this.formObj.code = data.stdNum
          this.formObj.name = ''
          this._getJudgeBasisList(data.stdNum)
          break
        case 'focus':
          this._getJudgeBasisList()
          break
        case 'blur':
          if (this.formObj.stdId === '') {
            this.formObj.code = ''
            this._getJudgeBasisList()
          }
          this.$refs.formObj.validateField('code')
      }
    }
  }
}
</script>
