<!--检测项目编辑列表（添加、导入检测项目）-->
<template>
  <div>
    <Modal
      v-model="showModal"
      v-drag
      :mask-closable="false"
      title="检测项目管理"
      :width="1200"
      footer-hide
    >
      <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.catalogueId" type="hidden">
          </Form-item>
          <Form-item class="search-item" label="检测依据:">
            <Input v-model="formObj.code" 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" />
          <Button type="primary" style="margin-bottom: 5px" @click="_importItem">导入检测项目</Button>
        </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-if="item.key==='orderBy'"
                @click.stop="_handleRow(scope)"
              >
                <el-input-number
                  v-model="scope.row.orderBy"
                  size="small"
                  placeholder="排序"
                  @change="_handleOrderValueEdit(scope.row.id,scope.row.orderBy)"
                />
              </span>
              <span v-else>
                {{ scope.row[item.key] }}
              </span>
            </template>
          </el-table-column>
        </element-table>
        </Col>
      </Row>
    </Modal>
    <!--导入项目  左右关联的modal   单个导入项目-->
    <SampleItemSelect ref="relItemModal" @on-result-change="_formSearch" />
  </div>
</template>
<script>
import { drugCatalogueItem } from '../../api'
import SampleItemSelect from './SampleItemSelect'

export default {
  components: {
    SampleItemSelect
  },
  data() {
    return {
      formObj: {
        catalogueId: '',
        name: '',
        code: '',
        standardName: ''
      },
      showModal: false,
      searchOpen: false,
      btn: [{ type: 'primary', id: '', name: '导入检测项目' }],
      iconMsg: [{ type: 'pt-a-trash', id: '', name: '删除' }],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      pageColumns: [
        { title: '检测项目', key: 'name', width: 140 },
        { title: '检测类别', key: 'type', width: 140 },
        { title: '检测依据', key: 'code', width: 160 },
        { title: '检测依据名称', key: 'standardName', width: 220 },
        { title: '检测科室', key: 'groupName', width: 110 },
        { title: '比较符', key: 'compareSymbol', width: 90 },
        { title: '限值', key: 'limitValue', width: 90 },
        { title: '排序', key: 'orderBy', width: 180 },
        { title: '单位', key: 'unit', width: 100 },
        { title: '是否系统判定', key: 'judged', width: 120, judged: true },
        { title: '备注', key: 'remark' }
      ],
      catalogueId: '',
      getPage: {},
      selectData: []
    }
  },
  methods: {
    _open(catalogueId, label) {
      this.formObj = this.$resetFields(this.formObj)
      this.showModal = true
      this.catalogueId = catalogueId
      this.formObj.catalogueId = catalogueId
      this.getPage.records = []
      this.$refs.pageTable._hideLoading()
      if (catalogueId) {
        this._page()
      }
    },

    // 操作的序号
    _handleRow(data) {
      this.currentRow = data.row
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await drugCatalogueItem.pageCatalogueItem(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '导入检测项目':
          this.$refs.relItemModal._open(this.catalogueId)
          break
      }
    },
    _importItem() {
      this.$refs.relItemModal._open(this.catalogueId)
    },
    // 操作列操作
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该数据？',
        onOk: () => {
          this._deleteOk(id)
        }
      })
    },

    _handleOrderValueEdit(id, value) {
      this._edit({ id: id, obj: { orderBy: value } })
    },

    _edit: async function(data) {
      const result = await drugCatalogueItem.edit(data)
      if (result) {
        this._page()
      }
    },

    _deleteOk: async function(id) {
      const result = await drugCatalogueItem.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
      }
    }
  }
}
</script>
