@extends($activeTemplate . 'layouts.master')

@section('content')

    {{-- Header Nav --}}
    <div class="flex flex-col sm:flex-row items-stretch sm:items-center gap-3 mb-6">
        <div class="flex-1">
            <x-search-form placeholder="Loan No.| Plan" dateSearch='yes' btn="btn--base" />
        </div>
        @if (request()->date || request()->search)
            <a href="{{ request()->fullUrlWithQuery(['download' => 'pdf']) }}"
               class="inline-flex items-center justify-center gap-2 px-5 py-2.5 border border-primary-400 text-primary-700 hover:bg-primary-50 text-sm font-semibold rounded-xl transition-colors duration-150 shrink-0">
                <i data-lucide="download" class="h-4 w-4"></i> @lang('Download PDF')
            </a>
        @endif
    </div>

    {{-- ================================ --}}
    {{-- DESKTOP TABLE — md and above     --}}
    {{-- ================================ --}}
    <div class="hidden md:block bg-white border border-primary-100 rounded-2xl shadow-soft overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm text-left">
                <thead>
                    <tr class="bg-primary-900 text-white text-xs uppercase tracking-wider">
                        <th class="px-5 py-4 font-semibold">@lang('S.N.')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Loan No. | Plan')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Amount')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Installment Amount')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Installment')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Next Installment')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Paid')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Status')</th>
                        <th class="px-5 py-4 font-semibold">@lang('Action')</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-primary-100">
                    @forelse($loans as $loan)
                        @php $remainingAmount = $loan->payableAmount - $loan->paid_amount; @endphp
                        <tr class="hover:bg-primary-50 transition-colors duration-150">

                            {{-- S.N. --}}
                            <td class="px-5 py-4 text-primary-700 font-medium">
                                {{ __($loop->index + $loans->firstItem()) }}
                            </td>

                            {{-- Loan No. | Plan --}}
                            <td class="px-5 py-4">
                                <span class="block font-mono text-xs tracking-wider text-primary-900 font-semibold">{{ __($loan->loan_number) }}</span>
                                <span class="block text-xs text-primary-500 mt-0.5">{{ __($loan->plan->name) }}</span>
                            </td>

                            {{-- Amount --}}
                            <td class="px-5 py-4">
                                <span class="block font-bold text-primary-900">{{ $general->cur_sym . showAmount($loan->amount) }}</span>
                                <span class="block text-xs text-primary-500 mt-0.5">
                                    {{ $general->cur_sym . showAmount($loan->payable_amount) }} @lang('Need to pay')
                                </span>
                            </td>

                            {{-- Installment Amount --}}
                            <td class="px-5 py-4">
                                <span class="block font-bold text-primary-900">{{ $general->cur_sym . showAmount($loan->per_installment) }}</span>
                                <span class="block text-xs text-primary-500 mt-0.5">
                                    @lang('In Every') {{ __($loan->installment_interval) }} @lang('Days')
                                </span>
                            </td>

                            {{-- Installment Count --}}
                            <td class="px-5 py-4">
                                <span class="block text-primary-900 font-medium">@lang('Total'): {{ __($loan->total_installment) }}</span>
                                <span class="block text-xs text-primary-500 mt-0.5">@lang('Given'): {{ __($loan->given_installment) }}</span>
                            </td>

                            {{-- Next Installment --}}
                            <td class="px-5 py-4 text-primary-700 text-xs font-medium">
                                @if ($loan->nextInstallment)
                                    {{ showDateTime($loan->nextInstallment->installment_date, 'd M, Y') }}
                                @else
                                    <span class="text-primary-300">—</span>
                                @endif
                            </td>

                            {{-- Paid --}}
                            <td class="px-5 py-4">
                                <span class="block font-bold text-emerald-600">{{ $general->cur_sym . showAmount($loan->paid_amount) }}</span>
                                <span class="block text-xs text-amber-600 font-semibold mt-0.5">
                                    {{ $general->cur_sym . showAmount($remainingAmount) }} @lang('Remaining')
                                </span>
                            </td>

                            {{-- Status --}}
                            <td class="px-5 py-4">
                                <div class="flex items-center gap-1.5">
                                    @php echo $loan->statusBadge; @endphp
                                    @if ($loan->status == 3)
                                        <button type="button"
                                                class="admin-feedback p-1 rounded-lg hover:bg-rose-100 text-rose-500 transition-colors"
                                                data-feedback="{{ __($loan->admin_feedback) }}"
                                                title="@lang('View Rejection Reason')">
                                            <i data-lucide="info" class="h-4 w-4"></i>
                                        </button>
                                    @endif
                                </div>
                            </td>

                            {{-- Action --}}
                            <td class="px-5 py-4">
                                <a href="{{ route('user.loan.instalment.logs', $loan->loan_number) }}"
                                   class="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-semibold rounded-lg border transition-colors duration-150
                                       {{ !$loan->nextInstallment ? 'border-primary-200 text-primary-300 pointer-events-none opacity-50' : 'border-primary-400 text-primary-700 hover:bg-primary-100' }}">
                                    <i data-lucide="wallet" class="h-3.5 w-3.5"></i> @lang('Installments')
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td class="px-5 py-10 text-center text-primary-400 italic" colspan="100%">
                                {{ __($emptyMessage) }}
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- ================================ --}}
    {{-- MOBILE CARDS — below md          --}}
    {{-- ================================ --}}
    <div class="flex flex-col gap-4 md:hidden">
        @forelse($loans as $loan)
            @php $remainingAmount = $loan->payableAmount - $loan->paid_amount; @endphp

            <div class="bg-white border border-primary-100 rounded-2xl shadow-soft overflow-hidden">

                {{-- Card Header --}}
                <div class="flex items-start justify-between px-4 py-3 bg-primary-900">
                    <div>
                        <span class="block font-mono text-xs tracking-widest text-white font-bold">{{ __($loan->loan_number) }}</span>
                        <span class="block text-primary-300 text-xs mt-0.5">{{ __($loan->plan->name) }}</span>
                    </div>
                    <div class="flex items-center gap-1.5">
                        @php echo $loan->statusBadge; @endphp
                        @if ($loan->status == 3)
                            <button type="button"
                                    class="admin-feedback p-1 rounded-lg bg-white/10 hover:bg-white/20 text-rose-300 transition-colors"
                                    data-feedback="{{ __($loan->admin_feedback) }}">
                                <i data-lucide="info" class="h-4 w-4"></i>
                            </button>
                        @endif
                    </div>
                </div>

                {{-- Amount + Paid --}}
                <div class="grid grid-cols-2 divide-x divide-primary-100 border-b border-primary-100">
                    <div class="px-4 py-3">
                        <p class="text-xs text-primary-400 mb-0.5">@lang('Loan Amount')</p>
                        <p class="text-sm font-bold text-primary-900">{{ $general->cur_sym . showAmount($loan->amount) }}</p>
                        <p class="text-xs text-primary-500 mt-0.5">{{ $general->cur_sym . showAmount($loan->payable_amount) }} @lang('to pay')</p>
                    </div>
                    <div class="px-4 py-3">
                        <p class="text-xs text-primary-400 mb-0.5">@lang('Paid')</p>
                        <p class="text-sm font-bold text-emerald-600">{{ $general->cur_sym . showAmount($loan->paid_amount) }}</p>
                        <p class="text-xs font-semibold text-amber-600 mt-0.5">{{ $general->cur_sym . showAmount($remainingAmount) }} @lang('Remaining')</p>
                    </div>
                </div>

                {{-- Installment Info --}}
                <div class="grid grid-cols-2 divide-x divide-primary-100 border-b border-primary-100">
                    <div class="px-4 py-3">
                        <p class="text-xs text-primary-400 mb-0.5">@lang('Installment')</p>
                        <p class="text-sm font-bold text-primary-900">{{ $general->cur_sym . showAmount($loan->per_installment) }}</p>
                        <p class="text-xs text-primary-500 mt-0.5">@lang('Every') {{ __($loan->installment_interval) }} @lang('Days')</p>
                    </div>
                    <div class="px-4 py-3">
                        <p class="text-xs text-primary-400 mb-0.5">@lang('Progress')</p>
                        <p class="text-sm font-bold text-primary-900">{{ __($loan->given_installment) }} / {{ __($loan->total_installment) }}</p>
                        <p class="text-xs text-primary-500 mt-0.5">@lang('Given / Total')</p>
                    </div>
                </div>

                {{-- Next Installment + Action --}}
                <div class="flex items-center justify-between px-4 py-3">
                    <div>
                        <p class="text-xs text-primary-400 mb-0.5">@lang('Next Installment')</p>
                        @if ($loan->nextInstallment)
                            <p class="text-xs font-semibold text-primary-700">{{ showDateTime($loan->nextInstallment->installment_date, 'd M, Y') }}</p>
                        @else
                            <p class="text-xs text-primary-300">—</p>
                        @endif
                    </div>
                    <a href="{{ route('user.loan.instalment.logs', $loan->loan_number) }}"
                       class="inline-flex items-center gap-1.5 px-3 py-2 text-xs font-semibold rounded-xl border transition-colors duration-150
                           {{ !$loan->nextInstallment ? 'border-primary-200 text-primary-300 pointer-events-none opacity-50' : 'border-primary-400 text-primary-700 hover:bg-primary-100' }}">
                        <i data-lucide="wallet" class="h-3.5 w-3.5"></i> @lang('Installments')
                    </a>
                </div>

            </div>
        @empty
            <div class="bg-white border border-primary-100 rounded-2xl shadow-soft px-5 py-10 text-center text-primary-400 italic">
                {{ __($emptyMessage) }}
            </div>
        @endforelse
    </div>

    {{-- Pagination --}}
    @if ($loans->hasPages())
        <div class="mt-5">
            {{ paginateLinks($loans) }}
        </div>
    @endif

@endsection

@push('modal')
    <div class="modal fade" id="adminFeedback">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content rounded-2xl border border-primary-100 shadow-soft overflow-hidden">

                {{-- Modal Header --}}
                <div class="flex items-center justify-between px-6 py-4 border-b border-rose-100 bg-gradient-to-r from-rose-50 to-white">
                    <div class="flex items-center gap-2">
                        <i data-lucide="circle-x" class="h-5 w-5 text-rose-500"></i>
                        <h5 class="text-base font-semibold text-primary-900">@lang('Reason of Rejection')</h5>
                    </div>
                </div>

                {{-- Modal Body --}}
                <div class="modal-body px-6 py-4 text-sm text-primary-700 leading-relaxed"></div>

                {{-- Modal Footer --}}
                <div class="flex justify-end px-6 py-4 border-t border-primary-100 bg-primary-50/40">
                    <button type="button"
                            data-bs-dismiss="modal"
                            class="inline-flex items-center gap-2 px-4 py-2 bg-primary-800 hover:bg-primary-700 text-white text-sm font-semibold rounded-xl transition-colors duration-150">
                        <i data-lucide="x" class="h-4 w-4"></i> @lang('Close')
                    </button>
                </div>

            </div>
        </div>
    </div>
@endpush

@push('script')
    <script>
        (function ($) {
            "use strict";
            $('.admin-feedback').on('click', function () {
                var modal = $('#adminFeedback');
                modal.find('.modal-body').text($(this).data('feedback'));
                modal.modal('show');
            });
        })(jQuery);
    </script>
@endpush

@push('bottom-menu')
    <div class="col-12 order-lg-3 order-4">
        <div class="flex flex-wrap items-center gap-2 md:gap-3">
            <a href="{{ route('user.loan.plans') }}"
               class="inline-flex items-center gap-2 px-4 py-2 border border-primary-700 text-primary-800 text-sm font-semibold rounded-xl hover:bg-primary-50 transition-colors duration-150">
                <i data-lucide="layout-grid" class="h-4 w-4"></i> @lang('Loan Plans')
            </a>
            <a href="{{ route('user.loan.list') }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-primary-800 text-white text-sm font-semibold rounded-xl shadow-soft hover:bg-primary-700 transition-colors duration-150">
                <i data-lucide="list" class="h-4 w-4"></i> @lang('My Loan List')
            </a>
        </div>
    </div>
@endpush