@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">
                    Borrower Detail — {{ $loan->loan_no ?? '—' }}
                </h3>
            </div>
        </div>
    </div>
    <div class="m-content">

        {{-- Action Buttons --}}
        <div style="margin-bottom: 15px; display: flex; gap: 10px;">
            <a href="/admin/da-system/loans" class="btn btn-default btn-sm">
                <i class="fa fa-arrow-left"></i> Back to Loans
            </a>
            <a href="#" class="btn btn-primary btn-sm">
                <i class="fa fa-calendar"></i> Loan Schedule
            </a>
            <a href="#" class="btn btn-success btn-sm">
                <i class="fa fa-credit-card"></i> Payment Page
            </a>
        </div>

        @php
            function daVal($v) { return ($v !== null && $v !== '') ? $v : '—'; }
        @endphp
{{-- Personal Information --}}
        <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-user"></i></span>
                        <h3 class="m-portlet__head-text">Personal Information</h3>
                    </div>
                </div>
            </div>
            <div class="m-portlet__body">
                <div class="row" style="font-size: 13px;">
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Member Name</div>
                        <div style="font-weight: 600;">{{ daVal($loan->member_name) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Gender</div>
                        <div>{{ daVal($loan->gender) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Date of Birth</div>
                        <div>{{ daVal($loan->date_of_birth) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Age</div>
                        <div>{{ daVal($loan->age) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Mobile Number</div>
                        <div>{{ daVal($loan->mobile_number) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Aadhaar Number</div>
                        <div>{{ daVal($loan->aadhar_number) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">PAN Card</div>
                        <div>{{ daVal($loan->pan_card_number) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Father Name</div>
                        <div>{{ daVal($loan->father_name) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Spouse Name</div>
                        <div>{{ daVal($loan->spouse_name) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Caste</div>
                        <div>{{ daVal($loan->caste) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Religion</div>
                        <div>{{ daVal($loan->religion) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Monthly Income</div>
                        <div>{{ daVal($loan->monthly_income) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Credit Score</div>
                        <div>{{ daVal($loan->credit_score) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Bank Account</div>
                        <div>{{ daVal($loan->bank_account) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Bank IFSC</div>
                        <div>{{ daVal($loan->bank_ifsc_code) }}</div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Address --}}
        <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-map-marker"></i></span>
                        <h3 class="m-portlet__head-text">Address</h3>
                    </div>
                </div>
            </div>
            <div class="m-portlet__body">
                <div class="row" style="font-size: 13px;">
                    <div class="col-md-6 col-sm-12" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Address</div>
                        <div>{{ daVal($loan->address) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Village</div>
                        <div>{{ daVal($loan->village_name) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">City</div>
                        <div>{{ daVal($loan->city) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">District</div>
                        <div>{{ daVal($loan->district) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">State</div>
                        <div>{{ daVal($loan->state) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Pincode</div>
                        <div>{{ daVal($loan->pincode) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Rural / Urban</div>
                        <div>{{ daVal($loan->rural_urban) }}</div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Nominee & Co-borrower --}}
        <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-users"></i></span>
                        <h3 class="m-portlet__head-text">Nominee & Co-borrower</h3>
                    </div>
                </div>
            </div>
            <div class="m-portlet__body">
                <div class="row" style="font-size: 13px;">
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Nominee Name</div>
                        <div>{{ daVal($loan->nominee_name) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Nominee DOB</div>
                        <div>{{ daVal($loan->nominee_dob) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Nominee Relation</div>
                        <div>{{ daVal($loan->nominee_relation) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Co-borrower Name</div>
                        <div>{{ daVal($loan->co_borrower_name) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Co-borrower Contact</div>
                        <div>{{ daVal($loan->co_borrower_contact_no) }}</div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Loan Information --}}
        <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-file-text-o"></i></span>
                        <h3 class="m-portlet__head-text">Loan Information</h3>
                    </div>
                </div>
            </div>
            <div class="m-portlet__body">
                <div class="row" style="font-size: 13px;">
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Loan No</div>
                        <div style="font-weight: 600;">{{ daVal($loan->loan_no) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Partner</div>
                        <div>{{ daVal($loan->partner_id) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Status</div>
                        <div>
                            @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
                        </div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Disbursement Date</div>
                        <div>{{ daVal($loan->disbursement_date) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Disbursement Amount</div>
                        <div>{{ daVal($loan->disbursement_amount) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Tenure</div>
                        <div>{{ daVal($loan->tenure) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Interest Rate</div>
                        <div>{{ daVal($loan->int_rate) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Repayment Frequency</div>
                        <div>{{ daVal($loan->repayment_frequency) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Next Demand Date</div>
                        <div>{{ daVal($loan->next_demand_date) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Last Maturity Date</div>
                        <div>{{ daVal($loan->last_maturity_date) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Branch</div>
                        <div>{{ daVal($loan->branch_name) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Product</div>
                        <div>{{ daVal($loan->prod_classification) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Repayment Mode</div>
                        <div>{{ daVal($loan->repayment_mode) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Repayment Bank</div>
                        <div>{{ daVal($loan->repayment_bank) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Instalment Amount</div>
                        <div>{{ daVal($loan->inst_amt_rs) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Loan Closure Date</div>
                        <div>{{ daVal($loan->loan_closure_date) }}</div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Financial Details --}}
        <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-bar-chart"></i></span>
                        <h3 class="m-portlet__head-text">Financial Details</h3>
                    </div>
                </div>
            </div>
            <div class="m-portlet__body">
                <div class="row" style="font-size: 13px;">
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Outstanding Principal</div>
                        <div style="font-weight: 600;">{{ daVal($loan->outstanding_principal) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Outstanding Interest</div>
                        <div>{{ daVal($loan->outstanding_interest) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">OD Days</div>
                        <div>{{ daVal($loan->od_days) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">OD Bucket</div>
                        <div>{{ daVal($loan->od_bucket) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Principal Arrear</div>
                        <div>{{ daVal($loan->principal_arrear) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Interest Arrear</div>
                        <div>{{ daVal($loan->interest_arrear) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Total Arrear</div>
                        <div>{{ daVal($loan->total_arrear) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Last Collection Date</div>
                        <div>{{ daVal($loan->last_coll_date) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Last Collection Amount</div>
                        <div>{{ daVal($loan->last_coll_amount) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Overdue Instalments</div>
                        <div>{{ daVal($loan->over_due_inst) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Overdue Amount</div>
                        <div>{{ daVal($loan->over_due_amount_rs) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Waive-off Amount</div>
                        <div>{{ daVal($loan->waiveoff_amount) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Write-off Amount</div>
                        <div>{{ daVal($loan->write_off_amount) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">NPA Date</div>
                        <div>{{ daVal($loan->npa_date) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">Peak DPD</div>
                        <div>{{ daVal($loan->peak_dpd) }}</div>
                    </div>
                    <div class="col-md-3 col-sm-6" style="margin-bottom: 12px;">
                        <div style="color: #888; font-size: 11px; text-transform: uppercase; margin-bottom: 3px;">DPD String</div>
                        <div>{{ daVal($loan->dpd_string) }}</div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Documents --}}
        <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-folder-open"></i></span>
                        <h3 class="m-portlet__head-text">Documents</h3>
                    </div>
                </div>
            </div>
            <div class="m-portlet__body">
                @if(isset($documents) && count($documents) > 0)
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered align-middle mb-0" style="font-size: 13px;">
                            <thead>
                                <tr style="background-color: #f4f5f8;">
                                    <th style="width: 50px;" class="text-center">#</th>
                                    <th style="width: 60px;" class="text-center">Type</th>
                                    <th>Document Name</th>
                                    <th>Created At</th>
                                    <th style="width: 100px;" class="text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($documents as $index => $doc)
                                    @php
                                        $fileName = $doc->file_name ?? basename($doc->file_path);
                                        $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
                                        $iconClass = 'fa-file-text-o';
                                        if (in_array($ext, ['pdf'])) {
                                            $iconClass = 'fa-file-pdf-o text-danger';
                                        } elseif (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'])) {
                                            $iconClass = 'fa-file-image-o text-info';
                                        } elseif (in_array($ext, ['doc', 'docx'])) {
                                            $iconClass = 'fa-file-word-o text-primary';
                                        } elseif (in_array($ext, ['xls', 'xlsx', 'csv'])) {
                                            $iconClass = 'fa-file-excel-o text-success';
                                        }
                                    @endphp
                                    <tr>
                                        <td class="text-center">{{ $index + 1 }}</td>
                                        <td class="text-center" style="font-size: 18px;">
                                            <i class="fa {{ $iconClass }}"></i>
                                        </td>
                                        <td>
                                            <a href="{{ route('admin.da-system.document.view', $doc->id) }}" target="_blank" style="font-weight: 600; color: #36a3f7;">
                                                {{ $fileName }}
                                            </a>
                                        </td>
                                        <td>
                                            {{ !empty($doc->created_at) ? \Carbon\Carbon::parse($doc->created_at)->format('Y-m-d H:i:s') : '—' }}
                                        </td>
                                        <td class="text-center">
                                            <a href="{{ route('admin.da-system.document.view', $doc->id) }}" target="_blank" class="btn btn-sm btn-outline-primary" style="padding: 2px 10px; font-size: 11px;">
                                                <i class="fa fa-eye"></i> View
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="text-center text-muted" style="padding: 25px;">
                        <i class="fa fa-folder-open-o" style="font-size: 24px; display: block; margin-bottom: 8px; color: #ccc;"></i>
                        No documents found for this loan.
                    </div>
                @endif
            </div>
        </div>
    </div>
</div>
@include("admin.layouts.footer")
