<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Loan Application {{ $loanApplication->application_id }}</title>
    <style>
        body {
            color: #222;
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
            line-height: 1.45;
        }

        h1 {
            color: #104042;
            font-size: 22px;
            margin: 0 0 6px;
        }

        h2 {
            border-bottom: 1px solid #d7dddd;
            color: #104042;
            font-size: 15px;
            margin: 22px 0 10px;
            padding-bottom: 6px;
        }

        table {
            border-collapse: collapse;
            width: 100%;
        }

        td {
            border: 1px solid #e1e5e5;
            padding: 7px 8px;
            vertical-align: top;
            width: 50%;
        }

        .label {
            color: #666;
            display: block;
            font-size: 10px;
            margin-bottom: 2px;
            text-transform: uppercase;
        }

        .value {
            font-weight: 600;
        }

        .meta {
            color: #555;
            margin-bottom: 14px;
        }
    </style>
</head>
<body>
@php
    $money = function ($value) {
        return 'Rs. ' . number_format((float) ($value ?? 0));
    };

    $date = function ($value) {
        return $value ? \Carbon\Carbon::parse($value)->format('m-d-Y') : 'N/A';
    };

    $sections = [
        'Application' => [
            'Application Number' => $loanApplication->application_id,
            'Status' => ucfirst($loanApplication->status ?? 'N/A'),
            'Phone' => $loanApplication->phone,
            'Email' => $loanApplication->email,
            'Organization' => $loanApplication->organization,
        ],
        'Personal Information' => [
            'Full Name' => $loanApplication->full_name,
            'Date of Birth' => $date($loanApplication->dob),
            'Gender' => $loanApplication->gender,
            'Marital Status' => $loanApplication->marital_status,
            'Father Name' => $loanApplication->father_name,
            'Aadhaar Number' => $loanApplication->aadhaar_number,
            'PAN Number' => $loanApplication->pan_number,
            'Passport' => $loanApplication->passport,
        ],
        'Address Details' => [
            'Current Address' => $loanApplication->current_address,
            'Current City' => $loanApplication->current_city,
            'Current State' => $loanApplication->current_state,
            'Current Country' => $loanApplication->current_country,
            'Permanent Address' => $loanApplication->permanent_address,
            'Permanent City' => $loanApplication->permanent_city,
            'Permanent State' => $loanApplication->permanent_state,
            'Permanent Country' => $loanApplication->permanent_country,
            'Residence Type' => $loanApplication->residence_type,
            'Address Duration' => $loanApplication->address_duration,
            'Pincode' => $loanApplication->pincode,
        ],
        'Loan Details' => [
            'Loan Type' => $loanApplication->loan_type,
            'Purpose' => $loanApplication->purpose_of_loan,
            
            'Kit Cost' => $money($loanApplication->kit_cost),
            'Down Payment' => $money($loanApplication->down_payment),
            'Loan Amount' => $money($loanApplication->loan_amount),
            'Period in Months' => ($loanApplication->loan_tenure ?? 0) . ' Months',
            'Membership Fees' => $money($loanApplication->membership_fees),
            'Documentation Fees' => $money($loanApplication->documentation_fees),
             'Processing Charges' => $money($loanApplication->processing_charges),
            'Total Payment' => $money($loanApplication->total_payment),
            'Rate of Interest' => $loanApplication->rate_of_intrest !== null && $loanApplication->rate_of_intrest !== ''
                ? $loanApplication->rate_of_intrest . '%'
                : 'N/A',
            'Total Interest' => $money($loanApplication->total_interest),  
           
            'Loan Principle' => $money($loanApplication->loan_principle),
            'Total Intrest' => $money($loanApplication->intrest_principle),
            ' Total Deduction ' => $money($loanApplication->deduction_principle),

            'Preferred EMI' => $money($loanApplication->preferred_emi),
            'Current EMI' => $money($loanApplication->current_emi),
            'Existing Loans' => $loanApplication->existing_loans,
            'Credit Card Dues' => $loanApplication->credit_card_dues,
            'Outstanding Amount' => $money($loanApplication->outstanding_amount),

        ],
        'Financial Details' => [
            'Occupation' => $loanApplication->occupation,
            'Employment Type' => $loanApplication->employment_type,
            'Monthly Income' => $money($loanApplication->monthly_income),
            'Annual Income' => $money($loanApplication->annual_income),
            'Other Income' => $loanApplication->other_income,
            'Income Particulars' => $loanApplication->income_particulars,
            'Repayment Schedule' => $loanApplication->repayment_schedule,
            'Renewal After' => $loanApplication->renewal_after,
            'Security Purpose' => $loanApplication->security_purpose,
            'Salary Bank' => $loanApplication->salary_bank,
            'Investments' => $loanApplication->investments,
            'Property Details' => $loanApplication->preperty_details,
            'Vehicles Owned' => $loanApplication->vehicles_owned,
            'Net Worth' => $loanApplication->networth,
            'Type of Security' => $loanApplication->type_of_security,
            'Estimated Value' => $loanApplication->estimated_value,
            'Ownership Details' => $loanApplication->ownership_details,
            'Bank Details' => $loanApplication->bank_details,
            'Account Number' => $loanApplication->account_number,
            'IFSC Code' => $loanApplication->ifsc_code,
        ],
        'Documents Included' => [
            'ID Proof' => $loanApplication->id_proof ? 'Yes' : 'No',
            'Address Proof' => $loanApplication->address_proof ? 'Yes' : 'No',
            'Income Proof' => $loanApplication->income_proof ? 'Yes' : 'No',
            'Bank Statements Proof' => $loanApplication->bankstatements_proof ? 'Yes' : 'No',
            'Business Proof' => $loanApplication->business_proof ? 'Yes' : 'No',
            'OnBoarding Letter' => $loanApplication->on_boarding_letter ? 'Yes' : 'No',
            ' Background Verification' => $loanApplication->background_verification ? 'Yes' : 'No',
            'File Name 3' => $loanApplication->file_name3 ? 'Yes' : 'No',
            'File Name 4' => $loanApplication->file_name4 ? 'Yes' : 'No',
            'File Name 5' => $loanApplication->file_name5 ? 'Yes' : 'No',
            'Photo Proof' => $loanApplication->photo_proof ? 'Yes' : 'No',
        ],
    ];
@endphp

<h1>Loan Application Details</h1>
<div class="meta">
    Application Number: {{ $loanApplication->application_id ?? 'N/A' }}<br>
    Generated: {{ now()->format('m-d-Y h:i A') }}
</div>

@foreach($sections as $title => $items)
    <h2>{{ $title }}</h2>
    <table>
        @foreach(array_chunk($items, 2, true) as $row)
            <tr>
                @foreach($row as $label => $value)
                    <td>
                        <span class="label">{{ $label }}</span>
                        <span class="value">{{ $value !== null && $value !== '' ? $value : 'N/A' }}</span>
                    </td>
                @endforeach

                @if(count($row) === 1)
                    <td></td>
                @endif
            </tr>
        @endforeach
    </table>
@endforeach
</body>
</html>
