<?php
    $coAppMandateOverride = isset($fees['co_applicant_required_for_mandate']) && $fees['co_applicant_required_for_mandate'] == 1;
    $isKYCStepAvailable = (isset($kycDetails->applicant) && $kycDetails->applicant->kyc_completed && $kycDetails->applicant->kyc_status == 'rejected') || (isset($kycDetails->applicant)) && !$kycDetails->applicant->kyc_completed;
    if (isset($skipSteps->applicant) && isset($skipSteps->applicant['vkyc']) && $skipSteps->applicant['vkyc'] == 0) {
        $isKYCStepAvailable = false;
    }

    $isCoKYCStepAvailable = (isset($kycDetails->co_applicant) && $kycDetails->co_applicant->kyc_completed && $kycDetails->co_applicant->kyc_status == 'rejected') || (isset($kycDetails->co_applicant) && !$kycDetails->co_applicant->kyc_completed);
    if (isset($skipSteps->co_applicant) && isset($skipSteps->co_applicant['vkyc']) && $skipSteps->co_applicant['vkyc'] == 0) {
        $isCoKYCStepAvailable = false;
    }

    $isMandateStepAvailable = 
            (isset($fees['set_auto_debit']) && $applicantType==1)  ||
            (request()->has('mandate_setup') && request()->get('mandate_setup') != 'complete');
    $isCoAppMandateStepAvailable = 
            (isset($fees['set_auto_debit_co_applicant']) && $applicantType==2) ||
            (request()->has('mandate_setup') && request()->get('mandate_setup') != 'complete');

    $paymentSkipped = request()->get('skip_payment') === '1';
    $isPaymentFailed = request()->has('payment') && request()->get('payment') == 'fail';

    $isPaymentStepAvailable = false;
    if (request()->has('payment') && request()->get('payment') == 'success') {
        $isPaymentStepAvailable = false;
    }
    elseif ((isset($fees['total_payment_link']) && $fees['total_payment_link'] != '') || $isPaymentFailed) {
        $isPaymentStepAvailable = true;
    }

    $showPaymentSection = false; 
    $paymentDataAvailable = $isPaymentStepAvailable && !$paymentSkipped;

    if ($applicantType == 1 && $paymentDataAvailable) {
        $showPaymentSection = true; // Show by default
        if (!$isPaymentFailed && isset($skipSteps->applicant, $skipSteps->applicant['payment']) && $skipSteps->applicant['payment'] == 0) {
            $showPaymentSection = false; 
        }
    } else if ($applicantType == 2 && $paymentDataAvailable) {
        $showPaymentSection = true; // Show by default
        if (!$isPaymentFailed && isset($skipSteps->co_applicant, $skipSteps->co_applicant['payment']) && $skipSteps->co_applicant['payment'] == 0) {
            $showPaymentSection = false; 
        }
    }
    
    $showApplicantMandate = false; // Default to false
    if ($applicantType == 1) {
        $showApplicantMandate = $isMandateStepAvailable && !$showPaymentSection;
        if (isset($skipSteps->applicant, $skipSteps->applicant['mandate']) && $skipSteps->applicant['mandate'] == 0) {
            $showApplicantMandate = false;
        }
    }

    $showCoAppMandateSection = false; // Default to false
    if ($applicantType == 2 && $isCoAppMandateStepAvailable) {
        $skipJsonSaysHide = isset($skipSteps->co_applicant, $skipSteps->co_applicant['mandate']) && $skipSteps->co_applicant['mandate'] == 0;

        if ($coAppMandateOverride) {
            $showCoAppMandateSection = true; 
        } else if (!$skipJsonSaysHide) {
            $showCoAppMandateSection = true;
        }
    }

    // 4. Final Decision for the @elseif (controls the whole mandate block)
    $showMandateSection = $showApplicantMandate || $showCoAppMandateSection;
    $showFallbackThankYou = !$showPaymentSection && !$showMandateSection;

    $applicantDisabled = false;
    $coApplicantDisabled = false;
    if(isset($links_data['signed_status'])) {
        $applicantDisabled = !$links_data['signed_status'] && (empty($links_data['applicant_link']));
        $coApplicantDisabled = !$links_data['signed_status'] && (empty($links_data['co_applicant_link']));
    }

    // 1. Applicant Signing Button
    $showApplicantSignButton = false;
    if ($applicantType == 1 && !$applicantDisabled && isset($links_data['applicant_link'])) {
        $showApplicantSignButton = true;
        // Check JSON skip logic
        if (isset($skipSteps->applicant, $skipSteps->applicant['loan_agreement']) && $skipSteps->applicant['loan_agreement'] == 0) {
            $showApplicantSignButton = false;
        }
    }

    // 2. Co-Applicant Signing Button
    $showCoApplicantSignButton = false;
    if ($applicantType == 2 && isset($links_data['co_applicant_exists']) && $links_data['co_applicant_exists'] && !$coApplicantDisabled) {
        $showCoApplicantSignButton = true;
        // Check JSON skip logic
        if (isset($skipSteps->co_applicant, $skipSteps->co_applicant['loan_agreement']) && $skipSteps->co_applicant['loan_agreement'] == 0) {
            $showCoApplicantSignButton = false;
        }
    }

    $isLoanAgreementStepActive = false;
    if (isset($links_data['signed_status']) && !$links_data['signed_status'] && !request()->has('documentId')) {
        if ($showApplicantSignButton || $showCoApplicantSignButton) {
            $isLoanAgreementStepActive = true;
        }
    }

    $kycPageUrl = isset($hashed_application_id) ? url('esign/id/' . $hashed_application_id) : '#';
?>
<head>
    <link href="<?php echo e(asset('assets/front/css/bootstrap.min.css')); ?>" rel="stylesheet">
    <link href="<?php echo e(asset('assets/front/css/mdb.min.css')); ?>" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet" />
    <style>
        .navbar-brand {
            height: 50px;
        }
        @media (max-width: 576px) {
            .payment-details-card {
                width: 400px !important;
                min-width: unset !important;
                max-width: 100% !important;
                flex-direction: column !important;
                align-items: stretch !important;
            }
        }
        @media (max-width: 576px) {
            .card.shadow-sm.border-0.mx-auto {
                max-width: 100% !important;
                min-width: unset !important;
            }
            .card-body {
                width: 100% !important;
                padding: 1rem !important;
            }
            .btn.btn-primary {
                min-width: 120px !important;
                font-size: 1em;
                padding: 10px !important;
            }
            h4 {
                font-size: 1.1em !important;
            }
        }
    </style>
</head>
<?php echo $__env->make('front.layouts.post_approval_header', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<!--Main Layout-->
<main style="padding-top: 10px">
    <div class="container">
        <section class="screen_5  text-center">
            <div class="row">
                <?php if($showPaymentSection): ?>
                    <?php if(request()->has('payment') && request()->get('payment') == 'fail'): ?>
                        <div class="col-12">
                            <h4 style="color:red;">Payment failed. Please try again.</h4>
                        </div>
                    <?php else: ?>
                        <div class="col-12">
                            <h4>Please proceed with your payment</h4>
                        </div>
                    <?php endif; ?>
                    <div class="card shadow-sm border-0" style="max-width: 800px; margin: auto;">
                        <div class="card-body payment-details-card" style="width: 600px; font-family: sans-serif;">
                            <h3 class="card-title text-center fw-bold">Payment Details</h3>
                            <?php $__currentLoopData = $fees['payment_details']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $payment_type => $details): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php if($payment_type=='Total Payment'): ?>
                                    <hr style="border-top:2px solid rgba(0,0,0,.3);">
                                <?php endif; ?>
                                <div class="d-flex justify-content-between gap-1 mb-3">
                                    <div>
                                        <span class="d-block"><?php echo $payment_type; ?></span>
                                        <?php if(!in_array($payment_type, ['Down Payment', 'Total Payment', 'Legal Charges'])): ?>
                                            <small class="text-muted">(Including GST)</small>
                                        <?php endif; ?>
                                    </div>
                                    <span class="fw-bold">₹ <?php echo $details['amount']; ?></span>
                                </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <div class="d-flex flex-wrap gap-2 justify-content-center align-items-center" style="width: 100%;">
                                
                                    <?php if($isMandateStepAvailable || $isCoAppMandateStepAvailable): ?>
                                    <a href="<?php echo e(url()->current()); ?>?type=<?php echo e($applicantType); ?>&skip_payment=1" class="btn btn-primary flex-grow-1" style="min-width: 120px;">
                                        <p style="padding-bottom: 0; margin-bottom: 0!important;">Skip</p>
                                    </a>
                                    <?php elseif($isKYCStepAvailable): ?>
                                    <a href="<?php echo e($kycPageUrl); ?>" class="btn btn-primary flex-grow-1" style="min-width: 120px;">
                                        <p style="padding-bottom: 0; margin-bottom: 0!important;">Skip</p>
                                    </a>
                                    <?php endif; ?>
                                
                                <a href="<?php echo $fees['total_payment_link']; ?>" class="btn btn-primary flex-grow-1" style="min-width: 120px;">
                                    
                                        <p style="padding-bottom: 0; margin-bottom: 0!important;">Pay Now</p>
                                    
                                </a>
                            </div>
                        </div>
                    </div>
                    <?php if(isset($fees['sanction_letter']) && $fees['sanction_letter']): ?>
                        <div class="col-12 mt-3">
                            <div class="sanction-download-text">
                                <a href="<?php echo e($fees['sanction_letter']); ?>" download='sanction_letter'>Download Sanction Letter</a>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php elseif($showMandateSection): ?>
                    <div class="col-12">
                        <div class="card shadow-sm border-0 mx-auto" style="max-width: 600px; width: 100%;">
                            <div class="card-body" style="width: 100%; font-family: sans-serif;">
                                <?php if(($isMandateStepAvailable)): ?>
                                    <h4 class="mb-3">Please click below button to continue with the bank setup step.</h4>
                                    <img src="<?php echo e(asset('assets/front/img/bank.png')); ?>" alt="Bank" class="img-fluid mb-3" style="max-width:120px;">
                                <?php else: ?>
                                    <h4 class="mb-3">Please proceed to setup your auto-debit.</h4>
                                    <img src="<?php echo e(asset('assets/front/img/hammer.png')); ?>" alt="Mandate" class="img-fluid mb-3" style="max-width:120px;">
                                <?php endif; ?>
                                <div class="mt-4 d-flex flex-column flex-md-row gap-2 justify-content-center align-items-center">
                                    <?php if($showApplicantMandate && $applicantType == "1" && isset($fees['set_auto_debit'])): ?>
                                        <?php if($isKYCStepAvailable || $isPaymentStepAvailable): ?>
                                            <a href="<?php echo e($kycPageUrl); ?>" class="btn btn-primary" style="min-width: 180px; padding: 22px; height: 63px;">
                                                <div style="padding-bottom: 0; margin-bottom: 0!important;">Skip Mandate Setup</div>
                                            </a>
                                        <?php endif; ?>
                                        <a href="<?php echo $fees['set_auto_debit']; ?>" class="btn btn-primary" style="min-width: 180px;">
                                            <div style="padding-bottom: 0; margin-bottom: 0!important;">Setup my Auto-Debit Now </div>
                                            <div style="text-align:center;font-size:11px;color:#ffffff">via Net-Banking</div>
                                        </a>
                                    <?php elseif($showCoAppMandateSection && $applicantType == "2" && isset($fees['set_auto_debit_co_applicant'])): ?>
                                        <?php if($isCoKYCStepAvailable  || $isPaymentStepAvailable): ?>
                                            <a href="<?php echo e($kycPageUrl); ?>" class="btn btn-primary" style="min-width: 180px; padding: 22px; height: 63px;">
                                                <div style="padding-bottom: 0; margin-bottom: 0!important;">Skip Mandate Setup</div>
                                            </a>
                                        <?php endif; ?>
                                        <a href="<?php echo $fees['set_auto_debit_co_applicant']; ?>" class="btn btn-primary" style="min-width: 180px;">
                                            <div style="padding-bottom: 0; margin-bottom: 0!important;">Setup my Auto-Debit Now </div>
                                            <div style="text-align:center;font-size:11px;color:#ffffff">via Net-Banking</div>
                                        </a>
                                    
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php elseif(($isKYCStepAvailable && $applicantType != "2") || ($isCoKYCStepAvailable && $applicantType == "2")): ?>
                    <script>
                        window.location.href = "<?php echo e($kycPageUrl); ?>";
                    </script>
                <?php elseif(($isLoanAgreementStepActive)): ?>
                    <div class="card shadow-sm border-0" style="max-width: 800px; margin: auto;">
                        <div class="card-body" style="width: 600px; font-family=sans-serif;">
                            <h3 class="card-title text-center fw-bold"><?php if((!$applicantDisabled && $applicantType != 2) || (!$coApplicantDisabled && $applicantType == 2)): ?> Esign Loan Agreement <?php else: ?> Esign Loan Agreement Completed !!! <?php endif; ?></h3>
                            <img src="/assets/front/img/svg/signature.png" class="img-responsive img">
                            <?php if($showApplicantSignButton): ?>
                            <a href="<?php echo e($links_data['applicant_link']); ?>">
                                <div class="col-sm-5 btn btn-primary">
                                    <p class="mb-0">Applicant Signing</p>
                                </div>
                            </a>
                            <?php elseif($showCoApplicantSignButton): ?>
                                <a href="<?php echo e($links_data['co_applicant_link']); ?>">
                                    <div class="col-sm-5 btn btn-primary">
                                        <p class="mb-0">Co-applicant Signing</p>
                                    </div>
                                </a>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php else: ?>
                    <script>
                    document.addEventListener('DOMContentLoaded', function() {
                        var headerContainer = document.querySelector('.header-container');
                        if (headerContainer) {
                            var h4 = headerContainer.querySelector('h4');
                            if (h4) {
                                h4.textContent = '';
                            }
                        }
                    });
                    </script>
                    <div class="col-12">
                            <img src="/assets/front/img/payment_success.png" class="img-responsive img">
                        </div>
                    </div>
                    <?php if(request()->has('documentId') || (!$isLoanAgreementStepActive && !$showPaymentSection && !$showMandateSection)): ?>
                        <h4>Thank you for completing the post approval process. We will update you about the disbursement soon</h4>
                    <?php else: ?>
                        <?php if(isset($user_details->lender_id) && $user_details->lender_id == 2): ?>
                            <h4>Thank you for completing the payment, KYC and mandate setup. Please wait for loan agreement to be generated.</h4>
                        <?php else: ?>
                            <h4>Please wait for mandate and esign link. We will share it with you.</h4>
                        <?php endif; ?>
                    <?php endif; ?>
                <?php endif; ?>
            </div>

            <div class="row footer-privacy">
                <div class="col-12">
                    <div style="text-align: center; padding: 20px 0; font-size: 1.1em; left: 0; bottom: 0; width: 100%;">
                        <p style="font-weight:bold; margin-bottom: 5px;">If you have any queries please email at operations@creditfair.in or whatsapp/call at +918928769161</p>
                        <p  class="footer-normal-text">We value your privacy. All your data is kept strictly confidential</p>
                    </div>
                </div>
            </div>
        </section>
    </div>
</main>
<?php echo $__env->make('front.layouts.footerfixed', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>