<template>
  <ElCard shadow="never" class="commission-stats-widget">
    <template #header>
      <div class="card-header">
        <div class="header-left">
          <span class="header-title">佣金资金统计</span>
        </div>
        <div class="header-right" v-if="stats">
          <span class="date-range">{{ startTime }} ~ {{ endTime }}</span>
        </div>
      </div>
    </template>
    <div v-if="stats" class="stats-info">
      <div class="stats-card">
        <div
          class="stats-icon"
          style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%)"
        >
          <i class="el-icon">💰</i>
        </div>
        <div class="stats-content">
          <div class="stats-label">总收入</div>
          <div class="stats-value">{{ formatMoney(stats.total_amount) }}</div>
        </div>
      </div>
      <div class="stats-card">
        <div
          class="stats-icon"
          style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)"
        >
          <i class="el-icon">📊</i>
        </div>
        <div class="stats-content">
          <div class="stats-label">总笔数</div>
          <div class="stats-value">{{ stats.total_count }} 笔</div>
        </div>
      </div>
      <div class="stats-card">
        <div
          class="stats-icon"
          style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)"
        >
          <i class="el-icon">📈</i>
        </div>
        <div class="stats-content">
          <div class="stats-label">成本价差收入</div>
          <div class="stats-value">{{ formatMoney(stats.cost_diff_amount) }}</div>
        </div>
      </div>
      <div class="stats-card">
        <div
          class="stats-icon"
          style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%)"
        >
          <i class="el-icon">⚡</i>
        </div>
        <div class="stats-content">
          <div class="stats-label">一次性佣金收入</div>
          <div class="stats-value">{{ formatMoney(stats.one_time_amount) }}</div>
        </div>
      </div>
    </div>
    <div class="stats-breakdown" v-if="stats">
      <div class="breakdown-item">
        <span class="breakdown-label">成本价差占比</span>
        <span class="breakdown-value">{{ formatPercent(stats.cost_diff_percent) }}</span>
      </div>
      <div class="breakdown-item">
        <span class="breakdown-label">一次性佣金占比</span>
        <span class="breakdown-value">{{ formatPercent(stats.one_time_percent) }}</span>
      </div>
    </div>
    <div v-if="!stats" class="empty-content">
      <i class="el-icon-info" style="font-size: 48px; color: var(--el-text-color-placeholder)"></i>
      <p>暂无佣金统计数据</p>
    </div>
  </ElCard>
</template>

<script setup lang="ts">
  import { CommissionService } from '@/api/modules'
  import type { CommissionStatsResponse } from '@/types/api/commission'
  import { useUserStore } from '@/store/modules/user'
  import { formatMoney } from '@/utils/business/format'
  import { usePermission } from '@/composables/usePermission'

  defineOptions({ name: 'CommissionStatsWidget' })

  const { hasPermission } = usePermission()
  const userStore = useUserStore()
  const currentShopId = computed(() => userStore.info?.shop_id)

  const stats = ref<CommissionStatsResponse | null>(null)
  const startTime = ref('')
  const endTime = ref('')

  const formatPercent = (value: number) => {
    return (value / 10).toFixed(1) + '%'
  }

  const loadCommissionStats = async () => {
    // 检查权限，没有权限不调用接口
    if (!hasPermission('dashboard_analysis:commission_stats')) {
      return
    }

    if (!currentShopId.value) return

    try {
      const res = await CommissionService.getShopCommissionStats(currentShopId.value)
      if (res.code === 0 && res.data) {
        stats.value = res.data
      }
    } catch (error) {
      console.error('获取佣金资金统计失败:', error)
    }
  }

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

<style lang="scss" scoped>
  .commission-stats-widget {
    :deep(.el-card__header) {
      padding: 18px 20px;
      background: var(--el-fill-color-light);
      border-bottom: 1px solid var(--el-border-color-lighter);
    }

    :deep(.el-card__body) {
      padding: 20px;
    }

    .card-header {
      display: flex;
      flex-wrap: wrap;
      gap: 12px;
      align-items: center;
      justify-content: space-between;

      .header-left {
        display: flex;
        gap: 10px;
        align-items: center;

        .header-title {
          font-size: 15px;
          font-weight: 600;
          color: var(--el-text-color-primary);
        }
      }

      .header-right {
        .date-range {
          font-size: 13px;
          color: var(--el-text-color-secondary);
        }
      }
    }

    .stats-info {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 16px;
      margin-bottom: 20px;

      .stats-card {
        display: flex;
        gap: 12px;
        align-items: center;
        padding: 16px;
        background: var(--el-fill-color-blank);
        border-radius: 8px;

        .stats-icon {
          display: flex;
          flex-shrink: 0;
          align-items: center;
          justify-content: center;
          width: 40px;
          height: 40px;
          font-size: 20px;
          border-radius: 8px;
        }

        .stats-content {
          flex: 1;
          min-width: 0;

          .stats-label {
            margin-bottom: 4px;
            font-size: 13px;
            color: var(--el-text-color-secondary);
          }

          .stats-value {
            overflow: hidden;
            font-size: 16px;
            font-weight: 600;
            color: var(--el-text-color-primary);
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
      }
    }

    .stats-breakdown {
      display: flex;
      gap: 24px;
      padding: 16px;
      background: var(--el-fill-color-light);
      border-radius: 8px;

      .breakdown-item {
        display: flex;
        gap: 8px;
        align-items: center;

        .breakdown-label {
          font-size: 13px;
          color: var(--el-text-color-secondary);
        }

        .breakdown-value {
          font-size: 14px;
          font-weight: 600;
          color: var(--el-text-color-primary);
        }
      }
    }

    &.empty-state {
      .empty-content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 40px 20px;
        color: var(--el-text-color-secondary);

        p {
          margin-top: 12px;
          font-size: 14px;
        }
      }
    }
  }

  @media (width <= 768px) {
    .commission-stats-widget {
      .card-header {
        .header-left,
        .header-right {
          width: 100%;
        }
      }

      .stats-info {
        grid-template-columns: 1fr;
        gap: 12px;
      }

      .stats-breakdown {
        flex-direction: column;
        gap: 12px;
      }
    }
  }
</style>
