{{--
    文件功能：后台婚姻列表页（支持状态筛选/用户名搜索）

    @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>
            <select name="status" class="border rounded-lg px-3 py-2 text-sm focus:outline-none">
                <option value="">全部</option>
                @foreach (['married' => '已婚', 'pending' => '求婚中', 'divorced' => '已离婚', 'rejected' => '被拒', 'expired' => '已过期'] as $val => $label)
                    <option value="{{ $val }}" @selected(request('status') === $val)>{{ $label }}</option>
                @endforeach
            </select>
        </div>
        <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-48 focus:outline-none">
        </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.list') }}" 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-gray-600 text-xs uppercase">
                    <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-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-center">结婚天数</th>
                        <th class="px-4 py-3 text-left">时间</th>
                        <th class="px-4 py-3 text-center">操作</th>
                    </tr>
                </thead>
                <tbody class="divide-y">
                    @forelse ($marriages as $m)
                        <tr class="hover:bg-gray-50 transition">
                            <td class="px-4 py-3 text-gray-400 font-mono text-xs">{{ $m->id }}</td>
                            <td class="px-4 py-3">
                                <div class="font-bold">{{ $m->user?->username }}</div>
                                <div class="text-gray-400 text-xs">× {{ $m->partner?->username }}</div>
                            </td>
                            <td class="px-4 py-3">
                                {{ $m->ringItem?->icon }} {{ $m->ringItem?->name ?? '—' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span
                                    class="px-2 py-0.5 rounded-full text-xs font-bold
                                    {{ match ($m->status) {
                                        'married' => 'bg-green-100 text-green-700',
                                        'pending' => 'bg-amber-100 text-amber-700',
                                        'divorced' => 'bg-red-100 text-red-700',
                                        'rejected' => 'bg-gray-100 text-gray-600',
                                        'expired' => 'bg-slate-100 text-slate-500',
                                        default => 'bg-gray-100 text-gray-600',
                                    } }}">
                                    {{ ['married' => '💑 已婚', 'pending' => '💍 求婚中', 'divorced' => '💔 已离婚', 'rejected' => '❌ 被拒', 'expired' => '⏰ 已过期'][$m->status] ?? $m->status }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-center">
                                <div class="font-bold text-indigo-600">{{ number_format($m->intimacy) }}</div>
                                <div class="text-xs text-gray-400">
                                    {{ \App\Services\MarriageIntimacyService::levelIcon($m->level) }}
                                    Lv{{ $m->level }}</div>
                            </td>
                            <td class="px-4 py-3 text-center text-gray-600">
                                {{ $m->married_at ? $m->married_at->diffInDays(now()) . ' 天' : '—' }}
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-500">
                                @if ($m->married_at)
                                    结婚：{{ $m->married_at->format('Y-m-d') }}
                                @elseif ($m->proposed_at)
                                    求婚：{{ $m->proposed_at->format('Y-m-d') }}
                                @endif
                                @if ($m->divorced_at)
                                    <div>离婚：{{ $m->divorced_at->format('Y-m-d') }}</div>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center" x-data="{ showDissolve: false }">
                                @if ($m->status === 'married')
                                    <button @click="showDissolve = true"
                                        class="text-xs bg-red-50 text-red-600 font-bold px-3 py-1.5 rounded hover:bg-red-600 hover:text-white transition">
                                        强制离婚
                                    </button>
                                    {{-- 强制离婚确认弹窗 --}}
                                    <div x-show="showDissolve" style="display:none"
                                        class="fixed inset-0 z-50 bg-black/60 flex items-center justify-center p-4">
                                        <div @click.away="showDissolve = false"
                                            class="bg-white rounded-xl shadow-2xl w-full max-w-sm p-6 text-left">
                                            <h4 class="font-bold text-red-600 mb-3">⚠️ 强制解除婚姻</h4>
                                            <p class="text-sm text-gray-600 mb-4">
                                                确认解除 <strong>{{ $m->user?->username }}</strong> 与
                                                <strong>{{ $m->partner?->username }}</strong> 的婚姻？<br>
                                                <span class="text-red-500 text-xs">管理员强制离婚不扣魅力、不转移财产。</span>
                                            </p>
                                            <form action="{{ route('admin.marriages.force-dissolve', $m->id) }}"
                                                method="POST">
                                                @csrf
                                                <input type="text" name="admin_note" required placeholder="请填写操作原因（必填）"
                                                    class="w-full border rounded px-3 py-2 text-sm mb-3">
                                                <div class="flex gap-2 justify-end">
                                                    <button type="button" @click="showDissolve = false"
                                                        class="px-3 py-1.5 border rounded text-sm text-gray-600">取消</button>
                                                    <button type="submit"
                                                        class="px-3 py-1.5 bg-red-600 text-white rounded text-sm font-bold">确认解除</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                @elseif ($m->status === 'pending')
                                    <form action="{{ route('admin.marriages.cancel-proposal', $m->id) }}" method="POST"
                                        onsubmit="return confirm('确定取消该求婚吗？')">
                                        @csrf
                                        <button type="submit"
                                            class="text-xs bg-amber-50 text-amber-600 font-bold px-3 py-1.5 rounded hover:bg-amber-600 hover:text-white transition">
                                            取消求婚
                                        </button>
                                    </form>
                                @else
                                    <span class="text-xs text-gray-300">—</span>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="px-4 py-12 text-center text-gray-400">暂无记录</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- 分页 --}}
        @if ($marriages->hasPages())
            <div class="px-4 py-4 border-t">{{ $marriages->links() }}</div>
        @endif
    </div>

@endsection
