@extends('layouts.aura-main')

@section('main-content')
<div x-data="{ qty: {{ $product->qty_on_hand ?? 0 }}, cost: {{ $product->cost ?? 0 }} }" class="pb-20 max-w-4xl mx-auto">
    <form action="{{ route('products.update', $product->id) }}" method="POST">
        @csrf
        @method('PUT')
        
        <div class="bg-white border border-slate-300 rounded-sm shadow-sm mb-8">
            <div class="p-4 flex justify-between items-center bg-slate-50/50">
                <div>
                    <h1 class="text-lg font-bold text-slate-800 tracking-tight">Edit Product or Service</h1>
                    <p class="text-[11px] text-slate-500 font-medium">Update item details, current stock, and accounting mappings</p>
                </div>
                <div class="flex items-center gap-3">
                    <a href="{{ route('products.index') }}" class="text-[11px] font-bold text-slate-400 hover:text-slate-600 transition uppercase tracking-tighter">Cancel</a>
                    <button type="submit" class="bg-indigo-600 text-white px-8 py-2 rounded text-[11px] font-bold shadow-sm hover:bg-indigo-700 transition uppercase tracking-tighter">
                        Update Item
                    </button>
                </div>
            </div>
        </div>

        <div class="space-y-6">
            
            <div class="bg-white border border-slate-300 rounded-sm shadow-sm p-8">
                <div class="flex items-center gap-2 mb-6 border-b border-slate-100 pb-4">
                    <div class="w-1.5 h-4 bg-indigo-500 rounded-full"></div>
                    <h2 class="text-[12px] font-black text-slate-700 uppercase tracking-widest">Primary information</h2>
                </div>

                <div class="grid grid-cols-2 gap-8 mb-6">
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Item name</label>
                        <input type="text" name="name" value="{{ $product->name }}" required class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white font-medium">
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">SKU / Item code</label>
                        <input type="text" name="sku" value="{{ $product->sku }}" class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white font-mono">
                    </div>
                </div>

                <div class="grid grid-cols-2 gap-8">
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Category</label>
                        <select name="category" required class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            <option value="Service" {{ $product->category == 'Service' ? 'selected' : '' }}>Service</option>
                            <option value="Raw Material" {{ $product->category == 'Raw Material' ? 'selected' : '' }}>Raw Material</option>
                            <option value="Finished Good" {{ $product->category == 'Finished Good' ? 'selected' : '' }}>Finished Good</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Country of origin</label>
                        <input type="text" name="country_of_origin" value="{{ $product->country_of_origin }}" placeholder="e.g. Zambia" class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white">
                    </div>
                </div>
            </div>

            <div class="bg-white border border-slate-300 rounded-sm shadow-sm p-8">
                <div class="flex items-center gap-2 mb-6 border-b border-slate-100 pb-4">
                    <div class="w-1.5 h-4 bg-emerald-500 rounded-full"></div>
                    <h2 class="text-[12px] font-black text-slate-700 uppercase tracking-widest">Sales details</h2>
                </div>

                <div class="grid grid-cols-2 gap-8 mb-6">
                    <div>
                        <label class="block text-[10px] font-bold text-emerald-600 mb-1 tracking-wider uppercase font-black">Sales Price (ZMW)</label>
                        <input type="number" step="0.01" name="sales_price" value="{{ $product->sales_price }}" class="w-full border border-emerald-200 rounded px-3 py-2 text-sm focus:ring-2 focus:ring-emerald-500/10 focus:border-emerald-500 outline-none shadow-sm bg-white font-mono font-bold text-emerald-700">
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-indigo-600 mb-1 tracking-wider uppercase font-black">Income Account</label>
                        <select name="income_account_id" required class="w-full border border-indigo-200 rounded px-3 py-2 text-xs focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            @foreach($incomeAccounts as $account)
                                <option value="{{ $account->id }}" {{ $product->income_account_id == $account->id ? 'selected' : '' }}>
                                    {{ $account->name }} ({{ $account->code }})
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div>
                    <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Sales Tax status</label>
                    <select name="tax_category" class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white">
                        <option value="standard" {{ $product->tax_category == 'standard' ? 'selected' : '' }}>Taxable - Standard Rate</option>
                        <option value="exempt" {{ $product->tax_category == 'exempt' ? 'selected' : '' }}>Exempt</option>
                        <option value="zero" {{ $product->tax_category == 'zero' ? 'selected' : '' }}>Zero Rated</option>
                    </select>
                </div>
            </div>

            <div class="bg-white border border-slate-300 rounded-sm shadow-sm p-8">
                <div class="flex items-center gap-2 mb-6 border-b border-slate-100 pb-4">
                    <div class="w-1.5 h-4 bg-amber-500 rounded-full"></div>
                    <h2 class="text-[12px] font-black text-slate-700 uppercase tracking-widest">Purchasing information</h2>
                </div>

                <div class="grid grid-cols-2 gap-8 mb-6">
                    <div>
                        <label class="block text-[10px] font-bold text-amber-600 mb-1 tracking-wider uppercase font-black">Cost Price (ZMW)</label>
                        <input type="number" step="0.01" name="cost" x-model="cost" class="w-full border border-amber-200 rounded px-3 py-2 text-sm focus:ring-2 focus:ring-amber-500/10 focus:border-amber-500 outline-none shadow-sm bg-white font-mono font-bold text-amber-700">
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-indigo-600 mb-1 tracking-wider uppercase font-black">Expense Account (COGS)</label>
                        <select name="expense_account_id" class="w-full border border-indigo-200 rounded px-3 py-2 text-xs focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            @foreach($expenseAccounts as $account)
                                <option value="{{ $account->id }}" {{ $product->expense_account_id == $account->id ? 'selected' : '' }}>
                                    {{ $account->name }} ({{ $account->code }})
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="grid grid-cols-2 gap-8 mb-6">
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Preferred Vendor</label>
                        <select name="preferred_vendor_id" class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white">
                            <option value="">Select Supplier...</option>
                            @foreach($suppliers as $supplier)
                                <option value="{{ $supplier->id }}" {{ $product->preferred_vendor_id == $supplier->id ? 'selected' : '' }}>
                                    {{ $supplier->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Item type</label>
                        <div class="flex gap-6 py-2">
                            <label class="flex items-center gap-2 text-xs font-semibold text-slate-600 cursor-pointer">
                                <input type="radio" name="type" value="Service" {{ $product->type == 'Service' ? 'checked' : '' }} class="text-indigo-600 focus:ring-indigo-500"> Service
                            </label>
                            <label class="flex items-center gap-2 text-xs font-semibold text-slate-600 cursor-pointer">
                                <input type="radio" name="type" value="Inventory" {{ $product->type == 'Inventory' ? 'checked' : '' }} class="text-indigo-600 focus:ring-indigo-500"> Inventory
                            </label>
                        </div>
                    </div>
                </div>

                <div>
                    <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Purchase Description</label>
                    <textarea name="purchase_description" rows="2" class="w-full border border-slate-300 rounded p-3 text-xs italic focus:border-indigo-500 outline-none shadow-sm transition">{{ $product->purchase_description }}</textarea>
                </div>
            </div>

            <div class="bg-white border border-slate-300 rounded-sm shadow-sm p-8">
                <div class="flex items-center gap-2 mb-6 border-b border-slate-100 pb-4">
                    <div class="w-1.5 h-4 bg-slate-400 rounded-full"></div>
                    <h2 class="text-[12px] font-black text-slate-700 uppercase tracking-widest">Inventory tracking</h2>
                </div>

                <div class="grid grid-cols-3 gap-8">
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Quantity on hand</label>
                        <input type="number" name="qty_on_hand" x-model="qty" class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white font-mono">
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-slate-500 mb-1 tracking-wider uppercase">Inventory Asset Account</label>
                        <select name="asset_account_id" class="w-full border border-slate-300 rounded px-3 py-2 text-xs focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            @foreach($assetAccounts ?? [] as $aAccount)
                                <option value="{{ $aAccount->id }}" {{ $product->asset_account_id == $aAccount->id ? 'selected' : '' }}>
                                    {{ $aAccount->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="bg-slate-50 p-3 rounded border border-slate-200">
                        <label class="block text-[9px] font-black text-slate-400 mb-1 tracking-widest uppercase">Current Stock Value</label>
                        <p class="text-lg font-mono font-black text-slate-700">
                            K<span x-text="(qty * cost).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})"></span>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection