@extends('layouts.master')
@section('title', 'Loans')

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

</header>
@endsection

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

    <div class="card card-body mb-2">
        <form method="GET" class="mb-4">
            <div class="form-row">
                <!-- Customer -->
                <div class="form-group col-md-3">
                    <select name="customer_id" class="form-control" data-provide="selectpicker" data-live-search="true">
                        <option value="">Select Customer</option>
                        @foreach($customers as $customer)
                        <option value="{{ $customer->id }}"
                            {{ old(key: 'customer_id') == $customer->id ? 'selected' : '' }}>
                            {{ $customer->name }} ({{ $customer->mobile_no }})
                        </option>
                        @endforeach
                    </select>
                </div>

                <!-- Status -->
                <div class="form-group col-md-3">
                    <select name="status" class="form-control">
                        <option value="">Select Status</option>
                        <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                        <option value="accept" {{ request('status') == 'accept' ? 'selected' : '' }}>Accept</option>
                        <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>Completed
                        </option>
                        <option value="overdue" {{ request('status') == 'overdue' ? 'selected' : '' }}>Overdue</option>
                    </select>
                </div>

                <!-- Start Date -->
                <div class="form-group col-md-2">
                    <input type="text" class="form-control" data-provide="datepicker" data-date-format="yyyy-mm-dd"
                        name="start_date" placeholder="Start Date" value="{{ request('start_date') }}">
                </div>

                <!-- End Date -->
                <div class="form-group col-md-2">
                    <input type="text" class="form-control" data-provide="datepicker" data-date-format="yyyy-mm-dd"
                        name="end_date" placeholder="End Date" value="{{ request('end_date') }}">
                </div>

                <!-- Buttons -->
                <div class="form-group col-md-2">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="#" class="btn btn-info">Reset</a>
                </div>
            </div>
        </form>
    </div>

    <div class="card">
        <div class="card-header">
            <h4 class="card-title fw-400">All Loans</h4>
        </div>

        <div class="card-body table-responsive">
            <table class="table table-bordered">
    <thead>
        <tr>
            <th>Loan ID</th>
            <th>Customer</th>
            <th>Loan Amount</th>
            <th>Total Payable</th>
            <th>Total Paid</th>
            <th>Total Due</th>
            <th>Status</th>
            <th>Issue Date</th>
        </tr>
    </thead>

    <tbody>
        @foreach($reportLoans as $loan)
        <tr>
            <td>{{ $loan['loan_id'] }}</td>
            <td>{{ $loan['customer'] }}</td>
            <td>{{ number_format($loan['loan_amount'], 2) }}</td>
            <td>{{ number_format($loan['total_payable'], 2) }}</td>
            <td>{{ number_format($loan['total_paid'], 2) }}</td>
            <td>{{ number_format($loan['total_due'], 2) }}</td>
            <td>{{ ucfirst($loan['status']) }}</td>
            <td>{{ $loan['issue_date'] }}</td>
        </tr>
        @endforeach
    </tbody>

    {{-- Totals --}}
    @if($reportLoans->count())
        <tfoot>
            <tr class="fw-bold bg-light">
                <td colspan="2">Grand Total</td>
                <td>{{ number_format($totalLoanAmount, 2) }}</td>
                <td>{{ number_format($totalPayable, 2) }}</td>
                <td>{{ number_format($totalPaid, 2) }}</td>
                <td>{{ number_format($totalDue, 2) }}</td>
                <td colspan="2"></td>
            </tr>
        </tfoot>
    @endif
</table>


            <div class="mt-3">
                {{ $loans->links() }}
            </div>
        </div>
    </div>
</div>
@endsection

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

</style>
@endsection

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

</script>
@endsection
