

<?php $__env->startSection('title', 'PG Finsev | Create Loan'); ?>
<?php $__env->startSection('page-title', 'Create Loan'); ?>

<?php $__env->startSection('page-description'); ?>
    Create and manage a new client loan
<?php $__env->stopSection(); ?>

<?php $__env->startPush('styles'); ?>
<link rel="stylesheet" href="<?php echo e(asset('admin/css/loan-create.css')); ?>">
<?php $__env->stopPush(); ?>

<?php
    $currencySettings = \App\Models\SystemSetting::query()->first();

    $currencySymbol = $currencySettings?->currency_symbol ?? '₹';
    $currencyDecimals = (int) ($currencySettings?->currency_decimal_places ?? 2);
    $currencyRound = (bool) ($currencySettings?->currency_round_display ?? false);

    /*
    |--------------------------------------------------------------------------
    | Interest Settings
    |--------------------------------------------------------------------------
    |
    | Interest ON/OFF is controlled from Loan Settings.
    | Each Loan Type has its own interest_rate.
    |
    */

    $interestEnabled = (bool) ($settings->interest_enabled ?? false);
?>

<?php $__env->startSection('content'); ?>

<div class="loan-create-page">

    <div class="loan-create-header">

        <div>

            <h1 class="h3 mb-1">
                Create Loan
            </h1>

            <p class="text-muted mb-0">
                Create a new loan for an existing client.
            </p>

        </div>

        <a
            href="<?php echo e(route('admin.loans')); ?>"
            class="loan-back-btn"
        >
            Back to Loans
        </a>

    </div>


    

    <?php if(session('error')): ?>

        <div class="alert alert-danger">
            <?php echo e(session('error')); ?>

        </div>

    <?php endif; ?>


    

    <?php if($errors->any()): ?>

        <div class="alert alert-danger">

            <strong>
                Please correct the following:
            </strong>

            <ul class="mb-0 mt-2">

                <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                    <li>
                        <?php echo e($error); ?>

                    </li>

                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

            </ul>

        </div>

    <?php endif; ?>


    <form
        method="POST"
        action="<?php echo e(route('admin.loans.create.store')); ?>"
        id="adminLoanForm"
    >

        <?php echo csrf_field(); ?>


        

        <div class="loan-section">

            <div class="loan-section-header">

                <strong>
                    Loan Information
                </strong>

            </div>


            <div class="loan-section-body">

                <div class="loan-grid">


                    

                    <div class="loan-field">

                        <label
                            for="user_id"
                            class="loan-label"
                        >
                            Client
                            <span class="text-danger">*</span>
                        </label>

                        <select
                            name="user_id"
                            id="user_id"
                            class="loan-control"
                            required
                        >

                            <option value="">
                                Select client
                            </option>

                            <?php $__currentLoopData = $clients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $client): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                <option
                                    value="<?php echo e($client->id); ?>"
                                    <?php if(
                                        old('user_id') == $client->id
                                    ): echo 'selected'; endif; ?>
                                >

                                    <?php echo e($client->name); ?>


                                    <?php if($client->mobile): ?>
                                        — <?php echo e($client->mobile); ?>

                                    <?php endif; ?>

                                </option>

                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        </select>

                    </div>


                    

                    <div class="loan-field">

                        <label
                            for="emi_card_id"
                            class="loan-label"
                        >
                            EMI Card
                            <span class="text-danger">*</span>
                        </label>

                        <select
                            name="emi_card_id"
                            id="emi_card_id"
                            class="loan-control"
                            required
                        >

                            <option value="">
                                Select client first
                            </option>

                            <?php $__currentLoopData = $emiCards; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $card): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                <option
                                    value="<?php echo e($card->id); ?>"
                                    data-user-id="<?php echo e($card->user_id); ?>"
                                    data-limit="<?php echo e((float)($card->available_limit ?? 0)); ?>"
                                    data-card-number="<?php echo e($card->card_number); ?>"
                                    <?php if(
                                        old('emi_card_id') == $card->id
                                    ): echo 'selected'; endif; ?>
                                >

                                    <?php echo e($card->card_number); ?>


                                    —
                                    Available:
                                    <?php echo e(money((float)($card->available_limit ?? 0))); ?>


                                </option>

                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        </select>


                        <small
                            class="loan-help"
                            id="cardInfo"
                        >
                            Select a client to view active EMI cards.
                        </small>

                    </div>


                    

                    <div class="loan-field">

                        <label
                            for="loan_type_id"
                            class="loan-label"
                        >
                            Loan Type
                            <span class="text-danger">*</span>
                        </label>

                        <select
                            name="loan_type_id"
                            id="loan_type_id"
                            class="loan-control"
                            required
                        >

                            <option value="">
                                Select loan type
                            </option>

                            <?php $__currentLoopData = $loanTypes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $type): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                <option
                                    value="<?php echo e($type->id); ?>"
                                    data-code="<?php echo e(strtolower($type->code ?? $type->name)); ?>"
                                    data-interest-rate="<?php echo e((float)($type->interest_rate ?? 0)); ?>"
                                    <?php if(
                                        old('loan_type_id') == $type->id
                                    ): echo 'selected'; endif; ?>
                                >

                                    <?php echo e($type->name); ?>


                                </option>

                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        </select>


                        <div
                            class="interest-status"
                            id="interestStatus"
                        >

                            <?php if($interestEnabled): ?>

                                Interest:
                                <strong id="selectedInterestRate">
                                    Select loan type
                                </strong>

                            <?php else: ?>

                                Interest:
                                <strong>
                                    0%
                                </strong>

                                <span>
                                    (Interest is disabled in Loan Settings)
                                </span>

                            <?php endif; ?>

                        </div>

                    </div>

                </div>


                

                <div class="loan-grid">


                    <div class="loan-field">

                        <div class="border rounded p-3 bg-light">

                            <small class="text-muted d-block">
                                Selected Card
                            </small>

                            <strong id="selectedCardNumber">
                                —
                            </strong>

                        </div>

                    </div>


                    <div class="loan-field">

                        <div class="border rounded p-3 bg-light">

                            <small class="text-muted d-block">
                                Available Limit
                            </small>

                            <strong id="selectedCardLimit">
                                —
                            </strong>

                        </div>

                    </div>


                    <div class="loan-field">

                        <div class="border rounded p-3 bg-light">

                            <small class="text-muted d-block">
                                Minimum Loan
                            </small>

                            <strong>
                                <?php echo e(money((float)($settings->minimum_loan_amount ?? 0))); ?>

                            </strong>

                        </div>

                    </div>

                </div>

            </div>

        </div>


        

        <div class="loan-section">

            <div class="loan-section-header">

                <strong>
                    Loan Amount & Tenure
                </strong>

            </div>


            <div class="loan-section-body">

                <div class="loan-grid">


                    

                    <div class="loan-field">

                        <label
                            for="loan_amount"
                            class="loan-label"
                        >

                            Loan Amount

                            <span class="text-danger">
                                *
                            </span>

                        </label>


                        <div class="loan-amount-wrap">

                            <span
                                class="loan-amount-prefix"
                                id="loanCurrencyPrefix"
                            >
                                <?php echo e($currencySymbol); ?>

                            </span>


                            <input
                                type="number"
                                step="0.01"
                                min="0.01"
                                name="loan_amount"
                                id="loan_amount"
                                class="loan-control loan-amount-input"
                                value="<?php echo e(old('loan_amount')); ?>"
                                required
                            >

                        </div>


                        <small class="loan-help">

                            Maximum is the lower of the configured maximum
                            and the selected card's available limit.

                        </small>

                    </div>


                    

                    <div class="loan-field">

                        <label
                            for="tenure_months"
                            class="loan-label"
                        >

                            Tenure

                            <span class="text-danger">
                                *
                            </span>

                        </label>


                        <select
                            name="tenure_months"
                            id="tenure_months"
                            class="loan-control"
                            required
                        >

                            <option value="">
                                Select tenure
                            </option>

                            <?php $__currentLoopData = ($settings->allowed_tenures ?? []); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tenure): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                <option
                                    value="<?php echo e((int)$tenure); ?>"
                                    <?php if(
                                        old('tenure_months') == $tenure
                                    ): echo 'selected'; endif; ?>
                                >

                                    <?php echo e((int)$tenure); ?>

                                    Months

                                </option>

                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        </select>

                    </div>


                    

                    <div class="loan-field">

                        <label class="loan-label">
                            Interest Status
                        </label>


                        <div
                            class="border rounded p-3 bg-light"
                            style="height:44px;box-sizing:border-box;display:flex;align-items:center"
                        >

                            <?php if($interestEnabled): ?>

                                <strong
                                    id="interestStatusBox"
                                    style="color:#198754"
                                >
                                    Enabled
                                </strong>

                            <?php else: ?>

                                <strong
                                    id="interestStatusBox"
                                    style="color:#64748b"
                                >
                                    Disabled — 0%
                                </strong>

                            <?php endif; ?>

                        </div>

                    </div>

                </div>

            </div>

        </div>


        

        <div class="loan-section">

            <div class="loan-section-header">

                <strong>
                    Loan Type Details
                </strong>

            </div>


            <div class="loan-section-body">

                <div
                    id="detailsHint"
                    class="loan-details-hint"
                >

                    Select a loan type to enter type-specific details.

                </div>


                

                <div
                    id="details-digital_product"
                    class="loan-details-group d-none"
                >

                    <div class="loan-grid">


                        <div class="loan-field">

                            <label class="loan-label">
                                Product / Device
                            </label>

                            <input
                                type="text"
                                name="details[product_name]"
                                class="loan-control"
                                value="<?php echo e(old('details.product_name')); ?>"
                                placeholder="e.g. Mobile Phone"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Brand
                            </label>

                            <input
                                type="text"
                                name="details[brand]"
                                class="loan-control"
                                value="<?php echo e(old('details.brand')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Model
                            </label>

                            <input
                                type="text"
                                name="details[model]"
                                class="loan-control"
                                value="<?php echo e(old('details.model')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                IMEI / Serial Number
                            </label>

                            <input
                                type="text"
                                name="details[imei_or_serial]"
                                class="loan-control"
                                value="<?php echo e(old('details.imei_or_serial')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Seller / Store
                            </label>

                            <input
                                type="text"
                                name="details[seller]"
                                class="loan-control"
                                value="<?php echo e(old('details.seller')); ?>"
                            >

                        </div>

                    </div>

                </div>


                

                <div
                    id="details-gold"
                    class="loan-details-group d-none"
                >

                    <div class="loan-grid">


                        <div class="loan-field">

                            <label class="loan-label">
                                Gold Item
                            </label>

                            <input
                                type="text"
                                name="details[gold_item]"
                                class="loan-control"
                                value="<?php echo e(old('details.gold_item')); ?>"
                                placeholder="Ring / Chain / etc."
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Gross Weight (g)
                            </label>

                            <input
                                type="text"
                                name="details[gross_weight]"
                                class="loan-control"
                                value="<?php echo e(old('details.gross_weight')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Purity
                            </label>

                            <input
                                type="text"
                                name="details[purity]"
                                class="loan-control"
                                value="<?php echo e(old('details.purity')); ?>"
                                placeholder="22K / 916"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Appraised Value
                            </label>

                            <input
                                type="number"
                                step="0.01"
                                name="details[appraised_value]"
                                class="loan-control"
                                value="<?php echo e(old('details.appraised_value')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Description
                            </label>

                            <input
                                type="text"
                                name="details[description]"
                                class="loan-control"
                                value="<?php echo e(old('details.description')); ?>"
                            >

                        </div>

                    </div>

                </div>


                

                <div
                    id="details-personal"
                    class="loan-details-group d-none"
                >

                    <div class="loan-grid">


                        <div class="loan-field">

                            <label class="loan-label">
                                Purpose
                            </label>

                            <input
                                type="text"
                                name="details[purpose]"
                                class="loan-control"
                                value="<?php echo e(old('details.purpose')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Employment / Occupation
                            </label>

                            <input
                                type="text"
                                name="details[occupation]"
                                class="loan-control"
                                value="<?php echo e(old('details.occupation')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Employer / Business
                            </label>

                            <input
                                type="text"
                                name="details[employer]"
                                class="loan-control"
                                value="<?php echo e(old('details.employer')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Monthly Income
                            </label>

                            <input
                                type="number"
                                step="0.01"
                                name="details[monthly_income]"
                                class="loan-control"
                                value="<?php echo e(old('details.monthly_income')); ?>"
                            >

                        </div>

                    </div>

                </div>


                

                <div
                    id="details-vehicle"
                    class="loan-details-group d-none"
                >

                    <div class="loan-grid">


                        <div class="loan-field">

                            <label class="loan-label">
                                Vehicle Type
                            </label>

                            <input
                                type="text"
                                name="details[vehicle_type]"
                                class="loan-control"
                                value="<?php echo e(old('details.vehicle_type')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Make
                            </label>

                            <input
                                type="text"
                                name="details[make]"
                                class="loan-control"
                                value="<?php echo e(old('details.make')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Model
                            </label>

                            <input
                                type="text"
                                name="details[model]"
                                class="loan-control"
                                value="<?php echo e(old('details.model')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Registration Number
                            </label>

                            <input
                                type="text"
                                name="details[registration_number]"
                                class="loan-control"
                                value="<?php echo e(old('details.registration_number')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Chassis Number
                            </label>

                            <input
                                type="text"
                                name="details[chassis_number]"
                                class="loan-control"
                                value="<?php echo e(old('details.chassis_number')); ?>"
                            >

                        </div>


                        <div class="loan-field">

                            <label class="loan-label">
                                Engine Number
                            </label>

                            <input
                                type="text"
                                name="details[engine_number]"
                                class="loan-control"
                                value="<?php echo e(old('details.engine_number')); ?>"
                            >

                        </div>

                    </div>

                </div>


                

                <div
                    id="details-other"
                    class="loan-details-group d-none"
                >

                    <div class="loan-grid">

                        <div class="loan-field">

                            <label class="loan-label">
                                Details / Notes
                            </label>

                            <textarea
                                name="details[description]"
                                class="loan-control"
                                rows="3"
                            ><?php echo e(old('details.description')); ?></textarea>

                        </div>

                    </div>

                </div>

            </div>

        </div>


        

        <div class="loan-section">

            <div class="loan-section-header">

                <strong>
                    Calculation Preview
                </strong>

            </div>


            <div class="loan-section-body">

                <div class="loan-preview-grid">


                    

                    <div class="loan-preview-box">

                        <small>
                            Loan Amount
                        </small>

                        <strong id="previewAmount">
                            <?php echo e($currencySymbol); ?>0.00
                        </strong>

                    </div>


                    

                    <div class="loan-preview-box loan-preview-interest">

                        <small>
                            Interest Rate
                        </small>

                        <strong id="previewInterestRate">
                            <?php echo e($interestEnabled ? '0%' : '0%'); ?>

                        </strong>

                    </div>


                    

                    <div class="loan-preview-box loan-preview-interest">

                        <small>
                            Interest Amount
                        </small>

                        <strong id="previewInterestAmount">
                            <?php echo e($currencySymbol); ?>0.00
                        </strong>

                    </div>


                    

                    <div class="loan-preview-box">

                        <small>
                            Processing Fee
                        </small>

                        <strong id="previewFee">
                            <?php echo e($currencySymbol); ?>0.00
                        </strong>

                    </div>


                    

                    <div class="loan-preview-box">

                        <small>
                            Total Payable
                        </small>

                        <strong id="previewTotal">
                            <?php echo e($currencySymbol); ?>0.00
                        </strong>

                    </div>


                    

                    <div class="loan-preview-box">

                        <small>
                            Estimated EMI
                        </small>

                        <strong id="previewEmi">
                            <?php echo e($currencySymbol); ?>0.00
                        </strong>

                    </div>

                </div>


                <div class="mt-3 text-muted small">

                    Final calculation and validation are performed by the server
                    using the existing loan calculation service.

                </div>

            </div>

        </div>


        

        <div class="loan-actions">

            <a
                href="<?php echo e(route('admin.loans')); ?>"
                class="loan-cancel-btn"
            >
                Cancel
            </a>

            <button
                type="submit"
                class="loan-submit-btn"
                id="createLoanBtn"
            >
                Create Loan
            </button>

        </div>

    </form>

</div>


<script>

document.addEventListener(
    'DOMContentLoaded',
    function () {


        const clientSelect =
            document.getElementById(
                'user_id'
            );


        const cardSelect =
            document.getElementById(
                'emi_card_id'
            );


        const typeSelect =
            document.getElementById(
                'loan_type_id'
            );


        const amountInput =
            document.getElementById(
                'loan_amount'
            );


        const tenureSelect =
            document.getElementById(
                'tenure_months'
            );


        const cardInfo =
            document.getElementById(
                'cardInfo'
            );


        const selectedCardNumber =
            document.getElementById(
                'selectedCardNumber'
            );


        const selectedCardLimit =
            document.getElementById(
                'selectedCardLimit'
            );


        /*
        |--------------------------------------------------------------------------
        | Interest Settings
        |--------------------------------------------------------------------------
        */

        const interestEnabled =
            <?php echo e($interestEnabled ? 'true' : 'false'); ?>;


        const settings = <?php echo json_encode([

            'processing_fee' =>
                (float)($settings->processing_fee ?? 0), 'processing_fee_type' =>
                $settings->processing_fee_type ?? 'fixed', ]) ?>;


        /*
        |--------------------------------------------------------------------------
        | Original Card Options
        |--------------------------------------------------------------------------
        */

        const originalCardOptions =
            Array.from(
                cardSelect.options
            ).map(
                option => ({

                    value:
                        option.value,

                    text:
                        option.text,

                    userId:
                        option.dataset.userId || '',

                    limit:
                        option.dataset.limit || '0',

                    cardNumber:
                        option.dataset.cardNumber || ''

                })
            );


        /*
        |--------------------------------------------------------------------------
        | Currency
        |--------------------------------------------------------------------------
        */

        const currencyConfig = {

            symbol:
                <?php echo \Illuminate\Support\Js::from($currencySymbol)->toHtml() ?>,

            decimals:
                Number(
                    <?php echo e((int) $currencyDecimals); ?>

                ),

            round:
                <?php echo e($currencyRound ? 'true' : 'false'); ?>


        };


        /*
        |--------------------------------------------------------------------------
        | Money Format
        |--------------------------------------------------------------------------
        */

        function moneyJs(value)
        {

            let amount =
                Number(value || 0);


            if (
                currencyConfig.round
            ) {

                amount =
                    Math.round(amount);

            }


            return currencyConfig.symbol +
                amount.toLocaleString(
                    'en-IN',
                    {

                        minimumFractionDigits:
                            currencyConfig.round
                                ? 0
                                : currencyConfig.decimals,

                        maximumFractionDigits:
                            currencyConfig.round
                                ? 0
                                : currencyConfig.decimals

                    }
                );

        }


        /*
        |--------------------------------------------------------------------------
        | Get Interest Rate
        |--------------------------------------------------------------------------
        */

        function getInterestRate()
        {

            if (!interestEnabled) {

                return 0;

            }


            const option =
                typeSelect.options[
                    typeSelect.selectedIndex
                ];


            if (
                !option ||
                !option.value
            ) {

                return 0;

            }


            return Number(
                option.dataset.interestRate || 0
            );

        }


        /*
        |--------------------------------------------------------------------------
        | Update Interest Display
        |--------------------------------------------------------------------------
        */

        function updateInterestDisplay()
        {

            const rate =
                getInterestRate();


            const selectedRate =
                document.getElementById(
                    'selectedInterestRate'
                );


            const statusBox =
                document.getElementById(
                    'interestStatusBox'
                );


            const previewRate =
                document.getElementById(
                    'previewInterestRate'
                );


            if (previewRate) {

                previewRate.textContent =
                    rate + '%';

            }


            if (selectedRate) {

                if (
                    typeSelect.value
                ) {

                    selectedRate.textContent =
                        rate + '%';

                } else {

                    selectedRate.textContent =
                        'Select loan type';

                }

            }


            if (statusBox) {

                if (interestEnabled) {

                    statusBox.textContent =
                        'Enabled — ' + rate + '%';

                    statusBox.style.color =
                        '#198754';

                } else {

                    statusBox.textContent =
                        'Disabled — 0%';

                    statusBox.style.color =
                        '#64748b';

                }

            }

        }


        /*
        |--------------------------------------------------------------------------
        | Filter Cards
        |--------------------------------------------------------------------------
        */

        function filterCards()
        {

            const userId =
                clientSelect.value;


            const previous =
                cardSelect.value;


            cardSelect.innerHTML =
                '';


            const placeholder =
                document.createElement(
                    'option'
                );


            placeholder.value =
                '';


            placeholder.textContent =
                userId
                    ? 'Select EMI Card'
                    : 'Select client first';


            cardSelect.appendChild(
                placeholder
            );


            originalCardOptions.forEach(
                function (item) {


                    if (
                        !item.value ||
                        item.userId !== userId
                    ) {

                        return;

                    }


                    const option =
                        document.createElement(
                            'option'
                        );


                    option.value =
                        item.value;


                    option.textContent =
                        item.text;


                    option.dataset.limit =
                        item.limit;


                    option.dataset.cardNumber =
                        item.cardNumber;


                    if (
                        item.value === previous
                    ) {

                        option.selected =
                            true;

                    }


                    cardSelect.appendChild(
                        option
                    );

                }
            );


            updateCardSummary();


            if (
                userId &&
                cardSelect.options.length === 1
            ) {

                cardInfo.textContent =
                    'No active EMI card found for this client.';

            } else if (userId) {

                cardInfo.textContent =
                    'Select an active EMI card.';

            } else {

                cardInfo.textContent =
                    'Select a client to view active EMI cards.';

            }

        }


        /*
        |--------------------------------------------------------------------------
        | Card Summary
        |--------------------------------------------------------------------------
        */

        function updateCardSummary()
        {

            const option =
                cardSelect.options[
                    cardSelect.selectedIndex
                ];


            if (
                !option ||
                !option.value
            ) {

                selectedCardNumber.textContent =
                    '—';

                selectedCardLimit.textContent =
                    '—';

                return;

            }


            selectedCardNumber.textContent =
                option.dataset.cardNumber ||
                option.textContent;


            selectedCardLimit.textContent =
                moneyJs(
                    option.dataset.limit || 0
                );

        }


        /*
        |--------------------------------------------------------------------------
        | Loan Details
        |--------------------------------------------------------------------------
        */

        function showLoanDetails()
        {

            document
                .querySelectorAll(
                    '.loan-details-group'
                )
                .forEach(
                    function (group) {

                        group.classList.add(
                            'loan-hidden'
                        );

                    }
                );


            const option =
                typeSelect.options[
                    typeSelect.selectedIndex
                ];


            if (
                !option ||
                !option.value
            ) {

                document.getElementById(
                    'detailsHint'
                ).textContent =
                    'Select a loan type to enter type-specific details.';

                updateInterestDisplay();

                updatePreview();

                return;

            }


            let code =
                (
                    option.dataset.code ||
                    option.textContent ||
                    ''
                )
                    .toLowerCase()
                    .trim();


            if (
                code.includes('digital') ||
                code.includes('product') ||
                code.includes('mobile')
            ) {

                code =
                    'digital_product';

            } else if (
                code.includes('gold')
            ) {

                code =
                    'gold';

            } else if (
                code.includes('personal')
            ) {

                code =
                    'personal';

            } else if (
                code.includes('vehicle') ||
                code.includes('car') ||
                code.includes('bike')
            ) {

                code =
                    'vehicle';

            } else {

                code =
                    'other';

            }


            const group =
                document.getElementById(
                    'details-' + code
                );


            if (group) {

                group.classList.remove(
                    'loan-hidden'
                );

            }


            document.getElementById(
                'detailsHint'
            ).textContent =
                'Enter the details applicable to the selected loan type.';


            /*
            |--------------------------------------------------------------------------
            | Interest Update
            |--------------------------------------------------------------------------
            */

            updateInterestDisplay();


            /*
            |--------------------------------------------------------------------------
            | Calculation Update
            |--------------------------------------------------------------------------
            */

            updatePreview();

        }


        /*
        |--------------------------------------------------------------------------
        | Loan Calculation Preview
        |--------------------------------------------------------------------------
        */

        function updatePreview()
        {

            const amount =
                Number(
                    amountInput.value || 0
                );


            /*
            |--------------------------------------------------------------------------
            | Interest Rate
            |--------------------------------------------------------------------------
            */

            const interestRate =
                getInterestRate();


            /*
            |--------------------------------------------------------------------------
            | Simple Interest
            |--------------------------------------------------------------------------
            |
            | Same calculation model used by LoanCalculationService.
            |
            */

            const interestAmount =
                interestRate > 0
                    ? amount *
                        (
                            interestRate / 100
                        ) *
                        (
                            Number(
                                tenureSelect.value || 0
                            ) / 12
                        )
                    : 0;


            /*
            |--------------------------------------------------------------------------
            | Processing Fee
            |--------------------------------------------------------------------------
            */

            const fee =
                settings.processing_fee_type ===
                'percentage'

                    ? amount *
                        Number(
                            settings.processing_fee || 0
                        ) /
                        100

                    : Number(
                        settings.processing_fee || 0
                    );


            /*
            |--------------------------------------------------------------------------
            | Total Payable
            |--------------------------------------------------------------------------
            |
            | Processing fee is NOT included in EMI.
            |
            */

            const total =
                amount +
                interestAmount;


            /*
            |--------------------------------------------------------------------------
            | EMI
            |--------------------------------------------------------------------------
            */

            const tenure =
                Number(
                    tenureSelect.value || 0
                );


            const emi =
                tenure > 0
                    ? total / tenure
                    : 0;


            /*
            |--------------------------------------------------------------------------
            | Update UI
            |--------------------------------------------------------------------------
            */

            document.getElementById(
                'previewAmount'
            ).textContent =
                moneyJs(amount);


            document.getElementById(
                'previewInterestRate'
            ).textContent =
                interestRate + '%';


            document.getElementById(
                'previewInterestAmount'
            ).textContent =
                moneyJs(interestAmount);


            document.getElementById(
                'previewFee'
            ).textContent =
                moneyJs(fee);


            document.getElementById(
                'previewTotal'
            ).textContent =
                moneyJs(total);


            document.getElementById(
                'previewEmi'
            ).textContent =
                moneyJs(emi);

        }


        /*
        |--------------------------------------------------------------------------
        | Events
        |--------------------------------------------------------------------------
        */

        clientSelect.addEventListener(
            'change',
            filterCards
        );


        cardSelect.addEventListener(
            'change',
            updateCardSummary
        );


        typeSelect.addEventListener(
            'change',
            showLoanDetails
        );


        amountInput.addEventListener(
            'input',
            updatePreview
        );


        tenureSelect.addEventListener(
            'change',
            updatePreview
        );


        /*
        |--------------------------------------------------------------------------
        | Initial State
        |--------------------------------------------------------------------------
        */

        filterCards();

        showLoanDetails();

        updateInterestDisplay();

        updatePreview();

    }
);

</script>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('admin.layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\emi-card\resources\views/admin/loans/create.blade.php ENDPATH**/ ?>