<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="90" class="zIndex-1000 full-screen">
      <p slot="header">制备详情</p>
      <Row>
        <!--操作-->
        <Col span="24">
          <btn-list :msg="btn" class="contHide" @on-result-change="_btnClick"></btn-list>
        </Col>
        <!--内容-->
        <Col span="24">
          <PTVXETable
            ref="pageTable"
            :table-height="tableHeight"
            :get-page="getPage"
            :form-id="formId"
            select-data
            hide-page
            @on-result-change="_tableResultChange">
            <vxe-table-column
              v-for="item in pageColumns"
              :key="item.key"
              :field="item.key"
              :title="item.title"
              :min-width="item.width?item.width:200"
              :fixed="item.fixed?item.fixed:undefined"
              :edit-render="item.editCell?{autofocus: 'input'}:null"
              sortable
            >
              <!--只读模板-->
              <template v-slot="scope">
                <div v-if="item.key==='prepareDate'">
                  {{scope.row.prepareDate?$dateformat(scope.row.preparationDate,'yyyy-mm-dd HH:MM'):''}}
                </div>
                <span v-else-if="item.key==='type'">{{ scope.row[item.key]===0? '送检样':'余样' }}</span>
                <span v-else-if="item.key==='status'">{{ scope.row[item.key].display }}</span>
                <div v-else>{{scope.row[item.key]}}</div>
              </template>
            </vxe-table-column>
          </PTVXETable>
        </Col>
      </Row>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult"></modal-footer>
      </div>
      <!--打印标签-->
      <!--选择人员-->
    </Modal>
  </div>
</template>

<script>
import { soilEntrust } from '../../../api'

export default {
  components: {},
  data() {
    return {
      formId: 'preparationModal',
      showModal: false,
      wayData: new Map(),
      formObj: {
        sampleId: undefined
      },
      tableHeight: document.documentElement.clientHeight - 215,
      footerList: [],
      list: [
        { name: '取消', type: '', id: '' },
        { name: '关闭', type: 'primary', id: '' }
      ],
      btn: [{ type: 'error', id: 'ZBC', name: '删除' }],
      iconMsg: [
        { type: 'md-create', id: '', name: '批量赋值' },
        { type: 'md-trash', id: '', name: '删除' }
      ],
      getPage: {
        records: []
      },
      selectData: [],
      selectIds: [],
      pageColumns: [
        { title: '试样编号', key: 'sampleCode', width: 130 },
        { title: '试样深度', key: 'sampleDepth', width: 130 },
        { title: '检验科室', key: 'groupName', width: 130 },
        { title: '制备类型', key: 'type', width: 130 },
        {
          title: '制备方式',
          key: 'prepareWay',
          width: 130
        },
        {
          title: '制备数量',
          key: 'quantity',
          width: 150
        },
        { title: '单位', key: 'unit', width: 110 },
        { title: '状态', key: 'status', width: 150 },
        { title: '保存方式', key: 'keepWay', width: 150 },
        { title: '保存容器', key: 'keepContainer', width: 150 },
        {
          title: '制备时间',
          key: 'prepareDate',
          width: 200
        },
        {
          title: '制备人',
          key: 'preparer',
          width: 130
        },
        { title: '备注', key: 'remark', width: 150 }
      ],
      currentRow: {},
      preparationWayData: [],
      resultStateWayData: [],
      saveWayData: [],
      sampleId: '',

      unitData: [],
      locationList: [],
      packList: [],
      isAccurate: ''
    }
  },
  methods: {
    // 获取存储位置
    _locationChange(msg, data, handleObj) {
      this.currentRow = handleObj
      switch (msg) {
        case 'select':
          this.$set(this.currentRow, 'storePlace', data)
          break
        case 'query':
          this.$set(this.currentRow, 'storePlace', data)
          this._getLocation(data)
          break
      }
    },
    // 单位
    _unitChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      switch (msg) {
        case 'select':
          this.currentRow.unit = data
          break
        case 'query':
          this.currentRow.unit = data
          break
      }
    },
    _preparationChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      this.currentRow.prepareWay = data
    },
    _testItemChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      this.currentRow.resultState = data.name
    },
    _saveChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      this.currentRow.keepWay = data.name
    },
    timeChange(data) {
      console.log(data)
      this.currentRow.prepareDate = data
    },
    _packSaveChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      this.currentRow.keepContainer = data.name
    },
    _open(sampleId) {
      this.sampleIds = sampleId
      this.formObj.sampleId = sampleId
      console.log(this.sampleIds)
      this.showModal = true
      this.getPage.records = []
      this.unitData = []
      this.footerList = []
      for (let i = 0; i < this.list.length; i++) {
        if (this.$showBtn(this.list[i].id)) {
          this.footerList.push(this.list[i])
        }
      }
      this._page()
      // this._dicSearch()
    },
    _page: async function() {
      const result = await soilEntrust.pagePrepareDetailHis(this.formObj)
      if (result) {
        console.log(result)
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '关闭':
          this._cancel()
          break
      }
    },
    _cancel() {
      this.showModal = false
      this._hideLoading()
    },
    _resultChange(msg) {
      this.showModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this._hideLoading()
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this.$refs.pageTable._hideLoading()
          break
        case 'selectData':
          this.selectData = data
          const selectIds = []
          for (let i = 0; i < data.length; i++) {
            selectIds.push(data[i].id)
          }
          this.selectIds = selectIds
          break
        case 'iconClick':
          this._iconClick(
            data.name,
            data.rowData,
            data.componentName,
            data.rowIndex
          )
          break
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '打印标签':
          if (this.selectData.length > 0) {
            this.$refs.printer._open()
          } else {
            this.$Message.warning('请至少选择一条数据')
          }
          break
        case '删除':
          this._deleteSelected()
          break
      }
    },
    _printerResult(data) {
      this.$refs.printNumber._open(this.selectData, data.printerVal)
    },
    _deleteSelected() {
      // 批量删除
      const selectData = this.selectData
      if (selectData.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this._deleteByIds(
          selectData,
          '确定删除 ' + selectData.length + ' 条记录？'
        )
      }
    },
    _iconClick(res, data, componentName, rowIndex) {
      this.currentRow = data
      switch (res) {
        case '批量赋值':
          this._copyPlan(data)
          break
        case '删除':
          this._deleteByIds([data])
          break
      }
    },
    _deleteByIds(rows, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          for (let i = 0; i < rows.length; i++) {
            const index = this.getPage.records.indexOf(rows[i])
            this.getPage.records.splice(index, 1)
          }
          this.$Message.success('操作成功')
          this.selectData = []
          this.selectIds = []
        }
      })
    },
    _selectStaff() {
      this.$refs.userModal._open('preparationPerson')
    },
    _userData(data) {
      // 选择人员返回数据
      this.currentRow.preparationPerson = data.realname
      this.currentRow.preparationPersonId = data.userId
    },
    // 操作的序号
    _handleRow(data) {
      this.currentRow = data.row
    },
    _handleTimeRow(data) {
      console.log(this.currentRow)
      this.currentRow = data.row
    }
  }
}
</script>
