<?php
namespace Datacake\Core\Shortcodes;

use Datacake\Core\Utils;


class ChannelShortCode {
    public function __construct()
    {
        $this->utils = new Utils();
    }

    public function shortcode_canais($attrs = null){

        $opt = shortcode_atts(array(
            'id' => "lista-de-canais",
            'list_class' => 'g-cols via_flex valign_top type_default',
            'list_item_class' => 'vc_col-sm-2 vc_col-xs-6 wpb_column vc_column_container',
            'search' => true,
            'default_image' => "/wp-content/plugins/datacake-core/src/Pub/images/tv-icon.png"
        
        ), $attrs);
        $id = $opt['id'];
        $default_image = $opt['default_image'];
        $list_class = $opt['list_class'];
        $search_on = $opt['search'];
        $list_item_class = $opt['list_item_class'];
 

        $html = "";
        
       
        // Pegando Canais
        $canais = get_posts([
            'numberposts' => -1, // gett all posts
            'post_type' => 'isp_canais',
            'meta_key' => 'isp_number', //name of meta field
            'orderby' => 'isp_number', 
            'order' => 'ASC',
           
        ]);

        //print_r($canais);
        $html = "";
        
        
        $parent_cat_id = term_exists( 'Grupos','isp_canais_category')['term_id']; 
        $parent_produto_id = term_exists( 'Planos','isp_canais_category')['term_id']; 
      
        $tag_list = [];
        $cat_list = [];
        $plan_list = [];
        $html_channels = ""; 
        if (!empty($canais)) {
             foreach ($canais as $key => $canal) {
                
                $image = get_post_meta($canal->ID,'isp_logo');
                if($image && $image[0]){
                    $image = $image[0];
                }else{
                    $image = $default_image;
                }
                $number= get_post_meta($canal->ID,'isp_number');
                if($number && $number[0]){
                    $number = $number[0];
                }else{
                    $number = "N/A";
                }
                $tags_slugs = "";
                $tags = get_the_terms($canal->ID, 'isp_canais_tag');
                if($tags){
                    foreach($tags as $t){
                        $tag_list[$t->slug] = $t->name;
                        $tags_slugs = "$tags_slugs,$t->slug";
                    }
                }
                $prod_slugs = "";
                $cat_slugs = "";
                $categories = get_the_terms($canal->ID, 'isp_canais_category');
                if($categories){
                    foreach($categories as $c){
                        if($c->parent == $parent_cat_id){
                            $cat_list[$c->slug] = $c->name;
                            $cat_slugs = "$cat_slugs,$c->slug";
                        } else if($c->parent == $parent_produto_id){
                            $plan_list[$c->slug] = $c->name;
                            $prod_slugs = "$prod_slugs,$c->slug";
                        }
                    }
                }
                

                $html_channels .= "<div class='$list_item_class $canal->ID $canal->post_name text-center valign_top channel' data-prods='$prod_slugs' data-cats='$cat_slugs' data-tags='$tags_slugs'
                 data-number='$number' data-name='$canal->post_title' >
                <img alt='Imagem do $canal->post_title' src='$image' class='channel-image' id='channel-image-$canal->ID'>
                $canal->post_title<br>$number
                </div>";

            }
        }
      
        // Gerando HTML Final
        if($search_on){
            $html .= "<div id='search_$id' class='compartivo_canais_busca g-cols via_flex valign_top type_default' >
                    <div class='vc_col-sm-3 wpb_column vc_column_container' id='in-canal-type-container'>
                        <select name='in-canal-type' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required in-canal-type' id='in-canal-type' aria-required='true' aria-invalid='false'>
                            <option value=''>Categorias</option>";
                            foreach ($cat_list as $key => $value) {
                                $selected = isset($_GET['isp_category']) && $_GET['isp_category'] == $key ? 'selected' : '';
                                $html .=  "<option value='$key' $selected>$value</option>";
                            }
            $html .=    "</select>
                    </div>
                    <div class='vc_col-sm-3 wpb_column vc_column_container' id='in-canal-tag-container'>
                        <select name='in-canal-tag' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required in-canal-tag' id='in-canal-tag' aria-required='true' aria-invalid='false'>
                            <option value=''>Tags</option>";
                            foreach ($tag_list as $key => $value) {
                                $selected = isset($_GET['isp_tag']) &&  $_GET['isp_tag'] == $key ? 'selected' : '';
                                $html .=  "<option value='$key' $selected>$value</option>";
                            }
            $html .=    "</select>
                    </div>
                    <div class='vc_col-sm-3 wpb_column vc_column_container' id='in-canal-plano-container'>
                        <select name='in-canal-type' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required in-canal-plano' id='in-canal-plano' aria-required='true' aria-invalid='false'>
                            <option value=''>Plano</option>";
                            foreach ($plan_list as $key => $value) {
                                $selected = isset($_GET['isp_plan_sku']) &&  $_GET['isp_plan_sku'] == $key ? 'selected' : '';
                                $html .=  "<option value='$key' $selected>$value</option>";
                            }
            $html .=    "</select>
                    </div>
                    <div class='vc_col-sm-3 wpb_column vc_column_container' id='in-canal-search-container'>
                        <input type='search' placeholder='Buscar' name='in-canal-search' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required in-canal-search' id='in-canal-search' aria-required='true' aria-invalid='false' autocomplete='off'/>
                    </div>
                </div>";
        }
        $html .= "<div id=\"$id\" class=\"wpb_row channel-list\"><div id='dk_list_class' class=\"$list_class\">";
        $html .= $html_channels;
        $html .= "</div></div>";
        return $html;
        
    }
}