<template>
  <div>
    <Row>
      <Form
        v-show="searchOpen"
        id="sample-preparation-form"
        inline
        onsubmit="return false"
        :label-width="75"
        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 name="category" label="检测类型" class="search-item" prop="type">
          <Input name="category" placeholder="请输入检测类型" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      <Col span="24">
      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
      </Col>
      <Col span="24">
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="140"
        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"
          :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-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>
      </element-table>
      </Col>
    </Row>
    <!--制备-->
    <Preparation ref="preModal" @on-result-change="_formSearch" />
    <!--样本详情页-->
    <samplePreparationDetail ref="sampleModal" @on-result-change="_formSearch" />
  </div>
</template>

<script>
import Preparation from '../Preparation'
import samplePreparationDetail from '../samplePreparationDetail'
import { drugSample } from '../../../api'
export default {
  name: 'SamplePreparation',
  components: {
    Preparation,
    samplePreparationDetail
  },
  data() {
    return {
      btn: [{ id: '', type: 'primary', name: '制备' }],
      pageColumns: [
        {
          title: '样本编号',
          key: 'sampleCode',
          width: 150,
          fixed: 'left',
          align: 'center'
        },
        { title: '业务类型', key: 'category', width: 150, align: 'center' },
        { title: '物品名称', key: 'name', width: 150, align: 'center' },
        { title: '物品编码', key: 'code', width: 150, align: 'center' },
        { title: '计量单位', key: 'unit', width: 150, align: 'center' },
        { title: '规格', key: 'spec', width: 150, align: 'center' },
        { title: '数量', key: 'spec', width: 150, align: 'center' },
        { title: '请检人', key: 'pcPerson', width: 200, align: 'center' },
        {
          title: '报检单位',
          key: 'pcDepart',
          align: 'center'
        },
        { title: '备注', key: 'remark', align: 'center' }
      ],
      getPage: {},
      searchOpen: false,
      selectIds: [],
      selectData: []
    }
  },
  computed: {
    tableHeight() {
      if (this.searchOpen) {
        return this.$tableHeight('tabSearch')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  methods: {
    async _page() {
      const data = this.$serialize('sample-preparation-form')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await drugSample.pageSampleMake(data)
      if (result) {
        this.getPage = result
      }
      if (process.browser) {
        this.$refs.pageTable._hideLoading()
      }
    },
    _clearPage() {
      $('#sample-preparation-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
    },
    _btnClick(msg, data) {
      switch (msg) {
        case 'search':
          this.searchOpen = !this.searchOpen
          break
        case '制备':
          this._preparation()
          break
      }
    },
    _preparation() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else if (this.selectIds.length >= 2) {
        this.$Message.warning('制备不可超过一条数据！')
      } else {
        this.$refs.preModal._open(this.selectData, this.selectIds)
      }
    },
    // 详情页
    async _detailModal(data) {
      const result = await drugSample.getById(data.id)
      if (result) {
        console.log(result)
        this.$refs.sampleModal._open(result)
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this._selectData(data)
          break
        default:
          this._page()
      }
    },
    _selectData(data) {
      const selectIds = []
      for (let i = 0; i < data.length; i++) {
        selectIds.push(data[i].id)
      }
      this.selectIds = selectIds
      this.selectData = data
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>

<style scoped>
</style>
