@extends('layout.main')

@section('title', 'টিম তালিকা')

@section('content')
    <div class="min-h-screen bg-gray-50 p-4 sm:p-6">
        <div class="max-w-7xl mx-auto">

            {{-- Header --}}
            <div class="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-4 mb-6">
                <div>
                    <h1 class="text-2xl sm:text-3xl font-bold text-gray-900 mb-1">টিম তালিকা</h1>
                    <p class="text-gray-600 text-sm">
                        মোট টিম: <span class="font-semibold text-green-700">{{ $teams->total() }}</span>

                        @if (request('search'))
                            <span class="text-green-600"> (সার্চ: "{{ request('search') }}")</span>
                        @endif
                    </p>
                </div>

                <div class="flex items-center gap-3">
                    <a href="{{ route('teams.create') }}"
                        class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition flex items-center gap-2">
                        <i class="fas fa-plus"></i>
                        নতুন টিম
                    </a>
                </div>
            </div>

            {{-- Success --}}
            @if (session('success'))
                <div class="mb-4 p-4 rounded-lg bg-green-100 text-green-800">
                    {{ session('success') }}
                </div>
            @endif

            {{-- Search + Filter --}}
            <div class="bg-white p-4 rounded-xl shadow-sm border border-gray-200 mb-6">
                <form method="GET" action="{{ route('teams.index') }}" class="space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">

                        {{-- Search --}}
                        <div>
                            <input type="text" name="search" value="{{ request('search') }}"
                                placeholder="টিম / প্রার্থী / ম্যানেজার নাম লিখুন..."
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500">
                        </div>

                        {{-- Candidate Filter --}}
                        <div>
                            <select name="candidate_id"
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-white focus:ring-2 focus:ring-green-500 focus:border-green-500">
                                <option value="">সব প্রার্থী</option>
                                @foreach ($candidates as $candidate)
                                    <option value="{{ $candidate->id }}"
                                        {{ (string) request('candidate_id') === (string) $candidate->id ? 'selected' : '' }}>
                                        {{ $candidate->user?->name ?? 'N/A' }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        {{-- Status Filter --}}
                        <div>
                            <select name="status"
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-white focus:ring-2 focus:ring-green-500 focus:border-green-500">
                                <option value="">সব স্ট্যাটাস</option>
                                <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>সক্রিয়
                                </option>
                                <option value="inactive" {{ request('status') === 'inactive' ? 'selected' : '' }}>
                                    নিষ্ক্রিয়</option>
                            </select>
                        </div>
                    </div>

                    <div class="flex justify-end gap-2">
                        <button type="submit"
                            class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition">
                            খুঁজুন
                        </button>

                        @if (request()->hasAny(['search', 'candidate_id', 'status']))
                            <a href="{{ route('teams.index') }}"
                                class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition">
                                রিসেট
                            </a>
                        @endif
                    </div>
                </form>
            </div>

            {{-- List --}}
            {{-- List --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                @php
                    // If current user is the candidate of ALL listed teams, then hide candidate column.
                    // Otherwise show it.
                    $authId = auth()->id();
                    $showCandidateColumn = $teams->isNotEmpty()
                        ? $teams->contains(function ($t) use ($authId) {
                            return (int) optional($t->candidate)->user_id !== (int) $authId;
                        })
                        : true; // doesn't matter when empty, but keeps logic simple
                @endphp

                <table class="w-full">
                    <thead class="bg-gray-100">
                        <tr class="text-left text-sm text-gray-700">
                            <th class="px-4 py-3">#</th>
                            <th class="px-4 py-3">টিম নাম</th>

                            @if ($showCandidateColumn)
                                <th class="px-4 py-3">প্রার্থী</th>
                            @endif

                            <th class="px-4 py-3">ম্যানেজার</th>
                            <th class="px-4 py-3">ফোন</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-right">একশন</th>
                        </tr>
                    </thead>

                    <tbody class="divide-y">
                        @forelse($teams as $team)
                            @php
                                $phone = $team->manager?->phone;
                                $dial = $phone ? preg_replace('/[^0-9+]/', '', $phone) : null; // keep + and digits only
                            @endphp

                            <tr class="hover:bg-gray-50 text-sm">
                                <td class="px-4 py-3">
                                    {{ $loop->iteration + ($teams->currentPage() - 1) * $teams->perPage() }}
                                </td>

                                <td class="px-4 py-3 font-semibold text-gray-900">
                                    {{ $team->name }}
                                </td>

                                @if ($showCandidateColumn)
                                    <td class="px-4 py-3">
                                        {{ $team->candidate?->user?->name ?? '—' }}
                                    </td>
                                @endif

                                <td class="px-4 py-3">
                                    {{ $team->manager?->name ?? '—' }}
                                </td>

                                {{-- ✅ Phone (dialable + same UI pill style) --}}
                                <td class="px-4 py-3">
                                    @if ($dial)
                                        <a href="tel:{{ $dial }}"
                                            class="inline-flex items-center gap-2 px-3 py-1.5 rounded-full
                                      bg-blue-50 text-blue-700 font-semibold
                                      ring-1 ring-blue-100
                                      hover:bg-blue-100 hover:text-blue-800
                                      transition"
                                            title="কল করতে ক্লিক করুন">
                                            <i class="fas fa-phone-alt text-sm"></i>
                                            <span class="tracking-wide">{{ $phone }}</span>
                                        </a>
                                    @else
                                        <span class="text-gray-400">—</span>
                                    @endif
                                </td>

                                <td class="px-4 py-3 text-center">
                                    <span class="px-2 py-1 rounded-full bg-blue-100 text-blue-800 text-xs">
                                        {{ $team->members_count }}
                                    </span>
                                </td>

                                <td class="px-4 py-3 text-center">
                                    @if ($team->status === 'active')
                                        <span
                                            class="px-2 py-1 rounded-full bg-green-100 text-green-800 text-xs">সক্রিয়</span>
                                    @else
                                        <span
                                            class="px-2 py-1 rounded-full bg-gray-200 text-gray-700 text-xs">নিষ্ক্রিয়</span>
                                    @endif
                                </td>

                                <td class="px-4 py-3 text-right">
                                    <div class="flex justify-end gap-2">
                                        <a href="{{ route('teams.show', $team) }}"
                                            class="px-3 py-1 text-xs bg-blue-600 text-white rounded hover:bg-blue-700">
                                            দেখুন
                                        </a>

                                        <a href="{{ route('teams.edit', $team) }}"
                                            class="px-3 py-1 text-xs bg-yellow-500 text-white rounded hover:bg-yellow-600">
                                            এডিট
                                        </a>

                                        <form action="{{ route('teams.destroy', $team) }}" method="POST"
                                            onsubmit="return confirm('আপনি কি নিশ্চিত?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit"
                                                class="px-3 py-1 text-xs bg-red-600 text-white rounded hover:bg-red-700">
                                                ডিলিট
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="{{ $showCandidateColumn ? 8 : 7 }}"
                                    class="px-4 py-8 text-center text-gray-500">
                                    কোনো টিম পাওয়া যায়নি
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>


            {{-- Pagination --}}
            <div class="mt-6">
                {{ $teams->links() }}
            </div>

        </div>
    </div>
@endsection
