<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" 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>

                <!-- Investor Selection Widget -->
                <div x-data="{ investorType: '{{ old('investor_type', 'none') }}' }" 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">Investor Mapping</label>
                    
                    <div class="flex items-center space-x-6">
                        <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>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- Investor Share % -->
                    <div>
                        <label for="investor_share_pct" class="block text-sm font-semibold text-slate-600 uppercase tracking-wider mb-2">Investor Cost/Profit Share % *</label>
                        <div class="relative">
                            <input type="number" name="investor_share_pct" id="investor_share_pct" required step="0.1" min="0" max="100" value="{{ old('investor_share_pct', '50.0') }}"
                                class="w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm pr-8">
                            <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none text-slate-400 text-sm font-bold">%</div>
                        </div>
                        @error('investor_share_pct')
                            <p class="text-rose-600 text-xs mt-1">{{ $message }}</p>
                        @enderror
                    </div>

                    <!-- Status -->
                    <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>
