@extends('layout.main')

@section('title', 'ওয়ার্ড তালিকা')

@section('content')
@php
    $bn = fn($v) => strtr((string)$v, ['0'=>'০','1'=>'১','2'=>'২','3'=>'৩','4'=>'৪','5'=>'৫','6'=>'৬','7'=>'৭','8'=>'৮','9'=>'৯']);
@endphp

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

        <div class="mb-6 flex items-center justify-between gap-3 flex-wrap">
            <div>
                <h1 class="text-2xl font-bold text-gray-900">ওয়ার্ড তালিকা</h1>
                <p class="text-sm text-gray-600">মোট: <span class="font-semibold text-green-700">{{ $bn($wards->total()) }}</span></p>
            </div>

            <a href="{{ route('wards.create') }}"
               class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700">
                নতুন ওয়ার্ড
            </a>
        </div>

        <div class="bg-white p-4 rounded-xl shadow-sm border border-gray-200 mb-6">
            <form method="GET" action="{{ route('wards.index') }}" class="grid grid-cols-1 md:grid-cols-4 gap-3">
                <input type="text" name="search" value="{{ request('search') }}"
                       class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-green-500"
                       placeholder="ওয়ার্ড/জেলা/সিটি/উপজেলা খুঁজুন...">

                <select name="district_id"
                        class="w-full px-4 py-2 border rounded-lg bg-white focus:ring-2 focus:ring-green-500">
                    <option value="">সব জেলা</option>
                    @foreach($districts as $d)
                        <option value="{{ $d->id }}" {{ (string)request('district_id')===(string)$d->id ? 'selected':'' }}>
                            {{ $d->name_bn }}
                        </option>
                    @endforeach
                </select>

                <select name="parent_type"
                        class="w-full px-4 py-2 border rounded-lg bg-white focus:ring-2 focus:ring-green-500">
                    <option value="">সব ধরন</option>
                    <option value="city" {{ request('parent_type')==='city' ? 'selected':'' }}>সিটি/পৌরসভা</option>
                    <option value="upazila" {{ request('parent_type')==='upazila' ? 'selected':'' }}>উপজেলা</option>
                </select>

                <div class="flex gap-2">
                    <button class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700">খুঁজুন</button>
                    @if(request()->hasAny(['search','district_id','parent_type']))
                        <a href="{{ route('wards.index') }}"
                           class="px-4 py-2 border rounded-lg hover:bg-gray-50">রিসেট</a>
                    @endif
                </div>
            </form>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase">ওয়ার্ড</th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase">ধরন</th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase">জেলা</th>
                            <th class="px-6 py-3 text-right text-xs font-semibold text-gray-700 uppercase">অ্যাকশন</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($wards as $w)
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4">
                                    <div class="font-medium text-gray-900">
                                        ওয়ার্ড {{ $bn($w->ward_no) }}
                                    </div>
                                    <div class="text-sm text-gray-600">
                                        {{ $w->name_bn ?: '' }}
                                    </div>
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-800">
                                    @if($w->city_id)
                                        সিটি/পৌরসভা — {{ $w->city?->name_bn }}
                                    @else
                                        উপজেলা — {{ $w->upazila?->name_bn }}
                                    @endif
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-800">
                                    {{ $w->district?->name_bn }}
                                </td>
                                <td class="px-6 py-4 text-right">
                                    <a href="{{ route('wards.edit', $w) }}"
                                       class="text-blue-600 hover:text-blue-800 mr-3">
                                        <i class="fas fa-edit"></i>
                                    </a>

                                    <form action="{{ route('wards.destroy', $w) }}" method="POST" class="inline"
                                          onsubmit="return confirm('আপনি কি নিশ্চিত?')">
                                        @csrf
                                        @method('DELETE')
                                        <button class="text-red-600 hover:text-red-800">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="4" class="px-6 py-10 text-center text-sm text-gray-600">
                                    কোনো ওয়ার্ড পাওয়া যায়নি
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="p-4">
                {{ $wards->links() }}
            </div>
        </div>

    </div>
</div>
@endsection
