<template>
  <div>
    <Row>
      <Form
        v-show="searchOpen"
        id="sample-record-form"
        inline
        onsubmit="return false"
        :label-width="70"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="样本编号:">
          <Input name="sampleCode" placeholder="请输入样本编号" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="物料名称:">
          <Input name="name" placeholder="请输入物料名称" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item label="检测类型" class="search-item" prop="type">
          <Input name="category" placeholder="请输入检测类型" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="制备人:">
          <Input name="maker" placeholder="请输入制备人" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item label="制备时间" class="search-item">
          <Date-picker
            ref="preDateTime"
            type="datetimerange"
            placeholder="制备时间"
            style="width: 100%;"
            :editable="false"
            transfer
            @on-change="_preTimeChange"
          />
          <input type="hidden" name="makeTimeBegin">
          <input type="hidden" name="makeTimeEnd">
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      <Col />
      <!--<btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />-->
      </Col>
      <Col />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="140"
        @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"
          :align="item.align"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <span v-if="item.key==='sampleCode'">
              <a @click.stop="_detailModal(scope.row)">
                {{ scope.row[item.key] }}
              </a>
            </span>
            <span v-else-if="item.datetime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </span>
            <span v-else>
              {{ scope.row[item.key] }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          slot="col"
          label="操作"
          align="center"
          :width="90"
          fixed="right"
        >
          <template slot-scope="scope">
            <icon-list
              :msg="iconMsg"
              :row-data="scope.row"
              :row-index="scope.$index"
              @on-result-change="_iconClick"
            />
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--样本详情页-->
    <samplePreparationDetail ref="sampleModal" @on-result-change="_formSearch" />
    <!--制备详情页-->
    <preparationDetail ref="preDetailModal" />
  </div>
</template>

<script>
import samplePreparationDetail from '../samplePreparationDetail'
import preparationDetail from '../preparationDetail'
import { drugSampleKeep, drugSample } from '../../../api'
export default {
  name: 'SamplePreparationManage',
  components: {
    samplePreparationDetail,
    preparationDetail
  },
  data() {
    return {
      pageColumns: [
        {
          title: '样本编号',
          key: 'sampleCode',
          width: 200,
          fixed: 'left',
          align: 'center'
        },
        { title: '检测类型', key: 'category', width: 200, align: 'center' },
        { title: '物品名称', key: 'name', width: 200, align: 'center' },
        { title: '物品编号', key: 'code', width: 200, align: 'center' },
        { title: '供应商', key: 'supplier', width: 200, align: 'center' },
        { title: '制备人', key: 'maker', width: 200, align: 'center' },
        {
          title: '制备时间',
          key: 'makeTime',
          width: 200,
          align: 'center',
          datetime: true
        },
        {
          title: '请检部门',
          key: 'pcDepart',
          align: 'center'
        },
        {
          title: '请检时间',
          key: 'pcTime',
          align: 'center',
          datetime: true
        },
        { title: '备注', key: 'remark', align: 'center' }
      ],
      iconMsg: [
        { id: 'drug-sample-make-detail', type: 'pt-a-view', name: '制备详情' }
      ],
      getPage: {},
      // btn: [{ id: '', type: 'primary', name: '打印标签' }],
      searchOpen: true,
      selectIds: []
    }
  },
  computed: {
    tableHeight() {
      if (this.searchOpen) {
        return this.$tableHeight('searchTwoNoBtn')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  methods: {
    async _page() {
      const data = this.$serialize('sample-record-form')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await drugSample.pageSampleMakeHis(data)
      if (result) {
        this.getPage = result
        this.$nextTick(() => {
          this.$refs.pageTable._initTable()
        })
      }
    },
    _clearPage() {
      $('#sample-record-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
      this.category = ''
      this.$refs.preDateTime.handleClear()
    },
    _btnClick(msg, data) {
      switch (msg) {
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    // 详情页
    async _detailModal(data) {
      const result = await drugSampleKeep.getById(data.id)
      if (result) {
        console.log(result)
        this.$refs.sampleModal._open(result)
      }
    },
    // 制备详情
    _iconClick(msg, data) {
      switch (msg) {
        case '制备详情':
          this._preDetail(data)
          break
      }
    },
    _preDetail(data) {
      this.$refs.preDetailModal._open(
        data.id,
        data.declarationName,
        this.$dateformat(data.inspectionDate, 'yyyy-mm-dd HH:MM:ss')
      )
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
        default:
          this._page()
      }
    },
    _preTimeChange(data) {
      $('input[name="makeTimeBegin"]').val(data[0])
      $('input[name="makeTimeEnd"]').val(data[1])
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>

<style scoped>
</style>
