<template>
  <div>
    <Row>
      <Form
        v-show="searchOpen"
        id="his-sample-form"
        inline
        onsubmit="return false"
        :label-width="100"
        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 class="search-item" label="物品编号:">
          <Input name="code" placeholder="请输入物品编号" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="请检时间:">
          <Date-picker
            ref="pcTime"
            type="datetimerange"
            placeholder="报检时间"
            style="width: 100%;"
            :editable="false"
            transfer
            @on-change="_histDateChange"
          />
          <input type="hidden" name="pcTimeBegin">
          <input type="hidden" name="pcTimeEnd">
        </Form-item>
        <!--<Form-item class="search-item" label="报检单位:">-->
        <!--<Input name="declarationInspection" placeholder="请输入报检单位" clearable @on-enter="_formSearch" />-->
        <!--</Form-item>-->
        <Form-item class="search-item" label="送样人:">
          <Input name="sender" placeholder="请输入送样人" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="样品接收人:">
          <Input name="receiver" placeholder="请输入样品接收人" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="样品接收时间:">
          <Date-picker
            ref="receiveTime"
            type="datetimerange"
            placeholder="样品接收时间"
            style="width: 100%;"
            :editable="false"
            transfer
            @on-change="_receiveTimeChange"
          />
          <input type="hidden" name="receiveTimeBegin">
          <input type="hidden" name="receiveTimeEnd">
        </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"
        @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" @click.stop="_handleIndex(scope)">
            <span v-if="item.datetime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </span>
            <span v-else-if="item.key==='status'">
              {{ scope.row[item.key]?scope.row[item.key].display:'' }}
            </span>
            <span v-else>
              {{ scope.row[item.key] }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          slot="col"
          label="操作"
          align="center"
          :width="120"
          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>

    <!--操作日志-->
    <sampleRecord ref="recordModal" />
  </div>
</template>

<script>
import { drugSample } from '../../../api'
import sampleRecord from '../../record/sampleRecord'
export default {
  name: 'PreparationManage',
  components: { sampleRecord },
  data() {
    return {
      name: '',
      pageColumns: [
        {
          title: '样本编号',
          key: 'sampleCode',
          width: 200,
          fixed: 'left',
          align: 'center'
        },
        { title: '物品名称', key: 'name', width: 200, align: 'center' },
        {
          title: '样品接收时间',
          key: 'receiveTime',
          align: 'center',
          datetime: true
        },
        {
          title: '样品接收人',
          key: 'receiver',
          align: 'center'
        },
        { title: '物品编号', key: 'code', width: 200, align: 'center' },
        { title: '检测类型', key: 'category', width: 150, align: 'center' },
        {
          title: '请检时间',
          key: 'pcTime',
          align: 'center',
          datetime: true
        },
        { title: '送样人', key: 'sender', align: 'center' },
        {
          key: 'sendTime',
          title: '送样人接收时间',
          align: 'center',
          datetime: true
        }
      ],
      receiveTime: '',
      getPage: {},
      searchOpen: true,
      currentIndex: '',
      selectIds: [],
      iconMsg: [
        // { id: '', type: 'pt-a-cloud', name: '附件' }
        { id: '', type: 'pt-a-clock', name: '操作日志' }
      ]
    }
  },
  computed: {
    tableHeight() {
      if (this.searchOpen) {
        return this.$tableHeight('searchTwoNoBtn')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  methods: {
    async _page() {
      const data = this.$serialize('his-sample-form')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await drugSample.pageReportIssueHis(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _clearPage() {
      $('#his-sample-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
      this.$refs.pcTime.handleClear()
      this.$refs.receiveTime.handleClear()
      this.$refs.receiveTime.handleClear()
    },
    _btnClick(msg, data) {
      switch (msg) {
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _iconClick(name, data) {
      switch (name) {
        case '附件':
          this._eRecord(data.id)
          break
        case '操作日志':
          this._record(data.id)
          break
      }
    },
    // 操作日志
    _record(id) {
      this.$refs.recordModal._open(id)
    },

    _uploadByIds() {},
    _handleIndex(data) {
      this.currentIndex = data.$index
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
        default:
          this._page()
      }
    },
    _eRecord(id) {
      const tempData = {
        id: id,
        uploadFileUrl: '/llgt/v1/sample_attachment/upload/',
        downloadFileUrl: '/llgt/v1/sample_attachment/download/'
      }
      this.$refs.fileManageModal._open(tempData, 'sampleId')
    },
    // 文件返回的数据
    _fileResult(msg, data) {
      switch (msg) {
        case 'file-page':
          this.$refs.fileManageModal._openFile(data)
          break
        case 'delete-page':
          this.$refs.fileManageModal._delPage('', data)
          break
        case 'success':
          this.$Message.success('删除成功！')
          this.$refs.fileManageModal._page()
          break
        default:
          this.$refs.fileManageModal._page()
      }
    },

    _histDateChange(data) {
      $('input[name="pcTimeBegin"]').val(data[0])
      $('input[name="pcTimeEnd"]').val(data[1])
    },
    _receiveTimeChange(data) {
      $('input[name="receiveTimeBegin"]').val(data[0])
      $('input[name="receiveTimeEnd"]').val(data[1])
    },
    _labTimeChange(data) {
      $('input[name="labReceiverTimeBegin"]').val(data[0])
      $('input[name="labReceiverTimeEnd"]').val(data[1])
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>

<style scoped>
</style>
