
<template>
  <el-dialog
    :title="title"
    :model-value="visible"
    @update:model-value="handleClose"
    width="600px"
    append-to-body
  >
    <el-form :model="form" label-width="120px">
      <!-- 尺寸选择 -->
      <el-form-item label="二维码尺寸">
        <el-radio-group v-model="form.sizeType" @change="handleSizeChange">
          <el-radio label="default">系统默认尺寸</el-radio>
          <el-radio label="custom">自定义尺寸</el-radio>
        </el-radio-group>
      </el-form-item>

      <!-- 默认尺寸选择 -->
      <el-form-item label="默认尺寸" v-if="form.sizeType === 'default'">
        <el-select v-model="form.defaultSize" placeholder="请选择尺寸">
          <el-option label="20mm×20mm" :value="{width: 20, height: 20}" />
          <el-option label="30mm×30mm" :value="{width: 30, height: 30}" />
          <el-option label="40mm×40mm" :value="{width: 40, height: 40}" />
          <el-option label="50mm×50mm" :value="{width: 50, height: 50}" />
          <el-option label="60mm×60mm" :value="{width: 60, height: 60}" />
        </el-select>
      </el-form-item>

      <!-- 自定义尺寸 -->
      <el-form-item label="自定义尺寸" v-if="form.sizeType === 'custom'">
        <el-row :gutter="10">
          <el-col :span="11">
            <el-input-number
              v-model="form.customWidth"
              :min="10"
              :max="200"
              :step="1"
              placeholder="宽度(mm)"
              style="width: 100%"
            />
          </el-col>
          <el-col :span="2" style="text-align: center; line-height: 38px;">×</el-col>
          <el-col :span="11">
            <el-input-number
              v-model="form.customHeight"
              :min="10"
              :max="200"
              :step="1"
              placeholder="高度(mm)"
              style="width: 100%"
            />
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handlePreview">预览二维码</el-button>
        <el-button type="primary" @click="handlePrint">打印</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed, watch } from 'vue'

// 组件属性
const props = defineProps({
  title: {
    type: String,
    default: '打印二维码'
  },
  visible: {
    type: Boolean,
    default: false
  },
  qrcodeValue: {
    type: String,
    default: ''
  }
})

// 组件事件
const emit = defineEmits(['update:visible', 'print'])

// 表单数据
const form = reactive({
  sizeType: 'default', // default: 默认尺寸, custom: 自定义尺寸
  defaultSize: { width: 40, height: 40 }, // 默认尺寸
  customWidth: 40, // 自定义宽度(mm)
  customHeight: 40 // 自定义高度(mm)
})

// 预览相关
const previewVisible = ref(false)
const previewSize = computed(() => {
  // 将毫米转换为像素进行预览 (1mm ≈ 3.7795275591px)
  const scale = 3.7795275591
  let width, height
  
  if (form.sizeType === 'default') {
    width = form.defaultSize.width
    height = form.defaultSize.height
  } else {
    width = form.customWidth
    height = form.customHeight
  }
  
  return {
    width: width * scale,
    height: height * scale
  }
})

// 尺寸变化处理
function handleSizeChange() {
  if (form.sizeType === 'custom') {
    // 切换到自定义尺寸时，使用当前默认尺寸的值
    form.customWidth = form.defaultSize.width
    form.customHeight = form.defaultSize.height
  }
}

// 预览二维码
function handlePreview() {
  handleQrcodePrint({
    width: previewSize.value.width,
    height: previewSize.value.height,
    qrcodeValue: props.qrcodeValue
  })
}

// 打印
function handlePrint() {
  // 获取当前选择的尺寸 (mm)
  let width, height
  if (form.sizeType === 'default') {
    width = form.defaultSize.width
    height = form.defaultSize.height
  } else {
    width = form.customWidth
    height = form.customHeight
  }

  // 使用LODOP直接打印二维码
  const LODOP = getLodop(document.getElementById('LODOP1'), document.getElementById('LODOP_EM1'))
  CreatePrintPage(width, height, props.qrcodeValue)
  LODOP.PRINT()

  // 关闭对话框
  cancel()
}

// 处理对话框关闭
function handleClose(newVal) {
  emit('update:visible', newVal)
}

// 取消
function cancel() {
  emit('update:visible', false)
}



/** 处理二维码打印 */
function handleQrcodePrint(params) {
  const { width, height, qrcodeValue } = params
  
  // 使用LODOP打印二维码
  const LODOP = getLodop(document.getElementById('LODOP1'), document.getElementById('LODOP_EM1'))
  CreatePrintPage(width, height, qrcodeValue)
  
  LODOP.PREVIEW() // 预览打印
}

function CreatePrintPage(width, height, qrcodeValue) {
  LODOP.SET_SHOW_MODE("SKIN_TYPE", '1');
  LODOP.SET_PRINT_PAGESIZE(1, width, height, '自定义二维码标签')
  LODOP.ADD_PRINT_BARCODE(0, 0, 150, 150, "QRCode", qrcodeValue);
};
</script>

<style scoped>
.qrcode-preview-container {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.qrcode-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px;
  background-color: #f5f7fa;
}

.qrcode-placeholder {
  color: #909399;
  text-align: center;
}

.qrcode-content {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.qrcode-value {
  margin-top: 10px;
  text-align: center;
  font-size: 14px;
  color: #606266;
}
</style>