<template>
  <el-dialog
    v-model="visible"
    :title="dialogType === 'add' ? '新增大富翁-玩家' : '编辑大富翁-玩家'"
    width="600px"
    align-center
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入昵称" />
      </el-form-item>
      <el-form-item label="密码" prop="password" :rules="passwordRules">
        <el-input
          v-model="formData.password"
          type="password"
          placeholder="编辑留空则不修改"
          show-password
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <sa-switch v-model="formData.status" />
      </el-form-item>
      <el-form-item label="平台币" prop="coin">
        <el-input-number
          v-model="formData.coin"
          :min="0"
          :precision="2"
          :disabled="dialogType === 'add'"
          placeholder="创建时默认0，不可改"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="倍率" prop="is_up">
        <el-select v-model="formData.is_up" placeholder="请选择倍率" clearable style="width: 100%">
          <el-option label="正常" :value="0" />
          <el-option label="强制杀猪" :value="1" />
          <el-option label="T1高倍率" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="T1池权重(%)" prop="t1_wight">
        <el-slider v-model="formData.t1_wight" :min="0" :max="100" :step="0.01" show-input />
      </el-form-item>
      <el-form-item label="T2池权重(%)" prop="t2_wight">
        <el-slider v-model="formData.t2_wight" :min="0" :max="100" :step="0.01" show-input />
      </el-form-item>
      <el-form-item label="T3池权重(%)" prop="t3_wight">
        <el-slider v-model="formData.t3_wight" :min="0" :max="100" :step="0.01" show-input />
      </el-form-item>
      <el-form-item label="T4池权重(%)" prop="t4_wight">
        <el-slider v-model="formData.t4_wight" :min="0" :max="100" :step="0.01" show-input />
      </el-form-item>
      <el-form-item label="T5池权重(%)" prop="t5_wight">
        <el-slider v-model="formData.t5_wight" :min="0" :max="100" :step="0.01" show-input />
      </el-form-item>
      <el-form-item>
        <div class="text-gray-500 text-sm">
          五个池权重总和：<span :class="Math.abs(weightsSum - 100) > 0.01 ? 'text-red-500' : ''">{{
            weightsSum
          }}</span
          >% / 100%（必须为100%）
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import api from '../../../api/player/index'
  import { ElMessage } from 'element-plus'
  import type { FormInstance, FormRules } from 'element-plus'

  interface Props {
    modelValue: boolean
    dialogType: string
    data?: Record<string, any>
  }

  interface Emits {
    (e: 'update:modelValue', value: boolean): void
    (e: 'success'): void
  }

  const props = withDefaults(defineProps<Props>(), {
    modelValue: false,
    dialogType: 'add',
    data: undefined
  })

  const emit = defineEmits<Emits>()

  const formRef = ref<FormInstance>()

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

  const WEIGHT_KEYS = ['t1_wight', 't2_wight', 't3_wight', 't4_wight', 't5_wight'] as const
  const weightsSum = computed(() => {
    return WEIGHT_KEYS.reduce((sum, key) => sum + Number(formData[key] ?? 0), 0)
  })

  /** 新增时密码必填，编辑时选填 */
  const passwordRules = computed(() =>
    props.dialogType === 'add' ? [{ required: true, message: '密码必需填写', trigger: 'blur' }] : []
  )

  const rules = reactive<FormRules>({
    username: [{ required: true, message: '用户名必需填写', trigger: 'blur' }],
    name: [{ required: true, message: '昵称必需填写', trigger: 'blur' }],
    status: [{ required: true, message: '状态必需填写', trigger: 'blur' }],
    coin: [{ required: true, message: '平台币必需填写', trigger: 'blur' }]
  })

  const initialFormData = {
    id: null as number | null,
    username: '',
    name: '',
    password: '',
    status: 1 as number,
    coin: 0 as number,
    is_up: null as number | null,
    t1_wight: 0 as number,
    t2_wight: 0 as number,
    t3_wight: 0 as number,
    t4_wight: 0 as number,
    t5_wight: 0 as number
  }

  const formData = reactive({ ...initialFormData })

  watch(
    () => props.modelValue,
    (newVal) => {
      if (newVal) initPage()
    }
  )

  const initPage = async () => {
    Object.assign(formData, initialFormData)
    if (props.data) {
      await nextTick()
      initForm()
    }
  }

  const numKeys = [
    'id',
    'status',
    'coin',
    'is_up',
    't1_wight',
    't2_wight',
    't3_wight',
    't4_wight',
    't5_wight'
  ]

  const initForm = () => {
    if (!props.data) return
    for (const key of Object.keys(formData)) {
      if (!(key in props.data)) continue
      if (key === 'password') {
        ;(formData as any).password = ''
        continue
      }
      const val = props.data[key]
      if (numKeys.includes(key)) {
        ;(formData as any)[key] =
          key === 'id' ? (val != null ? Number(val) || null : null) : Number(val) || 0
      } else {
        ;(formData as any)[key] = val ?? ''
      }
    }
  }

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

  const handleSubmit = async () => {
    if (!formRef.value) return
    try {
      await formRef.value.validate()
      if (Math.abs(weightsSum.value - 100) > 0.01) {
        ElMessage.warning('五个池权重总和必须为100%')
        return
      }
      const payload = { ...formData }
      if (props.dialogType === 'edit' && !payload.password) {
        delete (payload as any).password
      }
      if (props.dialogType === 'add') {
        await api.save(payload)
        ElMessage.success('新增成功')
      } else {
        await api.update(payload)
        ElMessage.success('修改成功')
      }
      emit('success')
      handleClose()
    } catch (error) {
      console.log('表单验证失败:', error)
    }
  }
</script>
