<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>签到配置</span>
          <div class="header-actions">
            <el-button type="primary" @click="handleAdd">
              <el-icon><Plus /></el-icon>新增配置
            </el-button>
          </div>
        </div>
      </template>

      <!-- Tab切换 -->
      <el-tabs v-model="activeTab" @tab-change="handleTabChange">
        <el-tab-pane label="每日签到" :name="SignType.Daily" />
        <el-tab-pane label="累计签到" :name="SignType.Cumulative" />
      </el-tabs>

      <!-- 签到配置表格 -->
      <SignConfigTable
        :data="signConfigList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        :sign-type="activeTab"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @edit="handleEdit"
        @edit-reward="handleEditReward"
        @delete="handleDelete"
        @status-change="handleStatusChange"
        @sort-change="handleSortChange"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <SignConfigFormDialog
      v-model="formDialogVisible"
      :sign-config="currentSignConfig"
      :is-edit="isEdit"
      :sign-type="activeTab"
      @success="fetchData"
    />

    <!-- 奖励配置弹窗 -->
    <RewardConfigDialog
      v-model="rewardDialogVisible"
      :sign-config="currentSignConfig"
      @success="fetchData"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import SignConfigTable from './components/SignConfigTable.vue'
import SignConfigFormDialog from './components/SignConfigFormDialog.vue'
import RewardConfigDialog from './components/RewardConfigDialog.vue'
import {
  getSignConfigs,
  deleteSignConfig,
  updateSignConfigStatus,
  updateSignConfigSort,
  SignType,
  type SignConfigListRequest,
  type SignConfigResponse
} from '@/api/business/signconfig'

// 当前Tab
const activeTab = ref<number>(SignType.Daily)

// 列表数据
const loading = ref(false)
const signConfigList = ref<SignConfigResponse[]>([])
const total = ref(0)

// 查询参数
const queryParams = reactive<SignConfigListRequest>({
  page: 1,
  pageSize: 20,
  type: SignType.Daily,
  keyword: undefined
})

// 弹窗控制
const formDialogVisible = ref(false)
const rewardDialogVisible = ref(false)
const isEdit = ref(false)
const currentSignConfig = ref<SignConfigResponse | null>(null)

// 获取签到配置列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getSignConfigs(queryParams)
    signConfigList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// Tab切换
const handleTabChange = (tab: number) => {
  queryParams.type = tab
  queryParams.page = 1
  fetchData()
}

// 分页
const handlePageChange = (page: number) => {
  queryParams.page = page
  fetchData()
}

const handleSizeChange = (size: number) => {
  queryParams.pageSize = size
  queryParams.page = 1
  fetchData()
}

// 新增配置
const handleAdd = () => {
  isEdit.value = false
  currentSignConfig.value = null
  formDialogVisible.value = true
}

// 编辑配置
const handleEdit = (row: SignConfigResponse) => {
  isEdit.value = true
  currentSignConfig.value = { ...row }
  formDialogVisible.value = true
}

// 编辑奖励
const handleEditReward = (row: SignConfigResponse) => {
  currentSignConfig.value = { ...row }
  rewardDialogVisible.value = true
}

// 删除配置
const handleDelete = async (row: SignConfigResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除该签到配置吗？删除后不可恢复！`,
      '删除确认',
      { type: 'warning' }
    )
    await deleteSignConfig(row.id)
    ElMessage.success('删除成功')
    fetchData()
  } catch {
    // 取消操作
  }
}

// 状态切换
const handleStatusChange = async (row: SignConfigResponse, status: number) => {
  try {
    await updateSignConfigStatus(row.id, { status })
    ElMessage.success('状态更新成功')
    fetchData()
  } catch {
    // 恢复原状态
    fetchData()
  }
}

// 排序更新
const handleSortChange = async (row: SignConfigResponse, sort: number) => {
  try {
    await updateSignConfigSort(row.id, { sort })
    ElMessage.success('排序更新成功')
    fetchData()
  } catch {
    // 恢复原值
    fetchData()
  }
}

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

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

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

.header-actions {
  display: flex;
  gap: 12px;
}
</style>
