<!-- 质检记录 -->
<!--页面中的打印未改成vue-easy-print-->
<template>
  <div class="container">
    <my-table
      ret="table"
      :data="data"
      :columns="columns"
      :table-filter="tableFilter"
      :table-page="params"
      selectKey="id"
      @on-select="select"
      @event-handle="eventHandle"
      @on-change="changePage">
    </my-table>
    <quality-form v-model="show" :data="formData"></quality-form>
    <!-- 打印二维码标签内容 -->
    <div class="print-wrap">
      <div ref="print">
        <div
          v-for="(item, index) in printList"
          :key="index"
          class="print"
          style="box-sizing: border-box;height: calc(100% - 20px);width: calc(100% - 20px);
          margin: 10px;border: 1px solid #000;page-break-after: always;
          ">
          <div class="top"
               style="height: 200px;display: flex;align-items: center;
               border-bottom: 1px solid #000;box-sizing: border-box;overflow: hidden;"
          >
            <div class="left" style="padding: 10px;display: flex;align-items: center;justify-content: center">
              <vue-qr :text="item.qrCode" :size="120" :margin="0"></vue-qr>
            </div>
            <div class="right"
                 style="height: 100%;
                 padding: 10px;
                 flex: 2;
                 display: flex;
                 justify-content: center;
                 align-items: center;
                 font-size: 12px;
                 border-left: 1px solid #000;
              ">
              RFID标签粘贴区
              <!--              <div class="row" style="font-size: 18px;font-weight: bold;">物料编码：{{ item.materialCode }}</div>-->
              <!--              <div class="row" style="font-size: 18px;font-weight: bold;">物料名称：{{ item.materialName }}</div>-->
              <!--              <div class="row" style="font-size: 18px;font-weight: bold;">标签编码：{{ item.rfidCode }}</div>-->
              <!--              <div class="row" style="font-size: 18px;font-weight: bold;">门幅：{{ item.width  }}</div>-->
              <!--              <div class="row" style="font-size: 18px;font-weight: bold;">克重：{{ item.unitWeight }}</div>-->
              <!--              <div class="row" style="font-size: 18px;font-weight: bold;">长度：{{ item.quantity }}米</div>-->
            </div>
          </div>
          <div class="bottom"
               style="
                  height: calc(100% - 200px);
                  "
          >
            <div style="border-bottom: 1px solid #000;
              padding: 0 10px;width: calc(100% - 20px);
              height: 30%;word-break:break-all;
              display: flex;flex-direction: column;
              align-items: flex-start;
              justify-content: space-around;
              "
            >
              <!--              <div class="row" style="font-size: 18px;font-weight: bold;">合同编号：{{ data.purchaseBillNo }}</div>-->
              <div class="row" style="font-size: 18px;font-weight: bold;">标签编码：{{ item.rfidCode }}</div>
              <div class="row" style="font-size: 18px;font-weight: bold;">创建日期：{{ item.createTime }}</div>
            </div>
            <div style="border-bottom: 1px solid #000;padding: 0 10px;width: calc(100% - 20px);height: 30%;display: flex;align-items: center;justify-content: center;font-size: 24px;font-weight: bold;text-align: center;">
              {{ item.materialName }}
            </div>
            <div style="border-bottom: 1px solid #000;padding: 0 10px;width: calc(100% - 20px);height: 20%;display: flex;align-items: center;justify-content: center;font-size: 24px;font-weight: bold;text-align: center;">
              数量：{{ item.quantity }}
            </div>
            <div style="width: 100%;
              height: 20%;
              display: flex;justify-content: space-between;align-items: center;;
            ">
              <div style="font-size: 24px;font-weight: bold;text-align: center;flex: 1;">门幅：{{ item.width }}</div>
              <div style="font-size: 24px;font-weight: bold;text-align: center;flex: 1;">克重：{{ item.unitWeight }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { exportExcel } from '@/libs/util'
import MyTable from '_c/my-table/my-table'
import QualityForm from './quality-form'
import { GetSupplierQRCodeTest, PrinterSupplierQRCode } from '@/api/supplierShip'
import VueQr from 'vue-qr'
export default {
  name: 'quality_record',
  components: {
    MyTable,
    QualityForm,
    VueQr
  },
  data () {
    return {
      printList: [],
      formData: {},
      show: false,
      data: [],
      columns: [
        {
          type: 'selection',
          width: 60
        },
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150,
          sortable: true
        },
        {
          title: '标签编码',
          key: 'rfidCode',
          minWidth: 180,
          tooltip: true
        },
        {
          title: '数量',
          key: 'quantity',
          minWidth: 100
        },
        {
          title: '二维码编码',
          key: 'qrCode',
          minWidth: 150
        },
        {
          title: '状态',
          key: 'width',
          minWidth: 150,
          render: (h, params) => {
            return h('span', params.row.hadPrinter ? '已打印' : '未打印')
          }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          width: '100px',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'success',
                    size: 'small'
                  },
                  style: {
                    marginRight: '.5vw'
                  },
                  on: {
                    click: (e) => {
                      e.stopPropagation()
                      this.printList = []
                      this.printList = [ { ...params.row } ]
                      if (!params.row.hadPrinter) {
                        this
                          .$post('/api/storage-restructure/waterTag/printer', [params.row.id]).then(res => {
                            if (res.code === 200) {
                              this.$Message.info('打印成功！')
                              this.getList()
                            }
                          })
                      }
                      if (params.row.qrCode) {
                        setTimeout(() => {
                          this.$print(this.$refs.print)
                        }, 100)
                      } else {
                        this.$Message.error('二维码为空！')
                      }
                    }
                  }
                },
                '打印'
              )
            ])
          }
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'keyword',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '导出Excel',
          e: 'exportBq'
        },
        {
          name: 'RadioGroup',
          value: 'hadPrinter',
          list: [
            { label: '未打印', value: 0 },
            { label: '已打印', value: 1 }
          ]
        },
        {
          name: 'Button',
          type: 'success',
          text: '批量打印',
          align: 'right',
          e: 'print'
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    print () {
      if (this.printList.length < 1) {
        return
      }
      this
        .$post('/api/storage-restructure/waterTag/printer', this.printList.map(item => item.id)).then(res => {
          if (res.code === 200) {
            this.$Message.info('打印成功！')
          this.getList()
          }
        })
      setTimeout(() => {
        this.$print(this.$refs.print)
        this.$refs.table.clearTempList()
        this.printList = []
      }, 100)
    },
    select (data) {
      this.printList = data
    },
    cfm (type, formData) {
      if (type === 'add') {
        FacPriceAdd({
          ...formData,
          materialCode: formData.code
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      } else if (type === 'edit') {
        FacPriceChange({
          ...formData,
          factoryPriceId: formData.id
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.keyWord = option.key
          this.params.hadPrinter = !!option.hadPrinter
          this.getList()
          break
        case 'export':
          GetSupplierQRCodeTest({
            ...this.params,
            pageIndex: 1,
            pageSize: 99999
          }).then(res => {
            if (res.code === 0) {
              let data = res.result.list
              if (data.length < 1) return this.$Message.error('数据为空！')
              exportExcel(this.columns, data, '质检记录')
            }
          })
          break
        case 'print':
          this.print()
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      this
        .$post('/api/storage-restructure/waterTag/page', {
          ...this.params,
        }).then(res => {
          if (res.code === 200) {
            this.data = res.data.records
            this.params.total = res.data.total
          }
        })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
.print-wrap {
  width: 100vw;
  height: 100vh;
  display: none;
  img {
    width: 50%;
  }
}
</style>
