<!--检测项目编辑列表（添加、导入检测项目）-->
<template>
  <div>
    <Modal
      v-model="showModal"
      v-drag
      :mask-closable="false"
      title="检测项目管理"
      :width="800"
    >
      <Row>
        <Col span="24" />
        <Form v-model="formObj" inline onsubmit="return false" :label-width="90">
          <Form-item class="search-item" label="检测项目:" style="margin-left: -25px">
            <Input v-model="formObj.name" placeholder="请输入检测项目" clearable @on-enter="_formSearch" />
            <input v-model="formObj.staffId" type="hidden">
          </Form-item>
          <Form-item class="search-item" label="检测依据:">
            <Input v-model="formObj.standardCode" placeholder="请输入检测依据" clearable @on-enter="_formSearch" />
          </Form-item>
          <Form-item class="search-item" label="检测依据名称:">
            <Input v-model="formObj.standardName" placeholder="请输入检测依据名称" clearable @on-enter="_formSearch" />
          </Form-item>
          <Form-item class="search-btn">
            <Button type="primary" @click="_formSearch">
              搜索
            </Button>
          </Form-item>
        </Form>
        </Col>
        <!--正常界面-->
        <Col span="24" />
        <btn-list :msg="btn" :open="searchOpen" @on-result-change="_btnClick" />
        </Col>
        <!-- 表格 -->
        <Col span="24" />
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          select-data
          :table-height="350"
          :get-page="getPage"
          :icon-msg="iconMsg"
          :opt-col-width="80"
          show-check-box
          @on-result-change="_tableResultChange"
        >
          <el-table-column
            v-for="item in pageColumns"
            :key="item.key"
            show-overflow-tooltip
            sortable
            :prop="item.key"
            :label="item.title"
            :width="item.width"
            :min-width="200"
          >
            <template slot-scope="scope">
              <span v-if="item.key==='judged'">
                {{ scope.row[item.key]===1?'是':'否' }}
              </span>
              <span v-else>
                {{ scope.row[item.key] }}
              </span>
            </template>
          </el-table-column>
        </element-table>
        </Col>
      </Row>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
import { staffEquip, equipInfo } from '../../../../api'
export default {
  components: {},
  data() {
    return {
      formObj: {
        staffId: '',
        name: '',
        standardCode: '',
        standardName: ''
      },
      showModal: false,
      searchOpen: false,
      btn: [],
      iconMsg: [],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      pageColumns: [
        { title: '仪器名称', key: 'name', width: 230 },
        { title: '仪器编号', key: 'labNum', width: 140 },
        { title: '品牌', key: 'brand', width: 150 },
        { title: '规格型号', key: 'spec', width: 180 },
        { title: '描述', key: 'remark', width: 260 }
      ],
      staffId: '',
      getPage: {},
      selectData: []
    }
  },
  methods: {
    _open(staffId, label) {
      this.formObj = this.$resetFields(this.formObj)
      this.showModal = true
      this.staffId = staffId
      this.formObj.staffId = staffId
      this.getPage.records = []
      this.$refs.pageTable._hideLoading()
      if (staffId) {
        this._page()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await equipInfo.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '导入检测项目':
          this.$refs.relItemModal._open(this.staffId)
          break
      }
    },
    // 操作列操作
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.relPackageId)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该数据？',
        onOk: () => {
          this._deleteOk(id)
        }
      })
    },
    _deleteOk: async function(id) {
      const result = await staffEquip.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    // table结果  返回整行
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this.selectData = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _ok() {
      const data = this.selectData
      if (data.length === 0) {
        this._hideLoading()
        this.$Message.warning('请选择至少一条数据！')
      }
      const ids = []
      data.forEach(item => {
        ids.push(item.id)
      })
      this._saveCatalogueItem(ids)
    },
    async _saveCatalogueItem(ids) {
      const result = await staffEquip.add({
        equipId: ids,
        staffId: this.staffId
      })
      if (result) {
        this.$message.success('添加成功')
        this.showModal = false
        this._hideLoading()
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _cancel() {
      this.selectData = []
      this.showModal = false
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    }
  }
}
</script>
