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

    @author   ChatRoom Laravel
    @version  1.0.0
--}}

@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 sm:flex-row sm:items-center sm:justify-between">
                <div>
                    <h2 class="{{ $adminListHeaderTitleClass }}">📋 婚姻列表</h2>
                    <p class="{{ $adminListHeaderSubtitleClass }}">统一查看婚姻状态、亲密度与后台管理操作。</p>
                </div>
                <a href="{{ route('admin.marriages.index') }}" class="{{ $adminListSecondaryButtonClass }}">返回总览</a>
            </div>
        </div>

        <div class="{{ $adminListFilterCardClass }}">
            <div class="{{ $adminListFilterInnerClass }}">
                <form method="GET" class="{{ $adminListFilterFormClass }}">
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">状态</label>
                        <select name="status" class="{{ $adminListFilterInputClass }}">
                            <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="{{ $adminListFilterLabelClass }}">搜索用户名</label>
                        <input type="text" name="search" value="{{ request('search') }}" placeholder="双方任一用户名..."
                            class="w-48 {{ $adminListFilterInputClass }}">
                    </div>
                    <button type="submit" class="{{ $adminListPrimaryButtonClass }}">筛选</button>
                    <a href="{{ route('admin.marriages.list') }}" class="{{ $adminListSecondaryButtonClass }}">重置</a>
                </form>
            </div>
        </div>

        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }}">
                <div>
                    <h3 class="{{ $adminListSectionTitleClass }}">婚姻记录</h3>
                    <p class="{{ $adminListSectionDescClass }}">保留婚姻状态语义色，其他列表结构按统一后台样式展示。</p>
                </div>
            </div>
            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }}">
                    <thead>
                        <tr class="{{ $adminListTableHeadRowClass }}">
                            <th class="{{ $adminListTableHeadCellClass }}">ID</th>
                            <th class="{{ $adminListTableHeadCellClass }}">双方</th>
                            <th class="{{ $adminListTableHeadCellClass }}">戒指</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">状态</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">亲密度/等级</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">结婚天数</th>
                            <th class="{{ $adminListTableHeadCellClass }}">时间</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">操作</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                        @forelse ($marriages as $m)
                            <tr class="{{ $adminListTableRowClass }}">
                                <td class="px-4 py-3 {{ $adminListSecondaryTextClass }} {{ $adminListNumericTextClass }}">{{ $m->id }}</td>
                                <td class="px-4 py-3">
                                    <div class="{{ $adminListPrimaryTextClass }}">{{ $m->user?->username }}</div>
                                    <div class="{{ $adminListSecondaryTextClass }}">× {{ $m->partner?->username }}</div>
                                </td>
                                <td class="px-4 py-3 {{ $adminListBodyTextClass }}">
                                    {{ $m->ringItem?->icon }} {{ $m->ringItem?->name ?? '—' }}
                                </td>
                                <td class="px-4 py-3 text-center">
                                    <span
                                        class="{{ $adminListBadgeBaseClass }} {{ match ($m->status) {
                                            'married' => 'border-green-200 bg-green-100 text-green-700',
                                            'pending' => 'border-amber-200 bg-amber-100 text-amber-700',
                                            'divorced' => 'border-red-200 bg-red-100 text-red-700',
                                            'rejected' => 'border-gray-200 bg-gray-100 text-gray-600',
                                            'expired' => 'border-slate-200 bg-slate-100 text-slate-500',
                                            default => 'border-gray-200 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="text-sm font-semibold text-indigo-600">{{ number_format($m->intimacy) }}</div>
                                    <div class="{{ $adminListSecondaryTextClass }}">
                                        {{ \App\Services\MarriageIntimacyService::levelIcon($m->level) }}
                                        Lv{{ $m->level }}</div>
                                </td>
                                <td class="px-4 py-3 text-center {{ $adminListBodyTextClass }}">
                                    {{ $m->married_at ? $m->married_at->diffInDays(now()) . ' 天' : '—' }}
                                </td>
                                <td class="px-4 py-3 {{ $adminListSecondaryTextClass }}">
                                    @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="{{ $adminListActionButtonClass }} bg-red-50 text-red-600 hover:bg-red-600 hover:text-white">
                                        强制离婚
                                    </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="{{ $adminListSecondaryButtonClass }}">取消</button>
                                                    <button type="submit"
                                                        class="{{ $adminListActionButtonClass }} bg-red-600 text-white hover:bg-red-700">确认解除</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                @elseif ($m->status === 'pending')
                                    <form action="{{ route('admin.marriages.cancel-proposal', $m->id) }}" method="POST"
                                        data-admin-confirm="确定取消该求婚吗？">
                                        @csrf
                                        <button type="submit"
                                            class="{{ $adminListActionButtonClass }} bg-amber-50 text-amber-600 hover:bg-amber-600 hover:text-white">
                                            取消求婚
                                        </button>
                                    </form>
                                @else
                                    <span class="text-xs text-gray-300">—</span>
                                @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="{{ $adminListEmptyClass }}">暂无记录</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if ($marriages->hasPages())
                <div class="{{ $adminListPaginationClass }}">{{ $marriages->links() }}</div>
            @endif
        </div>
    </div>

@endsection
