<?php $get = $this->input->get(null, true); ?>
<?php $ref_id = isset($get['ref']) && !empty($get['ref']) ? '?ref=' . $get['ref'] : ''; ?>
<?php $pricing = get_by_section_name('pricing');
$settings = settings();
?>
<?php if ($this->settings['is_registration'] == 1): ?>
    <!-- Pricing Section -->
    <section class="pricing-section" id="pricing">
        <div class="container py-lg-5 py-4">

            <div class="text-center mb-4 mb-lg-5">
                <h2 class="ops-section-title"><?= highlight_word(html_escape($pricing['heading']),'middle'); ?></span></h2>
                <p class="biz-types-lead mb-0"><?= html_escape($pricing['sub_heading']); ?></p>
            </div>

            <div class="row justify-content-center">
                <?php $colorType = ['--basic', '--pro', '--advanced', '--premium']; ?>
                <?php foreach ($packages as $key => $package): ?>
                    <!-- Pricing Basic Card -->
                    <div class="col-lg-3 col-md-6 mb-4">
                        <div class="pricing-card pricing-card<?= $colorType[array_rand($colorType)]  ?>">


                            <!-- Pricing header -->
                            <div class="pricing-crown-wrap" aria-hidden="true">👑</div>

                            <div class="pricing-plan-name"> <?= html_escape($package['package_name']); ?></div>


                            <?= html_escape($package['package_name']); ?></h5>
                            <?php if ($package['package_type'] == 'free'): ?>

                                <h6 class="card-price">
                                    <?= !empty(lang('free')) ? lang('free') : 'Free'; ?>
                                </h6>
                            <?php elseif ($package['package_type'] == 'trial'): ?>
                                <h6 class="card-price text-center"><?= !empty(lang('free')) ? lang('free') : 'Free'; ?><span
                                        class="period">/<?= !empty(lang('month')) ? lang('month') : 'month'; ?></span></h6>

                            <?php elseif ($package['package_type'] == 'weekly'): ?>
                                <h6 class="card-price text-center"><?= !empty(lang('free')) ? lang('free') : 'Free'; ?><span
                                        class="period">/<?= !empty(lang('week')) ? lang('week') : 'week'; ?></span></h6>

                            <?php elseif ($package['package_type'] == 'fifteen'): ?>
                                <h6 class="card-price text-center"><?= !empty(lang('free')) ? lang('free') : 'Free'; ?><span
                                        class="period">/<?= !empty(lang('15_days')) ? lang('15_days') : '15 days'; ?></span></h6>

                            <?php else: ?>
                                <h6 class="card-price text-center">
                                    <?= !empty($package['previous_price']) ? "<span class='previous_price fz-14'>" . admin_currency_position($package['previous_price']) . "</span>" : ""; ?> <?= admin_currency_position(html_escape($package['price'])); ?><span class="period"> /
                                        <?= !empty($package['package_type']) ? get_package_type($package['package_type'], $package['duration'], $package['duration_period']) : get_package_type($package['package_type'], $package['duration'], $package['duration_period']); ?></span>
                                </h6>
                            <?php endif; ?>

                            <div class="pz-divider"></div>

                            <!-- Pricing features -->
                            <ul class="pricing-feat-list">

                                <?php foreach ($all_features as $key2 => $feature): ?>
                                    <?php $feature_id = get_price_feature_id($feature['id'], $package['id']); ?>


                                    <?php if (LICENSE == '6fa1b959a5580d843a4ea03422873009' && $feature['slug'] == 'online-payment'): ?>
                                    <?php else: ?>

                                        <?php if (isset($feature_id['feature_id']) && $feature_id['feature_id'] == $feature['id']): ?>
                                            <li class="pricing-feat">
                                                <i class="fa fa-check" aria-hidden="true"></i> <span> <?= change_name($feature['slug']); ?>
                                                    <?= html_escape($feature['slug']) == 'menu' ? ' <b>(' . limit_text($package['item_limit']) . ' ' . lang('items') . ') </b>' : ''; ?>
                                                    <?= html_escape($feature['slug']) == 'order' ? ' <b>(' . limit_text($package['order_limit']) . ') </b>' : ''; ?></span>

                                            </li>
                                        <?php else: ?>
                                            <li class="pricing-feat not">
                                                <i class="fa fa-times" aria-hidden="true"></i> <span> <?= change_name($feature['slug']); ?>
                                                    <?= html_escape($feature['slug']) == 'menu' ? ' <b>(' . limit_text($package['item_limit']) . ' ' . lang('items') . ') </b>' : ''; ?>
                                                    <?= html_escape($feature['slug']) == 'order' ? ' <b>(' . limit_text($package['order_limit']) . ') </b>' : ''; ?></span>

                                            </li>
                                        <?php endif; ?>

                                    <?php endif; ?>

                                <?php endforeach; ?>

                            </ul>

                            <a href="javascript:;" class="btn btn-link" data-toggle="modal" data-target="#packageModal_<?= $package['id'] ?>"><?= __('see_more'); ?></a>
                            <!-- subscribe Btn -->
                            <div class="text-center mt-auto">
                                <a href="<?= base_url('signup-' . $package['slug']) . $ref_id; ?>" class="btn subscribe-btn mt-3"><?= !empty(lang('select_package')) ? lang('select_package') : 'Select Package'; ?></a></a>
                            </div>

                        </div>
                    </div>

                <?php endforeach; ?>





            </div>

        </div>
    </section>

    <?php
    // Define color themes
    $colorTypes = ['--basic', '--pro', '--advanced', '--premium'];
    ?>

    <style>
        /* Modern Pricing Card Styles */
        .pricing-card-modern {
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            background: #fff;
            border: none;
        }

        .pricing-card-modern:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
        }

        /* Dynamic Color Variables */
        .pricing-card-modern.--basic {
            --theme-color: #6c757d;
            --theme-gradient: linear-gradient(135deg, #6c757d 0%, #495057 100%);
        }

        .pricing-card-modern.--pro {
            --theme-color: #007bff;
            --theme-gradient: linear-gradient(135deg, #007bff 0%, #0056b3 100%);
        }

        .pricing-card-modern.--advanced {
            --theme-color: #6f42c1;
            --theme-gradient: linear-gradient(135deg, #6f42c1 0%, #4a2c82 100%);
        }

        .pricing-card-modern.--premium {
            --theme-color: #fd7e14;
            --theme-gradient: linear-gradient(135deg, #fd7e14 0%, #d35400 100%);
        }

        .card-header-custom {
            background: var(--theme-gradient);
            padding: 30px 20px;
            text-align: center;
            color: white;
            position: relative;
        }

        .card-header-custom h3 {
            font-weight: 700;
            margin-bottom: 5px;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .price-tag {
            font-size: 2.5rem;
            font-weight: 800;
            margin: 15px 0 5px;
        }

        .price-period {
            font-size: 1rem;
            opacity: 0.9;
            font-weight: 400;
        }

        .previous-price {
            text-decoration: line-through;
            opacity: 0.7;
            font-size: 1rem;
            margin-right: 5px;
        }

        .feature-list {
            padding: 20px 30px;
            list-style: none;
            margin: 0;
        }

        .feature-list li {
            padding: 10px 0;
            border-bottom: 1px solid #f0f0f0;
            display: flex;
            align-items: center;
            color: #555;
        }

        .feature-list li:last-child {
            border-bottom: none;
        }

        .feature-list i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }

        .feature-list .fa-check {
            color: #28a745;
        }

        .feature-list .fa-times {
            color: #dc3545;
        }

        .order-type-section {
            background: #f8f9fa;
            padding: 15px 30px;
            border-top: 1px solid #eee;
        }

        .order-type-section legend {
            font-size: 0.9rem;
            font-weight: 700;
            color: #333;
            margin-bottom: 10px;
            width: auto;
            padding: 0 5px;
        }

        .btn-select-package {
            background: var(--theme-gradient);
            border: none;
            color: white;
            padding: 12px;
            font-weight: 600;
            border-radius: 0 0 15px 15px;
            /* Match card bottom radius if button is outside, or adjust if inside */
            transition: opacity 0.2s;
        }

        .btn-select-package:hover {
            opacity: 0.9;
            color: white;
        }
    </style>

    <?php foreach ($packages as $key => $mpackage): ?>
        <?php
        // Assign random color class
        $randomColorClass = $colorTypes[array_rand($colorTypes)];

        // Prepare Price Display Logic
        $displayPrice = '';
        $periodLabel = '';

        if ($mpackage['package_type'] == 'free') {
            $displayPrice = !empty(lang('free')) ? lang('free') : 'Free';
        } elseif ($mpackage['package_type'] == 'trial') {
            $displayPrice = (!empty(lang('free')) ? lang('free') : 'Free');
            $periodLabel = '/ ' . (!empty(lang('month')) ? lang('month') : 'month');
        } elseif ($mpackage['package_type'] == 'weekly') {
            $displayPrice = (!empty(lang('free')) ? lang('free') : 'Free'); // Assuming trial/weekly might be free or specific logic needed here
            $periodLabel = '/ ' . (!empty(lang('week')) ? lang('week') : 'week');
        } elseif ($mpackage['package_type'] == 'fifteen') {
            $displayPrice = (!empty(lang('free')) ? lang('free') : 'Free');
            $periodLabel = '/ ' . (!empty(lang('15_days')) ? lang('15_days') : '15 days');
        } else {
            // Paid Package
            $prevPriceHtml = !empty($mpackage['previous_price'])
                ? '<span class="previous-price">' . admin_currency_position($mpackage['previous_price']) . '</span>'
                : '';
            $currentPrice = admin_currency_position(html_escape($mpackage['price']));
            $durationText = get_package_type($mpackage['package_type'], $mpackage['duration'], $mpackage['duration_period']);

            $displayPrice = $prevPriceHtml . $currentPrice;
            $periodLabel = '/ ' . $durationText;
        }

        // Custom Fields
        $customFields = isset($mpackage['custom_fields_config']) && !empty($mpackage['custom_fields_config'])
            ? json_decode($mpackage['custom_fields_config'], true)
            : [];

        // Order Types
        $orderList = isset($mpackage['order_types']) && isJson($mpackage['order_types'])
            ? json_decode($mpackage['order_types'], true)
            : [];
        ?>

        <div class="modal fade" id="packageModal_<?= $mpackage['id'] ?>" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="packageModalLabel_<?= $mpackage['id'] ?>" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content border-0 shadow-lg rounded-lg">

                    <!-- Close Button Absolute -->
                    <button type="button" class="close position-absolute" style="top: 15px; right: 15px; z-index: 10; color: white; opacity: 0.8;" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>

                    <div class="pricing-card-modern <?= $randomColorClass ?>">

                        <!-- Header / Price Section -->
                        <div class="card-header-custom">
                            <h3><?= html_escape($mpackage['package_name']); ?></h3>
                            <div class="price-tag">
                                <?= $displayPrice; ?>
                            </div>
                            <?php if ($periodLabel): ?>
                                <div class="price-period"><?= $periodLabel; ?></div>
                            <?php endif; ?>
                        </div>

                        <!-- Features Body -->
                        <div class="card-body p-0">
                            <ul class="feature-list">
                                <?php foreach ($all_features as $feature): ?>
                                    <?php
                                    $feature_id_data = get_price_feature_id($feature['id'], $mpackage['id']);
                                    $hasFeature = isset($feature_id_data['feature_id']) && $feature_id_data['feature_id'] == $feature['id'];

                                    // License Check for Online Payment
                                    if (LICENSE == '6fa1b959a5580d843a4ea03422873009' && $feature['slug'] == 'online-payment') {
                                        continue;
                                    }
                                    ?>
                                    <li>
                                        <i class="fas <?= $hasFeature ? 'fa-check' : 'fa-times'; ?>"></i>
                                        <span>
                                            <?= change_name($feature['slug']); ?>

                                            <!-- Specific Limits Logic -->
                                            <?php if (html_escape($feature['slug']) == 'menu'): ?>
                                                <b>(<?= limit_text($mpackage['item_limit']) ?> <?= lang('items') ?>)</b>
                                            <?php elseif (html_escape($feature['slug']) == 'order'): ?>
                                                <b>(<?= limit_text($mpackage['order_limit']) ?>)</b>
                                            <?php endif; ?>
                                        </span>
                                    </li>
                                <?php endforeach; ?>

                                <!-- Custom Fields -->
                                <?php if (is_array($customFields) && !empty($customFields)): ?>
                                    <?php foreach ($customFields as $field): ?>
                                        <?php if (!empty($field)): ?>
                                            <li>
                                                <i class="fas fa-check"></i>
                                                <span><?= $field; ?></span>
                                            </li>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>

                            <!-- Order Types Section -->
                            <?php if (!empty($orderList)): ?>
                                <div class="order-type-section">
                                    <fieldset>
                                        <legend><?= lang('order_types'); ?></legend>
                                        <ul class="list-unstyled mb-0" style="font-size: 0.9rem;">
                                            <?php foreach ($order_types as $order): ?>
                                                <li class="mb-1">
                                                    <i class="fas <?= in_array($order['id'], $orderList) ? 'fa-check text-success' : 'fa-times text-danger'; ?> mr-2"></i>
                                                    <?= !empty(__($order['slug'])) ? __($order['slug']) : $order['name']; ?>
                                                </li>
                                            <?php endforeach; ?>
                                        </ul>
                                    </fieldset>
                                </div>
                            <?php endif; ?>
                        </div>

                        <!-- Footer / Action -->
                        <div class="text-center pb-4 pt-2">
                            <a href="<?= base_url('signup-' . $mpackage['slug']) . $ref_id; ?>" class="btn btn-lg btn-block btn-select-package mx-auto" style="max-width: 80%;">
                                <?= !empty(lang('select_package')) ? lang('select_package') : 'Select Package'; ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php endforeach; ?>

<?php endif; ?>