<?php defined('ABSPATH') || die('Cheatin\' uh?'); ?>
<?php $features = $view->getFeatures(); ?>
<a name="features"></a>
<div class="strb_features my-2 py-2">
    <?php if(STRB_Classes_Helpers_Tools::getValue('page') == 'strb_settings'){ ?>
    <div class="row row-cols-1 row-cols-md-3 p-0 m-0" style="max-width: 1200px;">
        <?php
        foreach ($features as $index => $feature) {
            if(!$feature['mainfeature']){
                continue;
            }
        ?>
            <div class="col-3 p-0 pr-2 mb-3">
                <div class="strb_feature card h-100 p-0 bg-white shadow-0 rounded-0">
                    <div class="m-0 p-0">
                        <div class="mx-3 mt-4 p-0">

                            <div class="col-12 d-flex align-items-center m-0 p-0">
                                <div class="p-0 m-0">
                                    <a href="<?php echo esc_url($feature['link']) ?>" style="text-decoration: none"><?php echo wp_kses_post($feature['mainfeature']) ?></a>
                                </div>
                            </div>

                            <div class="col-12 d-flex align-items-center m-0 p-0 mt-2">
                                <h5 class="p-0 m-0 font-weight-bold">
                                    <?php echo wp_kses_post($feature['title']) ?>
                                </h5>
                            </div>
                        </div>

                    </div>
                </div>
            </div>

        <?php }?>
   <?php }else{ ?>
        <div class="row text-left m-0 p-0" style="max-width: 500px;">
            <div class="col px-2 py-3">
                <div class="col-12 m-0 p-0">
                    <h3><?php echo esc_html__("Main Features", 'starbox') ?></h3>
                </div>
                <div class="col-12 m-2 p-0">
                    <div class="row py-2">
                        <form method="get" class="d-flex flex-row flex-grow-1 justify-content-end m-0 p-0">
                            <input type="hidden" name="page" value="<?php echo STRB_Classes_Helpers_Tools::getValue('page', 'strb_features') ?>">
                            <input type="search" class="d-inline-block align-middle col m-0 p-0 px-1 rounded-0" autofocus name="sfeature" placeholder="<?php echo esc_html__("Enter a feature you want to search for", 'starbox') ?>" value="<?php echo STRB_Classes_Helpers_Sanitize::escapeKeyword(STRB_Classes_Helpers_Tools::getValue('sfeature')) ?>"/>
                            <?php if (STRB_Classes_Helpers_Tools::getIsset('sfeature')) { ?>
                                <a href="<?php echo STRB_Classes_Helpers_Tools::getAdminUrl(STRB_Classes_Helpers_Tools::getValue('page', 'strb_features')) ?>" class="strb_search_close">X</a>
                            <?php } ?>
                            <input type="submit" class="btn btn-primary m-0 px-4" value="<?php echo esc_html__("Search Feature", 'starbox') ?> >"/>
                        </form>
                    </div>
                </div>
            </div>
        </div>


        <div class="row row-cols-1 row-cols-md-3 p-0 m-0" style="max-width: 1200px;">
            <?php
            $current_category = '';

            //Search in the features
            if (STRB_Classes_Helpers_Tools::getIsset('sfeature')) {
                foreach ($features as &$feature) {

                    $feature['relevant'] = 0;

                    if(isset($feature['show']) && !$feature['show']) {
                        continue;
                    }

                    $sfeatures = STRB_Classes_Helpers_Tools::getValue('sfeature');
                    $sfeatures = explode(' ', $sfeatures);
                    if ($sfeatures && !empty($sfeatures)) {
                        $found = 0;

                        foreach ($sfeatures as $sfeature) {
                            if ($sfeature <> '') {
                                if (stripos($feature['title'], $sfeature) !== false) $found++;
                                if (stripos($feature['description'], $sfeature) !== false) $found++;
                                if (isset($feature['keywords']) && $feature['keywords'] <> ''){
                                    $keywords = explode(',',$feature['keywords']);
                                    if(!empty($keywords) && in_array($sfeature, $keywords)) {
                                        $found++;
                                    }
                                }
                            }
                        }

                        if (!$found) {
                            $feature['show'] = false;
                        }else{
                            $feature['relevant'] = $found;
                        }

                    }
                }
                usort($features, function ($a, $b) {
                    return ($a['relevant'] > $b['relevant']) ? -1 : 1;
                });

            }

            foreach ($features as $index => $feature) {

                if(isset($feature['show']) && !$feature['show']) {
                    continue;
                }

                $class = 'auto';
                if ($feature['active']) {
                    $class = 'active';
                } else {
                    $class = '';
                }
                ?>
                <?php if($feature['category'] <> $current_category){
                    if(!STRB_Classes_Helpers_Tools::getIsset('sfeature')){
                        $current_category = $feature['category']; ?>
                        <div class="col-12 p-0 m-0 pt-5" data-id="<?php echo md5($feature['category']) ?>"><h5 class="font-weight-bold"><?php echo esc_html($feature['category']) ?></h5></div>
                    <?php }?>
                <?php }?>
                <div class="col-3 p-0 pr-1 mb-3">
                    <div id="strb_feature_<?php echo (int)$index ?>" class="strb_feature card h-100 p-0 shadow-0 rounded-0 <?php echo esc_attr($class) ?>">
                        <div class="card-body m-0 p-0">
                            <div class="mx-3 mt-4 p-0">
                                <div class="col p-0 mb-3 d-flex align-items-center">
                                    <i class="<?php echo $feature['logo']  ?>"></i>
                                </div>
                                <div class="col-12 d-flex align-items-center m-0 p-0">
                                    <h5 class="p-0 m-0 font-weight-bold">
                                        <a href="<?php echo esc_url($feature['link']) ?>" class="text-dark" style="text-decoration: none"><?php echo wp_kses_post($feature['title']) ?></a>
                                        <?php if ($feature['details']) { ?>
                                            <a href="<?php echo esc_url($feature['details']) ?>" target="_blank">
                                                <i class="fa-solid fa-question-circle m-0 pl-1" style="display: inline; font-size: 16px !important;"></i>
                                            </a>
                                        <?php } ?>
                                    </h5>
                                </div>
                            </div>
                            <div class="m-3 p-0 text-black" style="min-height: 80px; font-size: 16px;">
                                <div class="pt-3 pb-1 small" style="color: #696868">
                                    <?php echo wp_kses_post($feature['description']) ?>
                                    <?php if ($feature['link']) { ?>
                                        <div class="col-12 p-0 pt-2">
                                            <?php if ($feature['optional']) { ?>
                                                <a href="<?php echo esc_url($feature['link']) ?>" class="small see_feature" <?php echo($feature['active'] ? '' : 'style="display:none;"') ?>>
                                                    <?php echo esc_html__("start feature setup", 'starbox') ?> >>
                                                </a>
                                            <?php } else { ?>
                                                <a href="<?php echo esc_url($feature['link']) ?>" class="small see_feature">
                                                    <?php echo esc_html__("see feature", 'starbox') ?> >>
                                                </a>
                                            <?php } ?>
                                        </div>
                                    <?php } ?>

                                </div>
                            </div>
                        </div>
                        <div class="card-footer p-0 m-0 bg-white border-0">
                            <div class="row m-0 p-0">
                                <div class="col-12 px-3 py-1 m-0 align-middle text-left" style="line-height: 30px">
                                    <?php if ($feature['optional']) { ?>
                                        <div class="checker row m-0 p-0 px-1 strb_save_ajax">
                                            <div class="col-12 p-0 sq-switch sq-switch-xxs">
                                                <input type="checkbox" id="activate_<?php echo (int)$index ?>" <?php echo($feature['active'] ? 'checked="checked"' : '') ?> data-name="<?php echo esc_attr($feature['option']) ?>" data-action="strb_ajax_seosettings_save" data-javascript="if($value){$this.closest('div.strb_feature').addClass('active');$('#strb_feature_<?php echo (int)$index ?>').find('.strb_feature_deactive').hide();$('#strb_feature_<?php echo (int)$index ?>').find('.strb_feature_active').show();$('#strb_feature_<?php echo (int)$index ?>').find('a.see_feature').show();}else{ $this.closest('div.strb_feature').removeClass('active');$('#strb_feature_<?php echo (int)$index ?>').find('.strb_feature_deactive').show();$('#strb_feature_<?php echo (int)$index ?>').find('.strb_feature_active').hide();$('#strb_feature_<?php echo (int)$index ?>').find('a.see_feature').hide();}" class="switch" value="1"/>
                                                <label for="activate_<?php echo (int)$index ?>" class="m-0 font-weight-light"><span class="strb_feature_active" <?php echo($feature['active'] ? '' : 'style="display:none"') ?>><?php echo esc_html__("click to deactivate", 'starbox') ?></span><span class="strb_feature_deactive" <?php echo($feature['active'] ? 'style="display:none"' : '') ?>><?php echo esc_html__("click to activate", 'starbox') ?></span></label>
                                            </div>
                                        </div>
                                    <?php } else {
                                        if ($feature['connection'] && !STRB_Classes_Helpers_Tools::getOption('strb_api')) { ?>
                                            <div class="pt-1 m-0 align-middle text-left">
                                                <a href="<?php echo STRB_Classes_Helpers_Tools::getAdminUrl('strb_settings') ?>" class="small text-black-50"><?php echo esc_html__("connect to cloud", 'starbox') ?></a>
                                            </div>
                                        <?php } elseif ($feature['active']) { ?>
                                            <div class="pt-1 m-0 align-middle text-left">
                                                <a href="<?php echo esc_url($feature['link']) ?>" class="small text-primary"><?php echo esc_html__("already active", 'starbox') ?></a>
                                            </div>
                                        <?php } else { ?>
                                            <div class="pt-1 m-0 align-middle text-left">
                                                <a href="<?php echo esc_url($feature['link']) ?>" class="small"><?php echo esc_html__("activate feature", 'starbox') ?></a>
                                            </div>
                                        <?php } ?>
                                    <?php } ?>

                                </div>
                            </div>
                        </div>
                    </div>


                </div>
            <?php } ?>
        </div>

       <div class="col-12 p-0 m-0 pt-5 align-middle text-center">
           <h5>
               <a href="https://www.squirrly.co/wordpress/plugins/seo/" target="_blank">
                   <?php if (STRB_Classes_Helpers_Tools::getIsset('sfeature')) { ?>
                       <?php echo esc_html__("Do you want to search in the 650 features list?", 'starbox') ?>
                   <?php } else { ?>
                       <?php echo esc_html__("Do you want to see all 650 features list?", 'starbox') ?>
                   <?php } ?>
               </a>
           </h5>
       </div>
    <?php }?>
</div>
