@include('admin.layouts.header')
@include('admin.layouts.sidebar')
<div class="m-grid__item m-grid__item--fluid m-wrapper">
    <div class="m-subheader">
        <div class="d-flex align-items-center">
            <div class="mr-auto">
                <h3 class="m-subheader__title m-subheader__title--separator">
                    Co-Lending Loans
                </h3>
            </div>
        </div>
    </div>
    <div class="m-content">
        <div class="m-portlet m-portlet--mobile">
            <div class="m-portlet__head">
                <div class="m-portlet__head-caption">
                    <div class="m-portlet__head-title">
                        <span class="m-portlet__head-icon">
                            <i class="fa fa-list"></i>
                        </span>
                        <h3 class="m-portlet__head-text">
                            Loans
                        </h3>
                    </div>
                </div>
                <div class="m-portlet__head-tools" style="padding-right: 13px;">
                    <a href="/admin/co-lending" class="btn btn-default btn-sm">
                        <i class="fa fa-arrow-left"></i> Back
                    </a>
                </div>
            </div>
            <div class="m-portlet__body">

                {{-- Search --}}
                <form method="GET" action="/admin/co-lending/loans" class="form-inline" style="margin-bottom: 15px; gap: 8px; display: flex; flex-wrap: wrap;">
                    <input type="text" name="search_loan" class="form-control form-control-sm" placeholder="Search by Loan ID"
                        value="{{ request('search_loan') }}" style="width: 220px;">
                    <input type="text" name="search_product" class="form-control form-control-sm" placeholder="Search by Product ID"
                        value="{{ request('search_product') }}" style="width: 200px;">
                    <input type="date" name="search_date" class="form-control form-control-sm"
                        value="{{ request('search_date') }}" style="width: 180px;">
                    <button type="submit" style="margin-top: -10px;" class="btn btn-accent btn-sm">Search</button>
                    @if(request('search_loan') || request('search_product') || request('search_date'))
                        <a href="/admin/co-lending/loans" style="margin-top: -10px;" class="btn btn-secondary btn-sm">Clear</a>
                    @endif
                </form>

                <div style="overflow-x: auto;">
                    <table class="table table-bordered table-hover" style="font-size: 13px; white-space: nowrap;">
                        <thead>
                            <tr style="background-color: #f4f5f8;">
                                <th>Loan ID</th>
                                <th>Loan Amount</th>
                                <th>Interest Rate</th>
                                <th>Tenure</th>
                                <th>Disbursement Date</th>
                                <th>First EMI Date</th>
                                <th>Product ID</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($loans as $loan)
                                <tr>
                                    <td style="font-weight: 600;">{{ $loan->loan_id ?? '—' }}</td>
                                    <td>{{ $loan->loan_amount !== null ? number_format($loan->loan_amount, 2) : '—' }}</td>
                                    <td>{{ $loan->interest_rate !== null ? $loan->interest_rate . '%' : '—' }}</td>
                                    <td>{{ $loan->tenure ?? '—' }}</td>
                                    <td>{{ $loan->disbursement_date ?? '—' }}</td>
                                    <td>{{ $loan->first_emi_date ?? '—' }}</td>
                                    <td>{{ $loan->product_id ?? '—' }}</td>
                                    <td>
                                        <a href="/admin/co-lending/loans/{{ $loan->id }}" class="btn btn-primary btn-sm" target="_blank">
                                            View RPS
                                        </a>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="8" class="text-center">No records found.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                <div class="text-center" style="margin-top: 15px;">
                    {!! $loans->appends(\Request::except('page', '_token'))->render() !!}
                </div>

            </div>
        </div>
    </div>
</div>
@include("admin.layouts.footer")
