<template>
  <div>
    <Modal v-model="showSampleModal" v-drag @on-visible-change="_visibleChange"
           width="1100" class="zIndex-900 modal-footer-none">
      <p slot="header">
        管理样品
      </p>
    <div>
      <!--内容-->
      <Row>
        <!--查询-->
        <Col span="24" style="padding-bottom: 5px">
          <Form id="search-wait" v-show="searchOpen" :label-width="90" inline onsubmit="return false">
            <label class="label-sign"></label>
            <Form-item label="试样编号:" class="search-item">
              <Input v-model="formObj.sampleCode" @on-enter="_formSearch" name="sampleCode" placeholder="请输入样品编号" clearable></Input>
              <input name="contractId" type="hidden">
            </Form-item>
            <Form-item label="试验科室:" class="search-item">
              <el-select v-model="formObj.groupId" @change="_selGroup" placeholder="请选择" clearable style="width: 100%">
                <el-option
                        v-for="(item,index) in groupoptions"
                        :key="index"
                        :label="item.name"
                        :value="item.id"
                />
              </el-select>
            </Form-item>
            <Form-item class="search-btn">
              <Button @click="_formSearch" type="primary">搜索</Button>
            </Form-item>
          </Form>
        </Col>
        <Col span="24">
          <btn-list :msg="btn" :open="searchOpen" :show-search-btn="true" @on-result-change="_btnClick" class="contHide">
            <!--<template slot="processTask">-->
            <!--<div class="fr process-task">-->
            <!--<Button @click="_exportReceiveRecord">导出交接记录</Button>-->
            <!--</div>-->
            <!--</template>-->
          </btn-list>
        </Col>
        <Col span="24">
          <PTVXETable
            ref="pageTable"
            :table-height="tableHeight"
            :get-page="getPage"
            :icon-msg="iconMsg"
            @on-result-change="_tableResultChange"
            select-data>
            <vxe-table-column
              v-for="item in pageColumns"
              :key="item.key"
              :field="item.key"
              :title="item.title"
              :width="item.width?item.width:200"
              :fixed="item.fixed?item.fixed:undefined"
              sortable
            >
              <template slot-scope="scope">
                <div v-if="item.detail"><a @click.stop="_detailModal(scope.row.id)">{{scope.row[item.key]}}</a></div>
                <div v-else-if="item.key ==='shape'">{{scope.row['shape']}} {{scope.row['shapeRemark']}}</div>
                <div v-else-if="item.date">
                  {{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):''}}
                </div>
                <div v-else>{{scope.row[item.key]}}</div>
              </template>
            </vxe-table-column>
          </PTVXETable>
        </Col>
      </Row>
    </div>
    <!--选择领样人-->
    </Modal>
    <ItemManage ref="ItemManage"></ItemManage>
  </div>
</template>
<script>
import { soilAptitude, soilEntrust, soilSample } from '../../../api'
import ItemManage from './ItemManage'
export default {
  components: { ItemManage },
  data() {
    return {
      btn: [{ type: 'primary', id: 'take-sample-get', name: '领样' }],
      selectIds: [],
      getPage: {},
      groupoptions: [],
      pageColumns: [
        { title: '试样编号', key: 'sampleCode', width: 100, fixed: 'left' },
        { title: '试样深度', key: 'sampleDepth', width: 95 },
        { title: '检测科室', key: 'groupName', width: 95 },
        { title: '土质描述', key: 'describeDetail', width: 285 },
        { title: '样品描述', key: 'sampleDescribe', width: 130 },
        { title: '样品包装类型', key: 'samplePack', width: 120 },
        { title: '样品制备人', key: 'preparer', width: 120 },
        { title: '样品制备时间', key: 'prepareDate', width: 135, date: true }
      ],
      iconMsg: [{ type: 'ios-list', id: 'take-sample-item', name: '试验项目' }],
      type: '',
      contractId: '', // 合同id
      selectData: [],
      currentComponent: '',
      showSampleModal: false,
      formObj: {},
      searchOpen: true,
      acceptSelectUserValue: ''
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
      } else {
      }
      return this.$tableHeight('tableModal')
    }
  },
  mounted() {},
  methods: {
    _selectSampleLink(data) {
      console.log(data)
    },
    _getGroupList: async function() {
      const result = await soilAptitude.getGroupList()
      if (result) {
        this.groupoptions = result
      }
    },
    _selGroup(data) {
      console.log(data)
      this.formObj.groupId = data
    },
    // 组件返回值
    _componentResult(data, msg) {
      switch (this.currentComponent) {
        case 'SelectPrinter':
          this._printerResult(data)
          break
        case 'ReceiveScan':
          // 扫码接收
          this._searchTop()
          break
        default:
          this._search()
      }
    },
    _open(contractId, type) {
      this.showSampleModal = true
      this.formObj = this.$resetFields(this.formObj)
      $('input[name=contractId]').val(contractId)
      this.entrustId = contractId
      this.type = type
      this.selectIds = []
      this.$nextTick(() => {
        this._page()
      })
      this._getGroupList()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      console.log('this.formObj', this.formObj)
      this.formObj.entrustId = this.entrustId
      const result = await soilEntrust.pageTake(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _iconClick(res, data, currentComponent) {
      this.currentComponent = currentComponent
      this.$nextTick(() => {
        switch (res) {
          case '编辑':
            this._editModal(true, data)
            break
          case '试验项目':
            this._itemManage(data.sampleId, data.groupId)
            break
          case '复制':
            this._copySample(data)
            break
          case '操作日志':
            this.$refs.refModal._open(data.id)
            break
          case '删除':
            this._deleteById(data.id)
            break
          case '附件':
            this.$refs.refModal._open(data.id, 'sampleId')
            break
        }
      })
    },
    _itemManage(sampleId, groupId) {
      // 管理检测项目
      this.$refs.ItemManage._open(sampleId, groupId)
    },
    _btnClick(msg, currentComponent) {
      this.currentComponent = currentComponent
      this.$nextTick(() => {
        switch (msg) {
          case '添加':
            this._editModal(false)
            break
          case '接收并发放':
            this._submitSend()
            break
          case '扫码接收':
            this.$refs.refModal._open()
            break
          case '扫码接收并发放':
            if (this.acceptSelectUserValue === '是') {
              // 扫码接收并发放根据字典是否选人
              this.$refs.userModal._open('sampleMan', '请选择领样人')
            } else {
              this.$refs.refModal._open('扫码接收发放')
            }
            break
          case '导入检测项目':
            this._importItem()
            break
          case '导入检测项目包':
            this._importItemPackage()
            break
          case '复制历史样品检测项目':
            this._copyHisItem()
            break
          case '收样室收样':
            this._sampleReceive(this.selectIds)
            break
          case '领样':
            this._sampleTake()
            break
          case '打印标签':
            this._selectPrinter('print-label')
            break
          case '自定义打印':
            if (this.selectData.length === 0) {
              this.$Message.warning('请至少选择一条数据')
            } else {
              this._cusTomePrint()
            }
            break
          case 'search':
            this.searchOpen = !this.searchOpen
            break
        }
      })
    },
    _sampleTake() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确认领样',
          onOk: () => {
            this._confirmTake(this.selectIds.join(','))
          }
        })
      }
    },
    _confirmTake: async function(ids) {
      const result = await soilSample.takeSample({ ids: ids })
      if (result) {
        this._resultChange('领样成功')
      }
    },
    _resultChange(msg) {
      this._page()
      this.$Message.success(msg)
      this.selectIds = []
    },
    _deleteById(id) {
      // 删除一条记录
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该记录？',
        onOk: () => {}
      })
    },
    _search() {
      this._page()
    },
    // 刷新上级页面
    _searchTop() {
      this._page()
      this.$emit('on-result-change')
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'selectData':
          this.selectData = data
          this.selectIds = []
          for (let i = 0; i < data.length; i++) {
            this.selectIds.push(data[i].id)
          }
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData, data.componentName)
          break
        case 'changeSize':
          this._page()
          break
      }
    },

    // 样品接收
    _sampleReceive(id) {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        const ids = id.join(',')
        this.$refs.locationModal._open(ids)
      }
    },

    // 导入检测项目包
    _importItemPackage() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$nextTick(() => {
          this.$refs.refModal._open(this.selectIds)
        })
      }
    },
    // 导入检测项目
    _importItem() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$nextTick(() => {
          this.$refs.refModal._open(this.selectIds, this.selectData[0])
        })
      }
    },
    _copyHisItem() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$nextTick(() => {
          this.$refs.refModal._open(this.selectIds)
        })
      }
    },
    // 选择打印机
    _selectPrinter(msg) {
      if (this.selectData.length === 0) {
        this.$Message.warning('请至少选择一条数据')
      } else {
        this.$refs.refModal._open(msg)
      }
    },
    // 选择完打印机之后
    _printerResult(data) {
      // printModal  不能使用动态组件，否则第二次打不开，原因未知
      switch (data.type) {
        // 打印标签--直接打印
        case 'print-label':
          this.$refs.printModal._printOpen(this.selectData, data.printerVal)
          break
      }
    },
    _visibleChange(data) {
      this.$emit('on-result-change')
    }
  }
}
</script>
