<template>
  <el-dialog
    v-model="visible"
    :title="$t('page.form.titleEdit')"
    width="600px"
    align-center
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="formData" label-width="120px">
      <el-form-item :label="$t('page.form.labelLotteryConfigId')" prop="lottery_config_id">
        <el-input v-model="formData.lottery_config_id" :placeholder="$t('page.form.placeholderLotteryConfigId')" disabled />
      </el-form-item>
      <el-form-item :label="$t('page.form.drawType')" prop="lottery_type">
        <el-select
          v-model="formData.lottery_type"
          :placeholder="$t('form.placeholderSelect')"
          clearable
          style="width: 100%"
          disabled
        >
          <el-option :label="$t('page.search.paid')" :value="0" />
          <el-option :label="$t('page.search.free')" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('page.search.direction')" prop="direction">
        <el-select
          v-model="formData.direction"
          :placeholder="$t('form.placeholderSelect')"
          clearable
          style="width: 100%"
          disabled
        >
          <el-option :label="$t('page.search.clockwise')" :value="0" />
          <el-option :label="$t('page.search.anticlockwise')" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('page.search.ante')" prop="ante">
        <el-input-number
          v-model="formData.ante"
          :placeholder="$t('table.searchBar.all')"
          :precision="0"
          controls-position="right"
          style="width: 100%"
          disabled
        />
      </el-form-item>
      <el-form-item :label="$t('page.search.paidAmount')" prop="paid_amount">
        <el-input-number
          v-model="formData.paid_amount"
          :placeholder="$t('table.searchBar.all')"
          :precision="0"
          controls-position="right"
          style="width: 100%"
          disabled
        />
      </el-form-item>
      <el-form-item :label="$t('page.search.isBigWin')" prop="is_win">
        <el-select v-model="formData.is_win" :placeholder="$t('form.placeholderSelect')" clearable style="width: 100%" disabled>
          <el-option :label="$t('page.search.noBigWin')" :value="0" />
          <el-option :label="$t('page.search.bigWin')" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('page.search.winCoin')" prop="win_coin">
        <el-input-number
          v-model="formData.win_coin"
          :placeholder="$t('page.form.placeholderWinCoin')"
          :precision="0"
          controls-position="right"
          style="width: 100%"
          disabled
        />
      </el-form-item>
      <el-form-item :label="$t('page.search.rewardTier')" prop="reward_tier">
        <el-select
          v-model="formData.reward_tier"
          :placeholder="$t('page.form.placeholderRewardTier')"
          clearable
          style="width: 100%"
          disabled
        >
          <el-option label="T1" value="T1" />
          <el-option label="T2" value="T2" />
          <el-option label="T3" value="T3" />
          <el-option label="T4" value="T4" />
          <el-option label="T5" value="T5" />
          <el-option label="BIGWIN" value="BIGWIN" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('page.table.startIndex')" prop="start_index">
        <el-input v-model="formData.start_index" :placeholder="$t('page.form.placeholderStartIndex')" disabled />
      </el-form-item>
      <el-form-item :label="$t('page.form.labelTargetIndex')" prop="target_index">
        <el-input v-model="formData.target_index" :placeholder="$t('page.form.placeholderTargetIndex')" disabled />
      </el-form-item>
      <el-form-item :label="$t('page.search.rollNumber')" prop="roll_number">
        <el-input v-model="formData.roll_number" :placeholder="$t('page.form.placeholderRollNumber')" disabled />
      </el-form-item>
      <el-form-item :label="$t('page.form.labelRollArray')" prop="roll_array">
        <el-input v-model="formData.roll_array" :placeholder="$t('page.form.placeholderRollArray')" disabled />
      </el-form-item>
      <el-form-item :label="$t('page.form.labelStatus')" prop="status">
        <sa-radio v-model="formData.status" dict="data_status" disabled />
      </el-form-item>
      <el-form-item :label="$t('page.table.superWinCoin')" prop="super_win_coin">
        <el-input-number
          v-model="formData.super_win_coin"
          :placeholder="$t('page.form.placeholderSuperWinCoin')"
          :precision="0"
          controls-position="right"
          style="width: 100%"
          disabled
        />
      </el-form-item>
      <el-form-item :label="$t('page.table.rewardWinCoin')" prop="reward_win_coin">
        <el-input-number
          v-model="formData.reward_win_coin"
          :placeholder="$t('page.form.placeholderRewardWinCoin')"
          :precision="0"
          controls-position="right"
          style="width: 100%"
          disabled
        />
      </el-form-item>
      <el-form-item :label="$t('page.form.labelAdminId')" prop="admin_id">
        <el-input v-model="formData.admin_id" :placeholder="$t('page.form.placeholderAdminId')" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">{{ $t('form.close') }}</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import type { FormInstance } 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: 'edit',
    data: undefined
  })

  const emit = defineEmits<Emits>()

  const formRef = ref<FormInstance>()

  /**
   * 弹窗显示状态双向绑定
   */
  const visible = computed({
    get: () => props.modelValue,
    set: (value) => emit('update:modelValue', value)
  })

  /**
   * 初始数据
   */
  const initialFormData = {
    id: null,
    lottery_config_id: null,
    lottery_type: null,
    is_win: null,
    ante: 1,
    paid_amount: 0,
    win_coin: 0,
    direction: null,
    reward_tier: undefined as string | undefined,
    start_index: null,
    target_index: null,
    roll_number: null,
    roll_array: '',
    status: 1,
    super_win_coin: 0,
    reward_win_coin: 0,
    admin_id: null
  }

  /**
   * 表单数据
   */
  const formData = reactive({ ...initialFormData })

  /**
   * 监听弹窗打开，初始化表单数据（仅查看）
   */
  watch(
    () => props.modelValue,
    (newVal) => {
      if (newVal) {
        initPage()
      }
    }
  )

  /**
   * 初始化页面数据
   */
  const initPage = async () => {
    Object.assign(formData, initialFormData)
    if (props.data) {
      await nextTick()
      initForm()
    }
  }

  /**
   * 回填表单数据
   */
  function normalizePlatformCoin(val: unknown): number {
    if (val === '' || val === null || val === undefined) return 0
    const n = typeof val === 'number' ? val : Number(val)
    if (!Number.isFinite(n)) return 0
    return Math.trunc(n)
  }

  const initForm = () => {
    if (props.data) {
      for (const key in formData) {
        if (props.data[key] != null && props.data[key] !== undefined) {
          ;(formData as Record<string, unknown>)[key] = props.data[key]
        }
      }
      formData.win_coin = normalizePlatformCoin(formData.win_coin)
      formData.super_win_coin = normalizePlatformCoin(formData.super_win_coin)
      formData.reward_win_coin = normalizePlatformCoin(formData.reward_win_coin)
    }
  }

  /**
   * 关闭弹窗
   */
  const handleClose = () => {
    visible.value = false
    formRef.value?.resetFields()
  }
</script>
