@extends('admin.layouts.app')
@section('title', 'Zone Rules')
@section('breadcrumb')
    <span class="current">Zone Rules</span>
@endsection
@section('content')
<div class="page-header" style="display:flex;align-items:center;justify-content:space-between;">
    <div>
        <h1 class="page-title">
            <span class="title-icon"><i data-lucide="sliders" style="width:20px;height:20px;"></i></span>
            Zone Rules
        </h1>
        <p class="page-subtitle">FAR, height, coverage and setback rules per zone / road width</p>
    </div>
    <a href="{{ route('admin.zone-rules.create') }}" class="btn btn-primary">
        <i data-lucide="plus" style="width:14px;height:14px;"></i> Add Rule
    </a>
</div>

@if(session('success'))
    <div class="alert alert-success"><i data-lucide="check-circle" class="alert-icon" style="width:15px;height:15px;"></i>{{ session('success') }}</div>
@endif

<div class="admin-table-wrap">
    <div class="admin-table-header">
        <div class="admin-table-title"><i data-lucide="sliders" class="icon" style="width:14px;height:14px;"></i> All Rules</div>
        <form method="GET" style="display:flex;gap:8px;">
            <select name="zone_id" class="form-control" style="width:140px;">
                <option value="">All Zones</option>
                @foreach($zones as $zone)
                    <option value="{{ $zone->id }}" {{ request('zone_id') == $zone->id ? 'selected' : '' }}>{{ $zone->name }}</option>
                @endforeach
            </select>
            <select name="city_id" class="form-control" style="width:140px;">
                <option value="">All Cities</option>
                @foreach($cities as $city)
                    <option value="{{ $city->id }}" {{ request('city_id') == $city->id ? 'selected' : '' }}>{{ $city->name }}</option>
                @endforeach
            </select>
            <button type="submit" class="btn btn-secondary btn-sm"><i data-lucide="filter" style="width:12px;height:12px;"></i></button>
        </form>
    </div>
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Zone</th>
                    <th>City</th>
                    <th>Road Width (m)</th>
                    <th>FAR</th>
                    <th>Max Height (m)</th>
                    <th>Coverage %</th>
                    <th>Source</th>
                    <th>Status</th>
                    <th style="text-align:right;">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($zoneRules as $rule)
                <tr>
                    <td>
                        <span style="font-weight:500;color:var(--vsc-blue);">{{ $rule->zone?->code ?? '—' }}</span>
                        <br><span style="font-size:11px;color:var(--vsc-text-secondary);">{{ $rule->zone?->state?->code ?? '' }}</span>
                    </td>
                    <td style="font-size:12px;">{{ $rule->city?->name ?? '—' }}</td>
                    <td style="font-size:12px;color:var(--vsc-teal);">
                        @if($rule->road_width_min_m !== null && $rule->road_width_max_m !== null)
                            {{ $rule->road_width_min_m }}–{{ $rule->road_width_max_m }}
                        @elseif($rule->road_width_min_m !== null)
                            ≥ {{ $rule->road_width_min_m }}
                        @else
                            Any
                        @endif
                    </td>
                    <td style="font-weight:600;color:var(--vsc-yellow);">{{ $rule->max_far }}</td>
                    <td style="font-size:12px;">{{ $rule->max_height_m ?? '—' }}</td>
                    <td style="font-size:12px;">{{ $rule->max_ground_coverage ? ($rule->max_ground_coverage * 100).'%' : '—' }}</td>
                    <td style="font-size:11px;color:var(--vsc-text-secondary);">{{ $rule->rule_source ?? '—' }}</td>
                    <td><span class="badge {{ $rule->is_active ? 'badge-green' : 'badge-gray' }}">{{ $rule->is_active ? 'Active' : 'Off' }}</span></td>
                    <td>
                        <div style="display:flex;justify-content:flex-end;gap:4px;">
                            <a href="{{ route('admin.zone-rules.edit', $rule) }}" class="btn btn-icon btn-secondary" data-tooltip="Edit">
                                <i data-lucide="edit-2" style="width:13px;height:13px;"></i>
                            </a>
                            <form method="POST" action="{{ route('admin.zone-rules.destroy', $rule) }}" onsubmit="return confirm('Delete this rule?')">
                                @csrf @method('DELETE')
                                <button type="submit" class="btn btn-icon btn-danger" data-tooltip="Delete">
                                    <i data-lucide="trash-2" style="width:13px;height:13px;"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr><td colspan="9" style="text-align:center;color:var(--vsc-text-secondary);padding:32px;">No zone rules found. <a href="{{ route('admin.zone-rules.create') }}" style="color:var(--vsc-blue);">Add one</a>.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    {{ $zoneRules->links('vendor.pagination.admin') }}
</div>
@endsection
