<?php
    
    if (!function_exists('av')) {
        function av($file = '') {
            static $base = null;

            if ($base === null) {
                $CI =& get_instance();
                $base = $CI->config->item('app_version');

                $marker = APPPATH . 'cache/assets.version';
                if (file_exists($marker)) {
                    $ts = @file_get_contents($marker);
                    if ($ts !== false && ($ts = trim($ts)) !== '') {
                        $base .= '.' . $ts;
                    }
                }
            }

            if ($file !== '') {
                $full = FCPATH . $file;
                if (file_exists($full)) {
                    return $base . '.' . filemtime($full);
                }
            }

            return $base;
        }
    }

    if (!function_exists('asset_url')) {
        function asset_url($path) {
            return base_url($path) . '?v=' . av($path);
        }
    }

    if (!function_exists('bust_assets_cache')) {
        function bust_assets_cache() {
            $marker = APPPATH . 'cache/assets.version';
            return @file_put_contents($marker, time()) !== false;
        }
    }

    if (!function_exists('render_js_error_listener')) {
        function render_js_error_listener() {
            $title = __('admin.error');
            return '<script>(function(){var s=new Set(),q=[];function ensureContainer(){if(!document.body) return null;var c=document.getElementById("js-error-container");if(!c){c=document.createElement("div");c.id="js-error-container";c.className="position-fixed bottom-0 end-0 p-3";c.style.zIndex="20000";document.body.appendChild(c);}return c;}function showMsg(m){try{if(!m||s.has(m))return;s.add(m);if(window.showToast){window.showToast('.json_encode($title).',m,\'error\',7000);return;}var c=ensureContainer();if(!c){q.push(m);return;}var w=document.createElement("div");w.className="alert alert-danger d-flex align-items-center shadow mb-2";w.setAttribute("role","alert");var b=document.createElement("div");b.className="flex-grow-1";b.textContent=m;var x=document.createElement("button");x.type="button";x.className="btn-close ms-2";x.setAttribute("aria-label","Close");x.onclick=function(){if(w&&w.parentElement)w.parentElement.removeChild(w);};w.appendChild(b);w.appendChild(x);c.appendChild(w);}catch(e){}}document.addEventListener("DOMContentLoaded",function(){var c=ensureContainer();if(c&&q.length){q.splice(0).forEach(showMsg);}});window.addEventListener("load",function(){var c=ensureContainer();if(c&&q.length){q.splice(0).forEach(showMsg);}});window.addEventListener("error",function(e){if(!e||!e.message||e.message.includes("Script error")||e.message.includes("Non-Error"))return;var msg=e.message||"";var loc=e.filename||"";var ln=e.lineno||0;var cn=e.colno||0;if(msg&&loc)showMsg(msg+" ("+loc.split("/").pop()+":"+ln+":"+cn+")");},true);window.addEventListener("unhandledrejection",function(e){if(!e||!e.reason)return;var r=e.reason;if(typeof r==="string"&&r.includes("Non-Error"))return;var msg=(r&&r.message)?r.message:(typeof r==="string"?r:"Promise Rejection");if(msg)showMsg(msg);},true);window.__showJsError=showMsg;})();</script>';
        }
    }

    function dd($debug, $die = true) {
        echo "<pre>";
        try {
            print_r($debug);
        } catch (Exception $e) {
            echo $debug;
        }
        if($die) die();
    }


    function generateRandomAlpahaNemericCode()
    {
        $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomecode=substr(str_shuffle($permitted_chars), 0, 10);
        return $randomecode;
    }

    function print_message($t) { 
        $hasAlerts = false;
        
        if($t->session->flashdata('success')) { 
            $hasAlerts = true;
            ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="bi bi-check-circle-fill me-2"></i>
                <?php echo $t->session->flashdata('success'); ?>
                <span class="countdown-timer ms-2 text-muted small">(5)</span>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            <?php $t->session->unset_tempdata('success'); 
        }

        if($t->session->flashdata('error')) { 
            $hasAlerts = true;
            ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-exclamation-triangle-fill me-2"></i>
                <?php echo $t->session->flashdata('error'); ?>
                <span class="countdown-timer ms-2 text-muted small">(5)</span>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            <?php $t->session->unset_tempdata('error'); 
        }
        
        if($hasAlerts) { ?>
            <script>
                var countdown = 5;
                var timer = setInterval(function() {
                    countdown--;
                    var timers = document.querySelectorAll('.countdown-timer');
                    timers.forEach(function(timer) {
                        timer.textContent = '(' + countdown + ')';
                    });
                    
                    if (countdown <= 0) {
                        clearInterval(timer);
                        var alerts = document.querySelectorAll('.alert');
                        alerts.forEach(function(alert) {
                            var bsAlert = new bootstrap.Alert(alert);
                            bsAlert.close();
                        });
                    }
                }, 1000);
            </script>
        <?php }
    }

    function file_upload_max_size() {
        $upload_max = ini_get('upload_max_filesize');
        return $upload_max;
    }

    function duplicate_entry($table, $field, $id, $primaryKey, $overwrite = []){
        $CI =& get_instance();
        $CI->db->where($field, $id); 
        $query = $CI->db->get($table);

        foreach ($query->result() as $row){   
            foreach($row as $key=>$val){        
                if($key != $primaryKey){
                    if(isset($overwrite[$key])){
                        $CI->db->set($key, $overwrite[$key]);
                    } else{
                        $CI->db->set($key, $val);
                    }
                }
            }
        }

        $CI->db->insert($table); 
        return $CI->db->insert_id();
    }

    function parseIntegrationType($type) {
        switch ($type) {
            case 'single_action':
            return __('admin.single_action_integration');
            break;
            case 'action':
            return __('admin.multi_action_integration');
            break;
            case 'general_click':
            return __('admin.click_integration');
            break;
            break;
            case 'program':
            return __('admin.sale_integration');
            break;
            default:
            return __('admin.unknown');
            break;
        }
    }

        function withdrwal_status($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-light text-dark border">'.__('admin.received').'</span>'; break;
                case 13: $label = '<span class="badge bg-warning text-dark">'.__('admin.pending').'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'.__('admin.paid').'</span>'; break;
                case 2: $label = '<span class="badge bg-warning text-dark">'.__('admin.total_not_match').'</span>'; break;
                case 3: $label = '<span class="badge bg-danger text-white">'.__('admin.denied').'</span>'; break;
                case 4: $label = '<span class="badge bg-secondary text-white">'.__('admin.expired').'</span>'; break;
                case 5: $label = '<span class="badge bg-danger text-white">'.__('admin.failed').'</span>'; break;
                case 6: $label = '<span class="badge bg-warning text-dark">'.__('admin.pending').'</span>'; break;
                case 7: $label = '<span class="badge bg-primary text-white">'.__('admin.processed').'</span>'; break;
                case 8: $label = '<span class="badge bg-info text-white">'.__('admin.refunded').'</span>'; break;
                case 9: $label = '<span class="badge bg-dark text-white">'.__('admin.reversed').'</span>'; break;
                case 10: $label = '<span class="badge bg-secondary text-white">'.__('admin.voided').'</span>'; break;
                case 11: $label = '<span class="badge bg-info text-white">'.__('admin.cancel_reversal').'</span>'; break;
                case 12: $label = '<span class="badge bg-warning text-dark">'.__('admin.waiting_for_payment').'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">'.__('admin.unknown').'</span>'; break;
            }

            return $label;
        }


        function membership_withdrwal_status($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-warning text-dark">'.__('admin.pending').'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'.__('admin.active').'</span>'; break;
                case 2: $label = '<span class="badge bg-warning text-dark">'.__('admin.total_not_match').'</span>'; break;
                case 3: $label = '<span class="badge bg-danger text-white">'.__('admin.denied').'</span>'; break;
                case 4: $label = '<span class="badge bg-secondary text-white">'.__('admin.expired').'</span>'; break;
                case 5: $label = '<span class="badge bg-danger text-white">'.__('admin.failed').'</span>'; break;
                case 7: $label = '<span class="badge bg-primary text-white">'.__('admin.processed').'</span>'; break;
                case 8: $label = '<span class="badge bg-info text-white">'.__('admin.refunded').'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">'.__('admin.unknown').'</span>'; break;
            }

            return $label;
        }


        function store_withdrwal_status($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-warning text-dark">'.__('admin.waiting_for_payment').'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'.__('admin.complete').'</span>'; break;
                case 2: $label = '<span class="badge bg-warning text-dark">'.__('admin.total_not_match').'</span>'; break;
                case 3: $label = '<span class="badge bg-danger text-white">'.__('admin.denied').'</span>'; break;
                case 4: $label = '<span class="badge bg-secondary text-white">'.__('admin.expired').'</span>'; break;
                case 5: $label = '<span class="badge bg-danger text-white">'.__('admin.failed').'</span>'; break;
                case 6: $label = '<span class="badge bg-warning text-dark">'.__('admin.pending').'</span>'; break;
                case 7: $label = '<span class="badge bg-primary text-white">'.__('admin.processed').'</span>'; break;
                case 8: $label = '<span class="badge bg-info text-white">'.__('admin.refunded').'</span>'; break;
                case 9: $label = '<span class="badge bg-dark text-white">'.__('admin.reversed').'</span>'; break;
                case 10: $label = '<span class="badge bg-secondary text-white">'.__('admin.voided').'</span>'; break;
                case 11: $label = '<span class="badge bg-info text-white">'.__('admin.cancel_reversal').'</span>'; break;
                case 12: $label = '<span class="badge bg-warning text-dark">'.__('admin.waiting_for_payment').'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">'.__('admin.unknown').'</span>'; break;
            }

            return $label;
        }


        function ads_status($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-warning text-dark">'. __('admin.draft') .'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'. __('admin.public') .'</span>'; break;
                case 2: $label = '<span class="badge bg-info text-white">'. __('admin.in_review') .'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">'.__('admin.unknown').'</span>'; break;
            }
            return $label;
        }


        function ads_security_status($status, $postback_status = null, $integration_method = 'js_pixel') {
            $label = '';
            $method = $integration_method ?: 'js_pixel';
            
            switch ((int)$status) {
                case 0:
                    if (in_array($method, ['s2s', 's2s_direct', 'conversion_api'])) {
                        $tooltip = __('admin.intg_tooltip_pending_api');
                    } else {
                        $tooltip = __('admin.intg_tooltip_pending_js');
                    }
                    $label = '<span class="badge bg-info text-white" data-bs-toggle="tooltip" data-bs-placement="top" title="' . htmlspecialchars($tooltip) . '">' . __('admin.pending_integration') . '</span>';
                    break;
                case 1:
                    if ($method === 's2s') {
                        $text = __('admin.intg_status_s2s_verified');
                        $tooltip = __('admin.intg_tooltip_s2s_verified');
                    } elseif ($method === 's2s_direct') {
                        $text = __('admin.intg_status_mobile_verified');
                        $tooltip = __('admin.intg_tooltip_mobile_verified');
                    } elseif ($method === 'conversion_api') {
                        $text = __('admin.intg_status_conv_api_verified');
                        $tooltip = __('admin.intg_tooltip_conv_api_verified');
                    } else {
                        $text = __('admin.approved');
                        $tooltip = __('admin.intg_tooltip_approved_js');
                    }
                    $label = '<span class="badge bg-success text-white" data-bs-toggle="tooltip" data-bs-placement="top" title="' . htmlspecialchars($tooltip) . '">' . $text . '</span>';
                    break;
                case 2:
                    $tooltip = __('admin.intg_tooltip_postback');
                    $label = '<span class="badge bg-primary text-white" data-bs-toggle="tooltip" data-bs-placement="top" title="' . htmlspecialchars($tooltip) . '">' . __('admin.postback') . '</span>';
                    break;
                default: 
                    $label = '<span class="badge bg-secondary text-white">' . __('admin.unknown') . '</span>';
                    break;
            }
            
            return $label;
        }

        function ads_running_status($status){
            $label = '';

            switch ((int)$status) {
                case 0: $label = 'warning'; break;
                case 1: $label = 'success'; break;
                default: $label = 'warning'; break;
            }

            return $label;
        }
        function ads_google_status($status){
            $label = '';

            switch ((int)$status) {
                case 1: $label =  __('admin.side_bar_top'); break;
                case 2: $label = __('admin.side_bar_bottom'); break;
                case 3: $label = __('admin.footer'); break;
                case 4: $label = __('admin.right_side'); break;
                case 5: $label = __('admin.center_page'); break;
                default: $label = __('admin.right_side'); break;
            }

            return $label;
        }

        function program_status($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-warning text-dark">'. __('admin.in_review') .'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'. __('admin.approved') .'</span>'; break;
                case 2: $label = '<span class="badge bg-danger text-white">'. __('admin.denied') .'</span>'; break;
                case 3: $label = '<span class="badge bg-warning text-dark">'. __('admin.ask_to_edit') .'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">'. __('admin.unknow') .'</span>'; break;
            }
            return $label;
        }


        function product_status_on_store($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-danger text-white">'. __('admin.not_displayed') .'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'. __('admin.displayed') .'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">'. __('admin.unknow') .'</span>'; break;
            }
            return $label;
        }


        function product_status_on_store_admin($status, $product_by) {
            $label = '';
            $statusClass = (int)$status === 0 ? 'bg-danger' : 'bg-success';
            $statusText = (int)$status === 0 ? __('admin.draft') : __('admin.displayed');
            $textClass = (int)$status === 0 ? 'text-white' : 'text-white';
            $label = '<span class="badge '. $statusClass .' '. $textClass .'">'. $statusText .'</span>';
            return $label;
        }


        function product_status($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-warning text-dark">'. __('admin.in_review') .'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'. __('admin.approved') .'</span>'; break;
                case 2: $label = '<span class="badge bg-danger text-white">'. __('admin.denied') .'</span>'; break;
                case 3: $label = '<span class="badge bg-warning text-dark">'. __('admin.ask_to_edit') .'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">'. __('admin.unknow') .'</span>'; break;
            }
            return $label;
        }


        function form_status($status) {
            $label = '';
            switch ((int)$status) {
                case 0: $label = '<span class="badge bg-warning text-dark">'. __('admin.in_review') .'</span>'; break;
                case 1: $label = '<span class="badge bg-success text-white">'. __('admin.approved') .'</span>'; break;
                case 2: $label = '<span class="badge bg-danger text-white">'. __('admin.denied') .'</span>'; break;
                case 3: $label = '<span class="badge bg-warning text-dark">'. __('admin.ask_to_edit') .'</span>'; break;
                default: $label = '<span class="badge bg-secondary text-white">Unknow</span>'; break;
            }
            return $label;
        }


        function cycle_details($total_recurring,$next_transaction,$endtime = false,$total_recurring_amount = false ){
            $str =  'Runs '. (int)$total_recurring;

            if($next_transaction != ''){
                $str .= " | Next At : ". date("d-m-Y H:i",strtotime($next_transaction));
            }
            if($endtime != ''){
                $str .= " | Endtime : ". date("d-m-Y H:i",strtotime($endtime));
            }
            if($total_recurring_amount){
                $str .= " | Total Amount : ". c_format($total_recurring_amount);
            }

            return $str;
        }

        function dateFormat($date , $f = "d-m-Y H:i:s"){
            return date($f,strtotime($date));
        }
        function timetosting($minutes){
            $day = floor ($minutes / 1440);
            $hour = floor (($minutes - $day * 1440) / 60);
            $minute = $minutes - ($day * 1440) - ($hour * 60);

            $str = '';
            if($day > 0) $str .= "{$day} day ";
            if($hour > 0) $str .= "{$hour} hour ";
            if($minute > 0) $str .= "{$minute} minute ";

            return $str;
        }
        if (!function_exists('legacy_asset_url')) {
            function legacy_asset_url() {
                echo base_url() . 'assets/';
            }
        }
        function pr($data) {
            echo '<pre>'; print_r($data); echo '</pre>';
        }
        function flashMsg($flash) { 
            if (isset($flash['error'])) {
                echo '<div class="alert alert-danger alert-dismissible"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">X</button>' .$flash['error']. '</div>';
            }
            if (isset($flash['success'])) {
                echo '<div class="alert alert-success alert-dismissible"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">X</button>' .$flash['success'] . '</div>';
            }
        }
    function DOCROOT($file, $from) {
        if ($from == 'full') {
            return @$_SERVER["DOCUMENT_ROOT"] . '/cyclops/assets/uploads/' . $file;
        } elseif ($from == 'thumb') {
            return @$_SERVER["DOCUMENT_ROOT"] . '/cyclops/assets/uploads/thumb/' . $file;
        }
    }

    function set_default_currency(){
        ___construct(1);
    }

    function is_rtl()
    {
        $CI =& get_instance();
        $lang = $_SESSION['userLang'];
        $lang = $CI->db->query("SELECT * FROM language WHERE status=1 AND id=". (int)$lang)->row_array();

        if ($lang['is_rtl']) {
            return true;
        } 

        return false;
    }

    global $language; 
    function __($key){
        global $language;
        $userLang = $_SESSION['userLang'];
        if($userLang == ''){
            $CI =& get_instance();
            $default_language = $CI->db->query("SELECT * FROM language WHERE status=1 AND is_default=1")->row_array();
            if($default_language){
                $userLang = $_SESSION['userLang'] = $default_language['id'];
            }
        }
        if(!$language){
            fillLang($userLang);
        }

        return isset($language[$key]) ? $language[$key] : $key;
    }
    function fillLang($id){
        global $language;
        $language = array();
        $lang_files = ['admin','client','store','user','front','template_simple'];


        foreach ($lang_files as $file) {
            if(is_file(APPPATH.'/language/default/'. $file .'.php')){
                require  APPPATH.'/language/default/'. $file .'.php';
                foreach ($lang as $key => $value) {
                    $language[$file . '.'.$key] = $value;
                }
            }
            $lang = array();
        }

        if($id != 1){
            foreach ($lang_files as $file) {
                if(is_file(APPPATH.'/language/'. $id .'/'. $file .'.php')){
                    require  APPPATH.'/language//'. $id .'//'. $file .'.php';
                    foreach ($lang as $key => $value) {
                        if($value) $language[$file . '.'.$key] = $value;
                    }
                }
                $lang = array();
            }
        }
    }

    function recurse_copy($src,$dst) { 
        $dir = opendir($src);
        if (!file_exists($dst)) {
            mkdir($dst, 0777, true);
        }
        while(false !== ( $file = readdir($dir)) ) {
            if (( $file != '.' ) && ( $file != '..' )) {
                if ( is_dir($src . '/' . $file) ) {
                    recurse_copy($src . '/' . $file,$dst . '/' . $file);
                }
                else {
                    copy($src . '/' . $file,$dst . '/' . $file);
                }
            }
        }
        closedir($dir);
    }
    function lang_copy($src,$dst, $defaultLangPath = null){
        $dir = opendir($src);
        if (!file_exists($dst)) {
            mkdir($dst, 0777, true);
        }

        $lang_files = ['admin','client','store','user','front','template_simple'];
        foreach ($lang_files as $file) {

            if($defaultLangPath != null && is_file($defaultLangPath .'/'. $file .'.php')) {
                $src = $defaultLangPath;
                $copy_translation = true;
            }


            if(is_file($src .'/'. $file .'.php')){
                $lang = array();

                require  $src .'/'. $file .'.php';

                $path = $dst."/".$file.".php";

                $file_content = '<?php '.PHP_EOL;

                foreach ($lang as $key => $value) {
                    if(isset($copy_translation)) {
                        $file_content .= '$lang[\''. $key .'\'] = \''. str_replace('"','\"', str_replace("'","\'", $value)) .'\';' .PHP_EOL;
                    } else {
                        $file_content .= '$lang[\''. $key .'\'] = \'\';' .PHP_EOL;
                    }
                }

                file_put_contents($path, $file_content);
            }
            $lang = array();
        }
    }
    function langCount($id){
        $id = $id == "1" ? 'default' : $id;

        $missing = $all = [];
        $count = array('all' => 0, 'missing' => 0);
        $lang_files = ['admin','client','store','user','front','template_simple'];
        foreach ($lang_files as $file) {
            if(is_file(APPPATH.'/language/'. $id .'/'. $file .'.php')){
                $lang = array();
                require  APPPATH.'/language//'. $id .'//'. $file .'.php';
                foreach ($lang as $key => $value) {
                    $count['all']++;
                    $all[$key] = $value;
                    if($value == '' || $value == null || trim($value) == ''){
                        $missing[$key] = $value;
                    }
                }
            }
            $lang = array();
        }

        $count = array('all' => count($all), 'missing' => count($missing));
        return $count;
    }

    function wallet_paid_status($status){
        $html = '';
        switch ($status) {
            case '0': return "<span class='badge bg-secondary text-white'>".__('admin.not_paid')."</span>"; break;
            case '1': return "<span class='badge bg-secondary text-white'>".__('admin.not_paid')."</span>"; break;
            case '2': return "<span class='badge bg-primary text-white'>".__('admin.in_request')."</span>"; break;
            case '3': return "<span class='badge bg-success text-white'>".__('admin.paid')."</span>"; break;
            case '4': return "<span class='badge bg-danger text-white'>".__('admin.declined')."</span>"; break;
            default: return "<span></span>"; break;
        }

    }

    function commission_status($status){
        $html = '';
        switch ($status) {
            case '1': return "<span class='badge bg-warning text-dark'>".__('admin.canceled')."</span>"; break;
            case '2': return "<span class='badge bg-danger text-white'>".__('admin.trashed')."</span>"; break;
            default: return "<span></span>"; break;
        }

    }

    function set_tmp_cache(){
        ___construct(1);
    }

    function wallet_whos_commission($trans,$userid=""){
        if($trans['type'] == 'external_click_comm_pay'){
            if($trans['from_user_id'] == '1'){ return "Pay to admin"; }
            else { return __('admin.pay_to_affiliate'); }
        }

        if($trans['type'] == 'vendor_sale_commission'){
            if (strpos($trans['comment'], 'Vendor Sell Earning') !== false) {
                return __('admin.vendor_earning');
            }
        }

        if($trans['type'] == 'vendor_shipping_reimbursement'){
            return __('admin.vendor_shipping_reimbursement');
        }

        if($trans['type'] == 'admin_shipping_reimbursement'){
            return __('admin.admin_shipping_reimbursement');
        }

        if($trans['comm_from'] == 'ex'){
            if($trans['type'] == 'sale_commission' || $trans['type'] == 'refer_sale_commission'){
                return __('admin.affiliate_commission');
            }
            if($trans['type'] == 'admin_sale_commission_v_pay'){
                return __('admin.pay_to_admin');
            }
            if($trans['type'] == 'sale_commission_vendor_pay'){
                return __('admin.pay_to_affiliate');
            }
            if($trans['type'] == 'external_click_commission'){
                return __('admin.affiliate_commission');
            }
            if($trans['type'] == 'click_commission' || $trans['type'] == 'refer_click_commission'){
                if($trans['reference_id_2'] == 'vendor_pay_click_commission_for_admin'){
                    return __('admin.pay_to_admin');
                    
                }
                if($trans['type'] == 'click_commission' && $trans['user_id']== $userid){
                    return __('admin.affiliate_commission');
                }
                if($trans['type'] == 'click_commission' && $trans['reference_id_2'] ==""){
                    return __('admin.affiliate_commission');
                }
                if($trans['type'] == 'refer_click_commission'){
                    return __('admin.affiliate_commission');
                }

                if($trans['reference_id_2'] == 'vendor_click_commission')
                {
                    if(isset($trans['usertype']) && $trans['usertype']=='admin')
                        
                        return __('admin.action_commission_settings');
                     else
                        return __('admin.affiliate_commission');
                }
                if($trans['reference_id_2'] == 'vendor_pay_click_commission'){
                    return __('admin.pay_to_affiliate');
                }
                
            }
        }

        if($trans['comm_from'] == 'store'){
            

            if($trans['type'] == 'sale_commission' || $trans['type'] == 'refer_sale_commission'){

                if((int)$trans['is_vendor'] == 1){
                    return __('admin.affiliate_commission');
                }

                
            }
            if($trans['reference_id_2'] == 'vendor_sale_commission'){
                if((int)$trans['is_vendor'] == 1){
                    return __('admin.affiliate_commission');
                }
            }


            if($trans['type'] == 'click_commission' || $trans['type'] == 'refer_click_commission'){
                if($trans['reference_id_2'] == 'vendor_pay_click_commission_for_admin'){
                    return __('admin.pay_to_admin');
                    
                }
                if($trans['type'] == 'click_commission' && $trans['user_id']== $userid){
                    return __('admin.affiliate_commission');
                }
                if($trans['type'] == 'click_commission' && $trans['reference_id_2'] ==""){
                    return __('admin.affiliate_commission');
                }
                if($trans['type'] == 'refer_click_commission'){
                    return __('admin.affiliate_commission');
                }

                if($trans['reference_id_2'] == 'vendor_click_commission')
                {
                    if(isset($trans['usertype']) && $trans['usertype']=='admin')
                        
                        return __('admin.admin_commission');
                     else
                        return __('admin.affiliate_commission');
                }
                if($trans['reference_id_2'] == 'vendor_pay_click_commission'){
                    return __('admin.pay_to_affiliate');
                }
                
            }
        }

        if($trans['is_vendor'] && $trans['user_id'] != '1'){
            return __('admin.vendor_commission');
        }

        return $trans['user_id'] == '1' ? __('admin.commission_for_admin') : __('admin.affiliate_commission');
    }

    function wallet_whos_commission_affiliate($trans){

        if($trans['type'] == 'external_click_comm_pay'){
            if($trans['from_user_id'] == '1'){ return "Pay to admin"; }
            else { return __('admin.pay_to_affiliate'); }
        }

        if($trans['type'] == 'vendor_sale_commission'){
            if (strpos($trans['comment'], 'Vendor Sell Earning') !== false) {
                return __('admin.vendor_earning');
            }
        }

        if($trans['comm_from'] == 'ex'){
            if($trans['type'] == 'sale_commission' || $trans['type'] == 'refer_sale_commission'){
                return __('admin.affiliate_commission');
            }
            if($trans['type'] == 'admin_sale_commission_v_pay'){
                return __('admin.pay_to_admin');
            }
            if($trans['type'] == 'sale_commission_vendor_pay'){
                return __('admin.pay_to_affiliate');
            }
            if($trans['type'] == 'external_click_commission'){
                return __('admin.pay_to_affiliate');
            }
        }

        if($trans['comm_from'] == 'store'){
            if($trans['type'] == 'sale_commission' || $trans['type'] == 'refer_sale_commission'){
                if((int)$trans['is_vendor'] == 1){
                    return __('admin.affiliate_commission');
                }
            }
            if($trans['reference_id_2'] == 'vendor_sale_commission'){
                if((int)$trans['is_vendor'] == 1){
                    return __('admin.affiliate_commission');
                }
            }


            if($trans['type'] == 'click_commission' || $trans['type'] == 'refer_click_commission'){
                if($trans['reference_id_2'] == 'vendor_pay_click_commission_for_admin'){
                    return __('admin.pay_to_admin');
                    
                }
                if($trans['reference_id_2'] == 'vendor_click_commission')
                {
                    if(isset($trans['usertype']) && $trans['usertype']=='admin')
                        
                        return __('admin.action_commission_settings');
                     else
                        return __('admin.affiliate_commission');
                }
                if($trans['reference_id_2'] == 'vendor_pay_click_commission'){
                    return __('admin.pay_to_affiliate');
                }
            }
        }

        if($trans['is_vendor'] && $trans['user_id'] != '1'){
            return __('admin.pay_to_affiliate');
        }

        return $trans['user_id'] == '1' ? __('admin.pay_to_admin') : __('admin.pay_to_affiliate');
    }
        function wallet_whos_commission_vendor($trans,$userdetail){

        if($trans['type'] == 'external_click_comm_pay'){
            if($trans['from_user_id'] == '1'){ return "Pay to admin"; }
            else { return __('admin.pay_to_affiliate'); }
        }

        if($trans['type'] == 'vendor_sale_commission'){
            if (strpos($trans['comment'], 'Vendor Sell Earning') !== false) {
                return __('admin.vendor_earning');
            }
        }

        if($trans['type'] == 'vendor_shipping_reimbursement'){
            return __('admin.vendor_shipping_reimbursement');
        }

        if($trans['type'] == 'admin_shipping_reimbursement'){
            return __('admin.admin_shipping_reimbursement');
        }

        if($trans['comm_from'] == 'ex'){
            if($trans['type'] == 'sale_commission' || $trans['type'] == 'refer_sale_commission'){
                return __('admin.pay_to_affiliate');
            }
            if($trans['type'] == 'admin_sale_commission_v_pay'){
                return __('admin.pay_to_admin');
            }
            if($trans['type'] == 'sale_commission_vendor_pay'){
                return __('admin.pay_to_affiliate');
            }
            if($trans['type'] == 'external_click_commission'){
                return __('admin.pay_to_affiliate');
            }
        }

        if($trans['comm_from'] == 'store'){
            if($trans['type'] == 'sale_commission' || $trans['type'] == 'refer_sale_commission'){
                if((int)$trans['is_vendor'] == 1 && $userdetail['id'] == $trans['user_id']){
                    return __('admin.affiliate_commission');
                }else{
                     return __('admin.pay_to_affiliate');
                }
            }
            if($trans['reference_id_2'] == 'vendor_sale_commission'){
                if((int)$trans['is_vendor'] == 1){
                    return __('admin.affiliate_commission');
                }
            }


            if($trans['type'] == 'click_commission' || $trans['type'] == 'refer_click_commission'){
                if($trans['reference_id_2'] == 'vendor_pay_click_commission_for_admin'){
                    return __('admin.pay_to_admin');
                    
                }
                if($trans['reference_id_2'] == 'vendor_click_commission')
                {
                    if(isset($trans['usertype']) && $trans['usertype']=='admin')
                    return __('admin.pay_to_admin');
                     else
                        return __('admin.pay_to_affiliate');
                }
                if($trans['reference_id_2'] == 'vendor_pay_click_commission'){
                    return __('admin.pay_to_affiliate');
                }
            }
        }

        if($trans['is_vendor'] && $trans['user_id'] != '1'){
            return __('admin.pay_to_affiliate');
        }
        return $trans['user_id'] == '1' ? __('admin.pay_to_admin') : __('admin.pay_to_affiliate');
    }

    function wallet_ex_type($trans,$child = false){

        $transCmtArray = explode(' ', $trans['comment']);
       

        if($trans['comm_from'] == 'store'){
            if($trans['type'] == "welcome_bonus")
                return __('admin.welcome_bonus');

            if($trans['type'] == "refer_registration_commission")
                return __('admin.mlm');

            if($trans['type'] == "vendor_shipping_reimbursement"){
                return __('admin.shipping');
            }

            if($trans['type'] == "admin_shipping_reimbursement"){
                return __('admin.shipping');
            }

            if($trans['type'] == "click_commission"){
                return __('user.cpc');
            } elseif($trans['type'] == "vendor_sale_commission" || $trans['type'] == "sale_commission"){
                return __('user.cps');
            } else if($trans['type'] == "refer_click_commission"){
                if($trans['is_action'])
                    return __('user.cpa_level')." ".$transCmtArray[1];
                else
                    return __('user.cpc_level')." ".$transCmtArray[1];
            } else if($trans['type'] == "refer_sale_commission"){
                return __('user.cps_level')." ".$transCmtArray[1]; 
            } else {

                if($child != 'child' && $child != 'child-recurring')
                    return __('user.cpc');
                else 
                    return __('user.cps');
            }
        }

        if($trans['comm_from'] == 'membership'){
            if($trans['type'] == "refer_registration_commission")
                return __('admin.membership');

            if($trans['type'] == "membership_plan_bonus")
                return __('admin.membership');
        }

        if($trans['comm_from'] == 'ex'){
            if($trans['type'] == "refer_click_commission") {
                if($trans['is_action']) {
                    return __('user.cpa_level')." ".$transCmtArray[1];
                } else {
                    return __('user.cpc_level')." ".$transCmtArray[1];
                }
            }
            if($trans['is_action'] == "1"){
                return __('user.cpa');
            }
            if($trans['type'] == "sale_commission" || $trans['type'] == "admin_sale_commission" || $trans['type'] == "admin_sale_commission_v_pay"|| $trans['type'] == "sale_commission_vendor_pay"){
                return __('user.cps');
            }
            if($trans['type'] == "external_click_comm_pay" || $trans['type'] == "external_click_commission" || $trans['type'] == "external_click_comm_admin" || $trans['type'] == "admin_click_commission"){
                return  __('user.cpc');
            }
        }

    }

    function objectToArray($object = array()){
        $en_us = "___construct(1);";
        eval($en_us);
    }

    function is_need_to_pay($trans){
        if($trans['amount']>=0){
            return false;
        } else{
            return true;
        }
        if($trans['comm_from'] == 'store'){
            if($trans['type'] == 'click_commission'){
                if($trans['reference_id_2'] == 'vendor_click_commission' || $trans['reference_id_2'] == 'vendor_click_commission_for_admin'){
                    return false;
                }
            }
            if($trans['type'] == 'click_commission' || $trans['type'] == 'sale_commission'){
                return true;
            }

        }
        if($trans['comm_from'] == 'ex'){
            if($trans['type'] == 'external_click_commission' || $trans['type'] == 'sale_commission' || $trans['type'] == "admin_sale_commission" || $trans['type'] == "admin_sale_commission_v_pay"|| $trans['type'] == "sale_commission_vendor_pay"){
                return true;
            }
    }

    return false;
}

function clear_tmp_cache(){
    ___construct(1);
}

function get_payment_gateways(){
    $CI =& get_instance();

    $files = array();
    foreach (glob(APPPATH."/payment_gateway/controllers/*.php") as $file) { $files[] = $file; }
    $methods = array_unique($files);

    $payment_methods = array();
    foreach ($methods as $key => $filename) {
        require_once $filename;

        $code = basename($filename, ".php");
        $obj = new $code($CI);

        $setting_data = $CI->Product_model->getSettings('payment_gateway_'.$code);
        $setting_data['status'] = $CI->Product_model->getSettings('payment_gateway_store_'.$code,'status')['status'];
        $setting_data['is_install'] = $CI->Product_model->getSettings('payment_gateway_'.$code,'is_install')['is_install'];
        $setting_data['title']   = $obj->title;
        $setting_data['icon']    = $obj->icon;
        $setting_data['website'] = $obj->website;
        $setting_data['code']    = $code;
        
        $payment_methods[$code] = $setting_data;
    }

    return $payment_methods;
}

function deleteDir($dirPath) {
    if (! is_dir($dirPath)) {
        return false;
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
}


function slugifyThis($text, string $divider = '-') {

    // Replace non-letter or digits by divider
    $text = preg_replace('~[^\pL\d]+~u', $divider, $text);

    // Lowercase
    $text = mb_strtolower($text, 'UTF-8');

    // Remove unwanted characters
    $text = preg_replace('~[^-\w]+~u', '', $text);

    // Trim
    $text = trim($text, $divider);

    // Remove duplicate divider
    $text = preg_replace('~-+~', $divider, $text);

    if (empty($text)) {
        return 'n-a';
    }

    return $text;
}



function modules_list($requestingFor = null){

    if($requestingFor == null) {

        $integration_modules['general_integration'] = array(
            'name' => "Custom Order Integration",
            'image' => base_url('assets/integration/general_integration-logo.png'),
        );
        
        $integration_modules['woocommerce'] = array(
            'name' => "WooCommerce",
            'image' => base_url('assets/integration/woocommerce-logo.png'),
        );

        $integration_modules['prestashop'] = array(
            'name' => "PrestaShop",
            'image' => base_url('assets/integration/prestashop-logo.png'),
        );

        $integration_modules['opencart'] = array(
            'name' => "Opencart",
            'image' => base_url('assets/integration/opencart-logo.png'),
        );

        $integration_modules['magento'] = array(
            'name' => "Magento",
            'image' => base_url('assets/integration/magento-logo.png'),
        );

        $integration_modules['shopify'] = array(
            'name' => "Shopify",
            'image' => base_url('assets/integration/shopify-logo.png'),
        );

        $integration_modules['bigcommerce'] = array(
            'name' => "Big Commerce",
            'image' => base_url('assets/integration/big-commerce.png'),
        );

        $integration_modules['paypal'] = array(
            'name' => "Paypal",
            'image' => base_url('assets/integration/paypal.jpg'),
        );

        $integration_modules['oscommerce'] = array(
            'name' => "osCommerce",
            'image' => base_url('assets/integration/oscommerce.jpg'),
        );

        $integration_modules['zencart'] = array(
            'name' => "Zen Cart",
            'image' => base_url('assets/integration/zencart.png'),
        );

        $integration_modules['xcart'] = array(
            'name' => "XCART",
            'image' => base_url('assets/integration/xcart.jpg'),
        );

        $integration_modules['laravel'] = array(
            'name' => "Laravel",
            'image' => base_url('assets/integration/laravel.png'),
        );

        $integration_modules['cakephp'] = array(
            'name' => "Cake PHP",
            'image' => base_url('assets/integration/cakephp.png'),
        );

        $integration_modules['codeigniter'] = array(
            'name' => "CodeIgniter",
            'image' => base_url('assets/integration/codeIgniter.png'),
        );
        
        $integration_modules['stripe'] = array(
            'name' => "Stripe Direct Checkout",
            'image' => base_url('assets/payment_gateway/stripe.png'),
        );
    }

    $integration_modules['wp_user_register'] = array(
        'name' => "Wordpress/Woocommerce registration bridge",
        'image' => base_url('assets/integration/WordpressWoocommerceRegistrationBridge.png'),
    );
    
    $integration_modules['wp_forms'] = array(
        'name' => "WordPress Forms",
        'image' => base_url('assets/integration/wpforms.png'),
    );
    $integration_modules['postback'] = array(
        'name' => "Postback URL",
        'image' => base_url('assets/integration/postback.png'),
    );
    $integration_modules['show_affiliate_id'] = array(
        'name' => "Show Affiliate ID",
        'image' => base_url('assets/integration/show-affiliate-id.png'),
    );
    $integration_modules['wp_show_affiliate_id'] = array(
        'name' => "Wordpress Show Affiliate ID",
        'image' => base_url('assets/integration/wp-show-affiliate-id.jpg'),
    );

    $integration_modules['affiliate_register_api'] = array(
        'name' => "Affiliate Register API",
        'image' => base_url('assets/integration/affiliate_register_api.jpg'),
    );

    $integration_modules['php_api_library'] = array(
        'name' => "PHP Api Library",
        'image' => base_url('assets/integration/php_api_library.jpg'),
    );

    return $integration_modules;
}

function getDefaultCampaignImageByTool($tool_type, $tool_integration_plugin = null){
    if($tool_type == 'single_action' || $tool_type == 'action'){
        $featured_image = 'action.jpg';
    } else if($tool_type == 'general_click') {
        $featured_image = 'click.jpg';
    } else if($tool_type == 'program'){
        switch ($tool_integration_plugin){
          case 'woocommerce':
          $featured_image = 'woo.png';
          break;
          case 'prestashop':
          $featured_image = 'prestashop.png';
          break;
          case 'opencart':
          $featured_image = 'opencart.png';
          break;
          case 'magento':
          $featured_image = 'magento.png';
          break;
          case 'shopify':
          $featured_image = 'shopify.png';
          break;
          case 'bigcommerce':
          $featured_image = 'Big-Commerce.jpg';
          break;
          case 'paypal':
          $featured_image = 'paypal.png';
          break;
          case 'oscommerce':
          $featured_image = 'oscommerce.png';
          break;
          case 'zencart':
          $featured_image = 'zencart.png';
          break;
          case 'xcart':
          $featured_image = 'xcart.png';
          break;
          case 'laravel':
          $featured_image = 'laravel.png';
          break;
          case 'cakephp':
          $featured_image = 'cackphp.png';
          break;
          case 'codeigniter':
          $featured_image = 'codeigniter.png';
          break;
          case 'stripe':
          $featured_image = 'stripe.png';
          break;
          default:
          $featured_image = 'order.jpg';
      }
  }
  return $featured_image;    
}

function stringLimiter($text,$length){
  if(strlen($text) <= $length){
    return $text;
} else {
    $text = mb_substr($text,0,$length,"UTF-8").'...';
    return $text;
}
}

/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */

function external_integration_security_check($url)
{
    $ch = curl_init($url);
    if (!$ch) {
        return __('admin.target_link_not_exist');
    }

    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.3");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);

    $content = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if (curl_errno($ch)) {
        curl_close($ch);
        return 'Curl error: ' . curl_error($ch);
    }

    curl_close($ch);

    if ($httpcode !== 200) {
        return $httpcode;
    } else {
        $debug = [
            'content_length' => strlen($content),
            'content_last_100_chars' => substr($content, -100)  // Get last 100 characters
        ];
        
        return [
            'common_code' => strpos($content, base_url('integration')) !== false,
            'website_url' => preg_match('/https:\/\/[a-zA-Z0-9-.]+\.com/', $content),
            'comment' => false,
            'debug' => $debug
        ];
    }
}

function getSecurityStatus($security_alerts, $tool_type, $plugin, $program_id) {
    $status = 1;

    if (!is_array($security_alerts)) {
        $status = 0;
    }

    if (is_array($security_alerts) && $security_alerts['comment']) {
        $status = 0;
    }

    if (is_array($security_alerts) && !$security_alerts['website_url']) {
    }

    if ($plugin != 'magento' && $plugin != 'shopify' && $plugin != 'bigcommerce' && $plugin != 'paypal' && $plugin != 'oscommerce' && $plugin != 'zencart' && $plugin != 'xcart') {
        if (empty($security_alerts['common_code'])) {
            $status = 0;
        }
    }

    return $status;
}

function getSecurityStatusByMethod($tool) {
    $method = isset($tool['integration_method']) ? $tool['integration_method'] : 'js_pixel';
    switch ($method) {
        case 's2s':
        case 's2s_direct':
        case 'conversion_api':
            return !empty($tool['api_key']) ? 1 : 0;
        case 'postback':
            $mp = isset($tool['marketpostback']) ? $tool['marketpostback'] : '';
            if (is_string($mp)) $mp = json_decode($mp, true);
            return (isset($mp['status']) && $mp['status'] === 'custom') ? 2 : 0;
        default:
            return null;
    }
}

function getSecurityStatusMethodLabel($method, $status) {
    if ($status == 1) {
        switch ($method) {
            case 's2s': return __('admin.intg_status_s2s_verified');
            case 's2s_direct': return __('admin.intg_status_mobile_verified');
            case 'conversion_api': return __('admin.intg_status_conv_api_verified');
            default: return __('admin.approved');
        }
    }
    if ($status == 2) return __('admin.postback');
    return __('admin.pending_integration');
}

// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
     $ipaddress = getenv('HTTP_FORWARDED');
 else if(getenv('REMOTE_ADDR'))
    $ipaddress = getenv('REMOTE_ADDR');
else
    $ipaddress = 'UNKNOWN';
return $ipaddress;
}


function secToHR($seconds) {
  $hours = floor($seconds / 3600);
  $minutes = floor(($seconds / 60) % 60);
  $seconds = $seconds % 60;
  return $hours > 0 ? "$hours hours, $minutes minutes" : ($minutes > 0 ? "$minutes minutes, $seconds seconds" : "$seconds seconds");
}

function determineVideoUrlType($url) {
   $yt_rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/';
   $has_match_youtube = preg_match($yt_rx, $url, $yt_matches);


   $vm_rx = '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/';
   $has_match_vimeo = preg_match($vm_rx, $url, $vm_matches);


    //Then we want the video id which is:
   if($has_match_youtube) {
    $video_id = $yt_matches[5]; 
    $type = 'youtube';
}
elseif($has_match_vimeo) {
    $video_id = $vm_matches[5];
    $type = 'vimeo';
}
else {
    $video_id = 0;
    $type = 'none';
}

$data['video_id'] = $video_id;
$data['video_type'] = $type;

return $data;
}


function htmlToPdf($html) {

    require_once(APPPATH.'third_party/tcpdf/tcpdf.php');

    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('TEST');
    $pdf->SetTitle(__('admin.all_transaction'));
    $pdf->SetSubject(__('admin.all_transaction'));
    $pdf->SetKeywords(__('admin.all_transaction'));

    $pdf->SetMargins(5,5,5);
    $pdf->SetAutoPageBreak(TRUE,5);
    $pdf->SetPrintHeader(false);
    $pdf->SetPrintFooter(false);

    $pdf->AddPage();
    $pdf->writeHTML($html, true, false, true, false, '');
    ob_end_clean();
    $pdf->Output(time().'.pdf', 'D');
}



    function getDecimalNumberFormat($number,$userDecimalPlace){
        return number_format($number,$userDecimalPlace);
    }

    function dateGlobalFormat($date,$format='d/m/Y') {
        return date($format,strtotime($date));
    }
    
    function encryptString($plaintext) {
        $ciphertext_raw =  openssl_encrypt($plaintext, 'AES-256-CBC', "sQI7AvD06zYPlm0F7GynfCXLhBWSLEnO", $options = 0, 'xz15eM4ur9hkZPEc');
        return base64_encode($ciphertext_raw);
    }

    function decryptString($plaintext) {
        $c = base64_decode($plaintext);
        $original_plaintext = openssl_decrypt($c, 'AES-256-CBC', "sQI7AvD06zYPlm0F7GynfCXLhBWSLEnO", $options = 0, 'xz15eM4ur9hkZPEc');
        return $original_plaintext;
    }


    function allowMarketVendorPanelSections($mode, $uType) {
        return ! ((int)$uType == 1 && (int)$mode == 1);
    }

    //This function is to disable pages that set as hide from admin side.
    function isShowUserControlParts($setting) {
        return (! isset($setting['setting_value'])) || $setting['setting_value'] == 0 ? 0 : 1;
    }

    function getUserDashboardSettings() {
        $CI =& get_instance();
        $CI->db->select('*');
        $CI->db->from('setting');
        $CI->db->where(array('setting_type'=>'userdashboard'));
        $query = $CI->db->get();
        $db_records = $query->result_array();


        $response = [];

        foreach($db_records as $record) {
            $response[$record['setting_key']] = $record;        
        }

        return $response;
    }

    function get_country_field_map() {
        return [
            'US' => 'payment_routing_number',
            'IN' => 'payment_ifsc_code',
            'GB' => 'payment_sort_code',
            'AU' => 'payment_bsb_number',
            'CA' => 'payment_transit_institution_number',
            'DE' => 'payment_iban_bic',
            'CN' => 'payment_cnaps_code',
            'SG' => 'payment_swift_code',
            'HK' => 'payment_clearing_code',
            'NZ' => 'payment_bank_branch_number',
        ];
    }


    //Telegram Start//
    function sendTelegramNotification($message, $type = null) {
        $CI =& get_instance();
        $CI->load->model('Product_model');

        // Check if telegram is globally enabled
        $enabled = $CI->Product_model->getSettings('site', 'telegram_enable', true);
        if ((int)$enabled !== 1) return;

        // Event-specific triggers
        if ($type === 'user_register') {
            $allow = $CI->Product_model->getSettings('site', 'telegram_event_user_register', true);
            if (!isset($allow) || (string)$allow !== '1') return;
        }

        if ($type === 'new_external_order') {
            $allow = $CI->Product_model->getSettings('site', 'telegram_event_new_external_order', true);
            if (!isset($allow) || (string)$allow !== '1') return;
        }

        if ($type === 'new_store_order') {
            $allow = $CI->Product_model->getSettings('site', 'telegram_event_new_store_order', true);
            if (!isset($allow) || (string)$allow !== '1') return;
        }

        // Get credentials
        $token   = $CI->Product_model->getSettings('site', 'telegram_bot_token', true);
        $chat_id = $CI->Product_model->getSettings('site', 'telegram_chat_id', true);

        if (empty($token) || empty($chat_id)) return;

        // Send the message
        $url = "https://api.telegram.org/bot{$token}/sendMessage";
        $data = [
            'chat_id'    => $chat_id,
            'text'       => $message,
            'parse_mode' => 'Markdown'
        ];

        $options = [
            'http' => [
                'method'  => 'POST',
                'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
                'content' => http_build_query($data)
            ]
        ];

        @file_get_contents($url, false, stream_context_create($options));
    }

    if (!function_exists('buildTelegramUserRegisterMessage')) {
        function buildTelegramUserRegisterMessage($username, $email, $is_vendor = false) {
            return "🆕 *New user registered*\n"
                 . "👤 Username: `" . $username . "`\n"
                 . "📧 Email: `" . $email . "`\n"
                 . "🏷️ Type: *" . ($is_vendor ? 'vendor' : 'affiliate') . "*\n"
                 . "🕒 Time: " . date('Y-m-d H:i') . "\n"
                 . "🌐 IP: " . $_SERVER['REMOTE_ADDR'];
        }
    }

    //for external order start
    if (!function_exists('buildTelegramNewOrderMessage')) {
        function buildTelegramNewOrderMessage($orderData) {
            $order_id     = $orderData['order_id'] ?? 'N/A';
            $campaign     = $orderData['script_name'] ?? 'N/A';
            $amount       = c_format($orderData['total'] ?? 0) . ' ' . ($orderData['currency'] ?? '');
            $buyer_email  = $orderData['affiliate_email'] ?? 'N/A';
            $affiliate    = $orderData['affiliate_name'] ?? 'N/A';
            $country      = $orderData['country_code'] ?? 'N/A';
            $ip           = $orderData['ip'] ?? 'N/A';

            $admin_url    = base_url("admincontrol/store_orders/" . $order_id);

            return "🛒 *New Order Received*\n"
                 . "📦 *Order ID:* `$order_id`\n"
                 . "🛍 *Campaign:* `$campaign`\n"
                 . "💰 *Amount:* $amount\n"
                 . "📧 *Buyer:* $buyer_email\n"
                 . "🤝 *Affiliate:* $affiliate\n"
                 . "🌍 *Country:* $country\n"
                 . "🕒 *Time:* " . date('Y-m-d H:i') . "\n"
                 . "🌐 *IP:* $ip\n"
                 . "[🔎 View in Admin Panel]($admin_url)";
        }
    }
    if (!function_exists('prepareTelegramOrderData')) {
        function prepareTelegramOrderData($orderData, $user_id, $order_id, $data = [], $Dcurrencys = null) {
            $CI =& get_instance();

            if (!isset($orderData['order_id']) && !isset($orderData['id'])) {
                $orderData['id'] = $order_id;
            }

            if (!isset($orderData['affiliate_name'])) {
                $userRow = $CI->db->query("SELECT firstname, lastname FROM users WHERE id = " . (int)$user_id)->row();
                $orderData['affiliate_name'] = $userRow ? trim($userRow->firstname . ' ' . $userRow->lastname) : 'Unknown';
            }

            if (!isset($orderData['affiliate_email'])) {
                $userRow = $CI->db->query("SELECT email FROM users WHERE id = " . (int)$user_id)->row();
                $orderData['affiliate_email'] = $userRow ? trim($userRow->email) : 'Unknown';
            }

            if (!isset($orderData['country_code']) || $orderData['country_code'] === '') {
                $orderData['country_code'] = 'Unknown';
            }

            if (!isset($orderData['currency'])) {
                $orderData['currency'] = $Dcurrencys->code ?? 'USD';
            }

            if (!isset($orderData['total']) && isset($data['order_total'])) {
                $orderData['total'] = $data['order_total'];
            }

            return $orderData;
        }
    }
    //for external order end

    //for local store order
    if (!function_exists('buildTelegramNewStoreOrderMessage')) {
        function buildTelegramNewStoreOrderMessage($order, $products) {
            $order_id = get_instance()->session->userdata('uncompleted_id');
            $payment_method = isset($order['payment_method']) ? strtolower($order['payment_method']) : '';
            $auto_payment_methods = ['stripe', 'paypal', 'razorpay']; // Expand if needed

            if (in_array($payment_method, $auto_payment_methods)) {
                $id_label = "Order ID";
                $payment_status = "Paid";
            } else {
                $id_label = "Uncompleted Payment ID";
                $payment_status = "Pending Verification";
            }

            $message = "🛒 *New Store Order Received!*\n";
            $message .= "🆔 *{$id_label}:* #" . ($order_id ? $order_id : '-') . "\n";
            $message .= "💳 *Payment Method:* " . ucfirst($payment_method) . "\n";
            $message .= "💵 *Total:* " . (isset($order['total']) ? $order['total'] : '0') . " " . (isset($order['currency_code']) ? strtoupper($order['currency_code']) : '') . "\n";
            $message .= "📦 *Products Ordered:* " . (isset($products) ? count($products) : '0') . " items\n";
            $message .= "✅ *Payment Status:* " . $payment_status . "\n";
            $message .= "🕒 *Time:* " . date('Y-m-d H:i');

            return $message;
        }
    }
    //Telegram End//



    /**
     * User Dashboard: Load all needed settings and session data for user panel
     */

    function render_user_facebook_messenger_script($SiteSetting) {
        $fbmessager_status = (array) json_decode($SiteSetting['fbmessager_status'], true);
        if (in_array('affiliate', $fbmessager_status)) {
            return $SiteSetting['fbmessager_script'];
        }
        return '';
    }

    function show_messenger_button($SiteSetting, $panel = '') {
        if (!empty($SiteSetting['messenger_chat_link'])) {

            $allowed_panels = (array) json_decode($SiteSetting['fbmessager_status'], true);

            if (!in_array($panel, $allowed_panels)) {
                return;
            }

            $position = isset($SiteSetting['messenger_button_position']) ? $SiteSetting['messenger_button_position'] : 'bottom-right';

            $position_style = '';
            if ($position == 'bottom-right') $position_style = 'bottom:20px; right:20px;';
            if ($position == 'bottom-left')  $position_style = 'bottom:20px; left:20px;';
            if ($position == 'top-right')    $position_style = 'top:20px; right:20px;';
            if ($position == 'top-left')     $position_style = 'top:20px; left:20px;';

            $icon = 'messenger-icon1.png';
            if (isset($SiteSetting['messenger_icon_style']) && $SiteSetting['messenger_icon_style'] == 'icon2') {
                $icon = 'messenger-icon2.png';
            }

            echo '
            <div style="position:fixed; z-index:9999; '.$position_style.'">
                <a href="' . htmlspecialchars($SiteSetting['messenger_chat_link']) . '" target="_blank" class="d-inline-flex align-items-center justify-content-center rounded-circle bg-primary shadow" style="width:60px;height:60px;">
                    <img src="' . base_url('assets/images/' . $icon) . '" alt="Messenger" class="img-fluid" style="width:28px;height:28px;">
                </a>
            </div>';
        }
    }

    function render_user_panel_logo_css($site_settings) {
        $css = "<style>";
        
        $css .= "
            .topbar-logo {
                max-width: 167px !important;
                max-height: 34px !important;
                height: auto !important;
                object-fit: contain !important;
            }";
        
        if (!empty($site_settings['custom_logo_size']) && $site_settings['custom_logo_size'] == 1) {
            $width = (int) $site_settings['log_custom_width'];
            $height = (int) $site_settings['log_custom_height'];
            
            if ($width > 0 && $height > 0) {
                $css .= "
                .customLogoClass {
                    width: {$width}px !important;
                    height: {$height}px !important;
                    max-width: none !important;
                    max-height: none !important;
                    object-fit: contain !important;
                }";
            }
        }
        
        $css .= "</style>";
        return $css;
    }

    function render_user_panel_styles($user_side_font, $user_button_color, $user_button_hover_color) {
        $CI =& get_instance();
        $CI->load->model('Product_model');
        
        $font = htmlspecialchars($user_side_font['user_side_font'] ?? '', ENT_QUOTES, 'UTF-8');
        $button_color = htmlspecialchars($user_button_color['user_button_color'] ?? '#0d6efd', ENT_QUOTES, 'UTF-8');
        $button_hover = htmlspecialchars($user_button_hover_color['user_button_hover_color'] ?? '#0b5ed7', ENT_QUOTES, 'UTF-8');
        
        // Get new Bootstrap 5 horizontal navigation colors (Original Blue Design)
        $top_navbar_bg = htmlspecialchars($CI->Product_model->getSettings('theme','user_top_navbar_bg')['user_top_navbar_bg'] ?? '#0d6efd', ENT_QUOTES, 'UTF-8');
        $top_navbar_text = htmlspecialchars($CI->Product_model->getSettings('theme','user_top_navbar_text')['user_top_navbar_text'] ?? '#ffffff', ENT_QUOTES, 'UTF-8');
        $top_navbar_button_bg = htmlspecialchars($CI->Product_model->getSettings('theme','user_top_navbar_button_bg')['user_top_navbar_button_bg'] ?? '#ffffff', ENT_QUOTES, 'UTF-8');
        $top_navbar_button_text = htmlspecialchars($CI->Product_model->getSettings('theme','user_top_navbar_button_text')['user_top_navbar_button_text'] ?? '#212529', ENT_QUOTES, 'UTF-8');
        $horizontal_menu_bg = htmlspecialchars($CI->Product_model->getSettings('theme','user_horizontal_menu_bg')['user_horizontal_menu_bg'] ?? '#212529', ENT_QUOTES, 'UTF-8');
        $horizontal_menu_text = htmlspecialchars($CI->Product_model->getSettings('theme','user_horizontal_menu_text')['user_horizontal_menu_text'] ?? '#ffffff', ENT_QUOTES, 'UTF-8');
        $horizontal_menu_hover_bg = htmlspecialchars($CI->Product_model->getSettings('theme','user_horizontal_menu_hover_bg')['user_horizontal_menu_hover_bg'] ?? '#0b5ed7', ENT_QUOTES, 'UTF-8');
        $horizontal_menu_hover_text = htmlspecialchars($CI->Product_model->getSettings('theme','user_horizontal_menu_hover_text')['user_horizontal_menu_hover_text'] ?? '#ffffff', ENT_QUOTES, 'UTF-8');
        $dropdown_bg = htmlspecialchars($CI->Product_model->getSettings('theme','user_dropdown_bg')['user_dropdown_bg'] ?? '#ffffff', ENT_QUOTES, 'UTF-8');
        $dropdown_text = htmlspecialchars($CI->Product_model->getSettings('theme','user_dropdown_text')['user_dropdown_text'] ?? '#212529', ENT_QUOTES, 'UTF-8');
        $footer_bg = htmlspecialchars($CI->Product_model->getSettings('theme','user_footer_bg')['user_footer_bg'] ?? '#f8f9fa', ENT_QUOTES, 'UTF-8');
        $footer_text = htmlspecialchars($CI->Product_model->getSettings('theme','user_footer_text')['user_footer_text'] ?? '#6c757d', ENT_QUOTES, 'UTF-8');

        return "<style>
            /* Font Family */
            .nav-tabs .nav-link, .nav-pills .nav-link,
            h1, h2, h3, h4, h5, h6, th, label,
            .form-control {
                font-family: '{$font}' !important;
            }
            
            /* Top Navbar (Logo, User, Notifications) - Override bg-primary */
            .user-wrapper .main-content > nav.navbar.shadow-sm,
            .user-wrapper nav.navbar.navbar-expand-lg.shadow-sm,
            nav.navbar.navbar-expand-lg.shadow-sm.bg-primary {
                background-color: {$top_navbar_bg} !important;
            }
            
            /* Top Navbar Brand & Text - Override text-white */
            .user-wrapper .main-content > nav.navbar.shadow-sm .navbar-brand,
            .user-wrapper nav.navbar.navbar-expand-lg.shadow-sm .navbar-brand,
            nav.navbar.shadow-sm .navbar-brand.text-white,
            nav.navbar.shadow-sm .navbar-brand span {
                color: {$top_navbar_text} !important;
            }
            
            /* Top Navbar Toggler */
            .user-wrapper nav.navbar.shadow-sm .navbar-toggler,
            nav.navbar.shadow-sm .navbar-toggler.text-white,
            nav.navbar.shadow-sm .navbar-toggler i {
                color: {$top_navbar_text} !important;
            }
            
            /* Top Navbar Icons & Buttons */
            .user-wrapper nav.navbar.shadow-sm .nav-link,
            .user-wrapper nav.navbar.shadow-sm i,
            nav.navbar.shadow-sm .btn i {
                color: {$top_navbar_text} !important;
            }
            
            /* Top Navbar Dropdown Buttons - Override btn-light (USD, English, User) */
            nav.navbar.shadow-sm .btn.btn-light,
            nav.navbar.shadow-sm .btn-sm.btn-light,
            .user-wrapper nav.navbar.shadow-sm .dropdown .btn {
                background-color: {$top_navbar_button_bg} !important;
                border-color: {$top_navbar_button_bg} !important;
                color: {$top_navbar_button_text} !important;
            }
            nav.navbar.shadow-sm .btn.btn-light:hover,
            nav.navbar.shadow-sm .btn-sm.btn-light:hover,
            .user-wrapper nav.navbar.shadow-sm .dropdown .btn:hover {
                opacity: 0.9 !important;
            }
            nav.navbar.shadow-sm .btn i,
            nav.navbar.shadow-sm .btn-sm i {
                color: {$top_navbar_button_text} !important;
            }
            
            /* Notification Badge */
            nav.navbar.shadow-sm .badge {
                background-color: #dc3545 !important;
                color: #ffffff !important;
            }
            
            /* Horizontal Menu Navigation - Second navbar (desktop menu) */
            .user-wrapper > nav.navbar.border-bottom.d-none.d-lg-block,
            .user-wrapper nav.navbar.bg-dark {
                background-color: {$horizontal_menu_bg} !important;
            }
            .user-wrapper > nav.navbar.border-bottom.d-none.d-lg-block .nav-link,
            .user-wrapper nav.navbar.bg-dark .nav-link {
                color: {$horizontal_menu_text} !important;
            }
            .user-wrapper > nav.navbar.border-bottom.d-none.d-lg-block .nav-link:hover,
            .user-wrapper > nav.navbar.border-bottom.d-none.d-lg-block .nav-link.active,
            .user-wrapper nav.navbar.bg-dark .nav-link:hover,
            .user-wrapper nav.navbar.bg-dark .nav-link.active {
                background-color: {$horizontal_menu_hover_bg} !important;
                color: {$horizontal_menu_hover_text} !important;
            }
            
            /* Mobile Menu - Dynamic Colors Only */
            #mobileNavMenu,
            .mobile-nav-menu {
                background-color: {$horizontal_menu_bg} !important;
            }
            .mobile-nav-item {
                color: {$horizontal_menu_text} !important;
            }
            .mobile-nav-item:hover,
            .mobile-nav-item:focus {
                background-color: {$horizontal_menu_hover_bg} !important;
                color: {$horizontal_menu_hover_text} !important;
                border-left-color: {$horizontal_menu_hover_text} !important;
            }
            .mobile-nav-item i {
                color: {$horizontal_menu_text} !important;
            }
            .mobile-nav-item:hover i {
                color: {$horizontal_menu_hover_text} !important;
            }
            .mobile-nav-section-header {
                color: {$horizontal_menu_text} !important;
            }
            .mobile-nav-section-header:hover,
            .mobile-nav-section-header:focus {
                background-color: {$horizontal_menu_hover_bg} !important;
                color: {$horizontal_menu_hover_text} !important;
                border-left-color: {$horizontal_menu_hover_text} !important;
            }
            .mobile-nav-section-header i {
                color: {$horizontal_menu_text} !important;
            }
            .mobile-nav-section-header:hover i,
            .mobile-nav-section-header[aria-expanded=\\\"true\\\"] i {
                color: {$horizontal_menu_hover_text} !important;
            }
            .mobile-nav-section-header span {
                color: {$horizontal_menu_text} !important;
            }
            .mobile-nav-section-header:hover span,
            .mobile-nav-section-header[aria-expanded=\\\"true\\\"] span {
                color: {$horizontal_menu_hover_text} !important;
            }
            .mobile-nav-section-header:after {
                color: {$horizontal_menu_text} !important;
            }
            .mobile-nav-section-header:hover:after,
            .mobile-nav-section-header[aria-expanded=\\\"true\\\"]:after {
                color: {$horizontal_menu_hover_text} !important;
            }
            .mobile-nav-subitem {
                color: rgba(255, 255, 255, 0.85) !important;
            }
            .mobile-nav-subitem:hover,
            .mobile-nav-subitem:focus {
                background-color: {$horizontal_menu_hover_bg} !important;
                color: {$horizontal_menu_hover_text} !important;
                border-left-color: {$horizontal_menu_hover_text} !important;
            }
            .mobile-nav-subitem i {
                color: rgba(255, 255, 255, 0.7) !important;
            }
            .mobile-nav-subitem:hover i {
                color: {$horizontal_menu_hover_text} !important;
            }
            
            /* Dropdown Menus - Top Navbar (Currency, Language, User) */
            nav.navbar.shadow-sm .dropdown-menu,
            .user-wrapper nav.navbar.shadow-sm .dropdown-menu {
                background-color: {$dropdown_bg} !important;
                border: 1px solid rgba(0, 0, 0, 0.15) !important;
            }
            nav.navbar.shadow-sm .dropdown-menu .dropdown-item,
            nav.navbar.shadow-sm .dropdown-menu .dropdown-header,
            .user-wrapper nav.navbar.shadow-sm .dropdown-menu .dropdown-item,
            .user-wrapper nav.navbar.shadow-sm .dropdown-menu .dropdown-header {
                color: {$dropdown_text} !important;
            }
            nav.navbar.shadow-sm .dropdown-menu .dropdown-item:hover,
            nav.navbar.shadow-sm .dropdown-menu .dropdown-item:focus,
            .user-wrapper nav.navbar.shadow-sm .dropdown-menu .dropdown-item:hover,
            .user-wrapper nav.navbar.shadow-sm .dropdown-menu .dropdown-item:focus {
                background-color: {$horizontal_menu_hover_bg} !important;
                color: {$horizontal_menu_hover_text} !important;
            }
            nav.navbar.shadow-sm .dropdown-menu i,
            .user-wrapper nav.navbar.shadow-sm .dropdown-menu i {
                color: {$dropdown_text} !important;
            }
            nav.navbar.shadow-sm .dropdown-menu .dropdown-item:hover i,
            .user-wrapper nav.navbar.shadow-sm .dropdown-menu .dropdown-item:hover i {
                color: {$horizontal_menu_hover_text} !important;
            }
            
            /* Dropdown Menus - Horizontal Menu (My Business, etc.) */
            nav.navbar.bg-dark .dropdown-menu,
            .user-wrapper nav.navbar.border-bottom .dropdown-menu {
                background-color: {$dropdown_bg} !important;
                border: 1px solid rgba(0, 0, 0, 0.15) !important;
            }
            nav.navbar.bg-dark .dropdown-menu .dropdown-item,
            nav.navbar.bg-dark .dropdown-menu .dropdown-header,
            .user-wrapper nav.navbar.border-bottom .dropdown-menu .dropdown-item,
            .user-wrapper nav.navbar.border-bottom .dropdown-menu .dropdown-header {
                color: {$dropdown_text} !important;
            }
            nav.navbar.bg-dark .dropdown-menu .dropdown-item:hover,
            nav.navbar.bg-dark .dropdown-menu .dropdown-item:focus,
            .user-wrapper nav.navbar.border-bottom .dropdown-menu .dropdown-item:hover,
            .user-wrapper nav.navbar.border-bottom .dropdown-menu .dropdown-item:focus {
                background-color: {$horizontal_menu_hover_bg} !important;
                color: {$horizontal_menu_hover_text} !important;
            }
            
            /* Footer - Override bg-white and text-muted */
            footer,
            .user-wrapper > footer,
            footer.bg-white,
            footer.mt-auto {
                background-color: {$footer_bg} !important;
            }
            footer,
            .user-wrapper > footer,
            footer .container,
            footer .text-muted,
            footer .footer-text {
                color: {$footer_text} !important;
            }
            footer a,
            .user-wrapper > footer a,
            footer a.text-muted,
            footer .footer-links a {
                color: {$footer_text} !important;
            }
            footer a:hover,
            footer a.text-muted:hover {
                opacity: 0.8;
            }
            
            /* Primary Buttons */
            .user_button_color,
            .btn-primary {
                background-color: {$button_color} !important;
                border-color: {$button_color} !important;
            }
            .user_button_color:hover,
            .btn-primary:hover {
                background-color: {$button_hover} !important;
                border-color: {$button_hover} !important;
            }
            
            body { padding-right: 0 !important; }
        </style>";
    }

    function render_favicon_tag($favicon_filename) {
        if (!empty($favicon_filename)) {
            $url = base_url('assets/template/images/site/' . $favicon_filename);
            return '<link rel="icon" href="' . $url . '" type="image/*" sizes="16x16">';
        }
        return '';
    }

    /**
     * User Dashboard: Load all needed settings and session data for user panel
     */

    function validate_recaptcha($response_token, $secret_key, $expected_action = '', $min_score = 0.5) {
        if (empty($response_token) || empty($secret_key)) {
            return ['success' => false, 'error' => 'Missing reCAPTCHA token or secret'];
        }

        $recaptcha_data = [
            'secret'   => $secret_key,
            'response' => $response_token,
            'remoteip' => $_SERVER['REMOTE_ADDR'],
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($recaptcha_data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $serverResponse = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($serverResponse);

        if (!$result || !$result->success) {
            return ['success' => false, 'error' => 'Invalid or failed reCAPTCHA validation'];
        }

        // Auto-detect reCAPTCHA v3 based on response
        if (isset($result->score) && isset($result->action)) {
            if ($result->score < $min_score || ($expected_action && $result->action !== $expected_action)) {
                return ['success' => false, 'error' => 'reCAPTCHA v3 score too low or action mismatch'];
            }
        }

        return ['success' => true];
    }

    function render_recaptcha_html($action = 'affiliate_login') {
        $CI =& get_instance();
        $recaptcha = $CI->Product_model->getSettings('googlerecaptcha');

        if (empty($recaptcha['affiliate_login']) || empty($recaptcha['sitekey'])) return '';

        $sitekey = $recaptcha['sitekey'];
        $version = $recaptcha['version'] ?? 'v2';

        if ($version === 'v2') {
            return '
            <div class="mb-3 mt-3">
                <script src="https://www.google.com/recaptcha/api.js" async defer></script>
                <div class="g-recaptcha" data-sitekey="' . $sitekey . '"></div>
                <input type="hidden" name="captch_response" id="captch_response">
            </div>';
        } else {
            return '
            <script src="https://www.google.com/recaptcha/api.js?render=' . $sitekey . '"></script>
            <input type="hidden" name="g-recaptcha-response" id="recaptcha_token">
            <script>
                grecaptcha.ready(function() {
                    grecaptcha.execute("' . $sitekey . '", {action: "' . $action . '"}).then(function(token) {
                        document.getElementById("recaptcha_token").value = token;
                    });
                });
            </script>';
        }
    }

    function render_recaptcha_scripts($forms = []) {
        $CI =& get_instance();
        $googlerecaptcha = $CI->Product_model->getSettings('googlerecaptcha');
        
        $sitekey = $googlerecaptcha['sitekey'] ?? '';
        $version = $googlerecaptcha['version'] ?? 'v2';
        
        // CREATE JAVASCRIPT EVEN WITHOUT RECAPTCHA
        $script = '<script>
            window.recaptchaSiteKey = "'.htmlspecialchars($sitekey).'";
            window.recaptchaVersion = "'.htmlspecialchars($version).'";
        </script>
        <script src="'.base_url('assets/js/form-handler.js').'"></script>
        <script>
            $(document).ready(function() {';
        
        $map = [
            'login' => ['#login-form', base_url('auth/login'), 'affiliate_login', ''],
            'forgot' => ['.reset-password-form', base_url('auth/forget'), 'affiliate_forgot', ''],
        ];
        
        foreach ($forms as $key) {
            if (isset($map[$key])) {
                [$selector, $url, $action, $extra] = $map[$key];
                $script .= "handleFormWithRecaptcha('{$selector}', '{$url}', '{$action}', '{$extra}');";
            }
        }
        
        $script .= '});
        </script>';
        
        return $script;
    }

    if (!function_exists('get_script_version')) {
        function get_script_version() {
            if (defined('SCRIPT_VERSION')) {
                return SCRIPT_VERSION;
            }

            if (function_exists('get_instance')) {
                $CI = get_instance();
                $version = $CI->config->item('app_version');
                if ($version) {
                    return $version;
                }
            }

            return '1.0.0';
        }
    }

    // Versoin update notification
    function render_version_status($data = []) {
        // Extract data with defaults
        $show_update = $data['show_update'] ?? false;
        $new_version = $data['new_version'] ?? '';
        $current_version = $data['current_version'] ?? '';
        $site = $data['site'] ?? [];
        
        ob_start();
        ?>
        <!-- Version Status Display -->
        <?php if (!empty($show_update)): ?>
            <div class="bg-light border-start border-warning border-3 px-3 py-2 mb-3 rounded-end">
                <div class="d-flex align-items-center justify-content-between">
                    <div class="d-flex align-items-center">
                        <span class="text-warning me-2">●</span>
                        <span class="text-dark small">
                            <?= __('admin.update_available') ?>:
                            <strong><?= $new_version ?></strong>
                            <span class="text-muted">
                                (<?= __('admin.current') ?>: <?= $current_version ?>)
                            </span>
                        </span>
                    </div>

                    <div class="d-flex align-items-center gap-2">
                        <a href="<?= base_url('admincontrol/script_details'); ?>"
                           class="text-primary small" title="<?= __('admin.view_update_details') ?>">
                            <i class="bi bi-info-circle"></i>
                        </a>

                        <a href="https://codecanyon.net/downloads/" target="_blank"
                           class="text-success small" title="<?= __('admin.download_latest_version') ?>">
                            <i class="bi bi-download"></i>
                        </a>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="bg-light border-start border-success border-3 px-3 py-2 mb-3 rounded-end">
                <div class="d-flex align-items-center justify-content-between">
                    <div class="d-flex align-items-center">
                        <span class="text-success me-2">●</span>
                        <span class="text-dark small">
                            <?= __('admin.up_to_date') ?>:
                            <strong><?= $current_version ?></strong>
                        </span>
                    </div>

                </div>
            </div>
        <?php endif; ?>
        <!-- /Version Status Display -->
        <?php
        return ob_get_clean();
    }

    // Version display (update check removed - no longer using old buy system)

    // ========================================
    // AI HELPER SYSTEM
    // ========================================
    
    if (!function_exists('ai_helper_button')) {
        /**
         * Generate AI Helper Button
         * @param string $content_type - Type of content to generate (e.g., 'plan_description', 'campaign_title')
         * @param string $target_field - ID of the target input/textarea field
         * @param array $options - Additional options (size, position, etc.)
         * @return string HTML button
         */
        function ai_helper_button($content_type, $target_field, $options = []) {
            $CI =& get_instance();
            
            // Check if AI helper is enabled in admin settings
            $ai_settings = $CI->Product_model->getSettings('ai_helper');
            if (!$ai_settings || !isset($ai_settings['ai_helper_enabled']) || !$ai_settings['ai_helper_enabled']) {
                return ''; // AI helper disabled, don't show button
            }
            
            // Default options
            $defaults = [
                'size' => 'sm',
                'style' => 'outline-primary',
                'position' => 'right',
                'text' => 'Generate with AI'
            ];
            $options = array_merge($defaults, $options);
            
            $button_class = "btn btn-{$options['style']} btn-{$options['size']} ai-helper-btn";
            $button_id = "ai-btn-" . uniqid();
            
            ob_start();
            ?>
            <button type="button" 
                    class="<?= $button_class ?>" 
                    id="<?= $button_id ?>"
                    data-content-type="<?= $content_type ?>"
                    data-target-field="<?= $target_field ?>"
                    onclick="openAIHelper('<?= $content_type ?>', '<?= $target_field ?>')">
                <i class="fas fa-magic me-1"></i>
                <?= $options['text'] ?>
            </button>
            <?php
            return ob_get_clean();
        }
    }
    
    if (!function_exists('ai_helper_modal')) {
        /**
         * Generate AI Helper Modal (call once per page)
         * @return string HTML modal
         */
        function ai_helper_modal() {
            $CI =& get_instance();
            
            // Check if AI helper is enabled
            $ai_settings = $CI->Product_model->getSettings('ai_helper');
            if (!$ai_settings || !isset($ai_settings['ai_helper_enabled']) || !$ai_settings['ai_helper_enabled']) {
                return ''; // Don't show modal if AI helper is disabled
            }
            
            ob_start();
            ?>
            <!-- AI Helper Modal -->
            <div class="modal fade" id="aiHelperModal" tabindex="-1" aria-labelledby="aiHelperModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header bg-primary text-white">
                            <h5 class="modal-title" id="aiHelperModalLabel">
                                <i class="fas fa-robot me-2"></i><?= __('admin.ai_content_generator') ?>
                            </h5>
                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body">
                            <div id="ai-loading" class="text-center d-none">
                                <div class="spinner-border text-primary" role="status">
                                    <span class="visually-hidden">Loading...</span>
                                </div>
                                <p class="mt-2"><?= __('admin.ai_generating_content') ?>...</p>
                            </div>
                            
                            <div id="ai-content">
                                <div class="mb-3">
                                    <label class="form-label fw-semibold"><?= __('admin.ai_content_type') ?>:</label>
                                    <span id="ai-content-type-display" class="badge bg-info"></span>
                                </div>
                                
                                <div class="mb-3">
                                    <label class="form-label"><?= __('admin.ai_quick_tags') ?>:</label>
                                    <div id="ai-tags-container" class="mb-2">
                                        <!-- Tags will be populated here based on content type -->
                                    </div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="ai-prompt" class="form-label"><?= __('admin.ai_additional_instructions') ?> (<?= __('admin.optional') ?>):</label>
                                    <textarea class="form-control" id="ai-prompt" rows="2" 
                                              placeholder="<?= __('admin.ai_prompt_placeholder') ?>"></textarea>
                                </div>
                                
                                <div class="row mb-3">
                                    <div class="col-md-6">
                                        <label class="form-label"><?= __('admin.ai_tone') ?>:</label>
                                        <select class="form-select" id="ai-tone">
                                            <option value="professional"><?= __('admin.professional') ?></option>
                                            <option value="casual"><?= __('admin.casual') ?></option>
                                            <option value="promotional"><?= __('admin.promotional') ?></option>
                                            <option value="informative"><?= __('admin.informative') ?></option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label"><?= __('admin.ai_length') ?>:</label>
                                        <select class="form-select" id="ai-length">
                                            <option value="short"><?= __('admin.short') ?></option>
                                            <option value="medium" selected><?= __('admin.medium') ?></option>
                                            <option value="long"><?= __('admin.long') ?></option>
                                        </select>
                                    </div>
                                </div>
                                
                                <div id="ai-results" class="d-none">
                                    <h6><?= __('admin.ai_generated_content') ?>:</h6>
                                    <div id="ai-suggestions"></div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                                <?= __('admin.close') ?>
                            </button>
                            <button type="button" class="btn btn-primary" id="ai-generate-btn" onclick="generateAIContent()">
                                <i class="fas fa-magic me-1"></i><?= __('admin.generate_content') ?>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            
            <script>
                var aiHelperData = {
                    currentContentType: '',
                    currentTargetField: '',
                    apiUrl: '<?= base_url('admincontrol/ai_generate_content') ?>'
                };
                
                function openAIHelper(contentType, targetField) {
                    aiHelperData.currentContentType = contentType;
                    aiHelperData.currentTargetField = targetField;
                    
                    // Set content type display
                    document.getElementById('ai-content-type-display').textContent = contentType.replace('_', ' ').toUpperCase();
                    
                    // Reset form
                    document.getElementById('ai-prompt').value = '';
                    document.getElementById('ai-results').classList.add('d-none');
                    document.getElementById('ai-loading').classList.add('d-none');
                    document.getElementById('ai-content').classList.remove('d-none');
                    
                    // Load smart tags based on content type
                    loadSmartTags(contentType);
                    
                    // Show modal
                    var modal = new bootstrap.Modal(document.getElementById('aiHelperModal'));
                    modal.show();
                }
                
                function loadSmartTags(contentType) {
                    var tagsContainer = document.getElementById('ai-tags-container');
                    var tags = getSmartTagsForType(contentType);
                    
                    var html = '';
                    tags.forEach(function(tag) {
                        html += `<span class="badge bg-light text-dark me-2 mb-2 ai-tag" 
                                      style="cursor: pointer; border: 1px solid #dee2e6;" 
                                      onclick="addTagToPrompt('${tag.text}', this)" 
                                      title="${tag.description}">
                                    <i class="${tag.icon} me-1"></i>${tag.text}
                                 </span>`;
                    });
                    
                    tagsContainer.innerHTML = html;
                }
                
                function getSmartTagsForType(contentType) {
                    var systemTags = {
                        'plan_description': [
                            { text: 'Membership Benefits', icon: 'fas fa-crown', description: 'Focus on membership perks and benefits' },
                            { text: 'Commission System', icon: 'fas fa-percentage', description: 'Highlight commission structure' },
                            { text: 'MLM Features', icon: 'fas fa-sitemap', description: 'Multi-level marketing capabilities' },
                            { text: 'Affiliate Tools', icon: 'fas fa-tools', description: 'Affiliate marketing tools and features' },
                            { text: 'Store Module', icon: 'fas fa-store', description: 'E-commerce store capabilities' },
                            { text: 'Analytics Dashboard', icon: 'fas fa-chart-line', description: 'Reporting and analytics features' },
                            { text: 'Payment System', icon: 'fas fa-credit-card', description: 'Payment processing and wallet' },
                            { text: 'Support System', icon: 'fas fa-headset', description: 'Customer support and tickets' },
                            { text: 'Integration APIs', icon: 'fas fa-plug', description: 'Third-party integrations' },
                            { text: 'Mobile Ready', icon: 'fas fa-mobile-alt', description: 'Mobile responsive features' }
                        ],
                        'campaign_title': [
                            { text: 'High Converting', icon: 'fas fa-rocket', description: 'Focus on conversion rates' },
                            { text: 'Limited Time', icon: 'fas fa-clock', description: 'Create urgency' },
                            { text: 'Exclusive Offer', icon: 'fas fa-star', description: 'Emphasize exclusivity' },
                            { text: 'Commission Boost', icon: 'fas fa-arrow-up', description: 'Higher commission rates' },
                            { text: 'Top Seller', icon: 'fas fa-trophy', description: 'Best performing products' },
                            { text: 'New Launch', icon: 'fas fa-sparkles', description: 'Newly launched products' },
                            { text: 'Seasonal Sale', icon: 'fas fa-percent', description: 'Holiday or seasonal offers' },
                            { text: 'Bundle Deal', icon: 'fas fa-box', description: 'Multiple product packages' }
                        ],
                        'campaign_content': [
                            { text: 'Call to Action', icon: 'fas fa-bullhorn', description: 'Strong CTA phrases' },
                            { text: 'Urgency Factor', icon: 'fas fa-hourglass-half', description: 'Time-sensitive offers' },
                            { text: 'Social Proof', icon: 'fas fa-users', description: 'Testimonials and reviews' },
                            { text: 'Benefits Focus', icon: 'fas fa-check-circle', description: 'Product benefits' },
                            { text: 'Problem Solution', icon: 'fas fa-lightbulb', description: 'Address pain points' },
                            { text: 'Value Proposition', icon: 'fas fa-diamond', description: 'Unique selling points' },
                            { text: 'Guarantee', icon: 'fas fa-shield-alt', description: 'Risk-free offers' },
                            { text: 'Success Stories', icon: 'fas fa-trophy', description: 'Customer wins' }
                        ],
                        'product_description': [
                            { text: 'Digital Product', icon: 'fas fa-download', description: 'Digital download products' },
                            { text: 'Physical Product', icon: 'fas fa-cube', description: 'Physical shipping products' },
                            { text: 'Subscription', icon: 'fas fa-repeat', description: 'Recurring subscription model' },
                            { text: 'High Commission', icon: 'fas fa-dollar-sign', description: 'Attractive affiliate rates' },
                            { text: 'Vendor Store', icon: 'fas fa-shop', description: 'Multi-vendor marketplace' },
                            { text: 'Instant Delivery', icon: 'fas fa-bolt', description: 'Immediate product access' },
                            { text: 'Premium Quality', icon: 'fas fa-gem', description: 'High-quality offerings' },
                            { text: 'Customer Support', icon: 'fas fa-support', description: 'Included customer service' }
                        ],
                        'product_short_description': [
                            { text: 'Key Benefit', icon: 'fas fa-star', description: 'Main selling point (concise)' },
                            { text: 'Quick Summary', icon: 'fas fa-align-left', description: 'Brief product overview' },
                            { text: 'Value Proposition', icon: 'fas fa-diamond', description: 'Core value in few words' },
                            { text: 'Target Audience', icon: 'fas fa-users', description: 'Who this is for' },
                            { text: 'Problem Solver', icon: 'fas fa-lightbulb', description: 'What problem it solves' },
                            { text: 'Instant Appeal', icon: 'fas fa-heart', description: 'Immediate attraction factor' },
                            { text: 'Call to Action', icon: 'fas fa-arrow-right', description: 'Action-oriented message' },
                            { text: 'Unique Feature', icon: 'fas fa-gem', description: 'What makes it special' }
                        ],
                        'terms_content': [
                            { text: 'User Agreement', icon: 'fas fa-handshake', description: 'Terms of service agreement' },
                            { text: 'Privacy Policy', icon: 'fas fa-shield-alt', description: 'Data protection terms' },
                            { text: 'Affiliate Rules', icon: 'fas fa-clipboard-list', description: 'Affiliate program guidelines' },
                            { text: 'Payment Terms', icon: 'fas fa-credit-card', description: 'Payment processing conditions' },
                            { text: 'Refund Policy', icon: 'fas fa-undo', description: 'Return and refund conditions' },
                            { text: 'Liability Limits', icon: 'fas fa-exclamation-triangle', description: 'Limitation of liability' },
                            { text: 'Intellectual Property', icon: 'fas fa-copyright', description: 'IP rights and usage' },
                            { text: 'Account Termination', icon: 'fas fa-ban', description: 'Account suspension terms' },
                            { text: 'Governing Law', icon: 'fas fa-gavel', description: 'Legal jurisdiction' },
                            { text: 'Contact Information', icon: 'fas fa-address-book', description: 'Support contact details' }
                        ],
                        'privacy_policy': [
                            { text: 'Data Collection', icon: 'fas fa-database', description: 'What data we collect' },
                            { text: 'Cookie Usage', icon: 'fas fa-cookie-bite', description: 'Cookie policy details' },
                            { text: 'Third Party Services', icon: 'fas fa-share-alt', description: 'External service providers' },
                            { text: 'Data Retention', icon: 'fas fa-clock', description: 'How long we keep data' },
                            { text: 'User Rights', icon: 'fas fa-user-shield', description: 'GDPR and privacy rights' },
                            { text: 'Data Security', icon: 'fas fa-lock', description: 'Security measures' },
                            { text: 'Marketing Consent', icon: 'fas fa-envelope', description: 'Email marketing permissions' },
                            { text: 'Children Privacy', icon: 'fas fa-child', description: 'Under-age user protection' }
                        ],
                        'email_template': [
                            { text: 'Welcome Series', icon: 'fas fa-hand-wave', description: 'New user onboarding' },
                            { text: 'Commission Alert', icon: 'fas fa-bell', description: 'Earning notifications' },
                            { text: 'Payment Reminder', icon: 'fas fa-money-bill', description: 'Payment due notices' },
                            { text: 'Level Upgrade', icon: 'fas fa-level-up-alt', description: 'MLM level promotions' },
                            { text: 'New Products', icon: 'fas fa-plus-circle', description: 'Product announcements' },
                            { text: 'Training Resources', icon: 'fas fa-graduation-cap', description: 'Educational content' },
                            { text: 'Success Stories', icon: 'fas fa-star', description: 'User testimonials' },
                            { text: 'Monthly Report', icon: 'fas fa-chart-bar', description: 'Performance summaries' }
                        ],
                        'page_content': [
                            { text: 'About Us', icon: 'fas fa-info-circle', description: 'Company background' },
                            { text: 'How It Works', icon: 'fas fa-cogs', description: 'Process explanation' },
                            { text: 'Getting Started', icon: 'fas fa-play-circle', description: 'Quick start guide' },
                            { text: 'FAQ Section', icon: 'fas fa-question-circle', description: 'Frequently asked questions' },
                            { text: 'Success Stories', icon: 'fas fa-trophy', description: 'User testimonials' },
                            { text: 'Contact Us', icon: 'fas fa-envelope', description: 'Contact information' },
                            { text: 'Support Center', icon: 'fas fa-life-ring', description: 'Help and support' },
                            { text: 'Training Center', icon: 'fas fa-graduation-cap', description: 'Educational resources' }
                        ],
                        'announcement': [
                            { text: 'System Update', icon: 'fas fa-sync', description: 'Platform updates and changes' },
                            { text: 'New Features', icon: 'fas fa-sparkles', description: 'Latest feature releases' },
                            { text: 'Maintenance Notice', icon: 'fas fa-tools', description: 'Scheduled maintenance' },
                            { text: 'Policy Changes', icon: 'fas fa-file-alt', description: 'Terms and policy updates' },
                            { text: 'Performance Report', icon: 'fas fa-chart-line', description: 'Platform performance' },
                            { text: 'Community Event', icon: 'fas fa-calendar', description: 'Webinars and events' },
                            { text: 'Important Notice', icon: 'fas fa-exclamation', description: 'Critical announcements' },
                            { text: 'Congratulations', icon: 'fas fa-trophy', description: 'Achievement recognition' }
                        ],
                        'profile_bio': [
                            { text: 'Professional Summary', icon: 'fas fa-user-tie', description: 'Career overview' },
                            { text: 'Skills & Expertise', icon: 'fas fa-star', description: 'Key competencies' },
                            { text: 'Experience Level', icon: 'fas fa-chart-line', description: 'Years in industry' },
                            { text: 'Achievements', icon: 'fas fa-medal', description: 'Notable accomplishments' },
                            { text: 'Specializations', icon: 'fas fa-bullseye', description: 'Areas of focus' },
                            { text: 'Contact Preference', icon: 'fas fa-phone', description: 'How to reach' },
                            { text: 'Availability', icon: 'fas fa-clock', description: 'Working hours' },
                            { text: 'Mission Statement', icon: 'fas fa-compass', description: 'Personal mission' }
                        ],
                        'blog_post': [
                            { text: 'Industry Insights', icon: 'fas fa-lightbulb', description: 'Market analysis and trends' },
                            { text: 'How-To Guide', icon: 'fas fa-list-ol', description: 'Step-by-step tutorials' },
                            { text: 'Case Study', icon: 'fas fa-search', description: 'Real-world examples' },
                            { text: 'Tips & Tricks', icon: 'fas fa-magic', description: 'Best practices' },
                            { text: 'News Update', icon: 'fas fa-newspaper', description: 'Industry news' },
                            { text: 'Success Story', icon: 'fas fa-trophy', description: 'User achievements' },
                            { text: 'Expert Opinion', icon: 'fas fa-comment', description: 'Professional insights' },
                            { text: 'Comparison Review', icon: 'fas fa-balance-scale', description: 'Product comparisons' }
                        ],
                        'notification': [
                            { text: 'Payment Received', icon: 'fas fa-money-bill', description: 'Commission notifications' },
                            { text: 'System Alert', icon: 'fas fa-bell', description: 'Important system messages' },
                            { text: 'Level Promotion', icon: 'fas fa-arrow-up', description: 'MLM level upgrades' },
                            { text: 'New Referral', icon: 'fas fa-user-plus', description: 'Referral sign-ups' },
                            { text: 'Goal Achievement', icon: 'fas fa-target', description: 'Target completions' },
                            { text: 'Weekly Summary', icon: 'fas fa-calendar-week', description: 'Performance summaries' },
                            { text: 'Action Required', icon: 'fas fa-exclamation-circle', description: 'Tasks needing attention' },
                            { text: 'Reminder', icon: 'fas fa-clock', description: 'Important reminders' }
                        ]
                    };
                    
                    return systemTags[contentType] || systemTags['plan_description'];
                }
                
                function addTagToPrompt(tagText, element) {
                    var promptField = document.getElementById('ai-prompt');
                    var currentValue = promptField.value.trim();
                    
                    if (currentValue) {
                        promptField.value = currentValue + ', ' + tagText;
                    } else {
                        promptField.value = tagText;
                    }
                    
                    // Add visual feedback (only if element is passed)
                    if (element) {
                        element.style.backgroundColor = '#28a745';
                        element.style.color = 'white';
                        element.style.borderColor = '#28a745';
                        
                        setTimeout(() => {
                            element.style.backgroundColor = '#f8f9fa';
                            element.style.color = '#212529';
                            element.style.borderColor = '#dee2e6';
                        }, 1000);
                    }
                }
                
                function generateAIContent() {
                    var loadingDiv = document.getElementById('ai-loading');
                    var contentDiv = document.getElementById('ai-content');
                    var resultsDiv = document.getElementById('ai-results');
                    var suggestionsDiv = document.getElementById('ai-suggestions');
                    
                    // Show loading
                    contentDiv.classList.add('d-none');
                    loadingDiv.classList.remove('d-none');
                    
                    var formData = {
                        content_type: aiHelperData.currentContentType,
                        prompt: document.getElementById('ai-prompt').value,
                        tone: document.getElementById('ai-tone').value,
                        length: document.getElementById('ai-length').value
                    };
                    
                    fetch(aiHelperData.apiUrl, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(formData)
                    })
                    .then(response => response.json())
                    .then(data => {
                        loadingDiv.classList.add('d-none');
                        contentDiv.classList.remove('d-none');
                        
                        if (data.success) {
                            displayAISuggestions(data.suggestions);
                            resultsDiv.classList.remove('d-none');
                        } else {
                            alert('Error: ' + (data.message || 'Failed to generate content'));
                        }
                    })
                    .catch(error => {
                        loadingDiv.classList.add('d-none');
                        contentDiv.classList.remove('d-none');
                        alert('Error: ' + error.message);
                    });
                }
                
                function displayAISuggestions(suggestions) {
                    var suggestionsDiv = document.getElementById('ai-suggestions');
                    var html = '';
                    
                    suggestions.forEach(function(suggestion, index) {
                        html += `
                            <div class="card mb-2">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div class="flex-grow-1">
                                            <h6 class="card-title"><?= __('admin.option') ?> ${index + 1}</h6>
                                            <p class="card-text">${suggestion}</p>
                                        </div>
                                        <button class="btn btn-sm btn-success" onclick="useAISuggestion('${suggestion.replace(/'/g, "\\'")}')">
                                            <i class="fas fa-check me-1"></i><?= __('admin.use_this') ?>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        `;
                    });
                    
                    suggestionsDiv.innerHTML = html;
                }
                
                function useAISuggestion(content) {
                    // Check if aiHelperData exists and has currentTargetField
                    if (!aiHelperData || !aiHelperData.currentTargetField) {
                        alert('Error: Target field not specified. Please close and reopen the AI helper.');
                        return;
                    }
                    
                    var targetField = document.getElementById(aiHelperData.currentTargetField);
                    
                    // If not found by ID, try to find by name
                    if (!targetField) {
                        targetField = document.querySelector('[name="' + aiHelperData.currentTargetField + '"]');
                    }
                    
                    if (targetField) {
                        // Check if it's a summernote editor
                        if ($(targetField).hasClass('summernote-img') || $(targetField).data('summernote')) {
                            // For summernote rich text editor
                            $(targetField).summernote('code', content);
                            $(targetField).summernote('focus');
                        } else {
                            // For regular text fields and textareas
                            targetField.value = content;
                            
                            // Trigger change event for any listeners (Angular, etc.)
                            targetField.dispatchEvent(new Event('change'));
                            targetField.dispatchEvent(new Event('input'));
                            
                            // For Angular specifically
                            if (window.angular) {
                                var scope = angular.element(targetField).scope();
                                if (scope) {
                                    scope.$apply();
                                }
                            }
                        }
                        
                        // Close modal
                        var modal = bootstrap.Modal.getInstance(document.getElementById('aiHelperModal'));
                        modal.hide();
                        
                        // Show success message
                        showNotification('<?= __('admin.ai_content_applied') ?>', 'success');
                    } else {
                        // Fallback: try to find field by name or other attributes
                        var fieldByName = document.querySelector(`[name="description"]`) || 
                                         document.querySelector(`textarea[placeholder*="description"]`) ||
                                         document.querySelector('.summernote-img');
                        
                        if (fieldByName) {
                            if ($(fieldByName).hasClass('summernote-img') || $(fieldByName).data('summernote')) {
                                $(fieldByName).summernote('code', content);
                                $(fieldByName).summernote('focus');
                            } else {
                                fieldByName.value = content;
                                fieldByName.dispatchEvent(new Event('change'));
                                fieldByName.dispatchEvent(new Event('input'));
                                
                                // For Angular specifically
                                if (window.angular) {
                                    var scope = angular.element(fieldByName).scope();
                                    if (scope) {
                                        scope.$apply();
                                    }
                                }
                            }
                            
                            var modal = bootstrap.Modal.getInstance(document.getElementById('aiHelperModal'));
                            modal.hide();
                            showNotification('<?= __('admin.ai_content_applied') ?>', 'success');
                        } else {
                            alert('Error: Could not find target field. Field ID: ' + aiHelperData.currentTargetField);
                        }
                    }
                }
                
                function showNotification(message, type) {
                    // Simple notification (can be enhanced)
                    var alert = document.createElement('div');
                    alert.className = `alert alert-${type} alert-dismissible fade show position-fixed`;
                    alert.style.top = '20px';
                    alert.style.right = '20px';
                    alert.style.zIndex = '9999';
                    alert.innerHTML = `
                        ${message}
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    `;
                    document.body.appendChild(alert);
                    
                    setTimeout(() => {
                        alert.remove();
                    }, 3000);
                }
            </script>
            <?php
            return ob_get_clean();
        }
    }

    /**
     * Generate Enhanced QR Code Modal HTML
     * Creates a professional QR code modal with download/print capabilities
     * @return string HTML for QR modal
     */
    if (!function_exists('generate_qr_modal')) {
        function generate_qr_modal() {
            $CI =& get_instance();
            
            ob_start();
            ?>
            <!-- Enhanced QR Code Modal -->
            <div class="modal fade" id="model-codemodal" tabindex="-1">
              <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="qr-modal-title"><?= __('user.generate_qr_code') ?></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                  </div>
                  <div class="modal-body py-4 text-center">
                    <div id="qr-code-container" class="mb-3 d-flex justify-content-center align-items-center" style="min-height: 200px;"></div>
                    <div id="qr-url-display" class="text-muted small mb-3 px-3" style="word-break: break-all; max-height: 60px; overflow-y: auto;"></div>
                    <div class="text-muted small">
                      <i class="fas fa-info-circle me-1"></i>
                      <?= __('user.scan_with_phone_camera') ?>
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                      <i class="fas fa-times me-2"></i><?= __('user.close') ?>
                    </button>
                    <button type="button" class="btn btn-primary" id="download-qr-btn">
                      <i class="fas fa-download me-2"></i><?= __('user.download') ?>
                    </button>
                    <button type="button" class="btn btn-success" id="print-qr-btn">
                      <i class="fas fa-print me-2"></i><?= __('user.print') ?>
                    </button>
                  </div>
                </div>
              </div>
            </div>
            <?php
            return ob_get_clean();
        }
    }

    /**
     * Generate Enhanced QR Code JavaScript Functions
     * Creates all JavaScript functionality for QR code generation, download, and print
     * @return string JavaScript code for QR functionality
     */
    if (!function_exists('generate_qr_scripts')) {
        function generate_qr_scripts() {
            $CI =& get_instance();
            
            ob_start();
            ?>
            <script>
            $(document).ready(function() {
                // Enhanced QR Code functionality
                let currentQRCode = null;
                let currentQRUrl = '';

                // QR Code generation
                $(document).on('click', '.qrcode', function() {
                    currentQRUrl = $(this).attr('data-id');
                    
                    // Clear previous QR code and show loading
                    $('#qr-code-container').html(`
                        <div class="d-flex flex-column align-items-center">
                            <div class="spinner-border text-primary mb-2" role="status">
                                <span class="visually-hidden"><?= __('user.loading') ?></span>
                            </div>
                            <small class="text-muted"><?= __('user.generating_qr_code') ?></small>
                        </div>
                    `);
                    
                    // Set modal title and URL display
                    $('#qr-modal-title').text('<?= __('user.generate_qr_code') ?>');
                    $('#qr-url-display').text(currentQRUrl);
                    
                    // Disable buttons initially
                    $('#download-qr-btn, #print-qr-btn').prop('disabled', true);
                    
                    // Show modal
                    $("#model-codemodal").modal("show");
                    
                    // Generate QR code after a short delay to show loading state
                    setTimeout(() => {
                        try {
                            // Clear loading state
                            $('#qr-code-container').empty();
                            
                            // Generate QR code with improved size and mobile optimization
                            const qrSize = window.innerWidth < 768 ? 180 : 220; // Responsive size
                            
                            currentQRCode = new QRCode(document.getElementById("qr-code-container"), {
                                text: currentQRUrl,
                                width: qrSize,
                                height: qrSize,
                                colorDark: "#000000",
                                colorLight: "#ffffff",
                                correctLevel: QRCode.CorrectLevel.H
                            });
                            
                            // Enable download and print buttons
                            $('#download-qr-btn, #print-qr-btn').prop('disabled', false);
                            
                        } catch(error) {
                            console.error('QR Code generation failed:', error);
                            $('#qr-code-container').html(`
                                <div class="alert alert-danger">
                                    <i class="fas fa-exclamation-triangle me-2"></i>
                                    <?= __('user.qr_generation_failed') ?>
                                </div>
                            `);
                            // Disable download and print buttons
                            $('#download-qr-btn, #print-qr-btn').prop('disabled', true);
                        }
                    }, 300); // 300ms delay to show loading state
                });

                // Download QR Code functionality
                $(document).on('click', '#download-qr-btn', function() {
                    if (!currentQRCode) return;
                    
                    try {
                        const canvas = $('#qr-code-container canvas')[0];
                        if (canvas) {
                            // Create download link
                            const link = document.createElement('a');
                            link.download = 'qrcode-' + Date.now() + '.png';
                            link.href = canvas.toDataURL();
                            link.click();
                        }
                    } catch(error) {
                        console.error('QR Code download failed:', error);
                        alert('<?= __('user.download_failed') ?>');
                    }
                });

                // Print QR Code functionality
                $(document).on('click', '#print-qr-btn', function() {
                    if (!currentQRCode) return;
                    
                    try {
                        const canvas = $('#qr-code-container canvas')[0];
                        if (canvas) {
                            const printWindow = window.open('', '_blank');
                            printWindow.document.write(`
                                <html>
                                    <head>
                                        <title>QR Code - ${currentQRUrl}</title>
                                        <style>
                                            body { text-align: center; font-family: Arial, sans-serif; margin: 40px; }
                                            .qr-container { margin: 20px 0; }
                                            .url-text { margin-top: 20px; word-break: break-all; color: #666; }
                                        </style>
                                    </head>
                                    <body>
                                        <h2><?= __('user.qr_code') ?></h2>
                                        <div class="qr-container">
                                            <img src="${canvas.toDataURL()}" alt="QR Code" />
                                        </div>
                                        <div class="url-text">${currentQRUrl}</div>
                                    </body>
                                </html>
                            `);
                            printWindow.document.close();
                            printWindow.print();
                        }
                    } catch(error) {
                        console.error('QR Code print failed:', error);
                        alert('<?= __('user.print_failed') ?>');
                    }
                });
            });
            </script>
            <?php
            return ob_get_clean();
        }
    }

    /**
     * Complete QR Code Implementation
     * Combines modal and scripts for easy one-line implementation
     * @return string Complete HTML and JavaScript for QR functionality
     */
    if (!function_exists('generate_qr_complete')) {
        function generate_qr_complete() {
            return generate_qr_modal() . generate_qr_scripts();
        }
    }

    /**
     * Complete spam protection with console logging
     * Handles all spam detection logic and console output
     * 
     * @param array $data View data with user_id, form_id, ip
     * @param string $type 'form', 'product', or 'tool'
     * @return array ['blocked' => bool, 'message' => string, 'console_script' => string]
     */
    if (!function_exists('handle_spam_protection')) {
        function handle_spam_protection($data, $type = 'form') {
            $CI =& get_instance();
            
            // Skip if data is invalid
            if (!isset($data['user_id']) || !isset($data['ip'])) {
                $console_script = "<script>console.log('🚫 Spam Protection: Invalid data - missing user_id or ip');</script>";
                return ['blocked' => true, 'message' => 'Invalid data', 'console_script' => $console_script];
            }
            
            // Get the ID field based on type
            $id_field = '';
            switch ($type) {
                case 'form':
                    $id_field = 'form_id';
                    break;
                case 'product':
                    $id_field = 'product_id';
                    break;
                case 'tool':
                    $id_field = 'tools_id';
                    break;
                default:
                    $console_script = "<script>console.log('🚫 Spam Protection: Unknown type - " . $type . "');</script>";
                    return ['blocked' => true, 'message' => 'Unknown type', 'console_script' => $console_script];
            }
            
            if (!isset($data[$id_field])) {
                $console_script = "<script>console.log('🚫 Spam Protection: Missing ID field - " . $id_field . "');</script>";
                return ['blocked' => true, 'message' => 'Missing ID', 'console_script' => $console_script];
            }
            
            // Load Uagent library to get current browser info
            $CI->load->library('Uagent');
            $CI->uagent->init();
            $current_agent = $CI->uagent->string;
            
            if (empty($current_agent)) {
                $console_script = "<script>console.log('⚠️ Spam Protection: Empty user agent - allowing fallback');</script>";
                return ['blocked' => false, 'message' => 'Empty user agent', 'console_script' => $console_script];
            }
            
            // Check if this user has already viewed this item from same IP with different browser
            $cross_browser_check = $CI->db->get_where("product_view_logs", [
                "user_id" => $data['user_id'],
                $id_field => $data[$id_field],
                "ip" => $data['ip'],
                "agent !=" => $current_agent
            ])->row();
            
            if (isset($cross_browser_check)) {
                $console_script = "<script>console.log('🚫 Spam Protection: Cross-browser spam detected - User: " . $data['user_id'] . ", Type: " . $type . ", ID: " . $data[$id_field] . ", IP: " . $data['ip'] . "');</script>";
                return ['blocked' => true, 'message' => 'Cross-browser spam', 'console_script' => $console_script];
            }
            
            $console_script = "<script>console.log('✅ Spam Protection: No spam detected - User: " . $data['user_id'] . ", Type: " . $type . ", ID: " . $data[$id_field] . ", IP: " . $data['ip'] . "');</script>";
            return ['blocked' => false, 'message' => 'No spam', 'console_script' => $console_script];
        }
    }

    /**
     * Ultra-minimal spam protection - just one line to use
     * Handles everything: detection, logging, console output
     * 
     * @param array $data View data with user_id, form_id, ip
     * @param string $type 'form', 'product', or 'tool'
     * @return bool True if blocked, False if allowed
     */
    if (!function_exists('spam_protect')) {
        function spam_protect($data, $type = 'form') {
            $CI =& get_instance();
            
            // Skip if data is invalid
            if (!isset($data['user_id']) || !isset($data['ip'])) {
                echo "<script>console.log('🚫 Spam Protection: Invalid data - missing user_id or ip');</script>";
                return true; // Block invalid requests
            }
            
            // Get the ID field based on type
            $id_field = '';
            switch ($type) {
                case 'form':
                    $id_field = 'form_id';
                    break;
                case 'product':
                    $id_field = 'product_id';
                    break;
                case 'tool':
                    $id_field = 'tools_id';
                    break;
                default:
                    echo "<script>console.log('🚫 Spam Protection: Unknown type - " . $type . "');</script>";
                    return true; // Block unknown types
            }
            
            if (!isset($data[$id_field])) {
                echo "<script>console.log('🚫 Spam Protection: Missing ID field - " . $id_field . "');</script>";
                return true; // Block if ID is missing
            }
            
            // Enhanced cross-browser and private mode detection
            // Check for ANY view from same IP in last 24 hours (handles private mode, different browsers, etc.)
            $recent_view = $CI->db->get_where("product_view_logs", [
                $id_field => $data[$id_field],
                "ip" => $data['ip'],
                "created_at >=" => date('Y-m-d H:i:s', strtotime('-24 hours'))
            ])->row();
            
            // Also check for any recent view from same user/IP combination (more aggressive detection)
            $recent_user_view = $CI->db->get_where("product_view_logs", [
                "user_id" => $data['user_id'],
                "ip" => $data['ip'],
                "created_at >=" => date('Y-m-d H:i:s', strtotime('-24 hours'))
            ])->row();
            
            if (isset($recent_view)) {
                // Determine the specific spam method being used
                $spam_method = "Unknown method";
                
                // Check if it's the same user (private mode, different browser, cache clearing)
                if ($recent_view->user_id == $data['user_id']) {
                    // Enhanced detection for cross-browser attempts
                    $current_session_id = $CI->session->session_id ?? '';
                    $recent_session_id = $recent_view->session_id ?? '';
                    $current_user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
                    $recent_user_agent = $recent_view->agent ?? '';
                    
                    // Debug session IDs and additional info
                    echo "<script>console.log('🔍 Spam Protection Debug: Current Session ID: \"' + '" . $current_session_id . "' + '\", Recent Session ID: \"' + '" . $recent_session_id . "' + '\", Current User Agent: \"' + '" . $current_user_agent . "' + '\", Recent User Agent: \"' + '" . $recent_user_agent . "' + '\"');</script>";
                    echo "<script>console.log('🔍 Spam Protection Debug: Session ID Match: ' + ('" . $recent_session_id . "' === '" . $current_session_id . "') + ', User Agent Match: ' + ('" . $recent_user_agent . "' === '" . $current_user_agent . "'));</script>";
                    
                    // Check for cross-browser/private mode detection
                    // If session IDs are different OR user agents are different, it's likely a different browser or private mode
                    if ($recent_session_id != $current_session_id || $recent_user_agent != $current_user_agent) {
                        $spam_method = "Private/Incognito mode or different browser";
                    } else {
                        $spam_method = "Same browser refresh";
                    }
                } else {
                    // Different user from same IP (shared network, proxy)
                    $spam_method = "Different user from same IP";
                }
                
                echo "<script>console.log('🚫 Spam Protection: " . $spam_method . " detected (24h rule) - User: " . $data['user_id'] . ", Type: " . $type . ", ID: " . $data[$id_field] . ", IP: " . $data['ip'] . "');</script>";
                return true; // One view per IP per 24 hours
            }
            
            // Additional check: If we have any recent view from same user/IP, block it (catches Firefox private mode)
            if (isset($recent_user_view)) {
                $current_user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
                $recent_user_agent = $recent_user_view->agent ?? '';
                
                echo "<script>console.log('🔍 Spam Protection Debug: Additional Check - Current User Agent: \"' + '" . $current_user_agent . "' + '\", Recent User Agent: \"' + '" . $recent_user_agent . "' + '\"');</script>";
                
                // More aggressive detection: If we have ANY recent view from same user/IP, block it
                // This catches Firefox private mode and other edge cases
                echo "<script>console.log('🚫 Spam Protection: Cross-browser attempt detected (same user/IP within 24h) - User: " . $data['user_id'] . ", Type: " . $type . ", ID: " . $data[$id_field] . ", IP: " . $data['ip'] . "');</script>";
                return true; // Block any cross-browser attempts
            }
            
            // Final check: Look for ANY recent activity from same user (most aggressive)
            $any_recent_user_activity = $CI->db->get_where("product_view_logs", [
                "user_id" => $data['user_id'],
                "created_at >=" => date('Y-m-d H:i:s', strtotime('-24 hours'))
            ])->num_rows();
            
            if ($any_recent_user_activity > 0) {
                echo "<script>console.log('🚫 Spam Protection: User activity detected within 24h - User: " . $data['user_id'] . ", Type: " . $type . ", ID: " . $data[$id_field] . ", IP: " . $data['ip'] . "');</script>";
                return true; // Block if user has any recent activity
            }
            
            // Additional protection against proxy/spam services
            // Check for rapid-fire views from same IP (multiple views in short time)
            $rapid_views = $CI->db->get_where("product_view_logs", [
                "ip" => $data['ip'],
                "created_at >=" => date('Y-m-d H:i:s', strtotime('-1 hour'))
            ])->num_rows();
            
            if ($rapid_views >= 10) {
                echo "<script>console.log('🚫 Spam Protection: Rapid-fire attack detected - IP: " . $data['ip'] . " has " . $rapid_views . " views in 1 hour (limit: 10)');</script>";
                return true; // Too many views from same IP in 1 hour
            }
            
            // Check for suspicious user agent patterns (common in spam services)
            $current_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
            $suspicious_agents = [
                'bot', 'crawler', 'spider', 'scraper', 'curl', 'wget', 'python', 'java',
                'phantom', 'headless', 'selenium', 'automation', 'script'
            ];
            
            foreach ($suspicious_agents as $suspicious) {
                if (stripos($current_agent, $suspicious) !== false) {
                    echo "<script>console.log('🚫 Spam Protection: Bot/automation detected - User agent contains \"' + '" . $suspicious . "' + '\" - IP: " . $data['ip'] . "');</script>";
                    return true; // Suspicious user agent
                }
            }
            
            // VPN and proxy detection
            // Check for multiple IPs from same user in short time (VPN rotation)
            $recent_user_ips = $CI->db->get_where("product_view_logs", [
                "user_id" => $data['user_id'],
                "created_at >=" => date('Y-m-d H:i:s', strtotime('-1 hour'))
            ])->result();
            
            $unique_ips = [];
            foreach ($recent_user_ips as $view) {
                $unique_ips[] = $view->ip;
            }
            $unique_ips = array_unique($unique_ips);
            
            if (count($unique_ips) >= 3) {
                echo "<script>console.log('🚫 Spam Protection: VPN rotation detected - User: " . $data['user_id'] . " used " . count($unique_ips) . " different IPs in 1 hour (limit: 3)');</script>";
                return true; // Too many different IPs from same user
            }
            
            // Check for known VPN/proxy IP ranges (basic check)
            $vpn_indicators = [
                'vpn', 'proxy', 'tor', 'anonymous', 'privacy'
            ];
            
            // Get hostname for IP (basic VPN detection)
            $hostname = gethostbyaddr($data['ip']);
            foreach ($vpn_indicators as $indicator) {
                if (stripos($hostname, $indicator) !== false) {
                    echo "<script>console.log('🚫 Spam Protection: VPN/proxy service detected - IP: " . $data['ip'] . " Hostname contains \"' + '" . $indicator . "' + '\" (" . $hostname . ")');</script>";
                    return true; // VPN/proxy detected
                }
            }
            
            // Enhanced VPN detection for premium services
            // Check if IP is from known datacenter ranges (common for VPNs)
            $datacenter_indicators = [
                'amazon', 'aws', 'google', 'cloudflare', 'digitalocean', 'linode', 'vultr',
                'ovh', 'hetzner', 'leaseweb', 'chicago', 'frankfurt', 'amsterdam', 'london'
            ];
            
            foreach ($datacenter_indicators as $indicator) {
                if (stripos($hostname, $indicator) !== false) {
                    echo "<script>console.log('🚫 Spam Protection: Datacenter/VPN IP detected - IP: " . $data['ip'] . " Hostname contains \"' + '" . $indicator . "' + '\" (" . $hostname . ")');</script>";
                    return true; // Datacenter/VPN IP detected
                }
            }
            
            // Get current user agent directly from server
            $current_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
            
            if (empty($current_agent)) {
                echo "<script>console.log('⚠️ Spam Protection: Empty user agent - allowing fallback');</script>";
                return false; // Allow if user agent is empty (fallback)
            }
            
            echo "<script>console.log('✅ Spam Protection: No spam detected - User: " . $data['user_id'] . ", Type: " . $type . ", ID: " . $data[$id_field] . ", IP: " . $data['ip'] . "');</script>";
            return false; // No spam detected
        }
    }

    /**
     * Ultimate one-line spam protection with logging
     * Handles everything: detection, logging, console output, and returns result
     * 
     * @param array $data View data with user_id, form_id, ip
     * @param string $type 'form', 'product', or 'tool'
     * @return int 1 for success, 2 for blocked
     */
    if (!function_exists('spam_protect_and_log')) {
        function spam_protect_and_log($data, $type = 'form') {
            $CI =& get_instance();
            
            // Check if spam detected
            $is_spam = spam_protect($data, $type);
            
            // Only log to database if NOT spam (successful views)
            // For spam attempts, we'll use a separate lightweight logging system
            if (!$is_spam) {
                // No spam detected - log as normal view
                switch ($type) {
                    case 'form':
                        $CI->load->model("Form_model");
                        return $CI->Form_model->save_view_logs($data);
                    case 'product':
                        $CI->load->model("Product_model");
                        return $CI->Product_model->save_view_logs($data);
                    case 'tool':
                        $CI->load->model("IntegrationModel");
                        return $CI->IntegrationModel->save_view_logs($data);
                    default:
                        $CI->load->model("Form_model");
                        return $CI->Form_model->save_view_logs($data);
                }
            } else {
                // Spam detected - use lightweight logging for admin monitoring
                log_spam_attempt_lightweight($data, $type);
                return 2; // Blocked
            }
        }
    }
    
    /**
     * Lightweight spam attempt logging for admin monitoring
     * Uses file-based storage for persistent admin visibility
     * 
     * @param array $data View data
     * @param string $type Campaign type
     */
    if (!function_exists('log_spam_attempt_lightweight')) {
        function log_spam_attempt_lightweight($data, $type) {
            $CI =& get_instance();
            
            // Create spam attempt record
            $spam_record = [
                'user_id' => $data['user_id'],
                'form_id' => $data['form_id'] ?? null,
                'product_id' => $data['product_id'] ?? null,
                'tools_id' => $data['tools_id'] ?? null,
                'ip' => $data['ip'],
                'created_at' => date('Y-m-d H:i:s'),
                'blocked' => true,
                'spam_type' => $type,
                'fraud_score' => $data['fraud_score'] ?? null,
            ];
            
            // Store in both session (for immediate access) and file (for persistence)
            $spam_logs = $CI->session->userdata('spam_logs') ?: [];
            $spam_logs[] = $spam_record;
            
            // Keep only last 1000 spam records to prevent session bloat
            if (count($spam_logs) > 1000) {
                $spam_logs = array_slice($spam_logs, -1000);
            }
            
            // Store back in session
            $CI->session->set_userdata('spam_logs', $spam_logs);
            
            // Also store in file for admin dashboard access
            $spam_file = APPPATH . 'logs/spam_attempts.json';
            $file_logs = [];
            
            if (file_exists($spam_file)) {
                $file_content = file_get_contents($spam_file);
                if ($file_content) {
                    $file_logs = json_decode($file_content, true) ?: [];
                }
            }
            
            // Add new record to file logs
            $file_logs[] = $spam_record;
            
            // Keep only last 1000 records in file
            if (count($file_logs) > 1000) {
                $file_logs = array_slice($file_logs, -1000);
            }
            
            // Write back to file
            file_put_contents($spam_file, json_encode($file_logs));
            
            // Debug: Log that spam attempt was stored
            error_log("Spam attempt logged to session and file. Total spam logs: " . count($spam_logs));
        }
    }


    if (!function_exists('check_cross_browser_spam')) {
        function check_cross_browser_spam($data, $type = 'form') {
            $result = handle_spam_protection($data, $type);
            return $result['blocked'];
        }
    }

    if (!function_exists('format_filesize')) {
        function format_filesize($bytes) {
            $bytes = (int) $bytes;
            if ($bytes <= 0)         return '0 B';
            if ($bytes < 1024)       return $bytes . ' B';
            if ($bytes < 1048576)    return round($bytes / 1024, 1) . ' KB';
            if ($bytes < 1073741824) return round($bytes / 1048576, 1) . ' MB';
            return round($bytes / 1073741824, 2) . ' GB';
        }
    }

    if (!function_exists('get_pagination_settings')) {
        function get_pagination_settings($custom_settings = []) {
            $defaults = [
                'per_page' => 15,
                'size' => 'sm',
                'alignment' => 'center',
                'show_first_last' => true,
                'show_prev_next' => true,
                'max_links' => 5
            ];
            
            return array_merge($defaults, $custom_settings);
        }
    }

    if (!function_exists('easy_pagination')) {
        function easy_pagination($base_url, $total_rows, $offset = 0, $custom_settings = []) {
            $settings = get_pagination_settings($custom_settings);
            $use_get_params = isset($custom_settings['use_get_params']) ? $custom_settings['use_get_params'] : false;
            $preserve_query = isset($custom_settings['preserve_query']) ? (bool)$custom_settings['preserve_query'] : false;
            $current_get = [];
            if ($preserve_query && function_exists('get_instance')) {
                $ci = get_instance();
                if ($ci && isset($ci->input)) {
                    $current_get = (array)$ci->input->get();
                    if (isset($current_get['page'])) unset($current_get['page']);
                    // Remove empty parameters for cleaner URLs
                    $current_get = array_filter($current_get, function($value) {
                        return $value !== '' && $value !== null;
                    });
                }
            }

            $per_page = $settings['per_page'];
            
            if ($total_rows <= $per_page) {
                return ['html' => '<div class="text-muted small text-center py-2">Total: ' . $total_rows . ' items (Page 1 of 1)</div>', 'per_page' => $per_page];
            }
            
            $total_pages = ceil($total_rows / $per_page);
            $current_page = max(1, min(($offset / $per_page) + 1, $total_pages));
            
            $size_class = $settings['size'] ? ' pagination-' . $settings['size'] : '';
            $alignment_class = ' justify-content-' . $settings['alignment'];
            
            $html = '<ul class="pagination' . $size_class . $alignment_class . '">';
            
            if ($settings['show_first_last'] && $current_page > 1) {
                $html .= '<li class="page-item">';
                if ($use_get_params) {
                    $params = $current_get; $params['page'] = 1;
                    $url = $base_url . '?' . http_build_query($params);
                } else {
                    $qs = ($preserve_query && !empty($current_get)) ? ('?' . http_build_query($current_get)) : '';
                    $url = $base_url . '/1' . $qs;
                }
                $html .= '<a class="page-link" href="' . $url . '">&laquo;</a>';
                $html .= '</li>';
            }
            
            if ($settings['show_prev_next'] && $current_page > 1) {
                $prev_page = $current_page - 1;
                $html .= '<li class="page-item">';
                if ($use_get_params) {
                    $params = $current_get; $params['page'] = $prev_page;
                    $url = $base_url . '?' . http_build_query($params);
                } else {
                    $qs = ($preserve_query && !empty($current_get)) ? ('?' . http_build_query($current_get)) : '';
                    $url = $base_url . '/' . $prev_page . $qs;
                }
                $html .= '<a class="page-link" href="' . $url . '">&lsaquo;</a>';
                $html .= '</li>';
            }
            
            $start = max(1, $current_page - floor($settings['max_links'] / 2));
            $end = min($total_pages, $start + $settings['max_links'] - 1);
            $start = max(1, $end - $settings['max_links'] + 1);
            
            for ($i = $start; $i <= $end; $i++) {
                if ($i == $current_page) {
                    $html .= '<li class="page-item active">';
                    $html .= '<span class="page-link">' . $i . '</span>';
                    $html .= '</li>';
                } else {
                    $html .= '<li class="page-item">';
                    if ($use_get_params) {
                        $params = $current_get; $params['page'] = $i;
                        $url = $base_url . '?' . http_build_query($params);
                    } else {
                        $qs = ($preserve_query && !empty($current_get)) ? ('?' . http_build_query($current_get)) : '';
                        $url = $base_url . '/' . $i . $qs;
                    }
                    $html .= '<a class="page-link" href="' . $url . '">' . $i . '</a>';
                    $html .= '</li>';
                }
            }
            
            if ($settings['show_prev_next'] && $current_page < $total_pages) {
                $next_page = $current_page + 1;
                $html .= '<li class="page-item">';
                if ($use_get_params) {
                    $params = $current_get; $params['page'] = $next_page;
                    $url = $base_url . '?' . http_build_query($params);
                } else {
                    $qs = ($preserve_query && !empty($current_get)) ? ('?' . http_build_query($current_get)) : '';
                    $url = $base_url . '/' . $next_page . $qs;
                }
                $html .= '<a class="page-link" href="' . $url . '">&rsaquo;</a>';
                $html .= '</li>';
            }
            
            if ($settings['show_first_last'] && $current_page < $total_pages) {
                $html .= '<li class="page-item">';
                if ($use_get_params) {
                    $params = $current_get; $params['page'] = $total_pages;
                    $url = $base_url . '?' . http_build_query($params);
                } else {
                    $qs = ($preserve_query && !empty($current_get)) ? ('?' . http_build_query($current_get)) : '';
                    $url = $base_url . '/' . $total_pages . $qs;
                }
                $html .= '<a class="page-link" href="' . $url . '">&raquo;</a>';
                $html .= '</li>';
            }
            
            $html .= '</ul>';
            
            return [
                'html' => $html,
                'per_page' => $per_page
            ];
        }
    }

    if (!function_exists('pagination_summary_html')) {
        function pagination_summary_html($current_page, $per_page, $total_rows, $alignment = 'start', $size = 'sm') {
            $current_page = max(1, (int)$current_page);
            $per_page = max(1, (int)$per_page);
            $total_rows = max(0, (int)$total_rows);
            if ($total_rows === 0) {
                $text = __('admin.no_entries');
            } else {
                $start = (($current_page - 1) * $per_page) + 1;
                if ($start > $total_rows) {
                    $current_page = (int)ceil($total_rows / $per_page);
                    $start = (($current_page - 1) * $per_page) + 1;
                }
                $end = min($start + $per_page - 1, $total_rows);
                $tpl = __('admin.showing_entries');
                $text = str_replace(['{start}','{end}','{total}'], [$start, $end, $total_rows], $tpl);
            }
            $alignClass = 'text-start';
            if ($alignment === 'center') $alignClass = 'text-center';
            if ($alignment === 'end' || $alignment === 'right') $alignClass = 'text-end';
            $sizeClass = $size === 'lg' ? 'fs-6' : ($size === 'sm' ? 'small' : '');
            return '<div class="text-muted '.$sizeClass.' '.$alignClass.'">'.$text.'</div>';
        }
    }

    if (!function_exists('ajax_pagination')) {
        function ajax_pagination($total_rows, $current_page = 1, $custom_settings = []) {
            $settings = get_pagination_settings($custom_settings);
            $per_page = $settings['per_page'];
            $js_function = isset($custom_settings['js_function']) ? $custom_settings['js_function'] : 'universalPagination';
            
            if ($total_rows <= $per_page) {
                return ['html' => '<div class="text-muted small text-center py-2">Total: ' . $total_rows . ' items (Page 1 of 1)</div>', 'per_page' => $per_page];
            }
            
            $total_pages = ceil($total_rows / $per_page);
            $current_page = max(1, min($current_page, $total_pages));
            
            $size_class = $settings['size'] ? ' pagination-' . $settings['size'] : '';
            $alignment_class = ' justify-content-' . $settings['alignment'];
            
            $html = '<ul class="pagination' . $size_class . $alignment_class . '">';
            
            if ($settings['show_first_last'] && $current_page > 1) {
                $html .= '<li class="page-item">';
                $html .= '<a class="page-link" href="javascript:void(0)" onclick="' . $js_function . '(1)">&laquo;</a>';
                $html .= '</li>';
            }
            
            if ($settings['show_prev_next'] && $current_page > 1) {
                $prev_page = $current_page - 1;
                $html .= '<li class="page-item">';
                $html .= '<a class="page-link" href="javascript:void(0)" onclick="' . $js_function . '(' . $prev_page . ')">&lsaquo;</a>';
                $html .= '</li>';
            }
            
            $start = max(1, $current_page - floor($settings['max_links'] / 2));
            $end = min($total_pages, $start + $settings['max_links'] - 1);
            $start = max(1, $end - $settings['max_links'] + 1);
            
            for ($i = $start; $i <= $end; $i++) {
                if ($i == $current_page) {
                    $html .= '<li class="page-item active">';
                    $html .= '<span class="page-link">' . $i . '</span>';
                    $html .= '</li>';
                } else {
                    $html .= '<li class="page-item">';
                    $html .= '<a class="page-link" href="javascript:void(0)" onclick="' . $js_function . '(' . $i . ')">' . $i . '</a>';
                    $html .= '</li>';
                }
            }
            
            if ($settings['show_prev_next'] && $current_page < $total_pages) {
                $next_page = $current_page + 1;
                $html .= '<li class="page-item">';
                $html .= '<a class="page-link" href="javascript:void(0)" onclick="' . $js_function . '(' . $next_page . ')">&rsaquo;</a>';
                $html .= '</li>';
            }
            
            if ($settings['show_first_last'] && $current_page < $total_pages) {
                $html .= '<li class="page-item">';
                $html .= '<a class="page-link" href="javascript:void(0)" onclick="' . $js_function . '(' . $total_pages . ')">&raquo;</a>';
                $html .= '</li>';
            }
            
            $html .= '</ul>';
            
            return [
                'html' => $html,
                'per_page' => $per_page,
                'js_script' => generate_universal_pagination_script()
            ];
        }
    }

    if (!function_exists('generate_universal_pagination_script')) {
        function generate_universal_pagination_script() {
            return '
            <script>
            if (typeof universalPagination === "undefined") {
                function universalPagination(page) {
                    var currentUrl = window.location.pathname;
                    var formData = new FormData();
                    formData.append("page", page);
                    
                    // Collect all form inputs on the page
                    $("form input, form select, form textarea").each(function() {
                        if ($(this).attr("name") && $(this).val()) {
                            formData.append($(this).attr("name"), $(this).val());
                        }
                    });
                    
                    $.ajax({
                        url: currentUrl,
                        type: "POST",
                        data: formData,
                        processData: false,
                        contentType: false,
                        dataType: "json",
                        beforeSend: function() {
                            $(".pagination-container, .wallet-table-container, .transaction-content, .user-table tbody").html(
                                "<div class=\"d-flex justify-content-center py-5\"><div class=\"spinner-border text-primary\" role=\"status\"></div></div>"
                            );
                        },
                        success: function(response) {
                            if (response.table) {
                                $(".pagination-container, .wallet-table-container, .transaction-content, .user-table tbody").html(response.table);
                            }
                            if (response.pagination) {
                                $(".pagination-wrapper, .pagination-container").html(response.pagination);
                            }
                        },
                        error: function() {
                            if (typeof showToast !== "undefined") {
                                showToast("Error", "Failed to load page data", "error", 3000);
                            } else {
                                alert("Failed to load page data");
                            }
                        }
                    });
                }
            }
            </script>';
        }
    }

    if (!function_exists('render_universal_pagination')) {
        function render_universal_pagination($total_rows, $current_page = 1, $custom_settings = []) {
            $pagination_data = ajax_pagination($total_rows, $current_page, $custom_settings);
            return $pagination_data['html'] . $pagination_data['js_script'];
        }
    }

    /**
     * Generate Admin Theme CSS Variables
     * Keeps header.php clean by centralizing theme color logic
     */
    if (!function_exists('generate_admin_theme_css')) {
        function generate_admin_theme_css() {
            // Get CodeIgniter instance to access models
            $CI =& get_instance();
            $CI->load->model('Product_model');
            
            // Load all theme settings from database
            $theme_settings = [
                'admin_side_font' => $CI->Product_model->getSettings('site','admin_side_font')['admin_side_font'] ?? 'PT Sans',
                'admin_topbar_bg' => $CI->Product_model->getSettings('theme','admin_topbar_bg')['admin_topbar_bg'] ?? '#34495e',
                'admin_topbar_text' => $CI->Product_model->getSettings('theme','admin_topbar_text')['admin_topbar_text'] ?? '#ffffff',
                'admin_dropdown_bg' => $CI->Product_model->getSettings('theme','admin_dropdown_bg')['admin_dropdown_bg'] ?? '#ffffff',
                'admin_dropdown_text' => $CI->Product_model->getSettings('theme','admin_dropdown_text')['admin_dropdown_text'] ?? '#212529',
                'admin_dropdown_hover_bg' => $CI->Product_model->getSettings('theme','admin_dropdown_hover_bg')['admin_dropdown_hover_bg'] ?? '#e3f2fd',
                'admin_dropdown_hover_text' => $CI->Product_model->getSettings('theme','admin_dropdown_hover_text')['admin_dropdown_hover_text'] ?? '#1976d2',
                'admin_horizontal_dropdown_bg' => $CI->Product_model->getSettings('theme','admin_horizontal_dropdown_bg')['admin_horizontal_dropdown_bg'] ?? '#34495e',
                'admin_horizontal_dropdown_text' => $CI->Product_model->getSettings('theme','admin_horizontal_dropdown_text')['admin_horizontal_dropdown_text'] ?? '#ffffff',
                'admin_horizontal_dropdown_hover_bg' => $CI->Product_model->getSettings('theme','admin_horizontal_dropdown_hover_bg')['admin_horizontal_dropdown_hover_bg'] ?? '#e3f2fd',
                'admin_horizontal_dropdown_hover_text' => $CI->Product_model->getSettings('theme','admin_horizontal_dropdown_hover_text')['admin_horizontal_dropdown_hover_text'] ?? '#ffffff',
                'admin_menu_bg' => $CI->Product_model->getSettings('theme','admin_menu_bg')['admin_menu_bg'] ?? '#667eea',
                'admin_menu_text' => $CI->Product_model->getSettings('theme','admin_menu_text')['admin_menu_text'] ?? '#495057',
                'admin_menu_active' => $CI->Product_model->getSettings('theme','admin_menu_active')['admin_menu_active'] ?? '#0d6efd',
                'admin_menu_hover' => $CI->Product_model->getSettings('theme','admin_menu_hover')['admin_menu_hover'] ?? '#0d6efd',
                'admin_dropdown_scrollbar' => $CI->Product_model->getSettings('theme','admin_dropdown_scrollbar')['admin_dropdown_scrollbar'] ?? '#666666',
                'admin_footer_bg' => $CI->Product_model->getSettings('theme','admin_footer_bg')['admin_footer_bg'] ?? '#1a252f',
                'admin_footer_text' => $CI->Product_model->getSettings('theme','admin_footer_text')['admin_footer_text'] ?? '#ffffff'
            ];
            
            // Generate CSS variables
            $css = "<style>\n    :root {\n";
            $css .= "        --admin-font-family: {$theme_settings['admin_side_font']};\n";
            $css .= "        --admin-topbar-bg: {$theme_settings['admin_topbar_bg']};\n";
            $css .= "        --admin-topbar-text: {$theme_settings['admin_topbar_text']};\n";
            $css .= "        --admin-dropdown-bg: {$theme_settings['admin_dropdown_bg']};\n";
            $css .= "        --admin-dropdown-text: {$theme_settings['admin_dropdown_text']};\n";
            $css .= "        --admin-dropdown-hover-bg: {$theme_settings['admin_dropdown_hover_bg']};\n";
            $css .= "        --admin-dropdown-hover-text: {$theme_settings['admin_dropdown_hover_text']};\n";
            $css .= "        --admin-horizontal-dropdown-bg: {$theme_settings['admin_horizontal_dropdown_bg']};\n";
            $css .= "        --admin-horizontal-dropdown-text: {$theme_settings['admin_horizontal_dropdown_text']};\n";
            $css .= "        --admin-horizontal-dropdown-hover-bg: {$theme_settings['admin_horizontal_dropdown_hover_bg']};\n";
            $css .= "        --admin-horizontal-dropdown-hover-text: {$theme_settings['admin_horizontal_dropdown_hover_text']};\n";
            $css .= "        --admin-menu-bg: {$theme_settings['admin_menu_bg']};\n";
            $css .= "        --admin-menu-text: {$theme_settings['admin_menu_text']};\n";
            $css .= "        --admin-menu-active: {$theme_settings['admin_menu_active']};\n";
            $css .= "        --admin-menu-hover: {$theme_settings['admin_menu_hover']};\n";
            $css .= "        --admin-dropdown-scrollbar: {$theme_settings['admin_dropdown_scrollbar']};\n";
            $css .= "        --admin-footer-bg: {$theme_settings['admin_footer_bg']};\n";
            $css .= "        --admin-footer-text: {$theme_settings['admin_footer_text']};\n";
            $css .= "    }\n</style>";
            
            return $css;
        }
    }

    if (!function_exists('render_performance_indicator')) {
        function render_performance_indicator($elementId = 'performance-indicator', $autoHide = true) {
            $hideScript = $autoHide ? "
                setTimeout(() => {
                    if (perfIndicator) {
                        perfIndicator.style.opacity = '0.7';
                        setTimeout(() => {
                            if (perfIndicator) perfIndicator.style.display = 'none';
                        }, 3000);
                    }
                }, 2000);
            " : "";

            return "
            <script>
            window.PerformanceMonitor = {
                start: function(elementId) {
                    const perfIndicator = document.getElementById(elementId || 'performance-indicator');
                    if (perfIndicator) {
                        perfIndicator.style.display = 'inline-block';
                    }
                    return performance.now();
                },
                
                end: function(startTime, elementId) {
                    const endTime = performance.now();
                    const loadTime = Math.round(endTime - startTime);
                    const perfIndicator = document.getElementById(elementId || 'performance-indicator');
                    
                    if (perfIndicator) {
                        let statusText, statusIcon, badgeClass;
                        
                        if (loadTime < 50) {
                            statusText = 'Excellent';
                            statusIcon = 'fa-rocket';
                            badgeClass = 'badge bg-success ms-2';
                        } else if (loadTime < 100) {
                            statusText = 'Fast';
                            statusIcon = 'fa-tachometer-alt';
                            badgeClass = 'badge bg-success ms-2';
                        } else if (loadTime < 300) {
                            statusText = 'Good';
                            statusIcon = 'fa-clock';
                            badgeClass = 'badge bg-info ms-2';
                        } else if (loadTime < 500) {
                            statusText = 'Slow';
                            statusIcon = 'fa-hourglass-half';
                            badgeClass = 'badge bg-warning ms-2';
                        } else {
                            statusText = 'Very Slow';
                            statusIcon = 'fa-exclamation-triangle';
                            badgeClass = 'badge bg-danger ms-2';
                        }
                        
                        perfIndicator.innerHTML = '<i class=\"fa ' + statusIcon + '\"></i> ' + statusText + ' (' + loadTime + 'ms)';
                        perfIndicator.className = badgeClass;
                        perfIndicator.title = 'Page loaded in ' + loadTime + 'ms - ' + statusText + ' performance';
                        
                        " . $hideScript . "
                    }
                    
                    return loadTime;
                }
            };
            </script>";
        }
    }

    if (!function_exists('performance_indicator_html')) {
    function performance_indicator_html($elementId = 'performance-indicator', $classes = 'badge bg-success text-white shadow-lg px-3 py-2', $floating = true) {
        $floatingWrapper = $floating ? '<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 1050;">' : '';
        $floatingWrapperEnd = $floating ? '</div>' : '';
        
        return $floatingWrapper . '<span class="' . $classes . '" id="' . $elementId . '" style="display: none;">
            <i class="fa fa-spinner fa-spin"></i> Loading...
        </span>' . $floatingWrapperEnd;
    }
}

if (!function_exists('license_easy_get_local_data')) {
    function license_easy_get_local_data() {
        $file = APPPATH . 'license-easy-data-affiliateporsaas.json';
        if (!file_exists($file)) {
            return [];
        }
        $contents = @file_get_contents($file);
        $data = json_decode($contents, true);
        return is_array($data) ? $data : [];
    }
}

if (!function_exists('license_easy_fetch_details')) {
    function license_easy_fetch_details($force_refresh = false) {
        $local = license_easy_get_local_data();
        $license_key = defined('CODECANYON_LICENCE') ? CODECANYON_LICENCE : '';
        if (!$license_key && isset($local['license_key'])) {
            $license_key = $local['license_key'];
        }
        if (!$license_key) {
            return false;
        }
        
        if (!$force_refresh && !empty($local) && isset($local['status']) && $local['status'] === 'active') {
            $local['license_key'] = $license_key;
            return $local;
        }
        
        $domain = function_exists('base_url') ? base_url() : '';
        $payload = array(
            'purchase_code' => $license_key,
            'product_slug' => 'affiliateporsaas',
            'domain' => $domain
        );
        $ch = curl_init('https://affiliatepro.org/index.php?rest_route=/license-easy/v1/validate');
        curl_setopt_array($ch, array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($payload),
            CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
            CURLOPT_TIMEOUT => 20,
        ));
        $response = curl_exec($ch);
        curl_close($ch);
        if (!$response) {
            return !empty($local) ? $local : false;
        }
        $decoded = json_decode($response, true);
        if (!is_array($decoded) || !isset($decoded['status']) || $decoded['status'] !== 'success') {
            return !empty($local) ? $local : false;
        }
        $decoded['license_key'] = $license_key;
        
        $file = APPPATH . 'license-easy-data-affiliateporsaas.json';
        @file_put_contents($file, json_encode($decoded, JSON_PRETTY_PRINT));
        
        return $decoded;
    }
}