<template>
  <ElDialog v-model="visible" title="设置WiFi" width="500px" @close="handleClose">
    <ElForm ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <ElFormItem label="设备信息">
        <span style="font-weight: bold; color: #409eff">{{ deviceInfo }}</span>
      </ElFormItem>
      <ElFormItem label="WiFi状态" prop="enabled">
        <ElRadioGroup v-model="formData.enabled">
          <ElRadio :value="1">启用</ElRadio>
          <ElRadio :value="0">禁用</ElRadio>
        </ElRadioGroup>
      </ElFormItem>
      <ElFormItem label="WiFi名称" prop="ssid">
        <ElInput
          v-model="formData.ssid"
          placeholder="请输入WiFi名称（1-32个字符）"
          maxlength="32"
          show-word-limit
          clearable
        />
      </ElFormItem>
      <ElFormItem label="WiFi密码" prop="password">
        <ElInput
          v-model="formData.password"
          type="password"
          placeholder="请输入WiFi密码（8-63个字符）"
          maxlength="63"
          show-word-limit
          show-password
          clearable
        />
      </ElFormItem>
    </ElForm>
    <template #footer>
      <ElButton @click="visible = false">取消</ElButton>
      <ElButton type="primary" @click="handleConfirm" :loading="confirmLoading">
        确认设置
      </ElButton>
    </template>
  </ElDialog>
</template>

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

  interface Props {
    modelValue: boolean
    deviceInfo: string
    loading?: boolean
  }

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

  const props = withDefaults(defineProps<Props>(), {
    loading: false
  })

  const emit = defineEmits<Emits>()

  const visible = ref(props.modelValue)
  const confirmLoading = ref(props.loading)
  const formRef = ref<FormInstance>()

  const formData = reactive({
    enabled: 1,
    ssid: '',
    password: ''
  })

  const rules = reactive<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' }
    ]
  })

  watch(
    () => props.modelValue,
    (val) => {
      visible.value = val
      if (val) {
        // 重置表单
        formData.enabled = 1
        formData.ssid = ''
        formData.password = ''
      }
    }
  )

  watch(visible, (val) => {
    emit('update:modelValue', val)
  })

  watch(
    () => props.loading,
    (val) => {
      confirmLoading.value = val
    }
  )

  const handleClose = () => {
    formRef.value?.resetFields()
  }

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

    await formRef.value.validate((valid) => {
      if (valid) {
        emit('confirm', {
          enabled: formData.enabled,
          ssid: formData.ssid,
          password: formData.password
        })
      }
    })
  }
</script>
