<x-app-layout>
    <div class="py-6">
        <div class="max-w-3xl mx-auto sm:px-6 lg:px-8">
            <a href="{{ route('consignments.index') }}" class="inline-flex items-center gap-1.5 text-slate-600 hover:text-slate-900 text-sm font-medium mb-4 transition duration-150">
                &larr; Back to Consignments Registry
            </a>
            
            <div class="mb-6">
                <h2 class="text-2xl font-bold text-slate-800">Add New Consignment Project</h2>
                <p class="text-slate-500 text-sm mt-1">Create a financial container to group transactions for imports or trade deals.</p>
            </div>

            <form action="{{ route('consignments.store') }}" method="POST" 
                x-data="{ 
                    buyerType: '{{ old('buyer_type', 'none') }}',
                    sellerType: '{{ old('seller_type', 'none') }}',
                    investorType: '{{ old('investor_type', 'none') }}', 
                    shareType: '{{ old('investor_share_type', 'percentage') }}' 
                }" 
                class="bg-white border border-slate-200 rounded-xl shadow-sm p-6 space-y-6">
                @csrf

                <!-- Title -->
                <div>
                    <label for="title" class="block text-sm font-semibold text-slate-600 uppercase tracking-wider mb-2">Project Name / Title *</label>
                    <input type="text" name="title" id="title" required value="{{ old('title') }}" placeholder="e.g. Toyota Prius Import JPN-02"
                        class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm">
                    @error('title')
                        <p class="text-rose-600 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Project Total Price -->
                <div>
                    <label for="project_total_price" class="block text-sm font-semibold text-slate-600 uppercase tracking-wider mb-2">Project Total Value / Price (Estimated Budget or Contract Value)</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none text-slate-400 text-sm font-bold">
                            {{ session('currency_symbol', '£') }}
                        </div>
                        <input type="number" name="project_total_price" id="project_total_price" step="0.01" min="0" value="{{ old('project_total_price', '0.00') }}"
                            class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm pl-8">
                    </div>
                    @error('project_total_price')
                        <p class="text-rose-600 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                {{-- ═══════════════════════════════════════════════════════════
                    👤 CLIENT (BUYER) MAPPING
                ═══════════════════════════════════════════════════════════ --}}
                <div class="bg-blue-50/60 rounded-xl p-4 border border-blue-200 space-y-4">
                    <label class="block text-sm font-bold text-blue-800">
                        <span class="mr-1.5">👤</span> Client (Buyer) Mapping
                        <span class="text-xs font-normal text-blue-500 ml-2">— Who is this project for?</span>
                    </label>
                    
                    <div class="flex flex-wrap items-center gap-x-6 gap-y-2">
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="buyer_type" value="none" x-model="buyerType" class="text-blue-500 focus:ring-blue-400">
                            <span class="ml-2 font-medium">No Buyer</span>
                        </label>
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="buyer_type" value="client" x-model="buyerType" class="text-blue-500 focus:ring-blue-400">
                            <span class="ml-2 font-medium">Client Directory</span>
                        </label>
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="buyer_type" value="custom" x-model="buyerType" class="text-blue-500 focus:ring-blue-400">
                            <span class="ml-2 font-medium">Custom Account</span>
                        </label>
                    </div>

                    <!-- Client dropdown -->
                    <div x-show="buyerType === 'client'" x-cloak class="transition duration-150">
                        <label for="buyer_id_client" class="block text-xs font-semibold text-slate-600 uppercase tracking-wider mb-2">Select Client Profile *</label>
                        <select name="buyer_id" id="buyer_id_client" x-bind:disabled="buyerType !== 'client'"
                            class="w-full rounded-lg border-slate-300 focus:border-blue-500 focus:ring focus:ring-blue-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="">-- Choose Client --</option>
                            @foreach($clients as $c)
                                <option value="{{ $c->id }}" {{ old('buyer_id') == $c->id ? 'selected' : '' }}>
                                    {{ $c->full_name }} ({{ $c->company_name ?? 'Individual' }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Custom Account dropdown -->
                    <div x-show="buyerType === 'custom'" x-cloak class="transition duration-150">
                        <label for="buyer_id_custom" class="block text-xs font-semibold text-slate-600 uppercase tracking-wider mb-2">Select Custom Account *</label>
                        <select name="buyer_id" id="buyer_id_custom" x-bind:disabled="buyerType !== 'custom'"
                            class="w-full rounded-lg border-slate-300 focus:border-blue-500 focus:ring focus:ring-blue-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="">-- Choose Custom Account --</option>
                            @foreach($customAccounts as $ca)
                                <option value="{{ $ca->id }}" {{ old('buyer_id') == $ca->id ? 'selected' : '' }}>
                                    {{ $ca->name }} ({{ $ca->account_type }})
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                {{-- ═══════════════════════════════════════════════════════════
                    🏪 SELLER (SUPPLIER) MAPPING
                ═══════════════════════════════════════════════════════════ --}}
                <div class="bg-amber-50/60 rounded-xl p-4 border border-amber-200 space-y-4">
                    <label class="block text-sm font-bold text-amber-800">
                        <span class="mr-1.5">🏪</span> Seller (Supplier) Mapping
                        <span class="text-xs font-normal text-amber-600 ml-2">— Who are you buying from?</span>
                    </label>
                    
                    <div class="flex flex-wrap items-center gap-x-6 gap-y-2">
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="seller_type" value="none" x-model="sellerType" class="text-amber-500 focus:ring-amber-400">
                            <span class="ml-2 font-medium">No Seller</span>
                        </label>
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="seller_type" value="client" x-model="sellerType" class="text-amber-500 focus:ring-amber-400">
                            <span class="ml-2 font-medium">Client Directory</span>
                        </label>
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="seller_type" value="custom" x-model="sellerType" class="text-amber-500 focus:ring-amber-400">
                            <span class="ml-2 font-medium">Custom Account</span>
                        </label>
                    </div>

                    <!-- Client dropdown -->
                    <div x-show="sellerType === 'client'" x-cloak class="transition duration-150">
                        <label for="seller_id_client" class="block text-xs font-semibold text-slate-600 uppercase tracking-wider mb-2">Select Client Profile *</label>
                        <select name="seller_id" id="seller_id_client" x-bind:disabled="sellerType !== 'client'"
                            class="w-full rounded-lg border-slate-300 focus:border-amber-500 focus:ring focus:ring-amber-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="">-- Choose Client --</option>
                            @foreach($clients as $c)
                                <option value="{{ $c->id }}" {{ old('seller_id') == $c->id ? 'selected' : '' }}>
                                    {{ $c->full_name }} ({{ $c->company_name ?? 'Individual' }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Custom Account dropdown -->
                    <div x-show="sellerType === 'custom'" x-cloak class="transition duration-150">
                        <label for="seller_id_custom" class="block text-xs font-semibold text-slate-600 uppercase tracking-wider mb-2">Select Custom Account *</label>
                        <select name="seller_id" id="seller_id_custom" x-bind:disabled="sellerType !== 'custom'"
                            class="w-full rounded-lg border-slate-300 focus:border-amber-500 focus:ring focus:ring-amber-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="">-- Choose Custom Account --</option>
                            @foreach($customAccounts as $ca)
                                <option value="{{ $ca->id }}" {{ old('seller_id') == $ca->id ? 'selected' : '' }}>
                                    {{ $ca->name }} ({{ $ca->account_type }})
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                {{-- ═══════════════════════════════════════════════════════════
                    💰 INVESTOR MAPPING
                ═══════════════════════════════════════════════════════════ --}}
                <div class="bg-slate-50 rounded-xl p-4 border border-slate-200 space-y-4">
                    <label class="block text-sm font-bold text-slate-700">
                        <span class="mr-1.5">💰</span> Investor Mapping
                        <span class="text-xs font-normal text-slate-500 ml-2">— Who is funding/sharing costs?</span>
                    </label>
                    
                    <div class="flex flex-wrap items-center gap-x-6 gap-y-2">
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="investor_type" value="none" x-model="investorType" class="text-brand-500 focus:ring-brand-400">
                            <span class="ml-2 font-medium">No Investor / Sole Project</span>
                        </label>
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="investor_type" value="client" x-model="investorType" class="text-brand-500 focus:ring-brand-400">
                            <span class="ml-2 font-medium">Client Directory Profile</span>
                        </label>
                        <label class="inline-flex items-center text-sm text-slate-700 cursor-pointer">
                            <input type="radio" name="investor_type" value="custom" x-model="investorType" class="text-brand-500 focus:ring-brand-400">
                            <span class="ml-2 font-medium">Custom Account (Partner/Investor)</span>
                        </label>
                    </div>

                    <!-- Client dropdown -->
                    <div x-show="investorType === 'client'" x-cloak class="transition duration-150">
                        <label for="investor_id_client" class="block text-xs font-semibold text-slate-600 uppercase tracking-wider mb-2">Select Client Profile *</label>
                        <select name="investor_id" id="investor_id_client" x-bind:disabled="investorType !== 'client'"
                            class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="">-- Choose Client --</option>
                            @foreach($clients as $c)
                                <option value="{{ $c->id }}" {{ old('investor_id') == $c->id ? 'selected' : '' }}>
                                    {{ $c->full_name }} ({{ $c->company_name ?? 'Individual' }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Custom Account dropdown -->
                    <div x-show="investorType === 'custom'" x-cloak class="transition duration-150">
                        <label for="investor_id_custom" class="block text-xs font-semibold text-slate-600 uppercase tracking-wider mb-2">Select Custom Account *</label>
                        <select name="investor_id" id="investor_id_custom" x-bind:disabled="investorType !== 'custom'"
                            class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="">-- Choose Custom Account --</option>
                            @foreach($customAccounts as $ca)
                                <option value="{{ $ca->id }}" {{ old('investor_id') == $ca->id ? 'selected' : '' }}>
                                    {{ $ca->name }} ({{ $ca->account_type }})
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <!-- Investor Share Settings Widget (Visible only if there is an investor) -->
                <div x-show="investorType !== 'none'" x-cloak class="grid grid-cols-1 md:grid-cols-2 gap-6 bg-slate-50 p-4 border border-slate-200 rounded-xl transition duration-150">
                    <!-- Share Type Selector -->
                    <div>
                        <label for="investor_share_type" class="block text-sm font-semibold text-slate-600 uppercase tracking-wider mb-2">Investor Share Type *</label>
                        <select name="investor_share_type" id="investor_share_type" x-model="shareType" x-bind:disabled="investorType === 'none'"
                            class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="percentage">Percentage Share (%)</option>
                            <option value="fixed">Fixed Flat Amount</option>
                        </select>
                        @error('investor_share_type')
                            <p class="text-rose-600 text-xs mt-1">{{ $message }}</p>
                        @enderror
                    </div>

                    <!-- Share Value Input -->
                    <div>
                        <label for="investor_share_value" class="block text-sm font-semibold text-slate-600 uppercase tracking-wider mb-2">
                            <span x-text="shareType === 'percentage' ? 'Investor Cost/Profit Share % *' : 'Investor Share Fixed Amount *'"></span>
                        </label>
                        <div class="relative">
                            <input type="number" name="investor_share_value" id="investor_share_value" required step="0.01" min="0" x-bind:disabled="investorType === 'none'"
                                :max="shareType === 'percentage' ? 100 : ''" value="{{ old('investor_share_value', '50.00') }}"
                                class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm"
                                :class="shareType === 'percentage' ? 'pr-8' : 'pl-8'">
                            
                            <!-- Percentage suffix icon -->
                            <div x-show="shareType === 'percentage'" class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none text-slate-400 text-sm font-bold">%</div>
                            
                            <!-- Currency prefix icon -->
                            <div x-show="shareType === 'fixed'" class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none text-slate-400 text-sm font-bold">
                                {{ session('currency_symbol', '£') }}
                            </div>
                        </div>
                        @error('investor_share_value')
                            <p class="text-rose-600 text-xs mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <!-- Hidden inputs for mapping share fallback if investor is none -->
                <div x-show="investorType === 'none'">
                    <input type="hidden" name="investor_share_type" value="percentage">
                    <input type="hidden" name="investor_share_value" value="0">
                </div>

                <!-- Status & Notes -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="status" class="block text-sm font-semibold text-slate-600 uppercase tracking-wider mb-2">Status *</label>
                        <select name="status" id="status" required
                            class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm text-slate-700">
                            <option value="active" {{ old('status') === 'active' ? 'selected' : '' }}>Active</option>
                            <option value="completed" {{ old('status') === 'completed' ? 'selected' : '' }}>Completed</option>
                            <option value="archived" {{ old('status') === 'archived' ? 'selected' : '' }}>Archived</option>
                        </select>
                    </div>
                </div>

                <!-- Notes -->
                <div>
                    <label for="notes" class="block text-sm font-semibold text-slate-600 uppercase tracking-wider mb-2">Project Description & Notes</label>
                    <textarea name="notes" id="notes" rows="4" placeholder="Enter key parameters, cargo details, or contract highlights..."
                        class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm">{{ old('notes') }}</textarea>
                </div>

                <!-- Actions -->
                <div class="flex items-center justify-end gap-3 pt-4 border-t border-slate-100">
                    <a href="{{ route('consignments.index') }}" class="px-5 py-2 border border-slate-300 rounded-lg text-slate-700 text-sm font-semibold hover:bg-slate-50 transition duration-150">
                        Cancel
                    </a>
                    <button type="submit" class="px-5 py-2 bg-brand-500 hover:bg-brand-600 text-white text-sm font-semibold rounded-lg shadow-sm transition duration-150">
                        Create Project
                    </button>
                </div>
            </form>
        </div>
    </div>
</x-app-layout>
