@extends('layouts.user')

@section('title', __('loans.payment_title'))
@section('page_title', __('loans.payment_title'))
@section('breadcrumb', __('loans.payment_breadcrumb'))

@section('content')
<div class="max-w-6xl mx-auto px-3 py-4 space-y-4">

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-4">
        <!-- Left Info Panel -->
        <div class="lg:col-span-1 border border-slate-200 bg-white shadow-sm">
            <div class="border-b border-slate-200 bg-slate-50 px-4 py-3">
                <div class="text-[10px] uppercase tracking-[0.22em] text-slate-500 font-semibold">
                    {{ __('loans.loan_info') }}
                </div>
                <div class="mt-1 text-base font-bold text-slate-900 leading-tight">
                    {{ $loan->lender_name ?: __('loans.unknown_lender') }}
                </div>
                <div class="mt-1 text-[11px] text-slate-500">
                    {{ __('loans.ref_label') }}: {{ $loan->loan_no ?: __('loans.na') }}
                </div>
            </div>

            <div class="p-4">
                <div class="space-y-2 text-xs">
                    <div class="flex items-center justify-between gap-3 border-b border-dashed border-slate-200 pb-2">
                        <span class="text-slate-500">{{ __('loans.remaining') }}</span>
                        <span class="font-semibold text-slate-800">{{ number_format($remaining, 2) }}</span>
                    </div>

                    <div class="flex items-center justify-between gap-3 border-b border-dashed border-slate-200 pb-2">
                        <span class="text-slate-500">{{ __('loans.interest_rate') }}</span>
                        <span class="font-semibold text-slate-800">{{ number_format((float) $loan->interest_rate, 2) }}%</span>
                    </div>

                    <div class="flex items-center justify-between gap-3 border-b border-dashed border-slate-200 pb-2">
                        <span class="text-slate-500">{{ __('loans.base_date') }}</span>
                        <span class="font-semibold text-slate-800">{{ optional($baseDate)->format('d M Y') }}</span>
                    </div>

                    <div class="flex items-center justify-between gap-3 border-b border-dashed border-slate-200 pb-2">
                        <span class="text-slate-500">{{ __('loans.days') }}</span>
                        <span class="font-semibold text-slate-800">{{ $days }} {{ __('loans.days_suffix') }}</span>
                    </div>

                    <div class="flex items-center justify-between gap-3">
                        <span class="text-slate-500">{{ __('loans.estimated_interest') }}</span>
                        <span class="font-bold text-slate-900">{{ number_format($interestDue, 2) }}</span>
                    </div>
                </div>

                <div class="mt-4 border border-slate-200 bg-slate-50 px-3 py-3 text-[11px] leading-5 text-slate-600">
                    {!! __('loans.payment_info_hint') !!}
                </div>
            </div>
        </div>

        <!-- Right Form Panel -->
        <div class="lg:col-span-2 border border-slate-200 bg-white shadow-sm">
            <div class="border-b border-slate-200 bg-slate-900 px-4 py-3 text-white">
                <p class="text-[10px] uppercase tracking-[0.22em] text-slate-300 font-semibold">
                    {{ __('loans.loan_repayment') }}
                </p>
                <h1 class="mt-1 text-base font-bold leading-tight">{{ __('loans.add_payment') }}</h1>
                <p class="mt-1 text-[11px] text-slate-300">{{ __('loans.payment_hero_text') }}</p>
            </div>

            <form method="POST"
                  action="{{ route('user.loans.payments.store', $loan) }}"
                  enctype="multipart/form-data"
                  class="p-4 md:p-5 grid grid-cols-1 md:grid-cols-2 gap-4"
                  id="loanPaymentForm">
                @csrf

                <div>
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">
                        {{ __('loans.payment_account') }} <span class="text-rose-500">*</span>
                    </label>
                    <select
                        name="user_account_id"
                        required
                        class="w-full border border-slate-300 bg-white px-3 py-2.5 text-xs font-medium text-slate-800 outline-none transition hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >
                        <option value="">{{ __('loans.select_account') }}</option>
                        @foreach($accounts as $account)
                            <option value="{{ $account->id }}" @selected(old('user_account_id') == $account->id)>
                                {{ $account->name }} ({{ strtoupper($account->type) }})
                            </option>
                        @endforeach
                    </select>
                    @error('user_account_id')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">{{ __('loans.paid_at') }}</label>
                    <input
                        type="datetime-local"
                        name="paid_at"
                        value="{{ old('paid_at') }}"
                        class="w-full border border-slate-300 bg-white px-3 py-2.5 text-xs font-medium text-slate-800 outline-none transition hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >
                    @error('paid_at')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">
                        {{ __('loans.total_paid') }} <span class="text-rose-500">*</span>
                    </label>
                    <input
                        type="number"
                        step="any"
                        min="0"
                        name="total_paid"
                        id="total_paid"
                        value="{{ old('total_paid') }}"
                        placeholder="{{ __('loans.enter_total_payment_amount') }}"
                        required
                        class="w-full border border-slate-300 bg-white px-3 py-2.5 text-xs font-medium text-slate-800 outline-none transition placeholder:text-slate-400 hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >
                    @error('total_paid')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">{{ __('loans.interest_paid') }}</label>
                    <input
                        type="number"
                        step="any"
                        min="0"
                        name="interest_paid"
                        id="interest_paid"
                        value="{{ old('interest_paid', number_format($interestDue, 2, '.', '')) }}"
                        placeholder="{{ __('loans.enter_interest_amount') }}"
                        class="w-full border border-slate-300 bg-white px-3 py-2.5 text-xs font-medium text-slate-800 outline-none transition placeholder:text-slate-400 hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >
                    @error('interest_paid')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">{{ __('loans.principal_paid') }}</label>
                    <input
                        type="number"
                        step="any"
                        min="0"
                        name="principal_paid"
                        id="principal_paid"
                        value="{{ old('principal_paid') }}"
                        placeholder="{{ __('loans.leave_blank_for_auto_calculate') }}"
                        class="w-full border border-slate-300 bg-white px-3 py-2.5 text-xs font-medium text-slate-800 outline-none transition placeholder:text-slate-400 hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >
                    <div class="mt-1 text-[10px] text-slate-500">{{ __('loans.auto_calculate_hint') }}</div>
                    @error('principal_paid')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">{{ __('loans.fee_paid') }}</label>
                    <input
                        type="number"
                        step="any"
                        min="0"
                        name="fee_paid"
                        id="fee_paid"
                        value="{{ old('fee_paid', 0) }}"
                        placeholder="{{ __('loans.enter_fee_amount') }}"
                        class="w-full border border-slate-300 bg-white px-3 py-2.5 text-xs font-medium text-slate-800 outline-none transition placeholder:text-slate-400 hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >
                    @error('fee_paid')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div class="md:col-span-2 border border-slate-200 bg-slate-50 px-3 py-2.5 text-xs">
                    <span class="text-slate-600">{{ __('loans.auto_principal_preview') }}:</span>
                    <span id="principal_preview" class="ml-2 font-bold text-slate-900">0.00</span>
                </div>

                <div class="md:col-span-2">
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">{{ __('loans.proof_photo_pdf') }}</label>
                    <input
                        type="file"
                        name="proof_photo"
                        class="block w-full border border-slate-300 bg-white px-3 py-2 text-xs font-medium text-slate-700 file:mr-3 file:border-0 file:bg-slate-900 file:px-3 file:py-2 file:text-xs file:font-semibold file:text-white hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >
                    @error('proof_photo')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div class="md:col-span-2">
                    <label class="mb-1.5 block text-xs font-semibold text-slate-700">{{ __('loans.note') }}</label>
                    <textarea
                        name="note"
                        rows="4"
                        placeholder="{{ __('loans.write_additional_note') }}"
                        class="w-full border border-slate-300 bg-white px-3 py-2.5 text-xs font-medium text-slate-800 outline-none transition placeholder:text-slate-400 hover:border-slate-400 focus:border-slate-900 focus:ring-0"
                    >{{ old('note') }}</textarea>
                    @error('note')
                        <p class="mt-1 text-[11px] text-rose-600">{{ $message }}</p>
                    @enderror
                </div>

                <div class="md:col-span-2 flex justify-end gap-2 pt-1">
                    <a href="{{ route('user.loans.statement', $loan) }}"
                       class="border border-slate-300 bg-white px-4 py-2 text-xs font-semibold text-slate-700 transition hover:border-slate-400 hover:bg-slate-50">
                        {{ __('loans.back') }}
                    </a>

                    <button
                        type="submit"
                        class="bg-slate-900 px-4 py-2 text-xs font-semibold text-white shadow-sm transition hover:bg-slate-800"
                    >
                        {{ __('loans.save_payment') }}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const totalInput = document.getElementById('total_paid');
    const interestInput = document.getElementById('interest_paid');
    const feeInput = document.getElementById('fee_paid');
    const principalInput = document.getElementById('principal_paid');
    const preview = document.getElementById('principal_preview');

    function num(v) {
        const n = parseFloat(v);
        return isNaN(n) ? 0 : n;
    }

    function updatePreview() {
        const total = num(totalInput.value);
        const interest = num(interestInput.value);
        const fee = num(feeInput.value);
        const principal = total - interest - fee;
        preview.textContent = principal > 0 ? principal.toFixed(2) : '0.00';
    }

    [totalInput, interestInput, feeInput].forEach(el => {
        el.addEventListener('input', updatePreview);
    });

    updatePreview();
});
</script>
@endsection