<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="1200">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100" inline>
          <Form-item label="检定依据" prop="code" class="width-48">
<!--            <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" @click.native="_selectjudgeBasis" name="code" placeholder="请选择检测依据" readonly/>
<!--            <Input-->
<!--              v-model="formObj.code"-->
<!--              name="code"-->
<!--              readonly-->
<!--              placeholder="请选择检测依据"-->
<!--              icon="ios-add-circle-outline"-->
<!--              @click.native="_selectjudgeBasis"-->
<!--            />-->
          </Form-item>
          <Form-item label="检定依据名称" prop="basis" class="width-48">
            <Input v-model="formObj.basis" name="basis" placeholder="请输入判定依据名称" />
          </Form-item>
          <Form-item label="检定周期 " prop="cycle" class="width-48">
<!--            <Input :rows="3" v-model="formObj.remark" placeholder="请输入备注" type="textarea" name="remark"/>-->
                <InputNumber :rows="3" v-model="formObj.cycle" name="cycle" style="width: 460px;"/>
          </Form-item>
          <Form-item label="授权仪器名称 " prop="name" class="width-48">
            <Input v-model="formObj.name" name="name" placeholder="请输入授权仪器名称" />
          </Form-item>
          <Form-item label="价格" prop="price" class="width-48">
            <InputNumber v-model="formObj.price" name="price" placeholder="请输入价格" style="width: 460px;"/>
          </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="grade" class="width-48">
            <Input v-model="formObj.grade" name="grade" placeholder="请输入准确度等级" />
          </Form-item>
          <input v-model="formObj.stdId" type="hidden" name="stdId">
        </Form>
        <Alert type="success" style="text-align: center;font-size: 15px;margin: 5px">
          检测指标表
        </Alert>
        <Col span="24">
<!--          <btn-list :msg="btn" :open="searchOpen" :showSearchBtn="false" @on-result-change="_btnClick"-->
<!--                    class="contHide"></btn-list>-->
          <Button @click="_add()" type="success">添加</Button>
        </Col>
        <Col span="24">
          <PTVXETable
            ref="pageTable"
            :form-id="formId"
            :tableHeight="500"
            :getPage="getPage"
            :iconMsg="iconMsg"
            @on-result-change="_tableResultChange">
            <vxe-table-column
              v-for="item in pageColumns"
              :key="item.key"
              :field="item.key"
              :title="item.title"
              :width="item.width"
              :min-width="200"
              :fixed="item.fixed?item.fixed:undefined"
              sortable
            >
              <template slot-scope="scope">
                <div v-if="item.key==='name'" @click.stop="_handleRow(scope)">
                  <el-input v-model="scope.row.name" blur placeholder="请输入或选择检测项目"
                  ></el-input>
                </div>
                <div v-if="item.key==='compare'" @click.stop="_handleRow(scope)">
                  <el-input v-model="scope.row.compare" blur placeholder="请输入或选择比较符"
                  ></el-input>
                </div>
                <div v-if="item.key==='limitValue'" @click.stop="_handleRow(scope)">
                  <el-input v-model="scope.row.limitValue" blur placeholder="请输入或选择限量值"
                  ></el-input>
                </div>
                <div v-if="item.key==='remark'" @click.stop="_handleRow(scope)">
                  <el-input v-model="scope.row.remark" blur placeholder="请输入备注"
                  ></el-input>
                </div>
                <div v-if="item.key==='unit'" @click.stop="_handleRow(scope)">
                  <el-input v-model="scope.row.unit" blur placeholder="请输入或选择检测单位"
                  ></el-input>
                </div>
              </template>
            </vxe-table-column>
          </PTVXETable>
        </Col>
      </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: '',
        code: '',
        cycle: 0,
        price: 0,
        stdId: '',
        extent: '',
        grade: ''
      },
      searchOpen: true,
      selectIds: [],
      getPage: {
        records: []
      },
      typeData: [],
      showModal: false,
      aptitudeData: [],
      ruleValidate: {
        extent: [
          { required: true, message: '测量范围不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '授权仪器名称不能为空', trigger: 'blur' }
        ],
        cycle: [
          {
            required: true,
            type: 'number',
            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.save(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._getJudgeBasisList()
      this.$refs.formObj.resetFields()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加检定依据'
      } else {
        this.formObj = formObj
        this.id = formObj.id
        console.log(this.formObj)
        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>
