<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.name" placeholder="请输入物料名称" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="供应商:">
          <Input v-model="formObj.supplier" 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="_ctimeChange"
          />
          <input v-model="formObj.inspectionBegin" type="hidden" name="inspectionBegin">
          <input v-model="formObj.inspectionEnd" type="hidden" name="inspectionEnd">
        </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"
        :opt-col-width="110"
        :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">
            <template v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </template>
            <template v-else-if="item.status">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </template>
            <template v-else>
              {{ scope.row[item.key] }}
            </template>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
  </div>
</template>
<script>
import { drugSample } from '../../../../api'
export default {
  components: {},
  data() {
    return {
      searchOpen: true,
      formObj: {
        sampleCode: '',
        name: '',
        supplier: '',
        status: '',
        inspectionBegin: '',
        inspectionEnd: ''
      },
      pageColumns: [
        { title: '样本编号', key: 'sampleCode', width: 150, fixed: 'left' },
        { title: '检测类型', key: 'category', width: 120, type: true },
        {
          title: '状态',
          key: 'status',
          status: true,
          width: 120,
          ADataEntry: true
        },
        { title: '物品名称', key: 'name', width: 120 },
        { title: '物品编号', key: 'code', width: 120 },
        { title: '供应商', key: 'supplier', width: 120 },
        { title: '请检部门', key: 'pcDepart', width: 120 },
        { title: '报检人', key: 'pcPerson', width: 120 },
        {
          title: '请检时间',
          key: 'pcTime',
          width: 160,
          dateTime: true
        },
        { title: '备注', key: 'remark', width: 120 }
      ],
      getPage: {
        records: []
      },
      btn: [
        // { type: 'success', id: '', name: '导出' },
        // { type: 'error', id: '', name: '中止' }
      ],
      iconMsg: [
        { type: 'pt-a-cloud', id: '', name: '附件' },
        { type: 'pt-a-clock', id: '', name: '操作日志' },
        { type: 'pt-a-view', id: '', name: '接收记录' }
      ],
      selectIds: []
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('tabSearch')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    // 参数
    _searchParamsTemp() {
      const data = this.$serializeForm(this.formObj)
      return this.$extend(data, { page: 1, rows: 5000 })
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await drugSample.pageSampleOosRegHis(
        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 '导出':
          if (this.getPage.records.length === 0) {
            this.$Message.warning('暂无数据，不可导出！')
          } else {
            this._export()
          }
          break
        case '中止':
          this._endByIds()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _endByIds() {
      const ids = this.selectIds
      if (ids.length === 0) return this.$Message.warning('请至少选择一条数据!')
      this.$Modal.confirm({
        title: '提示',
        content: `确定中止 ${ids.length} 条记录`,
        onOk: () => {
          this._endOk(ids)
        }
      })
    },
    _endOk: async function(id) {
      const result = await drugSample.endById(id)
      if (result) {
        this.$Message.success('中止成功!')
        this._page()
      }
    },
    _ctimeChange(data) {
      this.formObj.inspectionBegin = data[0]
      this.formObj.inspectionEnd = data[1]
    },
    _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._upload(data.id)
          break
        case '操作日志':
          this._record(data.id)
          break
        case '接收记录':
          this._receiveRecord(data.id)
          break
      }
    },
    // 附件
    _upload(id) {
      const tempData = {
        id: id,
        uploadFileUrl: '/llgt/v1/sample_attachment/upload/',
        downloadFileUrl: '/llgt/v1/sample_attachment/download/'
      }
      this.$refs.fileModal._open(tempData, 'sampleId')
    },
    // 文件返回的数据
    _fileResult(msg, data) {
      switch (msg) {
        case 'file-page':
          this.$refs.fileModal._openFile(data)
          break
        case 'delete-page':
          this.$refs.fileModal._delPage('', data)
          break
        case 'success':
          this.$Message.success('删除成功！')
          this.$refs.fileModal._page()
          break
        default:
          this.$refs.fileModal._page()
      }
    },
    _export: async function() {
      const result = await drugSample.otherPageHis(this._searchParamsTemp())
      // 导出全部数据
      if (result) {
        this.$Modal.confirm({
          title: '提示',
          content: '确定导出全部数据（最多导出5000条）？',
          onOk: () => {
            this.$exportExcel(
              'dataAccessTable',
              '其他报检',
              this.pageColumns,
              result.records
            )
          }
        })
      }
    },
    // 操作日志
    _record(id) {
      this.$refs.recordModal._open(id)
    },
    // 接收记录
    _receiveRecord(id) {
      this.$refs.receiveModal._open(id)
    }
  }
}
</script>
