<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="800">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100" inline>

          <Form-item label="计量器具名称 " prop="name" class="width-48">
            <Input v-model="formObj.name" name="name" placeholder="请输入计量器具名称" />
          </Form-item>
          <Form-item label="准确度" prop="grade" class="width-48">
            <Input v-model="formObj.grade" name="grade" placeholder="请输入准确度" />
          </Form-item>
          <Form-item label="等级" prop="testRank" class="width-48">
            <Input v-model="formObj.testRank" name="testRank" placeholder="请输入等级" />
          </Form-item>
          <Form-item label="测量范围 " prop="extent" class="width-48">
            <Input v-model="formObj.extent" name="extent" placeholder="请输入测量范围" style="border: none;"/>
          </Form-item>
          <Form-item label="收费单位 " prop="unit" class="width-48">
            <Input v-model="formObj.unit" name="unit" placeholder="请输入收费单位" style="border: none;"/>
          </Form-item>
          <Form-item label="收费标准（元）" prop="price" class="width-48">
            <InputNumber v-model="formObj.price" name="price" placeholder="请输入收费标准（元）" />
          </Form-item>

          <Form-item label="备注 " prop="remark" class="width-48">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注" style="border: none;"/>
          </Form-item>
        </Form>
      </div>
      <EditModal ref="EditModal" @on-result-change="_backData" is-change />
      <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 { meterManage } from '../../../api'
import EditModal from './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 {
      formId: 'meterAptitudeManageEditFormId',
      pageColumns: [
        { title: '指标名称', key: 'name', width: 160 },
        { title: '比较符', key: 'compare' },
        { title: '限量值', key: 'limitValue' },
        { title: '备注', key: 'remark' },
        // {title: '限制范围或说明', key: 'limitDescription'},
        { title: '单位', key: 'unit' }
      ],
      btn: [{ type: 'success', id: '', name: '添加' }],
      iconMsg: [
        { type: 'md-create', id: '', name: '编辑' },
        { type: 'md-trash', id: '', name: '删除' }
      ],
      currentRow: {},
      currentIndex: 0,
      id: '',
      modalTitle: '',
      formObj: {
        basis: '',
        name: '',
        testRank: '',
        code: '',
        cycle: 0,
        price: 0,
        remark: '',
        extent: '',
        grade: ''
      },
      searchOpen: true,
      selectIds: [],
      getPage: {
        records: []
      },
      typeData: [],
      showModal: false,
      aptitudeData: [],
      ruleValidate: {
        name: [
          { required: true, message: '计量器具名称不能为空', trigger: 'blur' }
        ]
      },
      footerList: [
        { name: '取消', type: '' },
        { name: '保存', type: 'primary' }
      ],
      // typeData: [],
      judgeBasisData: []
    }
  },
  methods: {
    _selectjudgeBasis() {
      this.$refs.EditModal._open()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          // this.getPage = this.$store.state.FoodJudgeBasis.page
          break
        case 'selectIds':
          // this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          // this._page()
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          // this._editModal(true, data.id)
          break
        case '删除':
          // this._deleteById(data.id)
          console.log('点击了删除按钮')
          console.log(data)
          this.getPage.records.splice(data, 1)
          break
        case '操作日志':
          // this._record(data.id)
          break
      }
    },
    _backData(data) {
      this.formObj.code = data.code
      this.formObj.basis = data.name
      // this.formObj.standardId = data.id
    },
    _handleRow(data) {
      this.currentRow = data.row
      this.currentIndex = data.rowIndex
    },
    _add() {
      const data = {
        name: '',
        compare: '',
        limitValue: '',
        remark: '',
        unit: ''
      }
      this.getPage.records.unshift(data)
    },
    _save: async function(data) {
      const result = await meterManage.saveBeyond(data)
      console.log(result)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await meterManage.edit(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    },
    _open(formObj) {
      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() {
      console.log(this.formObj)
      console.log(this.getPage.records)
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          if (this.$string(this.id).isEmpty()) {
            // 添加
            this._save({
              aptitude: data,
              aptitudeItemList: this.getPage.records
            })
          } 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
      this.getPage.records = []
    },
    _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>
