{{--
    文件功能：后台积分活动统计页面
    显示指定日期下各来源活动（钓鱼、存点等）产出的经验/金币/魅力统计，以及今日净流通量

    @extends admin/layouts
--}}
@extends('admin.layouts.app')

@section('title', '积分流水统计')

@section('content')
    @php require resource_path('views/admin/partials/list-theme.php'); @endphp

    <div class="{{ $adminListPageClass }}">
        <div class="{{ $adminListHeaderCardClass }}">
            <div class="flex flex-col gap-4 xl:flex-row xl:items-center xl:justify-between">
                <div>
                    <h1 class="{{ $adminListHeaderTitleClass }}">📊 积分流水活动统计</h1>
                    <p class="{{ $adminListHeaderSubtitleClass }}">查看指定日期下各活动来源的经验、金币、魅力产出，以及当日净流通量。</p>
                </div>
                <form method="GET" action="{{ route('admin.currency-stats.index') }}"
                    class="flex flex-wrap items-end gap-3">
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">查询日期</label>
                        <input type="date" name="date" value="{{ $date }}"
                            class="{{ $adminListFilterInputClass }}">
                    </div>
                    <button type="submit" class="{{ $adminListPrimaryButtonClass }}">查询</button>
                </form>
            </div>
        </div>

        {{-- 净流通量摘要卡片 --}}
        <div class="grid gap-4 md:grid-cols-3">
            @foreach (['exp' => ['label' => '经验', 'icon' => '⚡', 'color' => 'amber'], 'gold' => ['label' => '金币', 'icon' => '💰', 'color' => 'yellow'], 'charm' => ['label' => '魅力', 'icon' => '🌸', 'color' => 'pink']] as $cur => $info)
                @php $flow = $netFlow[$cur] ?? ['in'=>0,'out'=>0,'net'=>0]; @endphp
                <div class="{{ $adminListCardClass }} p-5">
                    <div class="flex items-center gap-2 mb-3">
                        <span class="text-2xl">{{ $info['icon'] }}</span>
                        <span class="font-semibold text-gray-700">{{ $info['label'] }} 流通</span>
                    </div>
                    <div class="flex justify-between text-sm">
                        <span class="text-green-600">+{{ number_format($flow['in']) }} 流入</span>
                        <span class="text-red-500">-{{ number_format($flow['out']) }} 消耗</span>
                    </div>
                    <div class="mt-2 text-lg font-bold {{ $flow['net'] >= 0 ? 'text-green-700' : 'text-red-600' }}">
                        净增：{{ $flow['net'] >= 0 ? '+' : '' }}{{ number_format($flow['net']) }}
                    </div>
                </div>
        @endforeach
        </div>

        {{-- 来源活动详细统计表 --}}
        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }}">
                <h2 class="{{ $adminListSectionTitleClass }}">各活动来源产出明细</h2>
                <p class="{{ $adminListSectionDescClass }}">日期：{{ $date }}（仅统计正向增加，不含消耗）</p>
            </div>
            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }}">
                    <thead class="{{ $adminListTableHeadRowClass }}">
                    <tr>
                        <th class="{{ $adminListTableHeadCellClass }}">来源活动</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-right">⚡ 经验产出</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-right">💰 金币产出</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-right">🌸 魅力产出</th>
                        <th class="{{ $adminListTableHeadCellClass }} text-right">参与人次</th>
                    </tr>
                </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                    @foreach ($allSources as $source)
                        @php
                            $expRow = $statsByType['exp'][$source->value] ?? null;
                            $goldRow = $statsByType['gold'][$source->value] ?? null;
                            $charmRow = $statsByType['charm'][$source->value] ?? null;
                            $hasData = $expRow || $goldRow || $charmRow;
                            $maxParticipants = max(
                                $expRow?->participant_count ?? 0,
                                $goldRow?->participant_count ?? 0,
                                $charmRow?->participant_count ?? 0,
                            );
                        @endphp
                        <tr class="{{ $adminListTableRowClass }} {{ $hasData ? '' : 'opacity-40' }}">
                            <td class="px-4 py-3">
                                <span class="{{ $adminListPrimaryTextClass }}">{{ $source->label() }}</span>
                            </td>
                            <td
                                class="px-4 py-3 text-right {{ $adminListNumericTextClass }} {{ $expRow?->total_amount > 0 ? 'text-amber-600 font-semibold' : 'text-gray-300' }}">
                                {{ $expRow ? number_format($expRow->total_amount) : '—' }}
                            </td>
                            <td
                                class="px-4 py-3 text-right {{ $adminListNumericTextClass }} {{ $goldRow?->total_amount > 0 ? 'text-yellow-600 font-semibold' : 'text-gray-300' }}">
                                {{ $goldRow ? number_format($goldRow->total_amount) : '—' }}
                            </td>
                            <td
                                class="px-4 py-3 text-right {{ $adminListNumericTextClass }} {{ $charmRow?->total_amount > 0 ? 'text-pink-600 font-semibold' : 'text-gray-300' }}">
                                {{ $charmRow ? number_format($charmRow->total_amount) : '—' }}
                            </td>
                            <td class="px-4 py-3 text-right {{ $adminListBodyTextClass }}">
                                {{ $maxParticipants > 0 ? $maxParticipants . ' 人' : '—' }}
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
