<template>
  <ElDialog v-model="visible" title="设置WiFi" width="30%" :append-to-body="true">
    <ElForm ref="formRef" :model="form" :rules="rules" label-width="80px" autocomplete="off">
      <ElFormItem label="WiFi状态" prop="enabled">
        <ElRadioGroup v-model="form.enabled">
          <ElRadio :value="true">启用</ElRadio>
          <ElRadio :value="false">禁用</ElRadio>
        </ElRadioGroup>
      </ElFormItem>
      <ElFormItem label="WiFi名称" prop="ssid">
        <ElInput
          v-model="form.ssid"
          placeholder="请输入WiFi名称（1-32个字符）"
          maxlength="32"
          show-word-limit
          clearable
          autocomplete="off"
          name="wifi_ssid"
        />
      </ElFormItem>
      <ElFormItem label="WiFi密码" prop="password">
        <ElInput
          v-model="form.password"
          type="password"
          placeholder="请输入WiFi密码（8-63个字符）"
          maxlength="63"
          show-word-limit
          show-password
          clearable
          autocomplete="off"
          name="wifi_password"
        />
      </ElFormItem>
    </ElForm>
    <template #footer>
      <ElButton @click="handleCancel">取消</ElButton>
      <ElButton type="primary" @click="handleConfirm" :loading="loading"> 确认设置 </ElButton>
    </template>
  </ElDialog>
</template>

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

  interface Props {
    modelValue: boolean
    cardNo?: string // 流量卡号（ICCID）
    deviceIdentifier?: string // 设备标识（IMEI）
    wifiEnabled?: boolean // WiFi启用状态
    wifiSsid?: string // WiFi名称
    wifiPassword?: string // WiFi密码
  }

  interface Emits {
    (e: 'update:modelValue', value: boolean): void
    (
      e: 'confirm',
      data: {
        deviceIdentifier?: string
        card_no: string
        enabled: boolean
        ssid: string
        password: string
      }
    ): void
  }

  const props = withDefaults(defineProps<Props>(), {
    cardNo: '',
    deviceIdentifier: '',
    wifiEnabled: true,
    wifiSsid: '',
    wifiPassword: ''
  })
  const emit = defineEmits<Emits>()

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

  const form = reactive({
    enabled: true,
    ssid: '',
    password: ''
  })

  const rules: FormRules = {
    ssid: [
      { required: true, message: '请输入WiFi名称', trigger: 'blur' },
      { min: 1, max: 32, message: 'WiFi名称长度为1-32个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入WiFi密码', trigger: 'blur' },
      { min: 8, max: 63, message: 'WiFi密码长度为8-63个字符', trigger: 'blur' }
    ]
  }

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

  // 监听对话框打开，重置表单
  watch(visible, (newVal) => {
    if (newVal) {
      form.enabled = props.wifiEnabled ?? true
      form.ssid = props.wifiSsid ?? ''
      form.password = props.wifiPassword ?? ''
    }
  })

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

  const handleConfirm = async () => {
    if (!formRef.value) return

    try {
      await formRef.value.validate()
      emit('confirm', {
        deviceIdentifier: props.deviceIdentifier,
        card_no: props.cardNo || '',
        enabled: form.enabled,
        ssid: form.ssid,
        password: form.password
      })
    } catch (error) {
      console.error('表单验证失败:', error)
    }
  }
</script>

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