@extends('layouts.admin')
@section('title', 'Edit Product: ' . $product->name)
@section('header_title', 'Edit Product')

@section('content')
<div class="mb-6 flex items-center justify-between">
    <a href="{{ route('admin.products.index') }}" class="flex items-center space-x-2 text-sm font-bold text-gray-500 hover:text-brand-red transition-colors">
        <i data-lucide="arrow-left" class="w-4 h-4"></i><span>Back to Products</span>
    </a>
</div>

<form action="{{ route('admin.products.update', $product->id) }}" method="POST" enctype="multipart/form-data" class="bg-white rounded-lg border border-gray-200 shadow-sm p-6 lg:p-10 max-w-4xl relative overflow-hidden">
    @csrf
    @method('PUT')
    
    <div class="absolute top-0 right-0 w-32 h-32 bg-gray-50 rounded-bl-full -mr-16 -mt-16 z-0"></div>

    <div class="relative z-10 flex items-center justify-between border-b border-gray-100 pb-4 mb-8">
        <h2 class="text-2xl font-extrabold text-brand-dark uppercase tracking-wider">Update Details</h2>
        <span class="text-xs font-bold text-gray-400 font-mono">ID: #{{ $product->id }}</span>
    </div>

    @if($errors->any())
        <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded mb-6 text-sm relative z-10">
            <ul class="list-disc pl-5 space-y-1">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-8 relative z-10">
        {{-- Basic Info --}}
        <div class="space-y-6">
            <div>
                <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Product Name *</label>
                <input type="text" name="name" value="{{ old('name', $product->name) }}" required class="w-full border border-gray-200 rounded px-4 py-3 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red">
            </div>
            
            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Category *</label>
                    <select name="category" required class="w-full border border-gray-200 rounded px-4 py-3 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red bg-white">
                        <option value="">Select Category</option>
                        @foreach(['Engine Parts', 'Body Parts', 'Interior Accessories', 'Lighting', 'Wheels & Tires', 'Brakes & Rotors', 'Maintenance', 'Electrical'] as $cat)
                            <option value="{{ $cat }}" {{ old('category', $product->category) == $cat ? 'selected' : '' }}>{{ $cat }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Brand *</label>
                    <input type="text" name="brand" value="{{ old('brand', $product->brand) }}" required class="w-full border border-gray-200 rounded px-4 py-3 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red">
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Current Price ($) *</label>
                    <input type="number" step="0.01" name="price" value="{{ old('price', $product->price) }}" required class="w-full border border-gray-200 rounded px-4 py-3 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red font-mono">
                </div>
                <div>
                    <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Old Price ($)</label>
                    <input type="number" step="0.01" name="old_price" value="{{ old('old_price', $product->old_price) }}" class="w-full border border-gray-200 rounded px-4 py-3 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red font-mono">
                </div>
            </div>

            <div>
                <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Stock Quantity *</label>
                <input type="number" name="stock" value="{{ old('stock', $product->stock) }}" required class="w-full border border-gray-200 rounded px-4 py-3 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red font-mono">
            </div>

            <div class="flex items-center bg-gray-50 border border-gray-200 p-4 rounded">
                <input type="checkbox" name="is_new" id="is_new" value="1" {{ old('is_new', $product->is_new) ? 'checked' : '' }} class="w-5 h-5 accent-brand-red cursor-pointer">
                <label for="is_new" class="ml-3 text-sm font-bold text-gray-700 uppercase tracking-wide cursor-pointer">Mark as "New Arrival"</label>
            </div>
        </div>

        {{-- Description & Media --}}
        <div class="space-y-6">
            <div>
                <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Description</label>
                <textarea name="description" rows="5" class="w-full border border-gray-200 rounded px-4 py-3 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red">{{ old('description', $product->description) }}</textarea>
            </div>

            <div>
                <label class="block text-sm font-bold text-gray-700 uppercase tracking-wide mb-2">Product Image</label>
                
                @if($product->image_path)
                <div class="mb-4 flex items-center space-x-4 p-4 border border-gray-200 rounded bg-gray-50">
                    <img src="{{ asset('images/' . $product->image_path) }}" alt="Current Image" class="w-16 h-16 object-contain mix-blend-multiply bg-white rounded border border-gray-200">
                    <div>
                        <p class="text-xs font-bold text-gray-500 uppercase">Current Image</p>
                        <p class="text-sm font-mono truncate max-w-[150px] text-gray-800">{{ $product->image_path }}</p>
                    </div>
                </div>
                @endif

                <div class="border-2 border-dashed border-gray-300 rounded-lg p-6 text-center bg-gray-50 hover:bg-gray-100 transition-colors relative">
                    <input type="file" name="image_file" id="image_file" accept="image/*" class="absolute inset-0 w-full h-full opacity-0 cursor-pointer">
                    <div class="flex flex-col items-center">
                        <i data-lucide="image-plus" class="w-8 h-8 text-gray-400 mb-2"></i>
                        <h3 class="text-sm font-bold text-gray-800 mb-1">Upload new image to replace current</h3>
                        <div id="file-name" class="mt-2 text-xs font-bold text-brand-red truncate px-4 hidden"></div>
                    </div>
                </div>
            </div>
            
            <div class="relative flex items-center py-2">
                <div class="flex-grow border-t border-gray-200"></div>
                <span class="flex-shrink-0 mx-4 text-gray-400 text-xs font-bold uppercase">OR UPDATE MANUALLY</span>
                <div class="flex-grow border-t border-gray-200"></div>
            </div>

            <div>
                <label class="block text-[10px] font-bold text-gray-500 uppercase tracking-widest mb-2">Server File Path</label>
                <input type="text" name="image_path" value="{{ old('image_path', $product->image_path) }}" class="w-full border border-gray-200 rounded px-4 py-2 outline-none focus:border-brand-red focus:ring-1 focus:ring-brand-red font-mono text-sm bg-gray-50">
            </div>
        </div>
    </div>

    <div class="border-t border-gray-100 pt-8 flex justify-end relative z-10">
        <button type="submit" class="bg-brand-red text-white px-10 py-4 rounded font-extrabold uppercase tracking-widest hover:bg-black transition-colors flex items-center space-x-2 shadow-md">
            <i data-lucide="refresh-cw" class="w-5 h-5"></i>
            <span>Update Product</span>
        </button>
    </div>
</form>

@push('scripts')
<script>
    document.getElementById('image_file').addEventListener('change', function(e) {
        if(this.files && this.files.length > 0) {
            const fileNameDiv = document.getElementById('file-name');
            fileNameDiv.textContent = 'Selected: ' + this.files[0].name;
            fileNameDiv.classList.remove('hidden');
        }
    });
</script>
@endpush
@endsection
