@include('admin.layouts.header')
@include('admin.layouts.sidebar')
<div class="m-grid__item m-grid__item--fluid m-wrapper">
    <div class="m-subheader">
       <iframe
                src="{!! $metabase_data->url or '' !!}"
                frameborder="0"
                width="100%"
                height="300"
                allowtransparency
        ></iframe>
    </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/da-system" 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/da-system/loans" class="form-inline" style="margin-bottom: 15px; gap: 8px; display: flex; flex-wrap: wrap;">
                    <input type="text" name="search_name" class="form-control form-control-sm" placeholder="Search by Name"
                        value="{{ request('search_name') }}" style="width: 220px;">
                    <input type="text" name="search_loan" class="form-control form-control-sm" placeholder="Search by Loan No"
                        value="{{ request('search_loan') }}" style="width: 200px;">
                    <input type="text" name="search_partner" class="form-control form-control-sm" placeholder="Search by Partner Name"
                        value="{{ request('search_partner') }}" style="width: 220px;">
                    <button type="submit" style="margin-top: -10px;" class="btn btn-accent btn-sm">Search</button>
                    @if(request('search_name') || request('search_loan') || request('search_partner'))
                        <a href="/admin/da-system/loans" style="margin-top: -10px;" class="btn btn-secondary btn-sm">Clear</a>
                    @endif
                </form>

                <div class="" 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 No</th>
                                <th>Partner Name</th>
                                <th>Member Name</th>
                                <th>Branch</th>
                                <th>District</th>
                                <th>State</th>
                                <th>Disbursement Date</th>
                                <th>Status</th>
                                <th>Outstanding Principal</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($loans as $loan)
                                <tr>
                                    <td><a style="text-decoration: underline;" target="_blank" href="/admin/da-system/loans/{{ $loan->id }}">{{ $loan->loan_no ?? '—' }}</a></td>
                                    <td>{{ $loan->partner_id ?? '—' }}</td>
                                    <td>{{ $loan->member_name ?? '—' }}</td>
                                    <td>{{ $loan->branch_name ?? '—' }}</td>
                                    <td>{{ $loan->district ?? '—' }}</td>
                                    <td>{{ $loan->state ?? '—' }}</td>
                                    <td>{{ $loan->disbursement_date ?? '—' }}</td>
                                    <td>
                                        @if($loan->status)
                                            <span style="padding: 3px 8px; border-radius: 3px; font-size: 11px; font-weight: 600;
                                                background-color: {{ strtolower($loan->status) == 'active' ? '#c9f7f5' : '#fff4de' }};
                                                color: {{ strtolower($loan->status) == 'active' ? '#1bc5bd' : '#ffa800' }};">
                                                {{ $loan->status }}
                                            </span>
                                        @else
                                            —
                                        @endif
                                    </td>
                                    <td>{{ $loan->outstanding_principal ?? '—' }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="12" 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")
