@extends('layouts.master')
@section('title', 'Loan Installments & Payments')

@section('page-header')
<header class="header bg-ui-general">
    <div class="header-info">
        <h1 class="header-title">
            <strong>Loan Installments & Payments</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="#">Installments & Payments</a>
        </nav>
    </div>
</header>
@endsection

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

    <!-- Loan Summary -->
    <div class="card mb-3">
        <div class="card-header">
            <h4 class="card-title fw-400">Loan Summary</h4>
        </div>
        <div class="card-body">
            <div class="row mb-2">
                <div class="col-md-6"><strong>Loan Number:</strong> {{ $loan->id }}</div>
                <div class="col-md-6"><strong>Customer:</strong> {{ $loan->customer->name ?? '-' }} - {{ $loan->customer->account_no ?? '-' }}</div>
            </div>
            <div class="row mb-2">
                <div class="col-md-6"><strong>Loan Amount:</strong> {{ number_format($loan->loan_amount, 2) }}</div>
                <div class="col-md-6"><strong>Interest Rate:</strong> {{ $loan->interest_rate }}%</div>
            </div>
            <div class="row mb-2">
                <div class="col-md-6"><strong>Total Paid:</strong>
                    {{ number_format($loan->installments->sum('paid_amount'), 2) }}</div>
                <div class="col-md-6"><strong>Due:</strong>
                    {{ number_format($loan->loan_amount + ($loan->loan_amount * $loan->interest_rate/100) - $loan->installments->sum('paid_amount'), 2) }}
                </div>
            </div>
            <div class="row mb-2">
                <div class="col-md-6"><strong>Total সঞ্চয়:</strong>
                    {{ number_format($totalSavings, 2) }}</div>
                
            </div>
                <div class="col-md-6">
                    <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#collectSavingsModal">
            <i class="fa fa-plus"></i> Add সঞ্চয়
        </button>

          <div class="modal fade" id="collectSavingsModal" tabindex="-1" role="dialog" aria-labelledby="collectSavingsModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <form action="{{ route('loans.savings.collect', $loan->id) }}" method="POST">
                    @csrf
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="collectSavingsModalLabel">Add সঞ্চয়</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <div class="form-group">
                                <label>সঞ্চয় Amount <span class="text-danger">*</span></label>
                                <input type="number" step="0.01" name="savings_amount" class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Payment Date <span class="text-danger">*</span></label>
                                <input type="date" name="paid_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                            </div>
                            <div class="form-group">
                                <label>Bank Account</label>
                                <select name="bank_account_id" class="form-control" required>
                                    @foreach(App\BankAccount::all() as $account)
                                        <option value="{{ $account->id }}">{{ $account->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary">Add সঞ্চয়</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
                
            </div>
        </div>
    </div>

    <!-- Installments Table -->
    <div class="card mb-3">
        <div class="card-header">
            <h4 class="card-title fw-400">Installments</h4>
        </div>
        <div class="card-body table-responsive">
            <table class="table table-bordered table-hover">
                <thead class="thead-light">
                    <tr>
                        <th>#</th>
                        <th>Installment No</th>
                        <!-- <th>Due Date</th> -->
                        <th>Total Amount</th>
                        <th>Paid Amount</th>
                        <th>Remaining Amount</th>
                        <th>Status</th>
                        <th>Paid On</th>
                        <th class="text-center">Action</th>
                    </tr>
                </thead>
                 <tbody>
                @forelse($loan->installments as $installment)
                @php
                    $remaining = $installment->total_amount - $installment->paid_amount;
                @endphp
                <tr @if($installment->status == 'overdue') class="table-danger" @endif>
                    <td>{{ $loop->iteration }}</td>
                    <td>{{ $installment->installment_no }}</td>
                    <!-- <td>{{ \Carbon\Carbon::parse($installment->due_date)->format('d-m-Y') }}</td> -->
                    <td>{{ number_format($installment->total_amount, 2) }}</td>
                    <td>{{ number_format($installment->paid_amount, 2) }}</td>
                    <td>{{ number_format($remaining, 2) }}</td>
                    <td>
                        @if($installment->status == 'pending')
                            <span class="badge badge-warning">Pending</span>
                        @elseif($installment->status == 'paid')
                            <span class="badge badge-success">Paid</span>
                        @elseif($installment->status == 'overdue')
                            <span class="badge badge-danger">Overdue</span>
                        @endif
                    </td>
                    <td>{{ $installment->paid_date ? \Carbon\Carbon::parse($installment->paid_date)->format('d-m-Y') : '-' }}</td>
                    <td>
                        @if($installment->status != 'paid')
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-sm btn-success" data-toggle="modal"
                            data-target="#collectPaymentModal{{ $installment->id }}">
                            <i class="fa fa-money-bill"></i> Collect
                        </button>

                        <!-- Modal -->
                        <div class="modal fade" id="collectPaymentModal{{ $installment->id }}" tabindex="-1"
                            role="dialog" aria-labelledby="collectPaymentModalLabel{{ $installment->id }}"
                            aria-hidden="true">
                            <div class="modal-dialog" role="document">
                                <form action="{{ route('loans.installments.collect', $installment->id) }}" method="POST">
                                    @csrf
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title"
                                                id="collectPaymentModalLabel{{ $installment->id }}">
                                                Collect Payment - Installment {{ $installment->installment_no }}
                                            </h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body">
                                            <div class="form-group">
                                                <label>Payment Amount<span class="text-danger">*</span></label>
                                                <input type="number" step="0.01" name="amount" class="form-control"
                                                    value="{{ $remaining }}" required>
                                            </div>
                                            <div class="form-group">
                                                    <label>সঞ্চয় </label>
                                                    <input type="number" step="0.01" name="savings_amount" class="form-control" placeholder="Savings Amount (optional)">
                                                </div>
                                            <div class="form-group">
                                                <label>Payment Date<span class="text-danger">*</span></label>
                                                <input type="date" name="paid_date" class="form-control"
                                                    value="{{ date('Y-m-d') }}" required>
                                            </div>
                                            <div class="form-group">
                                                   <label>Bank Account</label>
                                                         <select name="bank_account_id" class="form-control" required>
                                                            @foreach(App\BankAccount::all() as $account)
                                                                <option value="{{ $account->id }}">{{ $account->name }}</option>
                                                            @endforeach
                                                        </select>
                                                </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                            <button type="submit" class="btn btn-success">Collect Payment</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                        @else
                            <span class="text-muted">N/A</span>
                        @endif
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="9" class="text-center">No installments found.</td>
                </tr>
                @endforelse
            </tbody>
            </table>
        </div>
    </div>

</div>



@endsection

@section('styles')
<style>
    /* Custom styling if needed */

</style>
@endsection

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

</script>
@endsection
