<template>
  <ElButton @click="handleGenerate">
    {{ buttonText }}
  </ElButton>
</template>

<script setup lang="ts">
  import { ElMessage } from 'element-plus'
  import {
    generateSeriesCode,
    generatePackageCode,
    generateShopCode
  } from '@/utils/codeGenerator'

  interface Props {
    // 编码类型: series(套餐系列) | package(套餐) | shop(店铺)
    codeType: 'series' | 'package' | 'shop'
    // 按钮文字
    buttonText?: string
    // 表单字段名,用于清除验证
    fieldName?: string
  }

  const props = withDefaults(defineProps<Props>(), {
    buttonText: '生成编码'
  })

  const emit = defineEmits<{
    (e: 'generated', code: string): void
  }>()

  // 生成编码
  const handleGenerate = () => {
    let code = ''

    switch (props.codeType) {
      case 'series':
        code = generateSeriesCode()
        break
      case 'package':
        code = generatePackageCode()
        break
      case 'shop':
        code = generateShopCode()
        break
      default:
        ElMessage.error('未知的编码类型')
        return
    }

    // 触发生成事件,将编码传递给父组件
    emit('generated', code)
    ElMessage.success('编码生成成功')
  }
</script>

<style lang="scss" scoped>
  // 可以添加特定样式
</style>
