<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-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="140"
        :icon-msg="iconMsg"
        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"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <div v-else-if="item.status">
              {{  scope.row[item.key].display }}
            </div>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <fileManage ref="fileManageModal" @on-result-change="_fileResult" />
    <!--添加编辑-->
    <DrugSampleProductEdit ref="editModal" @on-result-change="_page" />

    <!--操作日志-->
    <sampleRecord ref="recordModal" />
  </div>
</template>
<script>
import { drugSample } from '../../../../api'
import fileManage from '../../../base-manage/fileManage'
import DrugSampleProductEdit from '../EnvPlanEdit'
import sampleRecord from '../../../record/sampleRecord'
export default {
  components: { DrugSampleProductEdit, fileManage, sampleRecord },
  data() {
    return {
      searchOpen: true,
      formObj: {
        sampleCode: '',
        name: '',
        supplier: ''
      },
      selectIds: [],
      btn: [
        { type: 'success', id: '', name: '添加' },
        { type: '', id: '', name: '提交' }
      ],
      pageColumns: [
        { title: '样本编号', key: 'sampleCode', width: 150, fixed: 'left' },
        { title: '检测类型', key: 'category', width: 120, type: true },
        { title: '状态', key: 'status', status: true, width: 120 },
        { title: '点位名称', key: 'name', width: 120 },
        { title: '点位编号', key: 'code' },
        { title: '请检单位', key: 'pcDepart', width: 120 },
        { title: '备注', key: 'remark', width: 120 }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-submit', id: '', name: '提交' },
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-cloud', id: '', name: '附件' },
        { type: 'pt-a-end', id: '', name: '中止' },
        { type: 'pt-a-clock', id: '', name: '操作日志' }
      ],
      statusList: [{ value: 0, label: '禁用' }, { value: 1, label: '正常' }],
      selectData: [],
      judStatus: false
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('tabSearch')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await drugSample.pageSampleEnvReg(
        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 '添加':
          this.$refs.editModal._open()
          break
        case '提交':
          this._submitByIds()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectData':
          this._selectDataEnvPlan(data)
          break
      }
    },
    _selectDataEnvPlan(data) {
      const selectIds = []
      data.forEach(i => {
        selectIds.push(i.id)
      })
      this.selectIds = selectIds
      this.selectData = data
    },
    _iconClick(res, data) {
      switch (res) {
        case '提交':
          this._submitById(data.id)
          break
        case '编辑':
          this._editModal(data.id)
          break
        case '附件':
          this._upload(data.id)
          break
        case '中止':
          this._endById(data.id)
          break
        case '操作日志':
          this._record(data.id)
          break
      }
    },
    // 提交
    _submitById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定提交该数据？',
        onOk: () => {
          this._submitOk(id)
        }
      })
    },
    _submitByIds() {
      // 批量提交
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定提交 ' + ids.length + ' 条记录？',
          onOk: () => {
            this._submitOk(ids)
          }
        })
      }
    },
    _submitOk: async function(id) {
      const result = await drugSample.submitEnvPlan(id)
      if (result) {
        this.$Message.success('提交成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await drugSample.getSampleVO(id)
      this.$refs.editModal._open(result)
    },
    // 附件
    _upload(id) {
      const tempData = {
        id: id,
        uploadFileUrl: '/v1/sample_annex/upload/',
        downloadFileUrl: '/v1/sample_annex/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()
      }
    },
    // 中止
    _endById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定中止该记录？',
        onOk: () => {
          this._endOk(id)
        }
      })
    },
    _endOk: async function(id) {
      const result = await drugSample.stopTest(id)
      if (result) {
        this.$Message.success('中止成功')
        this._page()
      }
    },
    // 操作日志
    _record(id) {
      this.$refs.recordModal._open(id)
    }
  }
}
</script>
