@extends('admin.layouts.app')
@section('title', isset($regulation) ? 'Edit Regulation' : 'Add Regulation')
@section('breadcrumb')
    <a href="{{ route('admin.regulations.index') }}">Regulations</a>
    <span class="separator">/</span>
    <span class="current">{{ isset($regulation) ? 'Edit' : 'Add' }}</span>
@endsection
@section('content')
<div class="page-header">
    <h1 class="page-title">
        <span class="title-icon"><i data-lucide="file-text" style="width:20px;height:20px;"></i></span>
        {{ isset($regulation) ? 'Edit Regulation' : 'Add New Regulation' }}
    </h1>
</div>
<div style="max-width:680px;">
    <div class="glass-card" style="padding:24px;">
        @if($errors->any())
            <div class="alert alert-error">
                <i data-lucide="alert-circle" class="alert-icon" style="width:15px;height:15px;"></i>
                <div>@foreach($errors->all() as $err)<div>{{ $err }}</div>@endforeach</div>
            </div>
        @endif
        <form method="POST" action="{{ isset($regulation) ? route('admin.regulations.update', $regulation) : route('admin.regulations.store') }}">
            @csrf
            @if(isset($regulation)) @method('PUT') @endif
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div class="form-group" style="grid-column:1/-1;">
                    <label class="form-label">Regulation Title</label>
                    <input type="text" name="title" value="{{ old('title', $regulation->title ?? '') }}"
                           class="form-control" placeholder="e.g., Maharashtra Unified DCPR 2020" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Type</label>
                    <select name="type" class="form-control" required>
                        @foreach(['National','State','Municipal','CRZ','RERA','Heritage','Other'] as $type)
                            <option value="{{ $type }}" {{ old('type', $regulation->type ?? '') === $type ? 'selected' : '' }}>{{ $type }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">State (leave blank for National)</label>
                    <select name="state_id" class="form-control">
                        <option value="">All India / National</option>
                        @foreach($states as $state)
                            <option value="{{ $state->id }}"
                                {{ old('state_id', $regulation->state_id ?? '') == $state->id ? 'selected' : '' }}>
                                {{ $state->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Issuing Authority</label>
                    <input type="text" name="authority" value="{{ old('authority', $regulation->authority ?? '') }}"
                           class="form-control" placeholder="e.g., MMRDA, MoUD">
                </div>
                <div class="form-group">
                    <label class="form-label">Effective Date</label>
                    <input type="date" name="effective_date"
                           value="{{ old('effective_date', isset($regulation->effective_date) ? $regulation->effective_date->format('Y-m-d') : '') }}"
                           class="form-control">
                </div>
                <div class="form-group" style="grid-column:1/-1;">
                    <label class="form-label">Description</label>
                    <textarea name="description" rows="3" class="form-control"
                              placeholder="Regulation summary...">{{ old('description', $regulation->description ?? '') }}</textarea>
                </div>
                <div class="form-group" style="grid-column:1/-1;">
                    <label class="form-label">Reference URL (document/gazette link)</label>
                    <input type="url" name="reference_url" value="{{ old('reference_url', $regulation->reference_url ?? '') }}"
                           class="form-control" placeholder="https://...">
                </div>
                <div class="form-group" style="grid-column:1/-1;">
                    <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-size:13px;color:var(--vsc-text-primary);">
                        <input type="checkbox" name="is_active" value="1"
                               {{ old('is_active', $regulation->is_active ?? 1) ? 'checked' : '' }}
                               style="accent-color:var(--vsc-blue);">
                        Regulation is Active (not superseded)
                    </label>
                </div>
            </div>
            <div class="divider"></div>
            <div style="display:flex;gap:10px;justify-content:flex-end;">
                <a href="{{ route('admin.regulations.index') }}" class="btn btn-secondary">
                    <i data-lucide="x" style="width:13px;height:13px;"></i> Cancel
                </a>
                <button type="submit" class="btn btn-primary">
                    <i data-lucide="save" style="width:13px;height:13px;"></i>
                    {{ isset($regulation) ? 'Save Changes' : 'Add Regulation' }}
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
