@extends('layouts.master')
@section('title', 'Loan Details')

@section('page-header')
<header class="header bg-ui-general">
    <div class="header-info">
        <h1 class="header-title">
            <strong>Loan Details</strong>
        </h1>
    </div>

    <div class="header-action">
        <nav class="nav">
            <a class="nav-link" href="{{ route('loans.index') }}">
                Loans
            </a>
            <a class="nav-link active" href="#">
                <i class="fa fa-eye"></i>
                Loan Details
            </a>
        </nav>
    </div>
</header>
@endsection

@section('content')
<div class="col-md-12">

    <div class="card mb-3">
        <div class="card-header">
            <h4 class="card-title fw-400">Loan Information</h4>
        </div>
        <div class="card-body">
            <div class="row mb-2">
                <div class="col-md-6"><strong>Loan Number: {{ $loan->id }}</strong></div>
                <div class="col-md-6"><strong>Customer: {{ $loan->customer->name ?? '-' }}</strong></div>
            </div>
            <div class="row mb-2">
                <div class="col-md-6"><strong>Loan Amount: {{ number_format($loan->loan_amount, 2) }}</strong></div>
                <div class="col-md-6"><strong>Interest Rate: {{ $loan->interest_rate }}%</strong></div>
            </div>
            <div class="row mb-2">
                <div class="col-md-6"><strong>Total Interest: {{ number_format($loan->total_interest, 2) }}
                    ({{ $loan->interest_rate }}%)</strong></div>
                <div class="col-md-6"><strong>Total Payable: {{ number_format($loan->total_payable, 2) }}</strong></div>
            </div>
            <div class="row mb-2">
                <div class="col-md-6"><strong>Installment Type: {{ ucfirst($loan->installment_type) }}</strong></div>
                <div class="col-md-6"><strong>Number of Installments: {{ $loan->installment_count }}</strong></div>
            </div>
            <div class="row mb-2">
                <div class="col-md-6"><strong>Issue Date:
                    {{ \Carbon\Carbon::parse($loan->issue_date)->format('d-m-Y') }}</strong></div>
                <div class="col-md-6">
                    <strong>Status:</strong>
                    <form action="{{ route('loans.updateStatus', $loan->id) }}" method="POST" class="d-inline">
                        @csrf
                        @method('PATCH')
                        @php
                        $currentStatus = $loan->status ?? $loan->computed_status;
                        @endphp
                        <span class="{{ $loan->status_badge_class }}">
                            {{ ucfirst($currentStatus) }}
                        </span>
                        <select name="status" class="form-control form-control-sm" onchange="this.form.submit()">
                            <option value="pending" {{ $currentStatus === 'pending' ? 'selected' : '' }}>
                                Pending
                            </option>

                            <option value="accept" {{ $currentStatus === 'accept' ? 'selected' : '' }}>
                                Accept
                            </option>

                            <option value="completed" {{ $currentStatus === 'completed' ? 'selected' : '' }}>
                                Completed
                            </option>

                            <option value="overdue" {{ $currentStatus === 'overdue' ? 'selected' : '' }}>
                                Overdue
                            </option>
                        </select>
                    </form>
                </div>
                <div class="col-md-6"><strong>Total সঞ্চয়: {{ number_format($totalSavings, 2) }}</strong></div>
            </div>
        </div>
    </div>


     <!-- Savings Transfer Form -->
       @if($loans->count() > 1)
    <div class="card mb-3">
        <div class="card-header">
            <h4 class="card-title fw-400">Transfer Savings Between Loans</h4>
        </div>
        <div class="card-body">
            <form action="{{ route('loans.transferSavings') }}" method="POST">
                @csrf

                <div class="form-group">
                    <label for="source_loan_id">Source Loan</label>
                    <select name="source_loan_id" class="form-control">
                        <option value="{{ $loan->id }}" selected>{{ $loan->id }} - {{ $loan->customer->name ?? '-' }}</option>
                        @foreach($loans as $otherLoan)
                            @if($otherLoan->id !== $loan->id)
                                <option value="{{ $otherLoan->id }}">{{ $otherLoan->id }} - {{ $otherLoan->customer->name ?? '-' }}</option>
                            @endif
                        @endforeach
                    </select>
                </div>

                <div class="form-group">
                    <label for="target_loan_id">Target Loan</label>
                    <select name="target_loan_id" class="form-control">
                        <option value="{{ $loan->id }}" selected>{{ $loan->id }} - {{ $loan->customer->name ?? '-' }}</option>
                        @foreach($loans as $otherLoan)
                            @if($otherLoan->id !== $loan->id)
                                <option value="{{ $otherLoan->id }}">{{ $otherLoan->id }} - {{ $otherLoan->customer->name ?? '-' }}</option>
                            @endif
                        @endforeach
                    </select>
                </div>

                <div class="form-group">
                    <label for="savings_amount">Amount to Transfer</label>
                    <input type="number" name="savings_amount" class="form-control" min="1" max="{{ $totalSavings }}" required>
                    <small class="form-text text-muted">Max Transferable: {{ number_format($totalSavings, 2) }}</small>
                </div>

                <button type="submit" class="btn btn-primary">Transfer Savings</button>
            </form>
        </div>
    </div>
    @else
    <div class="alert alert-info">
        There is only one loan for this customer, so savings cannot be transferred.
    </div>
    @endif


</div>
@endsection

@section('styles')
<style>
    /* Optional custom styles */

</style>
@endsection

@section('scripts')
<script>
    /* Optional scripts */

</script>
@endsection
