<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="code">
<!--            <autoComplete :value="formObj.judgeBasis" :downData="judgeBasisData" @on-result-change="_judgeBasisChange" name="judgeBasis"-->
<!--                          show-key="stdNum" placeholder="请输入或选择判定依据" focus-->
<!--            ></autoComplete>-->
            <Input v-model="formObj.code" @on-result-change="_judgeBasisChange" name="code" placeholder="请输入或选择检定依据" />

          </Form-item>
          <Form-item label="检定依据名称" prop="basis">
            <Input v-model="formObj.name" name="name" placeholder="请输入检定依据名称" />
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input :rows="3" v-model="formObj.remark" placeholder="请输入备注" type="textarea" name="remark"/>
          </Form-item>
          <input v-model="formObj.stdId" type="hidden" name="stdId">
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></ModalFooter>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑环境检测项目包
 */
// import ModalFooter from '../../../../components/base/ModalFooter'
// import autoComplete from '../../../../components/base/AutoCompletes'
import ModalFooter from '../../../components/base/modalFooter'
// import autoComplete from '../../../components/base/AutoCompletes'
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: {
        name: '',
        code: '',
        remark: '',
        stdId: ''
      },
      showModal: false,
      aptitudeData: [],
      ruleValidate: {
        testType: [
          { required: true, message: '类别不能为空', trigger: 'blur' }
        ],
        code: [
          // { required: true, validator: validateJudgeBasis, 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(formObj) {
      this._getJudgeBasisList()
      this.$refs.formObj.resetFields()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加检定依据'
      } else {
        this.formObj = formObj
        this.id = formObj.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.$refs.footerModal._hideLoading()
      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>
