
<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-item label="二维码预览">
        <div class="qrcode-preview-container">
          <div
            class="qrcode-preview"
            :style="{
              width: previewSize.width + 'px',
              height: previewSize.height + 'px',
              border: '1px solid #dcdfe6'
            }"
          >
            <!-- 这里使用假的二维码预览，实际应该使用二维码生成库 -->
            <div class="qrcode-placeholder" v-if="!previewVisible">
              点击预览按钮生成二维码
            </div>
            <div v-else>
              <!-- 实际项目中应该使用qrcode库生成二维码 -->
              <div class="qrcode-content">
                <svg width="100%" height="100%" viewBox="0 0 100 100">
                  <rect width="100" height="100" fill="white" />
                  <!-- 简单的二维码样式 -->
                  <rect x="10" y="10" width="80" height="80" fill="none" stroke="#333" stroke-width="2" />
                  <rect x="20" y="20" width="10" height="10" fill="#333" />
                  <rect x="45" y="20" width="10" height="10" fill="#333" />
                  <rect x="70" y="20" width="10" height="10" fill="#333" />
                  <rect x="20" y="45" width="10" height="10" fill="#333" />
                  <rect x="45" y="45" width="10" height="10" fill="#333" />
                  <rect x="70" y="45" width="10" height="10" fill="#333" />
                  <rect x="20" y="70" width="10" height="10" fill="#333" />
                  <rect x="45" y="70" width="10" height="10" fill="#333" />
                  <rect x="70" y="70" width="10" height="10" fill="#333" />
                </svg>
              </div>
              <div class="qrcode-value" v-if="qrcodeValue">{{ qrcodeValue }}</div>
            </div>
          </div>
        </div>
      </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() {
  previewVisible.value = true
}

// 打印
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
  }

  // 调用父组件的打印方法
  emit('print', {
    width,
    height,
    qrcodeValue: props.qrcodeValue
  })

  // 关闭对话框
  cancel()
}

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

// 取消
function cancel() {
  emit('update:visible', false)
}
</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>