﻿<template>
  <div class="page-container-flex">
    <van-nav-bar title="预算管理" placeholder>
      <template #right>
        <van-icon 
          v-if="activeTab !== BudgetCategory.Savings && uncoveredCategories.length > 0"
          name="warning-o" 
          size="20" 
          color="#ee0a24"
          style="margin-right: 12px"
          @click="showUncoveredDetails = true" 
        />
        <van-icon 
          v-if="activeTab !== BudgetCategory.Savings"
          name="plus" 
          size="20" 
          @click="budgetEditRef.open({ category: activeTab })" 
        />
        <van-icon
          v-else
          name="setting-o" 
          size="20" 
          @click="savingsConfigRef.open()"
        />
      </template>
    </van-nav-bar>

    <van-tabs v-model:active="activeTab" type="card" class="budget-tabs">
      <van-tab title="支出" :name="BudgetCategory.Expense">
        <BudgetSummary 
          v-if="activeTab !== BudgetCategory.Savings"
          v-model:date="selectedDate"
          :stats="overallStats" 
          :title="activeTabTitle" 
          :get-value-class="getValueClass" 
        />
        <van-pull-refresh v-model="isRefreshing" class="scroll-content" @refresh="onRefresh">
          <div class="budget-list">
            <template v-if="expenseBudgets?.length > 0">
              <van-swipe-cell v-for="budget in expenseBudgets" :key="budget.id">
                <BudgetCard 
                  :budget="budget"
                  :progress-color="getProgressColor(budget)"
                  :percent-class="{ 'warning': (budget.current / budget.limit) > 0.8 }"
                  :period-label="getPeriodLabel(budget.type)"
                  @switch-period="(dir) => handleSwitchPeriod(budget, dir)"
                  @click="budgetEditRef.open({
                    data: budget,
                    isEditFlag: true,
                    category: budget.category
                  })"
                >
                  <template #amount-info>
                    <div class="info-item">
                      <div class="label">已支出</div>
                      <div class="value expense">¥{{ formatMoney(budget.current) }}</div>
                    </div>
                    <div class="info-item">
                      <div class="label">预算</div>
                      <div class="value">¥{{ formatMoney(budget.limit) }}</div>
                    </div>
                    <div class="info-item">
                      <div class="label">余额</div>
                      <div class="value" :class="budget.limit - budget.current >= 0 ? 'income' : 'expense'">
                        ¥{{ formatMoney(budget.limit - budget.current) }}
                      </div>
                    </div>
                  </template>
                </BudgetCard>
                <template #right>
                  <van-button square text="删除" type="danger" class="delete-button" @click="handleDelete(budget)" />
                </template>
              </van-swipe-cell>
            </template>
            <van-empty v-else description="暂无支出预算" />
          </div>
          <div style="height: calc(50px + env(safe-area-inset-bottom, 0px))"></div>
        </van-pull-refresh>
      </van-tab>

      <van-tab title="收入" :name="BudgetCategory.Income">
        <BudgetSummary 
          v-if="activeTab !== BudgetCategory.Savings"
          v-model:date="selectedDate"
          :stats="overallStats" 
          :title="activeTabTitle" 
          :get-value-class="getValueClass" 
        />
        <van-pull-refresh v-model="isRefreshing" class="scroll-content" @refresh="onRefresh">
          <div class="budget-list">
            <template v-if="incomeBudgets?.length > 0">
              <van-swipe-cell v-for="budget in incomeBudgets" :key="budget.id">
                <BudgetCard 
                  :budget="budget"
                  :progress-color="getIncomeProgressColor(budget)"
                  :percent-class="{ 'income': (budget.current / budget.limit) >= 1 }"
                  :period-label="getPeriodLabel(budget.type)"
                  @switch-period="(dir) => handleSwitchPeriod(budget, dir)"
                  @click="budgetEditRef.open({
                    data: budget,
                    isEditFlag: true,
                    category: budget.category
                  })"
                >
                  <template #amount-info>
                    <div class="info-item">
                      <div class="label">已收入</div>
                      <div class="value income">¥{{ formatMoney(budget.current) }}</div>
                    </div>
                    <div class="info-item">
                      <div class="label">目标</div>
                      <div class="value">¥{{ formatMoney(budget.limit) }}</div>
                    </div>
                    <div class="info-item">
                      <div class="label">差额</div>
                      <div class="value" :class="budget.current >= budget.limit ? 'income' : 'expense'">
                        ¥{{ formatMoney(Math.abs(budget.limit - budget.current)) }}
                      </div>
                    </div>
                  </template>
                </BudgetCard>
                <template #right>
                  <van-button square text="删除" type="danger" class="delete-button" @click="handleDelete(budget)" />
                </template>
              </van-swipe-cell>
            </template>
            <van-empty v-else description="暂无收入预算" />
          </div>
          <div style="height: calc(50px + env(safe-area-inset-bottom, 0px))"></div>
        </van-pull-refresh>
      </van-tab>

      <van-tab title="存款" :name="BudgetCategory.Savings">
        <van-pull-refresh v-model="isRefreshing" class="scroll-content" style="padding-top:4px" @refresh="onRefresh">
          <div class="budget-list">
            <template v-if="savingsBudgets?.length > 0">
              <BudgetCard 
                v-for="budget in savingsBudgets" 
                :key="budget.id"
                :budget="budget"
                progress-color="#07c160"
                :percent-class="{ 'income': (budget.current / budget.limit) >= 1 }"
                :period-label="getPeriodLabel(budget.type)"
                style="margin: 0 12px 12px;"
                @switch-period="(dir) => handleSwitchPeriod(budget, dir)"
              >
                <template #amount-info>
                  <div class="info-item">
                    <div class="label">已存</div>
                    <div class="value income">¥{{ formatMoney(budget.current) }}</div>
                  </div>
                  <div class="info-item">
                    <div class="label">目标</div>
                    <div class="value">¥{{ formatMoney(budget.limit) }}</div>
                  </div>
                  <div class="info-item">
                    <div class="label">还差</div>
                    <div class="value expense">
                      ¥{{ formatMoney(Math.max(0, budget.limit - budget.current)) }}
                    </div>
                  </div>
                </template>
              </BudgetCard>
            </template>
            <van-empty v-else description="暂无存款计划" />
          </div>
          <div style="height: calc(50px + env(safe-area-inset-bottom, 0px))"></div>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>

    <BudgetEditPopup 
      ref="budgetEditRef"
      @success="fetchBudgetList" 
    />
    <SavingsConfigPopup
      ref="savingsConfigRef"
      @success="fetchBudgetList"
    />

    <PopupContainer
      v-model="showUncoveredDetails"
      title="未覆盖预算的分类"
      :subtitle="`本月共 <b style='color:var(--van-primary-color)'>${uncoveredCategories.length}</b> 个分类未设置预算`"
      height="60%"
    >
      <div class="uncovered-list">
        <div v-for="item in uncoveredCategories" :key="item.category" class="uncovered-item">
          <div class="item-left">
            <div class="category-name">{{ item.category }}</div>
            <div class="transaction-count">{{ item.transactionCount }} 笔记录</div>
          </div>
          <div class="item-right">
            <div class="item-amount" :class="activeTab === BudgetCategory.Expense ? 'expense' : 'income'">
              ¥{{ formatMoney(item.totalAmount) }}
            </div>
          </div>
        </div>
      </div>
      
      <template #footer>
        <van-button block round type="primary" @click="showUncoveredDetails = false">
          我知道了
        </van-button>
      </template>
    </PopupContainer>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { showToast, showConfirmDialog } from 'vant'
import { getBudgetList, deleteBudget, getBudgetStatistics, getCategoryStats, getUncoveredCategories } from '@/api/budget'
import { BudgetPeriodType, BudgetCategory } from '@/constants/enums'
import BudgetCard from '@/components/Budget/BudgetCard.vue'
import BudgetSummary from '@/components/Budget/BudgetSummary.vue'
import BudgetEditPopup from '@/components/Budget/BudgetEditPopup.vue'
import SavingsConfigPopup from '@/components/Budget/SavingsConfigPopup.vue'
import PopupContainer from '@/components/PopupContainer.vue'

const activeTab = ref(BudgetCategory.Expense)
const selectedDate = ref(new Date())
const budgetEditRef = ref(null)
const savingsConfigRef = ref(null)
const isRefreshing = ref(false)
const showUncoveredDetails = ref(false)
const uncoveredCategories = ref([])

const expenseBudgets = ref([])
const incomeBudgets = ref([])
const savingsBudgets = ref([])
const overallStats = ref({
  month: { rate: '0.0', current: 0, limit: 0, count: 0 },
  year: { rate: '0.0', current: 0, limit: 0, count: 0 }
})

const activeTabTitle = computed(() => {
  if (activeTab.value === BudgetCategory.Expense) return '使用'
  return '达成'
})

watch(activeTab, async () => {
  await Promise.all([fetchCategoryStats(), fetchUncoveredCategories()])
})

watch(selectedDate, async () => {
  await Promise.all([
    fetchBudgetList(),
    fetchCategoryStats(),
    fetchUncoveredCategories()
  ])
})

const getValueClass = (rate) => {
  const numRate = parseFloat(rate)
  if (numRate === 0) return ''
  if (activeTab.value === BudgetCategory.Expense) {
    if (numRate >= 100) return 'expense'
    if (numRate >= 80) return 'warning'
    return 'income'
  } else {
    if (numRate >= 100) return 'income'
    if (numRate >= 80) return 'warning'
    return 'expense'
  }
}

const fetchBudgetList = async () => {
  try {
    const res = await getBudgetList(selectedDate.value.toISOString())
    if (res.success) {
      const data = res.data || []
      expenseBudgets.value = data.filter(b => b.category === BudgetCategory.Expense)
      incomeBudgets.value = data.filter(b => b.category === BudgetCategory.Income)
      savingsBudgets.value = data.filter(b => b.category === BudgetCategory.Savings)
    }
  } catch (err) {
    console.error('加载预算列表失败', err)
  }
}

const onRefresh = async () => {
  try {
    await Promise.all([fetchBudgetList(), fetchCategoryStats(), fetchUncoveredCategories()])
  } catch (err) {
    console.error('刷新失败', err)
  } finally {
    isRefreshing.value = false
  }
}

const fetchCategoryStats = async () => {
  try {
    const res = await getCategoryStats(activeTab.value, selectedDate.value.toISOString())
    if (res.success) {
      // 转换后端返回的数据格式为前端需要的格式
      const data = res.data
      overallStats.value = {
        month: {
          rate: data.month?.rate?.toFixed(1) || '0.0',
          current: data.month?.current || 0,
          limit: data.month?.limit || 0,
          count: data.month?.count || 0
        },
        year: {
          rate: data.year?.rate?.toFixed(1) || '0.0',
          current: data.year?.current || 0,
          limit: data.year?.limit || 0,
          count: data.year?.count || 0
        }
      }
    }
  } catch (err) {
    console.error('加载分类统计失败', err)
  }
}

const fetchUncoveredCategories = async () => {
  if (activeTab.value === BudgetCategory.Savings) {
    uncoveredCategories.value = []
    return
  }
  try {
    const res = await getUncoveredCategories(activeTab.value, selectedDate.value.toISOString())
    if (res.success) {
      uncoveredCategories.value = res.data || []
    }
  } catch (err) {
    console.error('获取未覆盖分类失败', err)
  }
}

onMounted(async () => {
  try {
    await Promise.all([
      fetchBudgetList(),
      fetchCategoryStats(),
      fetchUncoveredCategories()
    ])
  } catch (err) {
    console.error('获取初始化数据失败', err)
  }
})

const formatMoney = (val) => {
  return parseFloat(val || 0).toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 })
}

const getPeriodLabel = (type) => {
  const isCurrent = (date) => {
    const now = new Date()
    return date.getFullYear() === now.getFullYear() && date.getMonth() === now.getMonth()
  }
  const isCurrentYear = (date) => {
    const now = new Date()
    return date.getFullYear() === now.getFullYear()
  }

  if (type === BudgetPeriodType.Month) {
    return isCurrent(selectedDate.value) ? '本月' : `${selectedDate.value.getMonth() + 1}月`
  }
  if (type === BudgetPeriodType.Year) {
    return isCurrentYear(selectedDate.value) ? '本年' : `${selectedDate.value.getFullYear()}年`
  }
  return '周期'
}

const getProgressColor = (budget) => {
  const ratio = budget.current / budget.limit
  if (ratio >= 1) return '#ee0a24'
  if (ratio > 0.8) return '#ff976a'
  return '#1989fa'
}

const getIncomeProgressColor = (budget) => {
  const ratio = budget.current / budget.limit
  if (ratio >= 1) return '#07c160'
  return '#1989fa'
}

const refDateMap = {}

const handleSwitchPeriod = async (budget, direction) => {
  let currentRefDate = refDateMap[budget.id] || new Date()
  const date = new Date(currentRefDate)

  if (budget.type === BudgetPeriodType.Month) {
    date.setMonth(date.getMonth() + direction)
  } else if (budget.type === BudgetPeriodType.Year) {
    date.setFullYear(date.getFullYear() + direction)
  }

  try {
    const res = await getBudgetStatistics(budget.id, date.toISOString())
    if (res.success) {
      refDateMap[budget.id] = date
      Object.assign(budget, res.data)
    }
  } catch (err) {
    showToast('加载历史统计失败')
    console.error('加载预算历史统计失败', err)
  }
}

const handleDelete = (budget) => {
  showConfirmDialog({
    title: '确认删除',
    message: `确定要删除预算 "${budget.name}" 吗？`,
  }).then(async () => {
    try {
      const res = await deleteBudget(budget.id)
      if (res.success) {
        showToast('已删除')
        fetchBudgetList()
      }
    } catch (err) {
      showToast('删除失败')
      console.error('删除预算失败', err)
    }
  }).catch(() => {})
}
</script>

<style scoped>
.budget-tabs {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  margin-top: 12px;
}

:deep(.van-tabs__content) {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

:deep(.van-tab__panel) {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.budget-list {
  padding-top: 8px;
  padding-bottom: 20px;
}

.budget-list :deep(.van-swipe-cell) {
  margin: 0 12px 12px;
}

.scroll-content {
  flex: 1;
  overflow-y: auto;
}

.delete-button {
  height: 100%;
}

:deep(.van-tabs__nav--card) {
  margin: 0 12px;
}

.uncovered-list {
  padding: 12px 16px;
}

.uncovered-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background-color: var(--van-background-2, #ffffff);
  border-radius: 12px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

.item-left {
  display: flex;
  flex-direction: column;
}

.category-name {
  font-size: 16px;
  font-weight: 500;
  color: var(--van-text-color, #323233);
  margin-bottom: 4px;
}

.transaction-count {
  font-size: 12px;
  color: var(--van-text-color-2, #969799);
}

.item-right {
  text-align: right;
}

.item-amount {
  font-size: 18px;
  font-weight: 600;
  font-family: DIN Alternate, system-ui;
}
</style>
