<!-- @desc:打印pdf选择模板与打印机  @auth:洪旭东  @time:2023-04-10 13:02 -->
<template>
  <div>
    <DkModal
      v-model="showChooseTemplate"
      :title="$t('W_136')"
      @modalOk="printBtn"
      @modalCancel="showChooseTemplate = false"
      @on-cancel="showChooseTemplate = false"
    >
      <DkForm :col-count="1">
        <DkFormItem :label="$t('printerId')" required>
          <SelectPop v-model="currentPrinter" :multiple="false"
                     :options="printerList"
                     labelKey="printerName"
                     valueKey="printerCode">
          </SelectPop>
        </DkFormItem>
        <DkFormItem :label="$t('printLayoutId')" required>
          <SelectPop v-model="currentTemplate" :multiple="false"
                     :options="templateList"
                     labelKey="layoutName"
                     valueKey="layoutId">
          </SelectPop>
        </DkFormItem>
      </DkForm>
    </DkModal>
  </div>
</template>

<script>

  import { hiprint } from 'vue-plugin-hiprint'

  export default {
    name: 'print_modal',
    components: {},
    props: {
      orderType: {
        type: String,
        default: ''
      },
      labelKind: {
        type: String
      },
      service: {
        type: Object
      },
      id: {
        type: Number
      },
    },
    data() {
      return {
        printerList: [],
        templateList: [],
        currentTemplate: null,
        currentPrinter: null,
        allAlign: null,
        showChooseTemplate: false,
      }
    },
    methods: {
      printBtn() {
        if (!this.currentTemplate) {
          this.$Message.warning('请先选择一个模板再打印')
          return
        }
        if (!this.currentPrinter) {
          this.$Message.warning('请先选择一个打印机再打印')
          return
        }
        if (!this.id) {
          this.$Message.warning('请先选择一条数据再打印')
          return
        }


        // console.log('currentTemplate', this.currentTemplate)
        // console.log('currentPrinter', this.currentPrinter)
        let template = this.templateList.find(it => it.layoutId == this.currentTemplate)?.layoutData
        // console.log('template', template)


        // 打印机code，保存到缓存
        let data = localStorage.getItem('currentPrinter')
        if (data) {
          data = JSON.parse(localStorage.getItem('currentPrinter'))
        } else {
          data = {}
        }
        data[this.$store.state.user.ftyId] = this.currentPrinter
        localStorage.setItem('currentPrinter', JSON.stringify(data))
        //获取打印数据
        this.excuteNoParams(this.$service.printDataService, this.$service.printDataService.getPrintData, [this.id]).then(res => {
          if (res.code === 200 && res.data) {
            let printData = res.data

            this.$printUtil.print(template,printData,this.currentPrinter)

          } else {
            this.$Message.warning(res.message)
          }
        })
      },
      getTemplateList() {
        this.excute(this.$service.labelPrintLayoutService, this.$service.labelPrintLayoutService.selectByCond, {
          flgValid: true,
          labelKind: this.labelKind,
          ftyId: this.$store.state.user.ftyId
        }).then(res => {
          if (res.code === 200) {
            this.templateList = res.data.list
          } else {
            this.$Message.warning(res.message)
          }
        })
      },
      /**
       * @desc   : 查询打印机
       * @author : 洪旭东
       * @date   : 2023-04-10 13:04
       */
      getPrinter() {
        this.excute(this.$service.labelPrinterService, this.$service.labelPrinterService.selectByCond, { flgValid: true,ftyId: this.$store.state.user.ftyId
      }).then(res => {
          if (res.code === this.$config.SUCCESS_CODE) {
            this.printerList = res.data.list
            //从缓存中取出打印机code
            if (localStorage.getItem('currentPrinter')) {
              let localPrinterCode = JSON.parse(localStorage.getItem('currentPrinter'))[this.$store.state.user.ftyId]
              let list = this.printerList.filter(it => it.printerCode == localPrinterCode)
              if (localPrinterCode && list.length > 0) {
                this.currentPrinter = localPrinterCode
              }
            }
          } else {
            this.$Message.warning(res.message)
          }
        })
      }
    },
    created() {
      this.getTemplateList()
      this.getPrinter()
    },

  }
</script>

<style scoped>

</style>
