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

<script>
import {formMixin} from '@/mixins/form'
import { hiprint } from 'vue-plugin-hiprint'

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

        let template = this.templateList.find(it => it.layoutId == this.currentTemplate)?.layoutData

        // let header=this.excuteNoParam(this.service, this.service.selectById,[this.id])
        let data =  this.excute(this.$service.printLayoutService,this.$service.printLayoutService.getPrintData,{dataId:this.id,objectCode:this.postName})
        // let detail=this.excuteNoParam(this.$service.orderService, this.$service.orderService.selectOrderInfoById, [this.id])
        Promise.all([data])
          .then((results) => {
            // 在这里执行接下来的操作
            if(results?.[0]?.code === this.$config.SUCCESS_CODE){
              let header =results?.[0]?.data
              let detail =results?.[0]?.data.itemList
              this.$set(header, 'table', detail)
              let printData = {...header}
              this.$printUtil.print(template,printData)
            }else {
              this.$Message.warning(this.$t('W_006'))
            }
          })
      },
      getTemplateList() {
        this.excute(this.$service.printLayoutService, this.$service.printLayoutService.selectByCond, {
          flgValid: true,
          docName: this.docName,
        }).then(res => {
          if (res.code === 200) {
            this.templateList = res.data.list
          } else {
            this.$Message.warning(res.message)
          }
        })
      },

    },
    created() {
      this.getTemplateList()
    },

  }
</script>

<style scoped>

</style>
