<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24">
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="90"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="检测项目:">
          <Input v-model="formObj.name" placeholder="请输入检测项目" clearable @on-enter="_formSearch" />
        </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.groupName" 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" show-search-btn="true" @on-result-change="_btnClick" />
      </Col>
      <!--table-->
      <Col span="24">
      <element-table
        id="testBasisTable"
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="140"
        :icon-msg="iconMsg"
        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"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <span v-else-if="item.judged">
              <span v-if="scope.row[item.key]===1">
                是
              </span>
              <span v-else-if="scope.row[item.key]===0">
                否
              </span>
            </span>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--添加编辑-->
    <ItemEdit ref="editModal" @on-result-change="_page" />

    <!--导入-->
    <ImportInfo ref="importModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { aptitudeItem } from '../../../api'
import ItemEdit from './ItemEdit'
import ImportInfo from './ImportInfo'

export default {
  components: { ItemEdit, ImportInfo },
  data() {
    return {
      searchOpen: true,
      formObj: {
        name: '',
        code: '',
        groupName: ''
      },
      selectIds: [],
      btn: [
        { type: 'success', id: '', name: '添加' },
        { type: 'error', id: '', name: '删除' },
        { type: 'error', id: '', name: '导入' }
      ],
      pageColumns: [
        { title: '检测项目', key: 'name', width: 140 },
        { title: '项目类别', key: 'type', width: 100 },
        { title: '所属产品', key: 'product', width: 100 },
        { title: '检测依据', key: 'code', width: 180 },
        { title: '检测依据名称', key: 'standardName', width: 300 },
        { title: '检测科室', key: 'groupName', width: 90 },
        { title: '比较符', key: 'compareSymbol', width: 80 },
        { title: '限值', key: 'limitValue', width: 240 },
        { title: '单位', key: 'unit', width: 180 },
        { title: '是否系统判定', key: 'judged', width: 120, judged: true },
        { title: '检测人员', key: 'tester', width: 120 },
        { title: '备注', key: 'remark', width: 240 }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await aptitudeItem.pageAptitudeItem(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case '导入':
          this.$refs.importModal._open()
          break
        case '删除':
          this._deleteByIds()
          break
        case '导出':
          if (this.getPage.records.length === 0) {
            this.$Message.warning('暂无数据，不可导出！')
          } else {
            this._export()
          }
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectIds':
          this.selectIds = data
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
        case '编辑':
          this._editModal(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该数据？',
        onOk: () => {
          this._deleteOk(id)
        }
      })
    },
    _deleteByIds() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定删除 ' + ids.length + ' 条记录？',
          onOk: () => {
            this._deleteOk(ids)
          }
        })
      }
    },
    _deleteOk: async function(id) {
      const result = await aptitudeItem.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await aptitudeItem.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    _export: async function() {
      const result = await aptitudeItem.page(this._searchParamsTemp())
      // 导出全部数据
      if (!result) {
        return
      }
      this.$Modal.confirm({
        title: '提示',
        content: '确定导出全部数据（最多导出5000条）？',
        onOk: () => {
          this.$exportExcel(
            'testBasisTable',
            '检测依据',
            this.pageColumns,
            result.records
          )
        }
      })
    },
    // 参数
    _searchParamsTemp: function() {
      const data = this.$serializeForm(this.formObj)
      return this.$extend(data, { page: 1, rows: 5000 })
    }
  }
}
</script>
