<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24">
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="70"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="样本编号:">
          <Input v-model="formObj.sampleCode" placeholder="请输入样本编号" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="物料名称:">
          <Input v-model="formObj.sampleName" placeholder="请输入物料名称" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="检测项目:">
          <Input v-model="formObj.name" placeholder="请输入检测项目" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="录入时间:">
          <Date-picker
            type="datetimerange"
            split-panels
            style="width:100%;"
            placeholder="请选择录入时间"
            :editable="false"
            @on-change="_signDateChange"/>
          <input v-model="formObj.testDateBegin" type="hidden" name="testDateBegin">
          <input v-model="formObj.testDateEnd" type="hidden" name="testDateEnd">
        </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
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        show-check-box
        select-data
        @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>
            <div v-else-if="item.status">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display}}
            </div>
            <span v-else-if="item.key === 'singleJudge'">
              {{ scope.row[item.key]?scope.row[item.key].display:'' }}
            </span>
            <div v-else-if="item.key==='judged'">
              {{ scope.row[item.key]===1?'是':'否' }}
            </div>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
  </div>
</template>
<script>
import { drugItem } from '../../../api'
export default {
  components: {},
  data() {
    return {
      searchOpen: false,
      formObj: {
        sampleCode: '',
        name: '',
        testDateBegin: '',
        testDateEnd: '',
        sampleName: ''
      },
      btn: [
        // { id: '', type: 'primary', name: '导出' }
      ],
      selectIds: [],
      selectData: [],
      pageColumns: [
        { title: '样本编号', key: 'sampleCode', width: 150, fixed: true },
        { title: '牌号', key: 'sampleName', width: 120 },
        { title: '检测项目', key: 'name', width: 120 },
        {
          title: '状态',
          key: 'status',
          width: 120,
          status: true,
          ADataEntry: true
        },
        { title: '检测结果', key: 'testValue', width: 120 },
        { title: '单位', key: 'unit', width: 100 },
        { title: '是否系统判定', key: 'judged', width: 120, judged: true },
        {
          title: '判定结果',
          key: 'singleJudge',
          width: 140,
          singleJudge: true
        },
        { title: '价类', key: 'category', width: 150 },
        { title: '检测依据', key: 'code', width: 160 },
        { title: '检测依据名称', key: 'standardName', width: 200 },
        { title: '检测科室', key: 'groupName', width: 120 },
        { title: '仪器设备', key: 'equipNames', width: 120 },
        { title: '比较符', key: 'compareSymbol', width: 120 },
        { title: '限值', key: 'limitValue', width: 120 },
        { title: '分配时间', key: 'allocateTime', width: 200, dateTime: true },
        { title: '录入时间', key: 'testTime', width: 200, dateTime: true }
      ],
      getPage: {}
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('', 305)
      } else {
        return this.$tableHeight('', 261)
      }
    }
  },
  methods: {
    // 录入时间
    _signDateChange(data) {
      this.formObj.testDateBegin = data[0]
      this.formObj.testDateEnd = data[1]
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await drugItem.pageInputHis(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectData':
          this.selectIds = data.map(({ id: i }) => i)
          this.selectData = data
          break
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '导出':
          this._exports()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _exports() {
      if (this.selectIds.length === 0)
        return this.$Message.warning('请至少选择一条数据!')
      this.$Modal.confirm({
        title: '提示',
        content: `确定要导出${this.selectIds.length}条数据!`,
        onOk: () => {
          this.$exportExcel(
            'dataAccessTable',
            'A类数据录入',
            this.pageColumns,
            this.selectData
          )
        }
      })
    }
  }
}
</script>
