{{-- Member Loan Application Form --}}
<x-app-layout title="Apply for Loan">

    <x-module-hero :title="'Apply for ' . $loanProduct->name" :description="($loanProduct->interest_rate_display ?? $loanProduct->interest_rate . '% APR') . ' • ' . $loanProduct->formatted_amount_range" icon="fa-paper-plane">
        <x-slot name="backButton">
            <a href="{{ route('member.loans.products') }}" class="inline-flex items-center gap-2 text-white/80 hover:text-white text-sm transition">
                <i class="fas fa-arrow-left"></i> Back to Products
            </a>
        </x-slot>
        <x-slot name="tabs">
            <a href="{{ route('member.dashboard') }}" class="module-hero-tab">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            <a href="{{ route('member.loans.index') }}" class="module-hero-tab">
                <i class="fas fa-list"></i> My Loans
            </a>
            <a href="{{ route('member.loans.products') }}" class="module-hero-tab active-tab">
                <i class="fas fa-plus-circle"></i> Apply for Loan
            </a>
            <a href="{{ route('member.contributions') }}" class="module-hero-tab">
                <i class="fas fa-hand-holding-usd"></i> Contributions
            </a>
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        @if($errors->any())
            <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <p class="font-semibold mb-2 text-sm">Please correct the following errors:</p>
                <ul class="list-disc list-inside text-sm">
                    @foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach
                </ul>
            </div>
        @endif

        <form action="{{ route('member.loans.store', $loanProduct) }}" method="POST" enctype="multipart/form-data">
            @csrf

            {{-- Loan Details --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden mb-6">
                <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                    <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                        <i class="fas fa-money-bill text-green-600"></i> Loan Details
                    </h3>
                </div>
                <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="amount_requested" class="block text-sm font-medium text-gray-700 mb-1">
                            Loan Amount <span class="text-red-500">*</span>
                        </label>
                        <input type="number" id="amount_requested" name="amount_requested"
                               value="{{ old('amount_requested', $loanProduct->default_amount) }}"
                               min="{{ $loanProduct->min_amount }}" max="{{ $loanProduct->max_amount }}" step="100"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm" required>
                        <p class="mt-1 text-xs text-gray-500">
                            Min: {{ number_format($loanProduct->min_amount, 2) }} — Max: {{ number_format($loanProduct->max_amount, 2) }} {{ $loanProduct->currency }}
                        </p>
                        @error('amount_requested')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                    </div>
                    <div>
                        <label for="term_months" class="block text-sm font-medium text-gray-700 mb-1">
                            Term (Months) <span class="text-red-500">*</span>
                        </label>
                        <input type="number" id="term_months" name="term_months"
                               value="{{ old('term_months', $loanProduct->default_term_months) }}"
                               min="{{ $loanProduct->min_term_months }}" max="{{ $loanProduct->max_term_months }}"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm" required>
                        <p class="mt-1 text-xs text-gray-500">
                            Min: {{ $loanProduct->min_term_months }} — Max: {{ $loanProduct->max_term_months }} months
                        </p>
                        @error('term_months')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                    </div>
                    <div class="md:col-span-2">
                        <label for="purpose" class="block text-sm font-medium text-gray-700 mb-1">
                            Purpose of Loan <span class="text-red-500">*</span>
                        </label>
                        <textarea id="purpose" name="purpose" rows="3"
                                  class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm"
                                  placeholder="Describe what you need the loan for..." required>{{ old('purpose') }}</textarea>
                        @error('purpose')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                    </div>
                </div>
            </div>

            {{-- Employment Information --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden mb-6">
                <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                    <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                        <i class="fas fa-briefcase text-green-600"></i> Employment Information
                    </h3>
                </div>
                <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="employment_status" class="block text-sm font-medium text-gray-700 mb-1">Employment Status</label>
                        <select id="employment_status" name="employment_status"
                                class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm">
                            <option value="">Select Status</option>
                            <option value="employed" {{ old('employment_status') === 'employed' ? 'selected' : '' }}>Employed</option>
                            <option value="self_employed" {{ old('employment_status') === 'self_employed' ? 'selected' : '' }}>Self Employed</option>
                            <option value="business_owner" {{ old('employment_status') === 'business_owner' ? 'selected' : '' }}>Business Owner</option>
                            <option value="retired" {{ old('employment_status') === 'retired' ? 'selected' : '' }}>Retired</option>
                            <option value="other" {{ old('employment_status') === 'other' ? 'selected' : '' }}>Other</option>
                        </select>
                    </div>
                    <div>
                        <label for="employer_name" class="block text-sm font-medium text-gray-700 mb-1">Employer / Business Name</label>
                        <input type="text" id="employer_name" name="employer_name" value="{{ old('employer_name') }}"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm">
                    </div>
                    <div>
                        <label for="monthly_income" class="block text-sm font-medium text-gray-700 mb-1">Monthly Income</label>
                        <input type="number" id="monthly_income" name="monthly_income" value="{{ old('monthly_income') }}"
                               step="0.01" min="0"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm">
                    </div>
                    <div>
                        <label for="income_currency" class="block text-sm font-medium text-gray-700 mb-1">Income Currency</label>
                        <select id="income_currency" name="income_currency"
                                class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm">
                            <option value="USD" {{ old('income_currency') === 'USD' ? 'selected' : '' }}>USD</option>
                            <option value="SSP" {{ old('income_currency') === 'SSP' ? 'selected' : '' }}>SSP</option>
                            <option value="KES" {{ old('income_currency') === 'KES' ? 'selected' : '' }}>KES</option>
                            <option value="UGX" {{ old('income_currency') === 'UGX' ? 'selected' : '' }}>UGX</option>
                        </select>
                    </div>
                </div>
            </div>

            {{-- Supporting Documents --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden mb-6">
                <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                    <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                        <i class="fas fa-file-upload text-green-600"></i> Supporting Documents
                    </h3>
                </div>
                <div class="p-6 space-y-4">
                    <p class="text-sm text-gray-600">Upload any supporting documents for your application. Accepted formats: PDF, JPG, PNG (max 5MB each).</p>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">ID Document</label>
                            <input type="file" name="documents[id_document]" accept=".pdf,.jpg,.jpeg,.png"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-green-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Proof of Income</label>
                            <input type="file" name="documents[proof_of_income]" accept=".pdf,.jpg,.jpeg,.png"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-green-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Bank Statement</label>
                            <input type="file" name="documents[bank_statement]" accept=".pdf,.jpg,.jpeg,.png"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-green-500">
                        </div>
                        @if($loanProduct->requires_collateral)
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Collateral Document</label>
                                <input type="file" name="documents[collateral_document]" accept=".pdf,.jpg,.jpeg,.png"
                                       class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-green-500">
                            </div>
                        @endif
                        @if($loanProduct->requires_guarantor)
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Guarantor ID</label>
                                <input type="file" name="documents[guarantor_id]" accept=".pdf,.jpg,.jpeg,.png"
                                       class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-green-500">
                            </div>
                        @endif
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Employment Letter</label>
                            <input type="file" name="documents[employment_letter]" accept=".pdf,.jpg,.jpeg,.png"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-green-500">
                        </div>
                    </div>
                    <p class="text-xs text-gray-500"><i class="fas fa-info-circle mr-1"></i> All documents are optional but may speed up your application review.</p>
                </div>
            </div>

            {{-- Product Summary --}}
            <div class="bg-blue-50 border border-blue-200 rounded-lg p-5 mb-6">
                <h4 class="font-semibold text-blue-900 mb-3 text-sm flex items-center gap-2">
                    <i class="fas fa-info-circle"></i> {{ $loanProduct->name }} — Key Terms
                </h4>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4 text-sm">
                    <div>
                        <span class="text-blue-600 text-xs">Interest Rate</span>
                        <p class="font-semibold text-blue-900">{{ $loanProduct->interest_rate_display ?? $loanProduct->interest_rate . '%' }}</p>
                    </div>
                    <div>
                        <span class="text-blue-600 text-xs">Amount Range</span>
                        <p class="font-semibold text-blue-900">{{ $loanProduct->formatted_amount_range }}</p>
                    </div>
                    <div>
                        <span class="text-blue-600 text-xs">Term Range</span>
                        <p class="font-semibold text-blue-900">{{ $loanProduct->min_term_months }}–{{ $loanProduct->max_term_months }} months</p>
                    </div>
                    <div>
                        <span class="text-blue-600 text-xs">Currency</span>
                        <p class="font-semibold text-blue-900">{{ $loanProduct->currency }}</p>
                    </div>
                </div>
            </div>

            <div class="flex justify-end gap-3">
                <a href="{{ route('member.loans.products') }}" class="px-6 py-2.5 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition text-sm font-medium">
                    <i class="fas fa-times mr-1"></i> Cancel
                </a>
                <button type="submit" class="px-8 py-2.5 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm font-semibold shadow-sm">
                    <i class="fas fa-paper-plane mr-1"></i> Submit Application
                </button>
            </div>
        </form>
    </div>

</x-app-layout>
