<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

$toast_msg = "";

// Handle form submissions
include_once('controller.php');

global $kirb_is_plugin_active;
global $kirb_active_tab_config;
global $kirb_active_initial_tab_config;
global $kirb_active_initial_tab;
global $kirb_is_plugin_initial_setup;
global $kirb_active_lang_tab_config;

$kirb_is_plugin_active = kirbGetMeta('_is_plugin_active') == 'true';
$kirb_active_tab_config = json_decode(kirbGetMeta('_admin_tab_config'), true);

foreach($kirb_active_tab_config as $key => $value){
    $kirb_active_tab_config[$key]['completed'] = $kirb_active_tab_config[$key]['completed'] && $kirb_is_plugin_active;
}

if($kirb_is_plugin_initial_setup) {
    kirbAddMeta('_is_plugin_initial_setup','true');
}

$kirb_active_initial_tab_config = array(
    'basic-tab' => true,
    'more-tab' => false,
    'premium-tab' => false,
);

$kirb_active_lang_tab_config = array(
    'basic-tab' => true,
    'translations-tab' => false,
);

$kirb_active_initial_tab = 'basic-tab';

function kirb_set_initial_active_tab($tab) {
    global $kirb_active_initial_tab_config;
    global $kirb_active_initial_tab;
    foreach ($kirb_active_initial_tab_config as $key => &$value) {
        $value = ($key === $tab);
        $kirb_active_initial_tab = $tab;
    }
}

function kirb_set_active_tab($tab) {
    global $kirb_active_tab_config;
    foreach ($kirb_active_tab_config as $key => &$value) {
        $value['active'] = ($key === $tab);
    }
}

if(!$kirb_is_plugin_active) {
    kirb_set_active_tab('pricing-tab');
}

$kirb_page_ids = json_decode(kirbGetMeta("_page_ids"), true);

$shop_name = kirbGetMeta("_shop_name");
$admin_email = kirbGetMeta("_shop_email");

$catg_lbls = array(
    'NATURAL' => 'Natural Diamonds',
    'LAB CREATED' => 'Lab Diamonds',
    'GEMSTONE' => 'Gemstones',
    'COLORED DIAMONDS' => 'Colored Diamonds'
);

$images = [
    KIRB_PLUGIN_DIR . '/assets/images/natural-diamond.webp',
    KIRB_PLUGIN_DIR . '/assets/images/lab-created-diamond.webp',
    KIRB_PLUGIN_DIR . '/assets/images/gemstone-page.webp',
    KIRB_PLUGIN_DIR . '/assets/images/colored-diamond-page.webp'
];

$primary_color = sanitize_hex_color(kirbGetMeta('_setting_primary_color'));
$accent_color = sanitize_hex_color(kirbGetMeta('_setting_accent_color'));
$primary_text_color = sanitize_hex_color(kirbGetMeta('_setting_text_primary_color')) ?? '#ffffff';
$accent_text_color = sanitize_hex_color(kirbGetMeta('_setting_text_accent_color')) ?? '#000000';
$catg_control_setting = json_decode(kirbGetMeta('_setting_catg_control'), true);
$products_per_page_setting = intval(kirbGetMeta('_setting_products_per_page'));
$cache_enabled_setting = kirbGetMeta('_setting_cache_enabled') == 'true';
$addons_data = json_decode(kirbGetMeta('_setting_addon_data'), true);
$primary_color_filter = esc_html(kirbGetMeta('_setting_primary_color_filter'));
$primary_text_color_filter = esc_html(kirbGetMeta('_setting_text_primary_color_filter'));
$accent_text_color_filter = esc_html(kirbGetMeta('_setting_text_accent_color_filter'));

$custom_css = "
    :root {
        --kirb-primary-color: {$primary_color};
        --kirb-accent-color: {$accent_color};
        --kirb-text-primary-color: {$primary_text_color};
        --kirb-text-accent-color: {$accent_text_color};
        --kirb-primary-color-filter: {$primary_color_filter};
        --kirb-text-primary-color-filter: {$primary_text_color_filter};
        --kirb-text-accent-color-filter: {$accent_text_color_filter};
    }
";

// Add the inline styles to the stylesheet that was enqueued
wp_add_inline_style('kirb-custom-style', $custom_css);

if (!kirbGetMeta('_setting_ring_setting_catg') && $kirb_is_plugin_active) {
    echo '<div class="notice notice-error is-dismissible"><p>Please select a setting category in the initial setup tab, otherwise plugin won\'t work correctly.</p></div>';
}

$user_info = kirbGetMeta('_user_info');
$plugin_info = get_plugin_data(KIRB_HOME_FILE);
if($user_info) {
    $user_info = json_decode($user_info, true);
    $user_info = array_map('stripslashes', $user_info);
}

$kirb_is_plugin_active = kirbGetMeta('_is_plugin_active') == 'true';
?>
<section class="main-div-tab d-flex">
    
    <!-- Nav tabs -->
    <div class="tab-btn-section">
        <div class="left-tab-heading">
            <div class="tab-image">
                <img src="<?php echo esc_html(KIRB_PLUGIN_DIR) ?>/assets/images/left-tab-image.svg" alt="">
                <h5 class="tab-heading">Keyideas<br>RingBuilder - Jewelry</h5>
            </div>
            <div class="version-p">
                <p>v<?php echo esc_html($plugin_info['Version']); ?></p>
            </div>
        </div>

        <ul class="nav nav-tabs" role="tablist">
            <div class="body-overlay-bg"></div>
            <div class="manage-heading">
                <svg height="17" width="17" xmlns="http://www.w3.org/2000/svg" class="kirb_highlight_svg" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M5.32943 3.27158C6.56252 2.8332 7.9923 3.10749 8.97927 4.09446C10.1002 5.21537 10.3019 6.90741 9.5843 8.23385L20.293 18.9437L18.8788 20.3579L8.16982 9.64875C6.84325 10.3669 5.15069 10.1654 4.02952 9.04421C3.04227 8.05696 2.7681 6.62665 3.20701 5.39332L5.44373 7.63C6.02952 8.21578 6.97927 8.21578 7.56505 7.63C8.15084 7.04421 8.15084 6.09446 7.56505 5.50868L5.32943 3.27158ZM15.6968 5.15512L18.8788 3.38736L20.293 4.80157L18.5252 7.98355L16.7574 8.3371L14.6361 10.4584L13.2219 9.04421L15.3432 6.92289L15.6968 5.15512ZM8.97927 13.2868L10.3935 14.7011L5.09018 20.0044C4.69966 20.3949 4.06649 20.3949 3.67597 20.0044C3.31334 19.6417 3.28744 19.0699 3.59826 18.6774L3.67597 18.5902L8.97927 13.2868Z">     
                    </path>
                </svg>
                MANAGE
                <div class="manage-popup-main">
                    <div class="manage-heading">
                        <div>To setup follow:</div>
                        <svg width="19" height="20" viewBox="0 0 19 20" fill="none" xmlns="http://www.w3.org/2000/svg" class="close-popup" style="cursor:pointer;">
                            <path fill-rule="evenodd" clip-rule="evenodd" d="M1.36685 1.76791C1.9711 1.16367 2.95078 1.16367 3.55502 1.76791L17.9045 16.1174C18.5088 16.7216 18.5088 17.7013 17.9045 18.3056C17.3003 18.9098 16.3206 18.9098 15.7163 18.3056L1.36685 3.95608C0.762606 3.35184 0.762606 2.37216 1.36685 1.76791Z" fill="black"/>
                            <path fill-rule="evenodd" clip-rule="evenodd" d="M1.36752 18.3089C0.763276 17.7047 0.763276 16.725 1.36752 16.1208L15.717 1.77127C16.3213 1.16702 17.3009 1.16703 17.9052 1.77127C18.5094 2.37552 18.5094 3.35519 17.9052 3.95944L3.55569 18.3089C2.95145 18.9132 1.97177 18.9132 1.36752 18.3089Z" fill="black"/>
                        </svg>
                    </div>
                    <div>
                    <div class="manage-popup-triangle-container"></div>
                    <div class="manage-popup-triangle"></div>
                        <div class="kirb-setup-popup">
                            <div class="kirb-setup-number">1</div>
                                <div class="kirb-popup-text">
                            <span class="kirb-popup-head">Plugin Activation:</span>
                            <span class="kirb-popup">Review and activate</span>
                        </div>
                            <div class="kirb-popup-line"></div>
                        </div>

                        <div class="kirb-setup-popup">
                            <div class="kirb-setup-number">2</div>
                                <div class="kirb-popup-text">
                                <span class="kirb-popup-head">Settings:</span>
                            <span class="kirb-popup">Customize options</span>
                            </div>
                            <div class="kirb-popup-line"></div>
                        </div>

                        <div class="kirb-setup-popup">
                            <div class="kirb-setup-number popup-step-3">3</div>
                                <div class="kirb-popup-text">
                                <span class="kirb-popup-head">Language &amp; Region:</span>
                            <span class="kirb-popup">Manage translations, and ring sizes</span>
                            </div>
                            <div class="kirb-popup-line"></div>
                        </div>

                        <div class="kirb-setup-popup">
                            <div class="kirb-setup-number popup-step-4">4</div>
                                <div class="kirb-popup-text">
                                <span class="kirb-popup-head">Feed Center:</span>
                            <span class="kirb-popup">Manage margins, inventory, and discounts for stones</span>
                            </div>
                            <div class="kirb-popup-line popup-line-step-4"></div>
                        </div>

                    </div>
                </div>
            </div>
            <li class="nav-item" role="presentation">
                <button class="<?php echo $kirb_active_tab_config['pricing-tab']['active'] ? 'active' : '' ?> <?php echo $kirb_active_tab_config['pricing-tab']['completed'] ? 'completed' : '' ?>" id="pricing-tab" data-bs-toggle="tab" data-bs-target="#pricing-tab-pane" type="button" role="tab" aria-controls="pricing-tab-pane" aria-selected="false">
                  1. My Plan
                </button>
            </li>
           
            <li class="nav-item" role="presentation">
                <button class="<?php echo $kirb_active_tab_config['initial-tab']['active'] ? 'active' : '' ?> <?php echo $kirb_active_tab_config['initial-tab']['completed'] ? 'completed' : '' ?>" id="initial-tab" data-bs-toggle="tab" data-bs-target="#initial-tab-pane" type="button" role="tab" aria-controls="initial-tab-pane" aria-selected="true" <?php echo !$kirb_active_tab_config['pricing-tab']['completed'] ? 'disabled' : '' ?>>
                2. Settings
                </button>
            </li>
        
            <li class="nav-item" role="presentation">
                <button class="<?php echo $kirb_active_tab_config['lang-tab']['active'] ? 'active' : '' ?> <?php echo $kirb_active_tab_config['lang-tab']['completed'] ? 'completed' : '' ?>" id="lang_region-tab" data-bs-toggle="tab" data-bs-target="#lang_region-tab-pane" type="button" role="tab" aria-controls="lang_region-tab-pane" aria-selected="false" <?php echo !$kirb_active_tab_config['initial-tab']['completed'] ? 'disabled' : '' ?>>
                3. Language & Region
                </button>
            </li>

            
            <li class="nav-item" role="presentation">
                <span class="client-login-btn">
                    4. Feed Center <br>
                    <span>(Not available in base version)</span>
                </span>
            </li>

            <div class="line"></div>

            <div class="manage-heading help-support-heading">
                <svg height="17" width="17" xmlns="http://www.w3.org/2000/svg" class="kirb_highlight_svg" viewBox="0 0 24 24" fill="currentColor"><path d="M21 8C22.1046 8 23 8.89543 23 10V14C23 15.1046 22.1046 16 21 16H19.9381C19.446 19.9463 16.0796 23 12 23V21C15.3137 21 18 18.3137 18 15V9C18 5.68629 15.3137 3 12 3C8.68629 3 6 5.68629 6 9V16H3C1.89543 16 1 15.1046 1 14V10C1 8.89543 1.89543 8 3 8H4.06189C4.55399 4.05369 7.92038 1 12 1C16.0796 1 19.446 4.05369 19.9381 8H21ZM7.75944 15.7849L8.81958 14.0887C9.74161 14.6662 10.8318 15 12 15C13.1682 15 14.2584 14.6662 15.1804 14.0887L16.2406 15.7849C15.0112 16.5549 13.5576 17 12 17C10.4424 17 8.98882 16.5549 7.75944 15.7849Z"></path></svg>
                HELP & SUPPORT
            </div>

            <li class="kirb-help-support-tab nav-item" role="presentation">
                <button id="tutorials-tab-parent" data-target-id="tutorials-tab" data-bs-toggle="tab" data-bs-target="#help-tab-pane" type="button" role="tab" aria-controls="help-tab-pane" aria-selected="false">
                  Video Tutorials
                </button>
            </li>

            <li class="kirb-help-support-tab nav-item" role="presentation">
                <button id="faq-tab-parent" data-target-id="faq-tab" data-bs-toggle="tab" data-bs-target="#help-tab-pane" type="button" role="tab" aria-controls="help-tab-pane" aria-selected="false">
                FAQs
                </button>
            </li>

            <li class="kirb-help-support-tab nav-item" role="presentation">
                <button class="<?php echo $kirb_active_tab_config['help-tab']['active'] ? 'active' : '' ?>" id="support-tab-parent" data-target-id="support-tab" data-bs-toggle="tab" data-bs-target="#help-tab-pane" type="button" role="tab" aria-controls="help-tab-pane" aria-selected="false">
                Contact Us
                </button>
            </li>
        </ul>
    </div>

    
    <div class="tab-content-section">
        <?php 
            require_once('partials/tab_pricing.php');
            if($kirb_active_tab_config['pricing-tab']['completed']) {
                require_once('partials/tab_settings.php'); 
            }
            if($kirb_active_tab_config['initial-tab']['completed']) {
                require_once('partials/tab_language.php');
            }
            require_once('partials/tab_help.php');
        ?>
    </div>

    <!-- Preview Image Modal Start-->
    <div class="view-360-section modal position-fixed" id="kirb_img_preview_modal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content preview-section-modal">
                <div class="d-flex ">
                    <div class="pera-content"></div>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"
                        aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <img id="kirb_img_preview_modal_img" src="" class="img-fluid" alt="Selected Image">
                </div>
                <div class="keyideas-logo" id="keyideas-logo-360">
                    <img src="<?php echo esc_html(KIRB_PLUGIN_DIR) ?>/assets/images/powered-by.svg" width="110px"
                        height="15px" alt="">
                </div>
            </div>
        </div>
    </div>
    <!-- Preview Image Modal End-->
    <?php if(!$kirb_is_plugin_active) { ?>
        <div class="kirb-consent-overlay d-flex align-items-center justify-content-center">
            <div class="card kirb-consent-card shadow-lg border-0">
                <form method="post" class="d-flex flex-column">
                    <div class="card-body p-4 p-md-4">
                        <h4 class="mb-2 fw-bold" style="font-size:14px;">Improve Your Experience</h4>

                        <span class="mb-2 kirb-font-12">
                            We’d like your consent to use your name and email to enhance performance, provide better usability,
                            and keep improving our free plugins.
                        </span>

                        <div class="form-check my-3 kirb-font-12" style="display: flex;align-items: baseline;">
                            <div class="kirb-input-check"><input class="form-check-input kirb-check" type="checkbox" value="1" id="kirbConsentCheck"></div>
                            <label class="form-check-label ml-3" for="kirbConsentCheck" style="margin-left: 8px;">
                                I agree to share my name and email for personalization and improvements.
                            </label>
                        </div>

                        <?php wp_nonce_field('kirb_email_permission', 'kirb_email_permission_nonce'); ?>
                        <input type="hidden" name="kirb_activation" value="1">

                        <div class="d-flex gap-3 mt-4 kirb-font-14">
                            <button type="submit" name="email_permission_action" value="accept" class="kirb-btn kirb-btn-primary kirb-consent-accept-btn" disabled>Agree</button>

                            <button type="submit" name="email_permission_action" value="decline" class="kirb-btn kirb-btn-outline kirb-font-14">Not Now</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    <?php } ?>
</section>
<div class="alert-section" style="<?php echo (empty($toast_msg)) ? 'display:none;' : '' ?>">
    <div class="alert success">
        <span class="close-btn" onclick="$('.alert-section').addClass('d-none');">×</span>
        <span class="kirb-toast-msg"><?php echo esc_html($toast_msg); ?></span>
    </div>
</div>
