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

@section('page-header')
<header class="header bg-ui-general">
    <div class="header-info">
        <h1 class="header-title">
            <strong>Loan Loan Payments History</strong>
        </h1>
    </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 ?? '-' }}</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->where('status', 'paid')->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->where('status', 'paid')->sum('paid_amount'), 2) }}
                </div>
            </div>
        </div>
    </div>

    <!-- Payments Table -->
    <div class="card">
        <div class="card-header">
            <h4 class="card-title fw-400">Payments History</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>Paid Amount</th>
                        <th>Payment Date</th>
                        <th>Bank Account</th>
                    </tr>
                </thead>
                <tbody>
                    @php $counter = 1; @endphp
                    @forelse($groupedPayments as $payment)
                    <tr>
                        <td>{{ $counter++ }}</td>
                        <!-- <td>{{ $payment['installment_no'] }}</td> -->
                        <td>{{ number_format($payment['total_amount'], 2) }}</td>
                        <td>{{ \Carbon\Carbon::parse($payment['payment_date'])->format('d-m-Y') }}</td>
                        <td>{{ $payment['bank_account_name'] }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="text-center">No payments found.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <!-- Savings Table -->
    <div class="card mt-3">
        <div class="card-header">
            <h4 class="card-title fw-400">সঞ্চয় History</h4>
        </div>
        <div class="card-body table-responsive">
            <table class="table table-bordered table-hover">
                <thead class="thead-light">
                    <tr>
                        <th>#</th>
                        <th>Type</th>
                        <th>Amount</th>
                        <th>Date</th>
                        @can('transaction')
                        <th class="text-center">Action</th>
                        @endcan
                    </tr>
                </thead>
                <tbody>
                    @php $counter = 1; @endphp
                    <!-- Deposits -->
                    @foreach($loan->savings as $saving)
                    <tr>
                        <td>{{ $counter++ }}</td>
                        <td>Deposit</td>
                        <td>{{ number_format($saving->amount, 2) }}</td>
                        <td>{{ \Carbon\Carbon::parse($saving->created_at)->format('d-m-Y') }}</td>
                        @can('transaction')
                        <td class="text-center">
                            <form action="{{ route('loans.savings.delete', $saving->id) }}" method="POST"
                                onsubmit="return confirm('Are you sure you want to delete this savings?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-sm btn-danger">Delete</button>
                            </form>
                        </td>
                        @endcan
                    </tr>
                    @endforeach

                    <!-- Withdrawals -->
                    @foreach($loan->savingsWithdrawals as $withdraw)
                    <tr>
                        <td>{{ $counter++ }}</td>
                        <td>Withdrawal</td>
                        <td>{{ number_format($withdraw->amount, 2) }}</td>
                        <td>{{ \Carbon\Carbon::parse($withdraw->withdraw_date)->format('d-m-Y') }}</td>
                        @can('transaction')
                        <td class="text-center">
                            <form action="{{ route('loans.savingsWithdrawals.delete', $withdraw->id) }}" method="POST"
                                onsubmit="return confirm('Are you sure you want to delete this withdrawal?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-sm btn-danger">Delete</button>
                            </form>
                        </td>
                        @endcan
                    </tr>
                    @endforeach

                    @if($loan->savings->isEmpty() && $loan->savingsWithdrawals->isEmpty())
                    <tr>
                        <td colspan="4" class="text-center">No সঞ্চয় activity found.</td>
                    </tr>
                    @endif
                </tbody>
                <tfoot>
                    <td></td>
                    <td></td>
                    <td> <strong>নিট সঞ্চয় :</strong> {{ number_format($totalSavings ?? 0, 2) }}</td>
                </tfoot>
            </table>

            <!-- <div class="mt-2">
            <strong>Net Savings:</strong> {{ number_format($totalSavings ?? 0, 2) }}
        </div> -->
        </div>
    </div>


</div>



@endsection

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

</style>
@endsection

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

</script>
@endsection
