<?php
if (!defined('WPINC')) {
    die();
}

use EstatebudConnect\Utilities\Helper;
use EstatebudConnect\Configs\Icons;

$settings_data = $settings_data ?? [];
$icon_type = $settings_data['icon_type'] ?? Icons::SHAPE_SHARP;

$icon_percent = Icons::get_svg_icon('percent', $icon_type, 'regular', 16, 16, '#B8B8B8');

$monthly_repayment = null;
$currency_data = [];
$currency_symbol = '';

if ($listing->get_price()) {
    $user_session = Helper::get_user_session();
    $currency = strtoupper($user_session['currency'] ?? $listing->get_price()['currency']);

    $currency_data = Helper::get_currency_data($listing->get_price()['currency'], $currency);
    $currency_symbol = $currency_data['exchange_rates']['symbol'] ?? '';

    $purchase_price = $listing->get_price()['price'];
    $purchase_price_converted = round(($purchase_price * $currency_data['exchange_rates']['rate']) / 100) * 100;

    $down_payment = $purchase_price * 0.2;
    $down_payment_converted = $purchase_price_converted * 0.2;
    
    $mortgage_amount = $purchase_price - $down_payment;
    $interest_rate = 3 / 1200;
    $period = 30 * 12;
    $monthly_repayment =
        ($mortgage_amount * ($interest_rate * (1 + $interest_rate) ** $period)) / ((1 + $interest_rate) ** $period - 1);
}
?>
<div class="estatebud-listing-mortgage-calculator">
    <h3 class="estatebud-listing-mortgage-heading"><?php echo esc_html(Helper::term('mortgage_calculator.title')); ?></h3>
    <div class="estatebud-listing-mortgage-group">
        <div class="estatebud-listing-mortgage-sub-group">
            <div class="estatebud-listing-mortgage-field-container">
                <label class="estatebud-listing-mortgage-field-label"><?php echo esc_html(Helper::term('mortgage_calculator.purchase_price')); ?></label>
                <div class="estatebud-listing-mortgage-field-content">
                    <label class="estatebud-listing-mortgage-field-icon" for="estatebud_purchase_price"><?php echo esc_html($currency_symbol); ?></label>
                    <?php Helper::render_markup_from_views('Public/partials/fields/input.php', [
                        'type' => 'text',
                        'value' => number_format(
                            $purchase_price_converted,
                            0,
                            $currency_data['exchange_rates']['decimal_separator'] ?? '',
                            $currency_data['exchange_rates']['thousand_separator'] ?? ''
                        ),
                        'name' => 'estatebud_purchase_price',
                        'id' => 'estatebud_purchase_price',
                    ]); ?>
                    <?php Helper::render_markup_from_views('Public/partials/fields/input.php', [
                        'type' => 'text',
                        'value' => number_format(
                            $purchase_price,
                            0,
                            $currency_data['exchange_rates']['decimal_separator'] ?? '',
                            $currency_data['exchange_rates']['thousand_separator'] ?? ''
                        ),
                        'name' => 'estatebud_base_purchase_price',
                        'extra_class' => 'estatebud-hidden',
                    ]); ?>
                </div>
            </div>

            <div class="estatebud-listing-mortgage-field-container">
                <label class="estatebud-listing-mortgage-field-label"><?php echo esc_html(Helper::term('mortgage_calculator.down_payment')); ?></label>
                <div class="estatebud-listing-mortgage-field-content">
                    <label class="estatebud-listing-mortgage-field-icon" for="estatebud_down_payment"><?php echo esc_html($currency_symbol); ?></label>
                    <?php Helper::render_markup_from_views('Public/partials/fields/input.php', [
                        'type' => 'text',
                        'value' => number_format(
                            $down_payment_converted,
                            0,
                            $currency_data['exchange_rates']['decimal_separator'] ?? '',
                            $currency_data['exchange_rates']['thousand_separator'] ?? ''
                        ),
                        'name' => 'estatebud_down_payment',
                        'id' => 'estatebud_down_payment',
                    ]); ?>
                    <?php Helper::render_markup_from_views('Public/partials/fields/input.php', [
                        'type' => 'text',
                        'value' => number_format(
                            $down_payment,
                            0,
                            $currency_data['exchange_rates']['decimal_separator'] ?? '',
                            $currency_data['exchange_rates']['thousand_separator'] ?? ''
                        ),
                        'name' => 'estatebud_base_down_payment',
                        'extra_class' => 'estatebud-hidden',
                    ]); ?>
                </div>
            </div>
        </div>

        <div class="estatebud-listing-mortgage-sub-group">
            <div class="estatebud-listing-mortgage-field-container">
                <label class="estatebud-listing-mortgage-field-label"><?php echo esc_html(Helper::term('mortgage_calculator.repayment_period')); ?></label>
                <?php Helper::render_markup_from_views('Public/partials/fields/select.php', [
                    'variable_name' => 'estatebud_repayment_period',
                    'extra_class' => 'estatebud-listing-select-field',
                    'options' => [
                        ['value' => '5', 'label' => sprintf(Helper::term('duration.x.year', 5), '5'), 'selected' => false],
                        ['value' => '10', 'label' => sprintf(Helper::term('duration.x.year', 10), '10'), 'selected' => false],
                        ['value' => '15', 'label' => sprintf(Helper::term('duration.x.year', 15), '15'), 'selected' => false],
                        ['value' => '20', 'label' => sprintf(Helper::term('duration.x.year', 20), '20'), 'selected' => false],
                        ['value' => '25', 'label' => sprintf(Helper::term('duration.x.year', 25), '25'), 'selected' => false],
                        ['value' => '30', 'label' => sprintf(Helper::term('duration.x.year', 30), '30'), 'selected' => true],
                        ['value' => '35', 'label' => sprintf(Helper::term('duration.x.year', 35), '35'), 'selected' => false],
                        ['value' => '40', 'label' => sprintf(Helper::term('duration.x.year', 40), '40'), 'selected' => false]
                    ],
                    'multiple' => false
                ]); ?>
            </div>

            <div class="estatebud-listing-mortgage-field-container">
                <label class="estatebud-listing-mortgage-field-label"><?php echo esc_html(Helper::term('mortgage_calculator.interest_rate')); ?></label>
                <div class="estatebud-listing-mortgage-field-content">
                    <label class="estatebud-listing-mortgage-field-icon" for="estatebud_interest_rate"><?php echo wp_kses($icon_percent, Icons::get_kses_ruleset()); ?></label>
                    <?php Helper::render_markup_from_views('Public/partials/fields/input.php', [
                        'type' => 'text',
                        'value' => '3',
                        'name' => 'estatebud_interest_rate',
                        'id' => 'estatebud_interest_rate',
                    ]); ?>
                </div>
            </div>
        </div>

        <div class="estatebud-listing-mortgage-result<?php echo $monthly_repayment ? '' : ' estatebud-hidden'; ?>">
            <label class="estatebud-listing-mortgage-result-heading"><?php echo esc_html(Helper::term('mortgage_calculator.monthly_repayment')); ?></label>
            <div class="estatebud-listing-mortgage-result-number">
                <?php echo esc_html(Helper::format_price(
                    ['price' => $monthly_repayment, 'currency' => $listing->get_price()['currency']],
                    $currency_data['exchange_rates']
                )); ?>
            </div>
            <div class="estatebud-listing-mortgage-base-result-number estatebud-hidden">
                <?php echo esc_html(Helper::format_price(
                    ['price' => $monthly_repayment, 'currency' => $listing->get_price()['currency']],
                    $currency_data['base_currency'] ?? []
                )); ?>
            </div>
        </div>
    </div>
</div>