<template>
  <ElDialog v-model="visible" title="设置实名认证策略" width="500px">
    <ElForm ref="formRef" :model="form" :rules="rules" label-width="120px">
      <ElFormItem label="资产标识">
        <span style="font-weight: bold; color: #409eff">{{ assetIdentifier }}</span>
      </ElFormItem>
      <ElFormItem label="当前策略">
        <ElTag :type="getPolicyTagType(currentPolicy)">
          {{ getPolicyName(currentPolicy) }}
        </ElTag>
      </ElFormItem>
      <ElFormItem label="实名认证策略" prop="realname_policy">
        <ElRadioGroup v-model="form.realname_policy">
          <ElRadio value="none">无需实名</ElRadio>
          <ElRadio value="before_order">先实名后充值/购买</ElRadio>
          <ElRadio value="after_order">先充值/购买后实名</ElRadio>
        </ElRadioGroup>
      </ElFormItem>
      <ElFormItem>
        <div style="font-size: 12px; color: #909399; line-height: 1.6">
          <p>说明：</p>
          <p>• <strong>无需实名</strong>：用户使用设备无需进行实名认证</p>
          <p>• <strong>先实名后充值/购买</strong>：用户必须先完成实名认证才能进行充值或购买套餐</p>
          <p>• <strong>先充值/购买后实名</strong>：用户可以先充值或购买套餐，之后需要完成实名认证</p>
        </div>
      </ElFormItem>
    </ElForm>

    <template #footer>
      <ElButton @click="handleCancel">取消</ElButton>
      <ElButton type="primary" @click="handleConfirm" :loading="confirmLoading">确认设置</ElButton>
    </template>
  </ElDialog>
</template>

<script setup lang="ts">
  import { ref, reactive, computed, watch } from 'vue'
  import { ElDialog, ElForm, ElFormItem, ElRadioGroup, ElRadio, ElTag } from 'element-plus'
  import type { FormInstance, FormRules } from 'element-plus'

  interface Props {
    modelValue: boolean
    assetIdentifier: string
    currentPolicy?: string
  }

  interface Emits {
    (e: 'update:modelValue', value: boolean): void
    (e: 'confirm', data: { realname_policy: string }): void
  }

  const props = withDefaults(defineProps<Props>(), {
    currentPolicy: 'none'
  })
  const emit = defineEmits<Emits>()

  const formRef = ref<FormInstance>()
  const confirmLoading = ref(false)

  const form = reactive({
    realname_policy: 'none'
  })

  const rules: FormRules = {
    realname_policy: [{ required: true, message: '请选择实名认证策略', trigger: 'change' }]
  }

  const visible = computed({
    get: () => props.modelValue,
    set: (value) => emit('update:modelValue', value)
  })

  const currentPolicy = computed(() => props.currentPolicy ?? 'none')

  const getPolicyName = (policy?: string) => {
    if (!policy || policy === '') return '未知'
    const map: Record<string, string> = {
      none: '无需实名',
      before_order: '先实名后充值/购买',
      after_order: '先充值/购买后实名'
    }
    return map[policy] || '未知'
  }

  const getPolicyTagType = (policy?: string) => {
    if (!policy || policy === '') return 'info'
    const map: Record<string, string> = {
      none: 'success',
      before_order: 'warning',
      after_order: 'info'
    }
    return map[policy] || 'info'
  }

  watch(visible, (newVal) => {
    if (newVal) {
      form.realname_policy = props.currentPolicy ?? 'none'
    }
  })

  const handleCancel = () => {
    visible.value = false
  }

  const handleConfirm = async () => {
    if (!formRef.value) return
    try {
      await formRef.value.validate()
      emit('confirm', { realname_policy: form.realname_policy })
    } catch (error) {
      console.error('表单验证失败:', error)
    }
  }
</script>

<style lang="scss" scoped></style>
