<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" />-->
            <el-input v-model="formObj.name" name="name" blur placeholder="请输入或选择样品名称" @on-result-change="_judgeBasisChange"  @click.native="_selectjudgeBasis()"
            ></el-input>
          </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">
            <Select v-model="formObj.type" name="type">
              <Option v-for="(item,index) in options" :key="item.name" :value="index" placeholder="请选择检测类型">
                {{ item.name }}
              </Option>
            </Select>
          </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>
    <EditModal ref="EditModal" is-change @on-result-change="_backData" />
  </div>
</template>
<script>
/**
 * 添加编辑环境检测项目包
 */
import ModalFooter from '../../components/base/modalFooter'
import { meterAptitude, meterSample } from '../../api'
import EditModal from './go-out-test/EditModal'

export default {
  components: {
    EditModal,
    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: '',
        aptitudeId: '',
        name: '',
        spec: '',
        factoryNumber: '',
        type: '',
        remark: '',
        quantity: 1,
        entrustId: ''
      },
      options: [
        {
          name: '检定'
        },
        {
          name: '校准'
        },
        {
          name: '外观检查'
        }
      ],
      showModal: false,
      aptitudeData: [],
      ruleValidate: {
        name: [{ required: true, message: '样品名称不能为空', trigger: 'blur' }]
      },
      footerList: [
        { name: '取消', type: '' },
        { name: '保存', type: 'primary' }
      ],
      typeData: [],
      judgeBasisData: []
    }
  },
  methods: {
    _selectjudgeBasis() {
      this.$refs.EditModal._open()
    },
    _backData(data) {
      this.formObj.name = data.name
      this.formObj.aptitudeId = data.id
      this.formObj.verification = data.code + data.basis
    },
    _save: async function(data) {
      console.log(data)
      const result = await meterSample.addSample(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()) {
            // 添加
            const saveData = this.formObj
            this._save(saveData)
          } else {
            // 编辑
            this._edit({ id: this.formObj.id, obj: data })
          }
        } 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>
