<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 label="检测类型:" class="search-item" prop="category">
          <Input v-model="formObj.category" placeholder="请输入检测类型" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--操作-->
      <Col span="24" />
      <btn-list :msg="btn" :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
        :opt-col-width="140"
        :icon-msg="iconMsg"
        @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-if="item.status">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </template>
            <template v-else-if="item.key === 'allInput'">
              {{ scope.row[item.key]?scope.row[item.key] === "f" ? '未全部录入': '已全部录入':'' }}
            </template>
            <template v-else-if="item.key==='judged'">
              {{ scope.row[item.key]===1?'是':'否' }}
            </template>
            <template v-else>
              {{ scope.row[item.key] }}
            </template>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--填写结果-->
    <fillResult ref="resultModal" @on-result-change="_formSearch" />
  </div>
</template>
<script>
import { drugItem, drugSample } from '../../../../api'
import fillResult from '../data-entry/fillResult'

export default {
  components: { fillResult },
  data() {
    return {
      searchOpen: true,
      formObj: {
        sampleCode: '',
        name: '',
        category: ''
      },
      btn: [
        // { type: 'primary', id: '', name: '提交至复核' }
      ],
      selectIds: [],
      pageColumns: [
        { title: '样本编号', key: 'sampleCode', width: 160, fixed: 'left' },
        { title: '状态', key: 'progress', width: 140, status: true },
        { title: '点位名称', key: 'name', width: 140 },
        { title: '点位编码', key: 'code', width: 150 },
        { title: '检测类型', key: 'category' },
        { title: '备注', key: 'remark' }
        // { title: '录入时间', key: '', dateTime: true }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '填写结果' },
        { type: 'pt-a-clock', id: '', name: '操作日志' }
      ],
      typeList: [
        { value: 'ORIGINAL', label: '进场原料' },
        { value: 'OTHER', label: '其他原料' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('', 305)
      } else {
        return this.$tableHeight('', 261)
      }
    }
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await drugSample.pageSampleInputEnv(
        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 'selectIds':
          this.selectIds = data
          break
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '提交至复核':
          this._submitToCheck()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _submitToCheck() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定提交至复核？',
          onOk: async () => {
            const result = await drugItem.submitForReview(this.selectIds)
            if (result) {
              this.$Message.success('提交成功')
              this._page()
            }
          }
        })
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '操作日志':
          this._record(data.id)
          break
        case '填写结果':
          this._fillResult(data.id)
          break
      }
    },
    _fillResult(id) {
      this.$refs.resultModal._open(id)
    },
    // 操作日志
    _record(id) {
      this.$refs.recordModal._open(id)
    }
  }
}
</script>
