<template>
  <div>
      <Row>
        <!--操作-->
        <!--内容-->
        <Col span="24">
          <element-table
            ref="pageTable"
            :page-columns="pageColumns"
            :table-height="tableHeight"
            :get-page="getPage"
            show-check-box
            hide-page
            @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"
              :align="item.align"
              :width="item.width"
              :min-width="200"
              :fixed="item.fixed?item.fixed:undefined"
            >
              <template slot-scope="scope">
              <span v-if="item.datetime">
                {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM'):'' }}
              </span>
                <span v-else-if="item.key==='content'" class="blue-color">
                {{ scope.row[item.key]?scope.row[item.key]:'' }}
              </span>
                <span v-else>
                {{ scope.row[item.key] }}
              </span>
              </template>
            </el-table-column>
          </element-table>
        </Col>
      </Row>
  </div>
</template>

<script>
export default {
  layout: 'blank-layout',
  components: {},
  data() {
    return {
      formId: '',
      showModal: false,
      wayData: new Map(),
      formObj: {},
      footerList: [],
      list: [
        { name: '取消', type: '', id: '' },
        { name: '制备完成', type: 'primary', id: '' }
      ],
      iconMsg: [
        // { type: 'md-create', id: '', name: '批量赋值' },
        // { type: 'md-trash', id: '', name: '删除' }
      ],
      getPage: {
        records: []
      },
      labelCode: {},
      currentIndex: -1,
      selectData: [],
      selectIds: [],
      pageColumns: [
        { title: '试样编号', key: 'sampleCode', width: 130 },
        { title: '试样深度', key: 'sampleDepth', width: 130 },
        { title: '打印数量', key: 'printNum', width: 130, editCell: true },
        { title: '检验科室', key: 'groupName', width: 130 },
        // 暂时屏蔽
        // {title: '打印份数', key: 'printNum', width: 110},
        { title: '制备类型', key: 'type', width: 130 },
        {
          title: '制备方式',
          key: 'prepareWay',
          width: 130,
          editCell: true
        },
        {
          title: '制备数量',
          key: 'quantity',
          width: 150,
          editCell: true
        },
        { title: '单位', key: 'unit', width: 110, editCell: true },
        { title: '状态', key: 'status', width: 150 },
        { title: '保存方式', key: 'keepWay', width: 150, editCell: true },
        { title: '保存容器', key: 'keepContainer', width: 150, editCell: true },
        {
          title: '制备时间',
          key: 'prepareDate',
          width: 200,
          date: true,
          editCell: true
        },
        {
          title: '制备人',
          key: 'preparer',
          width: 130,
          editCell: true
        },
        { title: '备注', key: 'remark', width: 150, editCell: true }
      ],
      currentRow: {},
      preparationWayData: [],
      resultStateWayData: [],
      saveWayData: [],
      preparerList: [],
      sampleIds: '',

      unitData: [],
      locationList: [],
      packList: [],
      isAccurate: ''
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  mounted() {},
  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
        case 'focus':
          this._getLocation()
          break
      }
    },
    _getLocation(data) {
      const obj = {}
      if (data) {
        obj.storePlace = data
      }
      this.$store
        .dispatch('FoodSamplePreparation/getPositionList', obj)
        .then(() => {
          this.locationList = this.$store.state.FoodSamplePreparation.list
        })
    },
    // 单位
    _unitChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      switch (msg) {
        case 'select':
          this.currentRow.unit = data
          break
        case 'query':
          this.currentRow.unit = data
          break
      }
    },
    // 多个参数查询
    // _dicSearch: async function() {
    //   const data = ['制备方式', '保存方式', '保存容器']
    //   const result = await soilSample.getDictList(data)
    //   if (result) {
    //     this.keepWayList = result
    //   }
    // },
    backData(data) {
      this.$forceUpdate()
      console.log(data)
      console.log(this.selectData)
      for (let i = 0; i < this.selectData.length; i++) {
        Object.assign(this.selectData[i], data)
      }
      console.log('合并后', this.selectData)
    },
    _preparationChange(msg, data, handleObj) {
      console.log('data', data)
      this._handleRow(handleObj)
      this.currentRow.prepareWay = data
    },
    _testItemChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      this.currentRow.resultState = data.name
    },
    _saveChange(msg, data, handleObj) {
      console.log('data', data)
      this._handleRow(handleObj)
      this.currentRow.keepWay = data
    },
    _preparerChange(msg, data, handleObj) {
      this._handleRow(handleObj)
      this.currentRow.preparer = data
    },
    timeChange(data) {
      console.log(data)
      this.$forceUpdate()
      this.currentRow.prepareDate = data.prepareDate
      console.log(this.getPage.records)
    },
    _packSaveChange(msg, data, handleObj) {
      console.log('data', data)
      this._handleRow(handleObj)
      this.currentRow.keepContainer = data
    },
    _open(sampleIds) {
      this.sampleIds = sampleIds
      this.showModal = true
      this.getPage.records = []
      this.preparationWayData = []
      this.saveWayData = []
      this.packList = []
      this.unitData = []
      this.preparerList = []
      this.footerList = []
      for (let i = 0; i < this.list.length; i++) {
        if (this.$showBtn(this.list[i].id)) {
          this.footerList.push(this.list[i])
        }
      }
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '仅流转微生物':
          this._changeMic()
          break
        case '制备完成':
          this._ok()
          break
      }
    },
    _cancel() {
      this.showModal = false
      this._hideLoading()
    },
    _ok() {
      this.$refs.pageTable._showLoading()
      const prepareList = []
      for (let i = 0; i < this.getPage.records.length; i++) {
        prepareList.push(this.getPage.records[i])
      }
      if (prepareList.length === 0) {
        this.$Message.warning('制备数据不能为空')
        this.$refs.pageTable._hideLoading()
        this._hideLoading()
        return false
      }
      this.save({ prepareList: prepareList })
    },
    _resultChange(msg) {
      this.showModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this._hideLoading()
      this.$refs.pageTable._hideLoading()
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          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._goto()
            // this._getLabelInfo()
            // this._printMessage()
          } else {
            this.$Message.warning('请至少选择一条数据')
          }
          break
        case '批量填写制备信息':
          this._writeInfo()
          break
        case '删除':
          this._deleteSelected()
          break
      }
    },
    _goto(methodId) {
      const routeData = this.$router.resolve({
        path: '/blank/item_by_method',
        query: { id: 123456 }
      })
      window.open(routeData.href, '_blank')
    },
    // 下载插件
    _pluginDownload() {
      this.$refs.downloadPlugin._open()
    },
    // 时间戳转换方法    date:时间戳数字
    _selectStaff() {
      this.$refs.userModal._open('preparationPerson')
    },
    _userData(data) {
      // 选择人员返回数据
      this.currentRow.preparationPerson = data.realname
      this.currentRow.preparationPersonId = data.userId
    },

    // 批量赋值(这里用index判断，不用id是因为id有时候没有值，而且id不能随意赋值)
    _copyPlan(data) {
      const ids = this.selectData
      const records = this.getPage.records
      if (ids.length === 0) {
        this.$Message.warning('请勾选数据进行赋值！')
      } else {
        for (let i = 0; i < ids.length; i++) {
          for (let j = 0; j < records.length; j++) {
            if (ids[i].currentIndex === records[j].currentIndex) {
              this._singleCopy(j, data)
            }
          }
        }
      }
    },
    // 单独赋值信息
    _singleCopy(j, data) {
      const tempData = {
        preparationWay: data.preparationWay,
        resultQuantity: data.resultQuantity ? data.resultQuantity : 0,
        unit: data.unit,
        resultState: data.resultState ? data.resultState : '',
        saveWay: data.saveWay ? data.saveWay : '',
        pack: data.pack ? data.pack : '',
        storePlace: data.storePlace ? data.storePlace : '',
        printNum: data.printNum ? data.printNum : 1,
        remark: data.remark ? data.remark : ''
      }
      this.getPage.records[j].preparationWay = tempData.preparationWay
      this.getPage.records[j].resultQuantity = tempData.resultQuantity
      this.getPage.records[j].unit = tempData.unit
      this.getPage.records[j].resultState = tempData.resultState
      this.getPage.records[j].saveWay = tempData.saveWay
      this.getPage.records[j].pack = tempData.pack
      this.getPage.records[j].storePlace = tempData.storePlace
      this.getPage.records[j].printNum = tempData.printNum
      this.getPage.records[j].remark = tempData.remark
    },
    // 操作的序号
    _handleRow(data) {
      console.log(data)
      this.currentIndex = data.index
      this.currentRow = data.row
    },
    _handleTimeRow(data) {
      console.log(this.currentRow)
      this.currentRow = data.row
    }
  }
}
</script>
