@props(['mode', 'user', 'administrations', 'selectedMonth'])

<div class="bg-white rounded-[24px] shadow-[0_10px_30px_rgba(15,23,42,0.05)] border border-white overflow-hidden flex flex-col h-full">
    <div class="px-6 py-5 border-b border-gray-100 bg-white flex flex-col sm:flex-row sm:items-center justify-between gap-3">
        <h3 class="text-[17px] font-extrabold text-[#1a2b49] leading-tight">
            @if($mode === 'self')
                Lịch sử nhận / gửi thẻ (Tháng {{ Carbon\Carbon::parse($selectedMonth)->format('m/Y') }})
            @else
                Lịch sử nhận / gửi
            @endif
        </h3>
        @if(!in_array($mode, ['edit']))
            <x-month-filter :mode="$mode" :user="$user" :selectedMonth="$selectedMonth" />
        @endif
    </div>
    
    <div class="p-0 overflow-y-auto max-h-[600px]">
        <table class="min-w-full divide-y divide-gray-100 text-sm">
            <thead class="bg-gray-50/50 sticky top-0">
                <tr>
                    <th class="px-6 py-4 text-left text-xs font-bold text-text-light uppercase tracking-wider">Ngày</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-text-light uppercase tracking-wider">
                        {{ $mode === 'self' ? 'Loại giao dịch' : 'Hành động' }}
                    </th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-text-light uppercase tracking-wider">
                        {{ $mode === 'self' ? 'Đối tác' : 'Đối tượng' }}
                    </th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-text-light uppercase tracking-wider">Số lượng</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-100">
                @forelse($administrations as $admin_record)
                    @php
                        $isReceiver = $admin_record->msnv == $user->msnv && $admin_record->received > 0;
                    @endphp
                    <tr class="hover:bg-slate-50/60 transition-colors">
                        <td class="px-6 py-3.5 whitespace-nowrap text-text-medium font-medium">{{ Carbon\Carbon::parse($admin_record->date)->format('d/m/Y') }}</td>
                        <td class="px-6 py-3.5 whitespace-nowrap">
                            @if($isReceiver)
                                <span class="px-2.5 py-1 inline-flex text-[10px] leading-5 font-bold rounded-full bg-green-50 text-green-700 border border-green-100">
                                    {{ $mode === 'self' ? 'Bạn đã nhận thẻ' : 'Nhận thẻ' }}
                                </span>
                            @else
                                <span class="px-2.5 py-1 inline-flex text-[10px] leading-5 font-bold rounded-full bg-blue-50 text-blue-700 border border-blue-100">
                                    {{ $mode === 'self' ? 'Bạn gửi tặng thẻ' : 'Gửi thẻ' }}
                                </span>
                            @endif
                        </td>
                        <td class="px-6 py-3.5 whitespace-nowrap font-bold text-[#1a2b49]">
                            @if($isReceiver)
                                @if($admin_record->senderUser)
                                    {{ $mode === 'self' ? 'Nhận từ: ' : 'Từ: ' }}<span class="text-[#3462f7]">{{ $admin_record->senderUser->name }}</span>
                                    <div class="text-[11px] text-gray-400 font-medium mt-1.5 leading-none">
                                        MSNV: {{ $admin_record->senderUser->msnv }} • {{ $admin_record->senderUser->mail }}
                                    </div>
                                @else
                                    {{ $mode === 'self' ? 'Nhận từ: ' : 'Từ: ' }}<span class="text-[#3462f7]">{{ $admin_record->sender }}</span>
                                @endif
                            @else
                                @if($admin_record->receiverUser)
                                    {{ $mode === 'self' ? 'Gửi cho: ' : 'Tới: ' }}<span class="text-[#3462f7]">{{ $admin_record->receiverUser->name }}</span>
                                    <div class="text-[11px] text-gray-400 font-medium mt-1.5 leading-none">
                                        MSNV: {{ $admin_record->receiverUser->msnv }} • {{ $admin_record->receiverUser->mail }}
                                    </div>
                                @else
                                    {{ $mode === 'self' ? 'Gửi cho: ' : 'Tới: ' }}<span class="text-[#3462f7]">{{ $admin_record->receiver }}</span>
                                @endif
                            @endif
                        </td>
                        <td class="px-6 py-3.5 whitespace-nowrap font-extrabold text-base flex items-center gap-1.5">
                            @if($isReceiver)
                                <span class="text-green-600">+{{ $admin_record->received }}</span>
                            @else
                                <span class="text-blue-600">-{{ $admin_record->sent }}</span>
                            @endif
                            @php
                                $themes = config('card_themes');
                                $themeId = $admin_record->template_id ?? 1;
                                $theme = $themes[$themeId] ?? $themes[1];
                            @endphp
                            <span class="text-xs font-semibold text-gray-500 flex items-center" title="{{ $theme['name'] }}">
                                <span>{{ $theme['icon'] }}</span>
                            </span>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="4" class="px-6 py-12 text-center text-text-light font-medium text-base">
                            Chưa có dữ liệu
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($administrations->hasPages())
        <div class="px-6 py-4 border-t border-gray-100 bg-white">
            {{ $administrations->links() }}
        </div>
    @endif
</div>
