<template>
  <div>
    <Modal v-model="prDetailModal" width="100" :mask-closable="false" class="zIndex-1000 full-screen" :styles="{marginTop:'-80px'}">
      <p slot="header">
        制备详情
      </p>
      <Row>
        <Form id="preparation-detail-form" onsubmit="return false" class="search-form" inline :label-width="70">
          <label class="label-sign" />
          <!--          <Form-item class="search-item" label="样本编号:">-->
          <!--            <Input name="sampleNum" placeholder="请输入样本编号" clearable @on-enter="_formSearch" />-->
          <!--          </Form-item>-->
          <!--          <Form-item class="search-item" label="物料名称:">-->
          <!--            <Input name="name" placeholder="请输入物料名称" clearable @on-enter="_formSearch" />-->
          <!--          </Form-item>-->
          <!--          <Form-item class="search-btn">-->
          <!--            <Button type="primary" @click="_formSearch">-->
          <!--              搜索-->
          <!--            </Button>-->
          <!--          </Form-item>-->
        </Form>
        <Button type="primary" style="margin-bottom: 5px" @click="_printLabel">
          打印标签
        </Button>
        <Col />
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          :table-height="tableHeight"
          :get-page="getPage"
          show-check-box
          select-data
          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:ss'):'' }}
              </span>
              <span v-else>{{ scope.row[item.key] }}</span>
            </template>
          </el-table-column>
        </element-table>
        </Col>
      </Row>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
    <!--下载插件-->
    <downloadPlugin ref="downloadPlugin" />
  </div>
</template>

<script>
import { drugSampleKeep } from '../../api'
import { getLodop } from '../../plugins/clodop/LodopFuncs'
import downloadPlugin from './download/downloadPlugin'
let LODOP
export default {
  name: 'Preparation',
  components: { downloadPlugin },
  data() {
    return {
      prDetailModal: false,
      btn: [{ id: '', type: 'primary', name: '打印标签' }],
      pageColumns: [
        {
          title: '样本编号',
          key: 'sampleCode',
          width: 200,
          fixed: 'left',
          align: 'center'
        },
        { title: '物品名称', key: 'name', width: 120, align: 'center' },
        { title: '物品编号', key: 'code', width: 120, align: 'center' },
        {
          title: '制备类型',
          key: 'type',
          width: 120,
          align: 'center'
        },
        {
          title: '取样数量',
          key: 'quantity',
          width: 200,
          align: 'center'
        },
        {
          title: '单位',
          key: 'unit',
          width: 200,
          align: 'center'
        },
        {
          title: '储存期限(天)',
          key: 'timeLimit',
          width: 120,
          align: 'center'
        },
        {
          title: '存储位置',
          key: 'position',
          width: 120,
          align: 'center'
        },
        {
          title: '制备时间',
          key: 'keepTime',
          width: 160,
          align: 'center',
          datetime: true
        },
        { title: '制样人', key: 'keeper', width: 160, align: 'center' },
        { title: '备注', key: 'remark', align: 'center' }
      ],
      footerList: [{ id: '', name: '关闭', type: '' }],
      getPage: {
        records: []
      },
      searchOpen: false,
      selectData: [],
      selectIds: [],
      sampleId: '',
      inspectionDate: '',
      declarationName: ''
    }
  },
  computed: {
    tableHeight() {
      return this.$tableHeight('search')
    }
  },
  methods: {
    _open(ids, declarationName, inspectionDate) {
      this._clearPage()
      this.prDetailModal = true
      this.inspectionDate = inspectionDate
      this.declarationName = declarationName
      this._hideLoading()
      this.getPage.records = []
      this.sampleId = ids
      this._page()
    },
    _clearPage() {
      $('preparation-detail-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
    },
    async _page() {
      const data = this.$serialize('preparation-detail-form')
      if (this.sampleId) {
        data.sampleId = this.sampleId
      }
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await drugSampleKeep.pageSampleMakeDetail(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this._selectData(data)
          break
        default:
          this._page()
      }
    },
    _selectData(data) {
      this.selectData = data
      const selectIds = []
      console.log(data)
      for (let i = 0; i < data.length; i++) {
        selectIds.push(data[i].id)
      }
      this.selectIds = selectIds
    },
    _footerResult(msg) {
      switch (msg) {
        case '关闭':
          this._cancel()
          break
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _btnClick(msg, data) {
      const ids = this.selectIds
      switch (msg) {
        case '打印标签':
          if (ids.length !== 0) {
            this.$Modal.confirm({
              title: '提示',
              content: `确定打印 ${ids.length} 条信息?`,
              onOk: () => {
                this._printMessage()
              }
            })
          } else {
            this.$Message.warning('请至少选择一条数据')
          }
          break
      }
    },
    _printLabel() {
      if (this.selectData.length > 0) {
        this.$Modal.confirm({
          title: '提示',
          content: `确定打印 ${this.selectData.length} 条信息吗?`,
          onOk: () => {
            this._printMessage()
          }
        })
      } else {
        this.$Message.warning('请至少选择一条数据')
      }
    },

    _printMessage: function() {
      LODOP = getLodop()
      if (LODOP === 'undefined' || LODOP === undefined) {
        this._pluginDownload()
        return false
      }
      if (LODOP.GET_PRINTER_COUNT() === 0) {
        this.$Messager.warning('系统未关联打印机,请确认....')
        return false
      }
      const data = this.selectData
      for (let i = 0; i < data.length; i++) {
        LODOP.PRINT_INITA(
          '0mm',
          '0mm',
          '60.01mm',
          '40.01mm',
          '打印控件功能演示_Lodop功能_打印图片3'
        )
        if (data[i].type === '留样') {
          LODOP.ADD_PRINT_BARCODE(
            '0.88mm',
            '5.16mm',
            '18.65mm',
            '19.71mm',
            'QRCode',
            `${data[i].sampleCode}`
          )
          LODOP.ADD_PRINT_TEXT(
            107,
            4,
            180,
            20,
            '样品编号：' + data[i].sampleCode
          )
          LODOP.ADD_PRINT_TEXT(130, 3, 180, 20, '物品名称：' + data[i].name)
          LODOP.ADD_PRINT_TEXT(
            85,
            4,
            180,
            20,
            '制备时间：' +
              this.$dateformat(data[i].keepTime, 'yyyy-mm-dd HH:MM')
          )
          LODOP.ADD_PRINT_TEXT(63, 3, 180, 20, '保存期限：' + data[i].timeLimit)
          LODOP.ADD_PRINT_TEXT(20, 106, 75, 31, '留样')
          LODOP.SET_PRINT_STYLEA(0, 'FontSize', 16)
        } else if (data[i].type === '送检样') {
          LODOP.ADD_PRINT_BARCODE(
            '0.87mm',
            '5.16mm',
            '18.65mm',
            '19.71mm',
            'QRCode',
            'D2020030006'
          )
          LODOP.ADD_PRINT_TEXT(
            94,
            4,
            180,
            20,
            '样品编号：' + data[i].sampleCode
          )
          LODOP.ADD_PRINT_TEXT(68, 4, 180, 20, '物品名称：' + data[i].name)
          LODOP.ADD_PRINT_TEXT(
            120,
            3,
            180,
            20,
            '制备时间：' +
              this.$dateformat(data[i].keepTime, 'yyyy-mm-dd HH:MM')
          )
          LODOP.ADD_PRINT_TEXT(20, 106, 75, 31, '检样')
          LODOP.SET_PRINT_STYLEA(0, 'FontSize', 16)
        }

        // LODOP.ADD_PRINT_TEXT(100, 112, 217, 19, data[i].name)
        // LODOP.SET_PRINT_STYLEA(0, 'FontSize', 12)
        // LODOP.SET_PRINT_STYLEA(0, 'Angle', 90)
        // LODOP.ADD_PRINT_BARCODE(
        //   '0.66mm',
        //   '5.16mm',
        //   '18.65mm',
        //   '19.71mm',
        //   'QRCode',
        //   `${data[i].sampleCode}`
        // )
        // LODOP.SET_PRINT_STYLEA(0, 'ShowBarText', 0)
        // LODOP.SET_PRINT_STYLEA(0, 'Vorient', 2)
        // LODOP.SET_PRINT_STYLEA(0, 'Angle', 90)
        // LODOP.ADD_PRINT_TEXT(100, 138, 217, 19, data[i].sampleCode)
        // LODOP.SET_PRINT_STYLEA(0, 'FontSize', 12)
        // LODOP.SET_PRINT_STYLEA(0, 'Angle', 90)
        // LODOP.ADD_PRINT_TEXT(
        //   100,
        //   163,
        //   210,
        //   19,
        //   this.$dateformat(data[i].pcTime, 'yyyy-mm-dd HH:MM')
        // )
        // LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10)
        // LODOP.SET_PRINT_STYLEA(0, 'Angle', 90)
        // LODOP.ADD_PRINT_TEXT(100, 186, 200, 19, data[i].pcDepart)
        // LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10)

        // LODOP.SET_PRINT_STYLEA(0, 'Angle', 90)
        // LODOP.SET_PRINT_COPIES(data[i].printNum)
        LODOP.PRINT_DESIGN()
        // LODOP.PRINT_DESIGN()
      }
    },
    _cancel() {
      this.prDetailModal = false
    },
    // 下载插件
    _pluginDownload() {
      this.$refs.downloadPlugin._open()
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    }
  }
}
</script>

<style scoped>
</style>
