@extends('layouts.aura-main')

@section('main-content')
<div x-data="{ 
    qty: 0, 
    cost: 0, 
    category: '{{ old('category', 'Service') }}',
    get type() {
        return this.category === 'Service' ? 'Service' : 'Inventory';
    }
}" class="pb-20 max-w-4xl mx-auto">

    <div class="fixed top-5 right-5 z-[100] space-y-3 w-80">
        @if ($errors->any())
            <div x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 8000)" 
                 class="bg-rose-50 border-l-4 border-rose-500 p-4 shadow-xl rounded-sm transition-all duration-300">
                <div class="flex justify-between items-start">
                    <p class="text-[11px] font-bold text-rose-700 uppercase tracking-wider">Validation Error</p>
                    <button @click="show = false" class="text-rose-400 hover:text-rose-600 font-black">&times;</button>
                </div>
                <ul class="mt-2 list-disc list-inside text-[10px] text-rose-600 font-medium">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        @if (session('error'))
            <div x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 8000)" 
                 class="bg-amber-50 border-l-4 border-amber-500 p-4 shadow-xl rounded-sm">
                <div class="flex justify-between">
                    <p class="text-[11px] font-bold text-amber-700 uppercase tracking-wider">System Alert</p>
                    <button @click="show = false" class="text-amber-400 hover:text-amber-600 font-black">&times;</button>
                </div>
                <p class="mt-1 text-[10px] text-amber-600 font-medium">{{ session('error') }}</p>
            </div>
        @endif
    </div>
    
    <form action="{{ route('products.store') }}" method="POST">
        @csrf
        
        <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-black text-slate-800 tracking-tight">New Product or Service</h1>
                    <p class="text-[11px] text-slate-500 font-medium">Define item categories, pricing, and financial mapping</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-widest">Cancel</a>
                    <button type="submit" class="bg-indigo-600 text-white px-8 py-2 rounded-sm text-[11px] font-bold shadow-lg hover:bg-indigo-700 transition uppercase tracking-widest">
                        Save 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" required class="w-full border border-slate-300 rounded-sm px-3 py-2 text-sm 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" class="w-full border border-slate-300 rounded-sm px-3 py-2 text-sm 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" x-model="category" required class="w-full border border-slate-300 rounded-sm px-3 py-2 text-sm focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            <option value="Service">Service</option>
                            <option value="Raw Material">Raw Material</option>
                            <option value="Finished Good">Finished Good</option>
                        </select>
                        <input type="hidden" name="type" :value="type">
                    </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" placeholder="e.g. Zambia" class="w-full border-slate-300 rounded-sm px-3 py-2 text-sm 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"  placeholder="0.00" class="w-full border border-emerald-200 rounded-sm 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-sm px-3 py-2 text-sm focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            <option value="">Select Revenue Mapping...</option>
                            @foreach($incomeAccounts as $account)
                                <option value="{{ $account->id }}">{{ $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">Tax Category</label>
                    <select name="tax_category" required class="w-full border border-slate-300 rounded-sm px-3 py-2 text-sm focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                        <option value="standard">Standard Rated (16%)</option>
                        <option value="exempt">Exempt</option>
                        <option value="zero">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" placeholder="0.00" class="w-full border border-amber-200 rounded-sm 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-sm px-3 py-2 text-sm focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            <option value="">Select COGS mapping...</option>
                            @foreach($expenseAccounts as $account)
                                <option value="{{ $account->id }}">{{ $account->name }} ({{ $account->code }})</option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="mb-6">
                    <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-sm px-3 py-2 text-sm focus:border-indigo-500 outline-none shadow-sm bg-white">
                        <option value="">Select Supplier...</option>
                        @foreach($suppliers as $supplier)
                            <option value="{{ $supplier->id }}">{{ $supplier->name }}</option>
                        @endforeach
                    </select>
                </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-sm p-3 text-xs italic focus:border-indigo-500 outline-none shadow-sm transition" placeholder="Appears on purchase orders..."></textarea>
                </div>
            </div>

            <div x-show="category !== 'Service'" 
                 x-transition:enter="transition ease-out duration-300"
                 x-transition:enter-start="opacity-0 transform -translate-y-2"
                 x-transition:enter-end="opacity-100 transform translate-y-0"
                 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">Initial quantity on hand</label>
                        <input type="number" name="qty_on_hand" x-model="qty" class="w-full border border-slate-300 rounded-sm px-3 py-2 text-sm 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" :required="category !== 'Service'" class="w-full border border-slate-300 rounded-sm px-3 py-2 text-sm focus:border-indigo-500 outline-none shadow-sm bg-white font-semibold">
                            <option value="">Select Asset Account...</option>
                            @foreach($assetAccounts as $aAccount)
                                <option value="{{ $aAccount->id }}">{{ $aAccount->code }} - {{ $aAccount->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="bg-slate-50 p-4 rounded-sm border border-slate-200 flex flex-col justify-center">
                        <label class="block text-[9px] font-black text-slate-400 mb-1 tracking-widest uppercase">Opening Stock Value</label>
                        <p class="text-xl 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