{{--
    文件功能：亲密度变更日志审计页

    @author   ChatRoom Laravel
    @version  1.0.0
--}}

@extends('admin.layouts.app')

@section('title', '亲密度日志')

@section('content')

    <div class="flex items-center justify-between mb-5">
        <h2 class="text-xl font-bold text-gray-800">💞 亲密度变更日志</h2>
        <a href="{{ route('admin.marriages.index') }}" class="text-sm text-indigo-600 hover:underline">← 返回总览</a>
    </div>

    {{-- 筛选 --}}
    <form method="GET" class="bg-white rounded-xl border shadow-sm p-4 mb-5 flex flex-wrap gap-3 items-end">
        <div>
            <label class="block text-xs font-bold text-gray-600 mb-1">搜索用户名</label>
            <input type="text" name="search" value="{{ request('search') }}" placeholder="双方任一用户名..."
                class="border rounded-lg px-3 py-2 text-sm w-44 focus:outline-none">
        </div>
        <div>
            <label class="block text-xs font-bold text-gray-600 mb-1">来源（Source）</label>
            <select name="source" class="border rounded-lg px-3 py-2 text-sm focus:outline-none">
                <option value="">全部</option>
                @foreach (\App\Enums\IntimacySource::cases() as $src)
                    <option value="{{ $src->value }}" @selected(request('source') === $src->value)>
                        {{ $src->label() }}
                    </option>
                @endforeach
            </select>
        </div>
        <button type="submit"
            class="bg-indigo-600 text-white px-4 py-2 rounded-lg text-sm font-bold hover:bg-indigo-700 transition">
            筛选
        </button>
        <a href="{{ route('admin.marriages.intimacy-logs') }}" class="text-sm text-gray-500 hover:underline py-2">重置</a>
    </form>

    <div class="bg-white rounded-xl shadow-sm border overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 text-xs uppercase text-gray-600">
                    <tr>
                        <th class="px-4 py-3 text-left">ID</th>
                        <th class="px-4 py-3 text-left">婚姻双方</th>
                        <th class="px-4 py-3 text-center">变动量</th>
                        <th class="px-4 py-3 text-center">变后余额</th>
                        <th class="px-4 py-3 text-left">来源</th>
                        <th class="px-4 py-3 text-left">备注</th>
                        <th class="px-4 py-3 text-left">时间</th>
                    </tr>
                </thead>
                <tbody class="divide-y">
                    @forelse ($logs as $log)
                        <tr class="hover:bg-gray-50">
                            <td class="px-4 py-3 text-gray-400 font-mono text-xs">{{ $log->id }}</td>
                            <td class="px-4 py-3">
                                <span class="font-bold">{{ $log->marriage?->user?->username }}</span>
                                <span class="text-gray-400 mx-1">×</span>
                                <span class="font-bold">{{ $log->marriage?->partner?->username }}</span>
                            </td>
                            <td
                                class="px-4 py-3 text-center font-bold {{ $log->amount >= 0 ? 'text-green-600' : 'text-red-500' }}">
                                {{ $log->amount >= 0 ? '+' : '' }}{{ $log->amount }}
                            </td>
                            <td class="px-4 py-3 text-center text-indigo-600 font-bold">
                                {{ number_format($log->balance_after) }}
                            </td>
                            <td class="px-4 py-3">
                                <span class="text-xs bg-purple-50 text-purple-700 px-2 py-0.5 rounded-full font-mono">
                                    {{ \App\Enums\IntimacySource::tryFrom($log->source)?->label() ?? $log->source }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-500">{{ $log->remark ?? '—' }}</td>
                            <td class="px-4 py-3 text-xs text-gray-400">
                                {{ $log->created_at?->format('m-d H:i') }}
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-4 py-12 text-center text-gray-400">暂无记录</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if ($logs->hasPages())
            <div class="px-4 py-4 border-t">{{ $logs->links() }}</div>
        @endif
    </div>

@endsection
