<template>
  <view class="home-page">
    <!-- 顶部欢迎区 -->
    <view class="welcome-section">
      <view class="welcome-content">
        <text class="welcome-title">欢迎使用租房管理系统</text>
        <text class="welcome-date">{{currentDate}}</text>
      </view>
      <view class="quick-actions">
        <view class="action-btn" @click="navigateTo('/pages/property-add/property-add')">
          <uni-icons type="plus-filled" size="20" color="#FFFFFF"></uni-icons>
          <text>添加房源</text>
        </view>
        <view class="action-btn secondary" @click="navigateTo('/pages/bills/bills')">
          <uni-icons type="wallet-filled" size="20" color="#2563EB"></uni-icons>
          <text>账单管理</text>
        </view>
      </view>
    </view>

    <!-- 核心KPI卡片区 -->
    <view class="kpi-section">
      <view class="kpi-card kpi-primary" @click="navigateTo('/pages/properties/properties')">
        <view class="kpi-icon">
          <uni-icons type="home-filled" size="28" color="#FFFFFF"></uni-icons>
        </view>
        <view class="kpi-content">
          <text class="kpi-value">{{apartmentCount}}</text>
          <text class="kpi-label">公寓总数</text>
        </view>
      </view>
      <view class="kpi-card kpi-success" @click="navigateTo('/pages/rooms/rooms')">
        <view class="kpi-icon">
          <uni-icons type="shop-filled" size="28" color="#FFFFFF"></uni-icons>
        </view>
        <view class="kpi-content">
          <text class="kpi-value">{{roomCount}}</text>
          <text class="kpi-label">房间总数</text>
        </view>
      </view>
      <view class="kpi-card kpi-warning" @click="navigateTo('/pages/bills/bills')">
        <view class="kpi-icon">
          <uni-icons type="wallet-filled" size="28" color="#FFFFFF"></uni-icons>
        </view>
        <view class="kpi-content">
          <text class="kpi-value">{{formatMoney(monthlyReceivable)}}</text>
          <text class="kpi-label">本月应收</text>
        </view>
      </view>
      <view class="kpi-card kpi-info" @click="navigateTo('/pages/bills/bills')">
        <view class="kpi-icon">
          <uni-icons type="checkbox-filled" size="28" color="#FFFFFF"></uni-icons>
        </view>
        <view class="kpi-content">
          <text class="kpi-value">{{formatMoney(monthlyReceived)}}</text>
          <text class="kpi-label">本月实收</text>
        </view>
      </view>
    </view>

    <!-- 房间状态分布 -->
    <view class="status-section">
      <view class="section-header">
        <text class="section-title">房间状态分布</text>
        <text class="section-subtitle">总房间数: {{roomCount}}</text>
      </view>
      <view class="status-list">
        <view class="status-item" @click="navigateToRoomStatus('empty')">
          <view class="status-info">
            <view class="status-dot dot-empty"></view>
            <text class="status-name">空房</text>
          </view>
          <view class="status-progress">
            <view class="progress-bar">
              <view class="progress-fill" :style="{width: getPercentage(emptyRoomCount) + '%', background: '#909399'}"></view>
            </view>
          </view>
          <text class="status-count">{{emptyRoomCount}}间</text>
        </view>
        <view class="status-item" @click="navigateToRoomStatus('reserved')">
          <view class="status-info">
            <view class="status-dot dot-reserved"></view>
            <text class="status-name">已预订</text>
          </view>
          <view class="status-progress">
            <view class="progress-bar">
              <view class="progress-fill" :style="{width: getPercentage(reservedRoomCount) + '%', background: '#E6A23C'}"></view>
            </view>
          </view>
          <text class="status-count">{{reservedRoomCount}}间</text>
        </view>
        <view class="status-item" @click="navigateToRoomStatus('rented')">
          <view class="status-info">
            <view class="status-dot dot-rented"></view>
            <text class="status-name">在租中</text>
          </view>
          <view class="status-progress">
            <view class="progress-bar">
              <view class="progress-fill" :style="{width: getPercentage(rentedRoomCount) + '%', background: '#67C23A'}"></view>
            </view>
          </view>
          <text class="status-count">{{rentedRoomCount}}间</text>
        </view>
        <view class="status-item" @click="navigateToRoomStatus('soon_expire')">
          <view class="status-info">
            <view class="status-dot dot-soon"></view>
            <text class="status-name">即将到期</text>
          </view>
          <view class="status-progress">
            <view class="progress-bar">
              <view class="progress-fill" :style="{width: getPercentage(soonExpireRoomCount) + '%', background: '#F56C6C'}"></view>
            </view>
          </view>
          <text class="status-count">{{soonExpireRoomCount}}间</text>
        </view>
        <view class="status-item" @click="navigateToRoomStatus('expired')">
          <view class="status-info">
            <view class="status-dot dot-expired"></view>
            <text class="status-name">已到期</text>
          </view>
          <view class="status-progress">
            <view class="progress-bar">
              <view class="progress-fill" :style="{width: getPercentage(expiredRoomCount) + '%', background: '#F56C6C'}"></view>
            </view>
          </view>
          <text class="status-count">{{expiredRoomCount}}间</text>
        </view>
        <view class="status-item">
          <view class="status-info">
            <view class="status-dot dot-rate"></view>
            <text class="status-name">出租率</text>
          </view>
          <view class="status-progress">
            <view class="progress-bar">
              <view class="progress-fill" :style="{width: occupancyRate + '%', background: '#409EFF'}"></view>
            </view>
          </view>
          <text class="status-count">{{occupancyRate}}%</text>
        </view>
      </view>
    </view>

    <!-- 预警提醒区 -->
    <view class="alert-section">
      <view class="alert-card alert-warning" @click="navigateToRentals('soon_expire')">
        <view class="alert-icon">
          <uni-icons type="clock-filled" size="24" color="#E6A23C"></uni-icons>
          <view v-if="soonExpireRoomCount > 0" class="badge">{{soonExpireRoomCount}}</view>
        </view>
        <view class="alert-content">
          <text class="alert-title">即将到期</text>
          <text class="alert-desc">{{soonExpireRoomCount}}个房间租约即将到期</text>
        </view>
        <uni-icons type="arrowright" size="18" color="#C0C4CC"></uni-icons>
      </view>
      <view class="alert-card alert-danger" @click="navigateToRentals('expired')">
        <view class="alert-icon">
          <uni-icons type="info-filled" size="24" color="#F56C6C"></uni-icons>
          <view v-if="expiredRoomCount > 0" class="badge">{{expiredRoomCount}}</view>
        </view>
        <view class="alert-content">
          <text class="alert-title">已到期</text>
          <text class="alert-desc">{{expiredRoomCount}}个房间租约已到期</text>
        </view>
        <uni-icons type="arrowright" size="18" color="#C0C4CC"></uni-icons>
      </view>
      <view class="alert-card alert-info" @click="navigateToBills('unpaid')">
        <view class="alert-icon">
          <uni-icons type="compose" size="24" color="#409EFF"></uni-icons>
          <view v-if="unpaidBillCount > 0" class="badge">{{unpaidBillCount}}</view>
        </view>
        <view class="alert-content">
          <text class="alert-title">未收账单</text>
          <text class="alert-desc">{{unpaidBillCount}}笔账单待收款</text>
        </view>
        <uni-icons type="arrowright" size="18" color="#C0C4CC"></uni-icons>
      </view>
    </view>

    <!-- 公寓分布 -->
    <view class="apartment-section" v-if="apartmentHouseStats.length > 0">
      <view class="section-header">
        <text class="section-title">公寓房间状态分布</text>
        <text class="refresh-btn" @click="refreshData">
          <uni-icons type="refresh" size="14" color="#409EFF"></uni-icons>
          刷新
        </text>
      </view>
      <view class="apartment-list">
        <view v-for="(item, index) in apartmentHouseStats" :key="index" class="apartment-item">
          <view class="apartment-header">
            <text class="apartment-name">{{item.apartment}}</text>
            <text class="apartment-total">共{{item.total}}间</text>
          </view>
          <view class="apartment-stats">
            <view class="stat-tag" :class="{active: item.empty > 0}" @click="navigateToRoomStatusByApartment(item.apartmentId, 'empty')">
              <text>空房 {{item.empty}}</text>
            </view>
            <view class="stat-tag" :class="{active: item.reserved > 0}" @click="navigateToRoomStatusByApartment(item.apartmentId, 'reserved')">
              <text>预订 {{item.reserved}}</text>
            </view>
            <view class="stat-tag success" @click="navigateToRoomStatusByApartment(item.apartmentId, 'rented')">
              <text>在租 {{item.rented}}</text>
            </view>
            <view class="stat-tag warning" :class="{active: item.soon_expire > 0}" @click="navigateToRoomStatusByApartment(item.apartmentId, 'soon_expire')">
              <text>即将到期 {{item.soon_expire}}</text>
            </view>
            <view class="stat-tag danger" :class="{active: item.expired > 0}" @click="navigateToRoomStatusByApartment(item.apartmentId, 'expired')">
              <text>已到期 {{item.expired}}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 快捷功能入口 -->
    <view class="quick-menu">
      <view class="menu-title">快捷功能</view>
      <view class="menu-grid">
        <view class="menu-item" @click="navigateTo('/pages/rental-add/rental-add')">
          <view class="menu-icon" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
            <uni-icons type="plus-filled" size="24" color="#FFFFFF"></uni-icons>
          </view>
          <text>办理入住</text>
        </view>
        <view class="menu-item" @click="navigateTo('/pages/tenants/tenants')">
          <view class="menu-icon" style="background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);">
            <uni-icons type="person-filled" size="24" color="#FFFFFF"></uni-icons>
          </view>
          <text>租客管理</text>
        </view>
        <view class="menu-item" @click="navigateTo('/pages/rentals/rentals')">
          <view class="menu-icon" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
            <uni-icons type="list-filled" size="24" color="#FFFFFF"></uni-icons>
          </view>
          <text>租赁记录</text>
        </view>
        <view class="menu-item" @click="navigateTo('/pages/stats/stats')">
          <view class="menu-icon" style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);">
            <uni-icons type="chart-filled" size="24" color="#FFFFFF"></uni-icons>
          </view>
          <text>收支统计</text>
        </view>
      </view>
    </view>

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

<script>
  import statisticsApi from '@/api/statistics.js'
  import billApi from '@/api/bill.js'
  
  export default {
    data() {
      return {
        currentDate: '',
        apartmentCount: 0,
        roomCount: 0,
        emptyRoomCount: 0,
        reservedRoomCount: 0,
        rentedRoomCount: 0,
        soonExpireRoomCount: 0,
        expiredRoomCount: 0,
        monthlyReceivable: 0,
        monthlyReceived: 0,
        unpaidBillCount: 0,
        apartmentHouseStats: []
      }
    },
    computed: {
      occupancyRate() {
        if (this.roomCount === 0) return 0
        return Math.round((this.rentedRoomCount / this.roomCount) * 100)
      }
    },
    onLoad() {
      this.updateCurrentDate()
      this.loadData()
    },
    onShow() {
      this.loadData()
    },
    onPullDownRefresh() {
      this.loadData().finally(() => {
        uni.stopPullDownRefresh()
      })
    },
    methods: {
      updateCurrentDate() {
        const now = new Date()
        const year = now.getFullYear()
        const month = String(now.getMonth() + 1).padStart(2, '0')
        const day = String(now.getDate()).padStart(2, '0')
        const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
        const weekday = weekdays[now.getDay()]
        this.currentDate = `${year}年${month}月${day}日 ${weekday}`
      },
      
      async loadData() {
        try {
          await Promise.all([
            this.loadDashboardStats(),
            this.loadApartmentStats(),
            this.loadBillStats()
          ])
        } catch (error) {
          console.error('加载数据失败:', error)
        }
      },
      
      async loadDashboardStats() {
        try {
          const res = await statisticsApi.getDashboardStats()
          const data = res.data || res
          this.apartmentCount = data.apartmentCount || 0
          this.roomCount = data.roomCount || 0
          this.emptyRoomCount = data.emptyRoomCount || 0
          this.reservedRoomCount = data.reservedRoomCount || 0
          this.rentedRoomCount = data.rentedRoomCount || 0
          this.soonExpireRoomCount = data.soonExpireRoomCount || 0
          this.expiredRoomCount = data.expiredRoomCount || 0
          this.monthlyReceivable = data.monthlyReceivable || 0
          this.monthlyReceived = data.monthlyReceived || 0
        } catch (error) {
          console.error('加载仪表盘数据失败:', error)
        }
      },
      
      async loadApartmentStats() {
        try {
          const res = await statisticsApi.getApartmentRoomStatusStats()
          this.apartmentHouseStats = res.data || res || []
        } catch (error) {
          console.error('加载公寓统计失败:', error)
        }
      },
      
      async loadBillStats() {
        try {
          const res = await billApi.getStatistics()
          const data = res.data || {}
          const statusList = data.status || []
          this.unpaidBillCount = statusList
            .filter(item => item.status === 'unpaid' || item.status === 'partial')
            .reduce((sum, item) => sum + (item.count || 0), 0)
        } catch (error) {
          console.error('加载账单统计失败:', error)
          this.unpaidBillCount = 0
        }
      },
      
      getPercentage(count) {
        if (this.roomCount === 0) return 0
        return Math.round((count / this.roomCount) * 100)
      },
      
      formatMoney(value) {
        if (!value || value === 0) return '¥0'
        if (value >= 10000) {
          return '¥' + (value / 10000).toFixed(1) + '万'
        }
        return '¥' + value.toLocaleString()
      },
      
      navigateTo(url) {
        uni.navigateTo({ url })
      },
      
      navigateToRoomStatus(status) {
        uni.navigateTo({
          url: `/pages/properties/properties?status=${status}`
        })
      },
      
      navigateToRoomStatusByApartment(apartmentId, status) {
        uni.navigateTo({
          url: `/pages/rooms/rooms?apartmentId=${apartmentId}&status=${status}`
        })
      },
      
      navigateToRentals(subStatus) {
        uni.navigateTo({
          url: `/pages/rentals/rentals?subStatus=${subStatus}`
        })
      },
      
      navigateToBills(status) {
        uni.navigateTo({
          url: `/pages/bills/bills?status=${status}`
        })
      },
      
      refreshData() {
        this.loadData()
        uni.showToast({ title: '数据已刷新', icon: 'success' })
      }
    }
  }
</script>

<style scoped>
  .home-page {
    min-height: 100vh;
    background: #F8FAFC;
    padding-bottom: 40rpx;
  }

  /* 欢迎区 */
  .welcome-section {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 40rpx 32rpx;
    border-radius: 0 0 32rpx 32rpx;
  }

  .welcome-content {
    margin-bottom: 32rpx;
  }

  .welcome-title {
    display: block;
    font-size: 40rpx;
    font-weight: 700;
    color: #FFFFFF;
    margin-bottom: 12rpx;
  }

  .welcome-date {
    display: block;
    font-size: 26rpx;
    color: rgba(255, 255, 255, 0.8);
  }

  .quick-actions {
    display: flex;
    gap: 20rpx;
  }

  .action-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12rpx;
    background: rgba(255, 255, 255, 0.2);
    padding: 24rpx;
    border-radius: 16rpx;
  }

  .action-btn.secondary {
    background: #FFFFFF;
  }

  .action-btn text {
    font-size: 28rpx;
    color: #FFFFFF;
    font-weight: 600;
  }

  .action-btn.secondary text {
    color: #2563EB;
  }

  /* KPI卡片区 */
  .kpi-section {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20rpx;
    padding: 24rpx 32rpx;
    margin-top: -20rpx;
  }

  .kpi-card {
    display: flex;
    align-items: center;
    padding: 28rpx;
    border-radius: 20rpx;
    background: #FFFFFF;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  }

  .kpi-icon {
    width: 72rpx;
    height: 72rpx;
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 20rpx;
  }

  .kpi-primary .kpi-icon {
    background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
  }

  .kpi-success .kpi-icon {
    background: linear-gradient(135deg, #67C23A 0%, #85ce61 100%);
  }

  .kpi-warning .kpi-icon {
    background: linear-gradient(135deg, #E6A23C 0%, #ebb563 100%);
  }

  .kpi-info .kpi-icon {
    background: linear-gradient(135deg, #909399 0%, #a6a9ad 100%);
  }

  .kpi-content {
    flex: 1;
  }

  .kpi-value {
    display: block;
    font-size: 36rpx;
    font-weight: 700;
    color: #1E293B;
    margin-bottom: 8rpx;
  }

  .kpi-label {
    display: block;
    font-size: 24rpx;
    color: #64748B;
  }

  /* 状态分布区 */
  .status-section {
    background: #FFFFFF;
    margin: 0 32rpx 24rpx;
    padding: 28rpx;
    border-radius: 20rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  }

  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24rpx;
  }

  .section-title {
    font-size: 32rpx;
    font-weight: 700;
    color: #1E293B;
  }

  .section-subtitle {
    font-size: 24rpx;
    color: #94A3B8;
  }

  .refresh-btn {
    display: flex;
    align-items: center;
    gap: 8rpx;
    font-size: 26rpx;
    color: #409EFF;
  }

  .status-list {
    display: flex;
    flex-direction: column;
    gap: 20rpx;
  }

  .status-item {
    display: flex;
    align-items: center;
    gap: 16rpx;
  }

  .status-info {
    display: flex;
    align-items: center;
    gap: 12rpx;
    width: 140rpx;
    flex-shrink: 0;
  }

  .status-dot {
    width: 16rpx;
    height: 16rpx;
    border-radius: 50%;
  }

  .dot-empty { background: #909399; }
  .dot-reserved { background: #E6A23C; }
  .dot-rented { background: #67C23A; }
  .dot-soon { background: #F56C6C; }
  .dot-expired { background: #F56C6C; }
  .dot-rate { background: #409EFF; }

  .status-name {
    font-size: 26rpx;
    color: #64748B;
  }

  .status-progress {
    flex: 1;
  }

  .progress-bar {
    height: 12rpx;
    background: #F1F5F9;
    border-radius: 6rpx;
    overflow: hidden;
  }

  .progress-fill {
    height: 100%;
    border-radius: 6rpx;
    transition: width 0.3s ease;
  }

  .status-count {
    width: 100rpx;
    text-align: right;
    font-size: 26rpx;
    font-weight: 600;
    color: #1E293B;
  }

  /* 预警提醒区 */
  .alert-section {
    padding: 0 32rpx;
    margin-bottom: 24rpx;
  }

  .alert-card {
    display: flex;
    align-items: center;
    background: #FFFFFF;
    padding: 28rpx;
    border-radius: 16rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
    border-left: 8rpx solid transparent;
  }

  .alert-warning {
    border-left-color: #E6A23C;
  }

  .alert-danger {
    border-left-color: #F56C6C;
  }

  .alert-info {
    border-left-color: #409EFF;
  }

  .alert-icon {
    position: relative;
    width: 72rpx;
    height: 72rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 16rpx;
    margin-right: 24rpx;
  }

  .alert-warning .alert-icon {
    background: #fdf6ec;
  }

  .alert-danger .alert-icon {
    background: #fef0f0;
  }

  .alert-info .alert-icon {
    background: #f0f9ff;
  }

  .badge {
    position: absolute;
    top: -8rpx;
    right: -8rpx;
    min-width: 32rpx;
    height: 32rpx;
    background: #F56C6C;
    color: #FFFFFF;
    font-size: 20rpx;
    font-weight: 600;
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 8rpx;
  }

  .alert-content {
    flex: 1;
  }

  .alert-title {
    display: block;
    font-size: 30rpx;
    font-weight: 600;
    color: #1E293B;
    margin-bottom: 8rpx;
  }

  .alert-desc {
    display: block;
    font-size: 24rpx;
    color: #909399;
  }

  /* 公寓分布 */
  .apartment-section {
    background: #FFFFFF;
    margin: 0 32rpx 24rpx;
    padding: 28rpx;
    border-radius: 20rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  }

  .apartment-list {
    display: flex;
    flex-direction: column;
    gap: 24rpx;
  }

  .apartment-item {
    padding-bottom: 24rpx;
    border-bottom: 2rpx solid #F1F5F9;
  }

  .apartment-item:last-child {
    padding-bottom: 0;
    border-bottom: none;
  }

  .apartment-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16rpx;
  }

  .apartment-name {
    font-size: 30rpx;
    font-weight: 600;
    color: #1E293B;
  }

  .apartment-total {
    font-size: 24rpx;
    color: #64748B;
  }

  .apartment-stats {
    display: flex;
    flex-wrap: wrap;
    gap: 12rpx;
  }

  .stat-tag {
    padding: 8rpx 16rpx;
    background: #F3F4F6;
    border-radius: 8rpx;
    font-size: 22rpx;
    color: #9CA3AF;
  }

  .stat-tag.active {
    background: #FEF3C7;
    color: #D97706;
  }

  .stat-tag.success {
    background: #D1FAE5;
    color: #059669;
  }

  .stat-tag.warning.active {
    background: #FEF3C7;
    color: #D97706;
  }

  .stat-tag.danger.active {
    background: #FEE2E2;
    color: #DC2626;
  }

  /* 快捷菜单 */
  .quick-menu {
    background: #FFFFFF;
    margin: 0 32rpx;
    padding: 28rpx;
    border-radius: 20rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  }

  .menu-title {
    font-size: 32rpx;
    font-weight: 700;
    color: #1E293B;
    margin-bottom: 24rpx;
  }

  .menu-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20rpx;
  }

  .menu-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12rpx;
  }

  .menu-icon {
    width: 96rpx;
    height: 96rpx;
    border-radius: 24rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .menu-item text {
    font-size: 24rpx;
    color: #64748B;
  }
</style>
