<template>
  <el-card v-loading="loading">
    <template #header>
      <div class="card-header">
        <span>内测配置</span>
        <el-button type="primary" :loading="saving" @click="handleSave">
          <el-icon><Check /></el-icon>保存配置
        </el-button>
      </div>
    </template>

    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="140px"
      class="config-form"
    >
      <el-alert
        title="内测模式说明"
        type="warning"
        :closable="false"
        show-icon
        style="margin-bottom: 20px;"
      >
        <template #default>
          <p>开启内测模式后，系统将进入测试状态，请谨慎操作。</p>
        </template>
      </el-alert>

      <el-form-item label="内测开关" prop="enable_test">
        <el-switch
          v-model="formData.enable_test"
          :active-value="1"
          :inactive-value="0"
          active-text="开启"
          inactive-text="关闭"
          inline-prompt
          style="--el-switch-on-color: #e6a23c; --el-switch-off-color: #dcdfe6"
        />
        <div class="form-tip">开启后系统进入内测模式</div>
      </el-form-item>

      <el-form-item label="禁用微信支付" prop="disable_wechat_pay">
        <el-switch
          v-model="formData.disable_wechat_pay"
          :active-value="1"
          :inactive-value="0"
          active-text="禁用"
          inactive-text="启用"
          inline-prompt
          style="--el-switch-on-color: #f56c6c; --el-switch-off-color: #67c23a"
        />
        <div class="form-tip">禁用后用户无法使用微信支付</div>
      </el-form-item>

      <el-form-item label="签到倍数" prop="sign_multiple">
        <el-input-number
          v-model.number="formData.sign_multiple"
          :min="1"
          :max="100"
          placeholder="请输入签到倍数"
          style="width: 200px"
        />
        <div class="form-tip">签到奖励倍数，默认为1</div>
      </el-form-item>

      <!-- 状态预览 -->
      <el-divider content-position="left">当前状态</el-divider>
      
      <div class="status-preview">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="内测模式">
            <el-tag :type="formData.enable_test === 1 ? 'warning' : 'success'">
              {{ formData.enable_test === 1 ? '已开启' : '已关闭' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="微信支付">
            <el-tag :type="formData.disable_wechat_pay === 1 ? 'danger' : 'success'">
              {{ formData.disable_wechat_pay === 1 ? '已禁用' : '已启用' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="签到倍数">
            <el-tag type="info">{{ formData.sign_multiple }}x</el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { Check } from '@element-plus/icons-vue'
import { getSystemTestSetting, updateSystemTestSetting, type SystemTestSetting } from '@/api/business/config'

// 加载状态
const loading = ref(false)
const saving = ref(false)

// 表单引用
const formRef = ref<FormInstance>()

// 表单数据
interface FormDataType {
  enable_test: number
  disable_wechat_pay: number
  sign_multiple: number
}

const formData = reactive<FormDataType>({
  enable_test: 0,
  disable_wechat_pay: 0,
  sign_multiple: 1
})

// 表单验证规则
const formRules: FormRules = {
  sign_multiple: [
    { type: 'number', min: 1, max: 100, message: '签到倍数必须在1-100之间', trigger: 'blur' }
  ]
}

// 加载配置数据
const loadData = async () => {
  loading.value = true
  try {
    const res = await getSystemTestSetting()
    if (res.data?.value) {
      const data = res.data.value
      Object.assign(formData, {
        enable_test: Number(data.enable_test) || 0,
        disable_wechat_pay: Number(data.disable_wechat_pay) || 0,
        sign_multiple: Number(data.sign_multiple) || 1
      })
    }
  } catch (error) {
    ElMessage.error('加载内测配置失败')
    console.error('加载内测配置失败:', error)
  } finally {
    loading.value = false
  }
}

// 保存配置
const handleSave = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
  } catch {
    ElMessage.warning('请检查表单填写是否正确')
    return
  }

  saving.value = true
  try {
    const submitData: SystemTestSetting = {
      enable_test: String(formData.enable_test),
      disable_wechat_pay: String(formData.disable_wechat_pay),
      sign_multiple: String(formData.sign_multiple)
    }

    await updateSystemTestSetting(submitData)
    ElMessage.success('保存成功')
  } catch (error) {
    ElMessage.error('保存失败')
    console.error('保存内测配置失败:', error)
  } finally {
    saving.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.config-form {
  max-width: 600px;
}

.form-tip {
  font-size: 12px;
  color: #909399;
  line-height: 1.5;
  margin-top: 4px;
}

.status-preview {
  padding: 10px 0;
}

:deep(.el-alert p) {
  margin: 4px 0;
  line-height: 1.6;
}

:deep(.el-divider__text) {
  font-weight: 600;
  color: #303133;
}
</style>
