<template>
  <div class="signin-activity-container">
    <!-- 操作栏 -->
    <el-card class="filter-container" shadow="never">
      <div class="action-bar">
        <el-button type="primary" @click="handleAddReward">
          <el-icon><Plus /></el-icon>新增签到奖励
        </el-button>
        <el-button type="success" @click="handleAddRewardType">
          <el-icon><Plus /></el-icon>新增奖励类型
        </el-button>
      </div>
    </el-card>

    <!-- 签到奖励列表 -->
    <el-card class="table-container" shadow="never">
      <template #header>
        <div class="card-header">
          <span>签到奖励配置</span>
        </div>
      </template>
      <el-table
        v-loading="loading"
        :data="rewardList"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="day" label="签到天数" width="120" />
        <el-table-column prop="reward_type" label="奖励类型" width="150">
          <template #default="{ row }">
            <el-tag :type="getRewardTypeColor(row.reward_type)">
              {{ getRewardTypeText(row.reward_type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="reward_value" label="奖励值" width="120">
          <template #default="{ row }">
            <span v-if="row.reward_type === 'balance'">¥{{ row.reward_value }}</span>
            <span v-else>{{ row.reward_value }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" min-width="200" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 奖励类型列表 -->
    <el-card class="table-container" shadow="never" style="margin-top: 20px;">
      <template #header>
        <div class="card-header">
          <span>奖励类型配置</span>
        </div>
      </template>
      <el-table
        v-loading="typeLoading"
        :data="typeList"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="type_code" label="类型代码" width="150" />
        <el-table-column prop="type_name" label="类型名称" min-width="200" />
        <el-table-column prop="description" label="描述" min-width="250" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEditType(row)">编辑</el-button>
            <el-button type="danger" link @click="handleDeleteType(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 签到奖励表单对话框 -->
    <el-dialog
      v-model="rewardDialogVisible"
      title="签到奖励配置"
      width="600px"
    >
      <el-form
        ref="rewardFormRef"
        :model="rewardForm"
        :rules="rewardRules"
        label-width="120px"
      >
        <el-form-item label="签到天数" prop="day">
          <el-input-number v-model="rewardForm.day" :min="1" placeholder="请输入签到天数" style="width: 100%" />
        </el-form-item>
        <el-form-item label="奖励类型" prop="reward_type">
          <el-select v-model="rewardForm.reward_type" placeholder="请选择奖励类型" style="width: 100%">
            <el-option
              v-for="item in typeOptions"
              :key="item.type_code"
              :label="item.type_name"
              :value="item.type_code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="奖励值" prop="reward_value">
          <el-input v-model.number="rewardForm.reward_value" placeholder="请输入奖励值" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="rewardForm.description" type="textarea" :rows="3" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="rewardForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="rewardDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitRewardForm">确定</el-button>
      </template>
    </el-dialog>

    <!-- 奖励类型表单对话框 -->
    <el-dialog
      v-model="typeDialogVisible"
      title="奖励类型配置"
      width="500px"
    >
      <el-form
        ref="typeFormRef"
        :model="typeForm"
        :rules="typeRules"
        label-width="120px"
      >
        <el-form-item label="类型代码" prop="type_code">
          <el-input v-model="typeForm.type_code" placeholder="请输入类型代码" />
        </el-form-item>
        <el-form-item label="类型名称" prop="type_name">
          <el-input v-model="typeForm.type_name" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="typeForm.description" type="textarea" :rows="3" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="typeForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="typeDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitTypeForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { addSignReward, addSignRewardType } from '@/api/admin/activity'

// 签到奖励表单
const rewardForm = reactive({
  id: undefined,
  day: 1,
  reward_type: '',
  reward_value: 0,
  description: '',
  status: 1
})

const rewardRules = {
  day: [
    { required: true, message: '请输入签到天数', trigger: 'blur' }
  ],
  reward_type: [
    { required: true, message: '请选择奖励类型', trigger: 'change' }
  ],
  reward_value: [
    { required: true, message: '请输入奖励值', trigger: 'blur' }
  ]
}

// 奖励类型表单
const typeForm = reactive({
  id: undefined,
  type_code: '',
  type_name: '',
  description: '',
  status: 1
})

const typeRules = {
  type_code: [
    { required: true, message: '请输入类型代码', trigger: 'blur' }
  ],
  type_name: [
    { required: true, message: '请输入类型名称', trigger: 'blur' }
  ]
}

// 状态数据
const loading = ref(false)
const typeLoading = ref(false)
const rewardList = ref([])
const typeList = ref([])
const typeOptions = ref([])
const rewardDialogVisible = ref(false)
const typeDialogVisible = ref(false)
const rewardFormRef = ref(null)
const typeFormRef = ref(null)

// 获取奖励类型文本
const getRewardTypeText = (type) => {
  const typeMap = {
    'balance': '余额',
    'points': '积分',
    'coupon': '优惠券',
    'voucher': '代金券'
  }
  return typeMap[type] || type
}

// 获取奖励类型颜色
const getRewardTypeColor = (type) => {
  const colorMap = {
    'balance': 'success',
    'points': 'primary',
    'coupon': 'warning',
    'voucher': 'info'
  }
  return colorMap[type] || 'info'
}

// 新增签到奖励
const handleAddReward = () => {
  rewardDialogVisible.value = true
  Object.assign(rewardForm, {
    id: undefined,
    day: 1,
    reward_type: '',
    reward_value: 0,
    description: '',
    status: 1
  })
  rewardFormRef.value?.resetFields()
}

// 新增奖励类型
const handleAddRewardType = () => {
  typeDialogVisible.value = true
  Object.assign(typeForm, {
    id: undefined,
    type_code: '',
    type_name: '',
    description: '',
    status: 1
  })
  typeFormRef.value?.resetFields()
}

// 编辑签到奖励
const handleEdit = (row) => {
  Object.assign(rewardForm, {
    id: row.id,
    day: row.day,
    reward_type: row.reward_type,
    reward_value: row.reward_value,
    description: row.description,
    status: row.status
  })
  rewardDialogVisible.value = true
}

// 编辑奖励类型
const handleEditType = (row) => {
  Object.assign(typeForm, {
    id: row.id,
    type_code: row.type_code,
    type_name: row.type_name,
    description: row.description,
    status: row.status
  })
  typeDialogVisible.value = true
}

// 删除签到奖励
const handleDelete = (row) => {
  ElMessageBox.confirm(`确认删除第${row.day}天的签到奖励吗？`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    // 这里应该调用删除API
  }).catch(() => {})
}

// 删除奖励类型
const handleDeleteType = (row) => {
  ElMessageBox.confirm(`确认删除奖励类型 ${row.type_name} 吗？`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    // 这里应该调用删除API
  }).catch(() => {})
}

// 提交签到奖励表单
const submitRewardForm = () => {
  rewardFormRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        const res = await addSignReward(rewardForm)
        if (res.code === 200) {
          ElMessage.success('添加成功')
          rewardDialogVisible.value = false
          // 刷新列表
        }
      } catch (error) {
        ElMessage.error('添加失败')
      }
    }
  })
}

// 提交奖励类型表单
const submitTypeForm = () => {
  typeFormRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        const res = await addSignRewardType(typeForm)
        if (res.code === 200) {
          ElMessage.success('添加成功')
          typeDialogVisible.value = false
          // 刷新列表
        }
      } catch (error) {
        ElMessage.error('添加失败')
      }
    }
  })
}

// 初始化
onMounted(() => {
  // 模拟数据
  rewardList.value = [
    {
      id: 1,
      day: 1,
      reward_type: 'balance',
      reward_value: 1,
      description: '连续签到1天奖励1元余额',
      status: 1,
      create_time: '2024-01-01 10:00:00'
    },
    {
      id: 2,
      day: 7,
      reward_type: 'balance',
      reward_value: 10,
      description: '连续签到7天奖励10元余额',
      status: 1,
      create_time: '2024-01-01 10:00:00'
    }
  ]

  typeList.value = [
    {
      id: 1,
      type_code: 'balance',
      type_name: '余额',
      description: '用户账户余额',
      status: 1,
      create_time: '2024-01-01 10:00:00'
    },
    {
      id: 2,
      type_code: 'points',
      type_name: '积分',
      description: '用户积分',
      status: 1,
      create_time: '2024-01-01 10:00:00'
    }
  ]

  typeOptions.value = typeList.value
})
</script>

<style scoped>
.signin-activity-container {
  padding: 0;
}

.filter-container {
  margin-bottom: 20px;
  border-radius: 8px;
}

.action-bar {
  display: flex;
  gap: 12px;
}

.table-container {
  border-radius: 8px;
}

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

