<form action="{{ $project ? route('admin.projects.update', $project) : route('admin.projects.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    @if($project) @method('PUT') @endif

    <div class="row g-3">
        <div class="col-md-8">
            <label class="form-label">Title <span class="text-danger">*</span></label>
            <input type="text" name="title" class="form-control" value="{{ old('title', $project->title ?? '') }}" required>
        </div>
        <div class="col-md-4">
            <label class="form-label">Category</label>
            <select name="project_category_id" class="form-select">
                <option value="">— None —</option>
                @foreach($categories as $category)
                    <option value="{{ $category->id }}" {{ old('project_category_id', $project->project_category_id ?? '') == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
                @endforeach
            </select>
        </div>
        <div class="col-md-4">
            <label class="form-label">Slug</label>
            <input type="text" name="slug" class="form-control" value="{{ old('slug', $project->slug ?? '') }}" placeholder="Auto-generate">
        </div>
        <div class="col-md-4">
            <label class="form-label">Client</label>
            <input type="text" name="client" class="form-control" value="{{ old('client', $project->client ?? '') }}">
        </div>
        <div class="col-md-4">
            <label class="form-label">Location</label>
            <input type="text" name="location" class="form-control" value="{{ old('location', $project->location ?? '') }}">
        </div>
        <div class="col-md-4">
            <label class="form-label">Project Value</label>
            <input type="text" name="project_value" class="form-control" value="{{ old('project_value', $project->project_value ?? '') }}" placeholder="e.g. SAR 25.8 million">
        </div>
        <div class="col-md-4">
            <label class="form-label">Duration</label>
            <input type="text" name="duration" class="form-control" value="{{ old('duration', $project->duration ?? '') }}" placeholder="e.g. 8 months">
        </div>
        <div class="col-md-2">
            <label class="form-label">Sort Order</label>
            <input type="number" name="sort_order" class="form-control" value="{{ old('sort_order', $project->sort_order ?? 0) }}" min="0">
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <div class="form-check mb-2">
                <input type="checkbox" class="form-check-input" id="status" name="status" value="1" {{ old('status', $project->status ?? true) ? 'checked' : '' }}>
                <label class="form-check-label" for="status">Published</label>
            </div>
        </div>
        <div class="col-12">
            <label class="form-label">Short Description</label>
            <textarea name="short_description" class="form-control" rows="3">{{ old('short_description', $project->short_description ?? '') }}</textarea>
        </div>
        <div class="col-12">
            <label class="form-label">Full Description</label>
            <textarea name="full_description" class="form-control" rows="5">{{ old('full_description', $project->full_description ?? '') }}</textarea>
        </div>
        <div class="col-md-6">
            <label class="form-label">Featured Image</label>
            <input type="file" name="featured_image" class="form-control">
            @if($project && $project->featured_image)
                <img src="{{ \App\Services\FileUploadService::url($project->featured_image) }}" class="img-fluid mt-2" style="max-height:120px">
            @endif
        </div>
    </div>

    <hr>
    <h6 class="fw-bold mb-3">Gallery Images</h6>
    <input type="file" name="gallery[]" class="form-control" multiple accept="image/*">
    <div class="form-text mt-1">Select multiple images to add to the gallery.</div>

    @if($project && $project->images->isNotEmpty())
        <div class="mt-3">
            <h6 class="fw-semibold">Existing Gallery</h6>
            <div class="d-flex flex-wrap gap-2">
                @foreach($project->images as $image)
                    <div class="text-center position-relative">
                        <img src="{{ \App\Services\FileUploadService::url($image->image) }}" class="media-grid-img border rounded" style="width:90px;height:90px;object-fit:cover">
                    </div>
                @endforeach
            </div>
            <div class="form-text mt-2 text-muted">To remove gallery images, delete the project images via the database or re-upload to replace. (Gallery appears under projects/show)</div>
        </div>
    @endif

    <hr>
    <h6 class="fw-bold mb-3">SEO</h6>
    <div class="row g-3">
        <div class="col-12">
            <label class="form-label">Meta Title</label>
            <input type="text" name="meta_title" class="form-control" value="{{ old('meta_title', $project->meta_title ?? '') }}">
        </div>
        <div class="col-12">
            <label class="form-label">Meta Description</label>
            <textarea name="meta_description" class="form-control" rows="2">{{ old('meta_description', $project->meta_description ?? '') }}</textarea>
        </div>
    </div>

    <div class="mt-4">
        <button type="submit" class="btn btn-primary">{{ $project ? 'Update' : 'Create' }} Project</button>
        <a href="{{ route('admin.projects.index') }}" class="btn btn-outline-secondary">Cancel</a>
    </div>
</form>
