<template>
  <view class="categories-page">
    <view class="custom-nav safe-area-top">
      <view class="nav-content">
        <view class="nav-back" @click="goBack">
          <uni-icons type="left" size="20" color="#1E293B"></uni-icons>
        </view>
        <text class="nav-title">收支类目</text>
        <view class="nav-actions">
          <view class="nav-btn" @click="addCategory">
            <uni-icons type="plus" size="20" color="#2563EB"></uni-icons>
          </view>
        </view>
      </view>
    </view>

    <scroll-view scroll-y class="page-content">
      <!-- 收入类目 -->
      <view class="category-section">
        <view class="section-title">收入类目</view>
        <view class="category-list">
          <view v-for="(item, index) in incomeCategories" :key="index" class="category-item">
            <view class="category-icon income">
              <text>{{item.name.charAt(0)}}</text>
            </view>
            <text class="category-name">{{item.name}}</text>
            <view class="category-actions">
              <uni-icons type="compose" size="18" color="#64748B" @click="editCategory(item)"></uni-icons>
              <uni-icons type="trash" size="18" color="#EF4444" @click="deleteCategory(item)"></uni-icons>
            </view>
          </view>
        </view>
      </view>

      <!-- 支出类目 -->
      <view class="category-section">
        <view class="section-title">支出类目</view>
        <view class="category-list">
          <view v-for="(item, index) in expenseCategories" :key="index" class="category-item">
            <view class="category-icon expense">
              <text>{{item.name.charAt(0)}}</text>
            </view>
            <text class="category-name">{{item.name}}</text>
            <view class="category-actions">
              <uni-icons type="compose" size="18" color="#64748B" @click="editCategory(item)"></uni-icons>
              <uni-icons type="trash" size="18" color="#EF4444" @click="deleteCategory(item)"></uni-icons>
            </view>
          </view>
        </view>
      </view>

      <view class="safe-area-bottom" style="height: 40rpx;"></view>
    </scroll-view>
  </view>
</template>

<script>
  import { settingApi } from '../../api/index.js'

  export default {
    data() {
      return {
        categories: []
      }
    },
    computed: {
      incomeCategories() {
        return this.categories.filter(c => c.type === 'income')
      },
      expenseCategories() {
        return this.categories.filter(c => c.type === 'expense')
      }
    },
    onLoad() {
      this.loadCategories()
    },
    onShow() {
      this.loadCategories()
    },
    methods: {
      async loadCategories() {
        try {
          const res = await settingApi.getCategories()
          this.categories = res.data || [
            { code: 'rent', name: '租金', type: 'income' },
            { code: 'deposit', name: '押金', type: 'income' },
            { code: 'water', name: '水费', type: 'income' },
            { code: 'electricity', name: '电费', type: 'income' },
            { code: 'maintenance', name: '维修费', type: 'expense' },
            { code: 'property', name: '物业费', type: 'expense' },
            { code: 'other', name: '其他', type: 'all' }
          ]
        } catch (error) {
          uni.showToast({ title: '加载失败', icon: 'none' })
        }
      },
      addCategory() {
        uni.showActionSheet({
          itemList: ['添加收入类目', '添加支出类目'],
          success: (res) => {
            const type = res.tapIndex === 0 ? 'income' : 'expense'
            uni.showModal({
              title: res.tapIndex === 0 ? '添加收入类目' : '添加支出类目',
              editable: true,
              placeholderText: '请输入类目名称',
              success: (modalRes) => {
                if (modalRes.confirm && modalRes.content) {
                  this.createCategory({
                    name: modalRes.content,
                    type: type,
                    code: modalRes.content.toLowerCase()
                  })
                }
              }
            })
          }
        })
      },
      async createCategory(data) {
        try {
          uni.showLoading({ title: '保存中...' })
          await settingApi.createCategory(data)
          uni.showToast({ title: '添加成功', icon: 'success' })
          this.loadCategories()
        } catch (error) {
          uni.showToast({ title: '添加失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      editCategory(item) {
        uni.showModal({
          title: '编辑类目',
          content: item.name,
          editable: true,
          success: (res) => {
            if (res.confirm && res.content && res.content !== item.name) {
              this.updateCategory(item.id, { ...item, name: res.content })
            }
          }
        })
      },
      async updateCategory(id, data) {
        try {
          uni.showLoading({ title: '更新中...' })
          await settingApi.updateCategory(id, data)
          uni.showToast({ title: '更新成功', icon: 'success' })
          this.loadCategories()
        } catch (error) {
          uni.showToast({ title: '更新失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      deleteCategory(item) {
        uni.showModal({
          title: '确认删除',
          content: `确定要删除类目"${item.name}"吗？`,
          confirmColor: '#EF4444',
          success: (res) => {
            if (res.confirm) {
              this.doDeleteCategory(item.id)
            }
          }
        })
      },
      async doDeleteCategory(id) {
        try {
          uni.showLoading({ title: '删除中...' })
          await settingApi.deleteCategory(id)
          uni.showToast({ title: '删除成功', icon: 'success' })
          this.loadCategories()
        } catch (error) {
          uni.showToast({ title: '删除失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      goBack() { uni.navigateBack() }
    }
  }
</script>

<style scoped>
  .categories-page { min-height: 100vh; background: #F8FAFC; display: flex; flex-direction: column; }
  .custom-nav { background: #FFFFFF; border-bottom: 2rpx solid #F1F5F9; }
  .nav-content { display: flex; align-items: center; justify-content: space-between; padding: 20rpx 32rpx; }
  .nav-back { width: 60rpx; height: 60rpx; display: flex; align-items: center; justify-content: center; }
  .nav-title { font-size: 36rpx; font-weight: 700; color: #1E293B; }
  .nav-actions { width: 60rpx; height: 60rpx; display: flex; align-items: center; justify-content: center; }
  .page-content { flex: 1; padding: 24rpx 32rpx; }
  .category-section { margin-bottom: 32rpx; }
  .section-title { font-size: 28rpx; font-weight: 600; color: #94A3B8; margin-bottom: 16rpx; padding-left: 16rpx; }
  .category-list { background: #FFFFFF; border-radius: 24rpx; overflow: hidden; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.04); }
  .category-item { display: flex; align-items: center; padding: 28rpx 32rpx; border-bottom: 2rpx solid #F8FAFC; }
  .category-item:last-child { border-bottom: none; }
  .category-icon { width: 64rpx; height: 64rpx; border-radius: 16rpx; display: flex; align-items: center; justify-content: center; margin-right: 24rpx; }
  .category-icon.income { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: #FFFFFF; font-size: 28rpx; font-weight: 600; }
  .category-icon.expense { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: #FFFFFF; font-size: 28rpx; font-weight: 600; }
  .category-name { flex: 1; font-size: 30rpx; color: #1E293B; }
  .category-actions { display: flex; gap: 32rpx; }
</style>
