<?php
namespace Elementor;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}


/**
 *
 * ele-blog elementor Contact widget.
 *
 * @since 1.0
 */
class Ele_Blog_Style_Seven extends Widget_Base {

    public function get_name() {
        return 'style_seven';
    }

    public function get_title() {
        return esc_html__( 'Category Filter', 'ele-blog' );
    }

    public function get_icon() {
        return 'eicon-post-list';
    }

    public function get_categories() {
        return ['ele-blog'];
    }

    protected function _register_controls() {


    // generel settings
     $this->start_controls_section(
            'ele_style_one_settings',
            [
                'label' => esc_html__( 'Generel Settings', 'ele-blog' ),
            ]
        );

        
        $this->add_control(
            'ppr', [
                'label'   => esc_html__( 'Amount of post to display', 'ele-blog' ),
                'type'    => Controls_Manager::TEXT,
                'default' => 6
            ]
        );

        $this->add_control(
            'select_cat', [
                'label'    => esc_html__( 'Select Category', 'ele-blog' ),
                'type'     => Controls_Manager::SELECT2,
                'description'=>esc_html__( 'Select Category To display Filter Option', 'ele-blog' ),
                'multiple' => true,
                'options'  => ele_blog_post_category(),

            ]
        );

        $this->end_controls_section(); // End generel settings

        $this->start_controls_section(
            'Post_filter_settings',
            [
                'label' => esc_html__( 'Post Filter', 'ele-blog' ),
            ]
        );


        $this->add_control(
            'exclude_cat', [
                'label'    => esc_html__( 'Exclude Category', 'ele-blog' ),
                'type'     => Controls_Manager::SELECT2,
                'multiple' => true,
                'options'  => ele_blog_post_category(),
            ]
        );

        $this->add_control(
            'select_tag', [
                'label'    => esc_html__( 'Select tag', 'ele-blog' ),
                'type'     => Controls_Manager::SELECT2,
                'multiple' => true,
                'options'  => ele_blog_post_tag(),
            ]
        );

        $this->add_control(
            'orderby', [
                'label'   => esc_html__( 'Order by', 'ele-blog' ),
                'type'    => Controls_Manager::SELECT2,
                'options' => array(
                    'author' => esc_html__( 'Author', 'ele-blog' ),
                    'title'  => esc_html__( 'Title', 'ele-blog' ),
                    'date'   => esc_html__( 'Date', 'ele-blog' ),
                    'rand'   => esc_html__( 'Random', 'ele-blog' ),
                ),
                'default' => 'date'

            ]
        );

        $this->add_control(
            'order', [
                'label'   => esc_html__( 'Order', 'ele-blog' ),
                'type'    => Controls_Manager::SELECT2,
                'options' => array(
                    'desc' => esc_html__( 'DESC', 'ele-blog' ),
                    'asc'  => esc_html__( 'ASC', 'ele-blog' ),
                ),
                'default' => 'desc'

            ]
        );

  
        $this->end_controls_section(); // End Contact content

        //title settings
        $this->start_controls_section(
            'tittle_settings',
            [
                'label' => esc_html__( 'Title Settings', 'ele-blog' ),
            ]
        );

         $this->add_control(
            'dtitle', [
                'label'   => esc_html__( 'Display', 'ele-blog' ),
                'type'    => Controls_Manager::SELECT2,
                'options' => array(
                    'full' => esc_html__( 'Full Title', 'ele-blog' ),
                    'excerpt'  => esc_html__( 'Excerpt', 'ele-blog' ),
                    'hide'  => esc_html__( 'Not Display', 'ele-blog' ),
                ),
                'default' => 'full'

            ]
        );

        $this->add_control(
            'title_excerpt_length', [
                'label'   => esc_html__( 'Title Excerpt Length', 'ele-blog' ),
                'type'    => Controls_Manager::TEXT,
                'default' => 3,
                'condition' =>[
                    'dtitle'=>'excerpt'
                ]
            ]
        );

        $this->add_control(
            'titltag', [
                'label'   => esc_html__( 'Title Tag', 'ele-blog' ),
                'type'    => Controls_Manager::SELECT,
                'condition'=>[
                    'dtitle!' => 'hide'
                ],
                'options' => array(
                    'h1' => 'H1',
                    'h2' => 'H2',
                    'h3' => 'H3',
                    'h4' => 'H4',
                    'h5' => 'H5',
                    'h6' => 'H6',
                ),
                'default' => 'h2'

            ]
        );

        $this->end_controls_section(); // End title

        // content settings
        $this->start_controls_section(
            'conent_settings',
            [
                'label' => esc_html__( 'Content Settings', 'ele-blog' ),
            ]
        );

         $this->add_control(
            'dcontent', [
                'label'   => esc_html__( 'Display', 'ele-blog' ),
                'type'    => Controls_Manager::SELECT,
                'options' => array(
                    'full' => esc_html__( 'Full Content', 'ele-blog' ),
                    'excerpt'  => esc_html__( 'Excerpt', 'ele-blog' ),
                    'hide'  => esc_html__( 'Hide', 'ele-blog' ),
                ),
                'default' => 'excerpt'

            ]
        );


        $this->add_control(
            'excerpt_length', [
                'label'   => esc_html__( 'Excerpt Length', 'ele-blog' ),
                'type'    => Controls_Manager::TEXT,
                'default' => 15,
                'condition' =>[
                    'dcontent'=>'excerpt'
                ]
            ]
        );

        $this->end_controls_section(); // End  content settings

        // read more button
        $this->start_controls_section(
            'readmore_settings',
            [
                'label' => esc_html__( 'Read More Settings', 'ele-blog' ),
            ]
        );

        $this->add_control(
            'enable_readmore',
            [
                'label' => esc_html__( 'Display Read More', 'ele-blog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Yes', 'ele-blog' ),
                'label_off' => esc_html__( 'no', 'ele-blog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'readmorestyle', [
                'label'   => esc_html__( 'Read More Style', 'ele-blog' ),
                'condition' =>[
                    'enable_readmore' => 'yes'
                ],
                'type'    => Controls_Manager::SELECT,
                'options' => array(
                    'readmore-text' => esc_html__( 'Normal Text', 'ele-blog' ),
                    'readmore-btn'  => esc_html__( 'Button', 'ele-blog' ),
                ),
                'default' => 'readmore-text'

            ]
        );

        $this->add_control(
            'btntext', [
                'label'   => esc_html__( 'Button Text', 'ele-blog' ),
                'condition' =>[
                    'enable_readmore' => 'yes'
                ],
                'type'    => Controls_Manager::TEXT,
                'default' => esc_html__( 'Read More','ele-blog' ),
            ]
        );

        $this->add_control(
            'enable_btn_icon',
            [
                'label' => esc_html__( 'Display Button Icon', 'ele-blog' ),
                'condition' =>[
                      'enable_readmore' => 'yes',                    
                ],
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Yes', 'ele-blog' ),
                'label_off' => esc_html__( 'no', 'ele-blog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'icon',
            [
                'label' => esc_html__( 'Button Icon', 'ele-blog' ),
                'condition' =>[
                      'enable_readmore' => 'yes',                    
                      'enable_btn_icon' => 'yes',                    
                ],
                'type' => \Elementor\Controls_Manager::ICON,
                'default' => 'fa fa-angle-right',
            ]
        );


        $this->end_controls_section(); // End readmore 


      //title style
        $this->start_controls_section(
            'title_style', [
                'label' => esc_html__( 'Title Style', 'ele-blog' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => esc_html__( 'Title Color', 'ele-blog' ),
                'type' => Controls_Manager::COLOR,
                 'default' => '#222',
                'selectors' => [
                    '{{WRAPPER}} .eblog-single-inner .inner-title a' => 'color: {{VALUE}}',
                ],
            ]
        );

       $this->add_control(
            'title_hover_color',
            [
                'label' => esc_html__( 'Title Hover Color', 'ele-blog' ),
                'type' => Controls_Manager::COLOR,
                 'default' => '#1368ab',
                'selectors' => [
                    '{{WRAPPER}} .eblog-single-inner .inner-title a:hover' => 'color: {{VALUE}}',
                ],
            ]
        );

        //title typography
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'title_typography',
                'label'          => esc_html__( 'Title Typography', 'ele-blog' ),
                'selector'       => '{{WRAPPER}} .eblog-single-inner .inner-title a',
                'fields_options' => [
                    // first mimic the click on Typography edit icon
                    'typography'  => [ 'default' => 'yes' ],
                    // then redifine the Elementor defaults
                    'font_size'   => [ 'default' => [ 'size' => 24 ] ],
                    'font_weight' => [ 'default' => 700 ],
                    'line_height' => [ 'default' => [ 'size' => 29 ] ],
                ],
            ]
        );

     $this->end_controls_section();

    //read more style
    $this->start_controls_section(
        'read_style', [
            'label' => esc_html__( 'Read More Style', 'ele-blog' ),
            'tab' => Controls_Manager::TAB_STYLE,
            'condition'=>[
                'enable_readmore'=>'yes'
            ]
        ]
    );

    $this->add_control(
        'read_more_btn_color',
        [
            'label' => esc_html__( 'Button Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#484848',
            'selectors' => [
                '{{WRAPPER}} .eblog-single-inner .readmore-btn' => 'color: {{VALUE}}',
            ],
            'condition'=>[
                'readmorestyle'=>'readmore-btn',
            ],
        ]
    );  

    $this->add_control(
        'read_more_btn_hover_color',
        [
            'label' => esc_html__( 'Button Hover Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#fff',
            'selectors' => [
                '{{WRAPPER}} .eblog-single-inner .readmore-btn:hover' => 'color: {{VALUE}}',
            ],
            'condition'=>[
                'readmorestyle'=>'readmore-btn',
            ],
        ]
    );

    $this->add_control(
        'read_more_btn_bg_color',
        [
            'label' => esc_html__( 'Button Background Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => 'transparent',
            'selectors' => [
                '{{WRAPPER}} .eblog-single-inner .readmore-btn' => 'background-color: {{VALUE}}',
            ],
            'condition'=>[
                'readmorestyle'=>'readmore-btn',
            ],
        ]
    );


    $this->add_control(
        'read_more_btn_hover_bg_color',
        [
            'label' => esc_html__( 'Button Hover Background Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#1368ab',
            'selectors' => [
                '{{WRAPPER}} .eblog-single-inner .readmore-btn:hover' => 'background-color: {{VALUE}};border-color:{{VALUE}}',
            ],
            'condition'=>[
                'readmorestyle'=>'readmore-btn',
            ],
        ]
    );


     $this->add_control(
        'read_more_text_color',
        [
            'label' => esc_html__( 'Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#484848',
            'selectors' => [
                '{{WRAPPER}} .readmore-text' => 'color: {{VALUE}}',
            ],
            'condition'=>[
                'readmorestyle'=>'readmore-text',
            ],
        ]
    ); 

   //read more typography
    $this->add_group_control(
        Group_Control_Typography::get_type(),
        [
            'name'           => 'read_more_text_typography',
            'label'          => esc_html__( 'Typography', 'ele-blog' ),
            'selector'       => '{{WRAPPER}} .eblog-single-inner .readmore-text',
            'fields_options' => [
                // first mimic the click on Typography edit icon
                'typography'  => [ 'default' => 'yes' ],
                // then redifine the Elementor defaults
                'font_size'   => [ 'default' => [ 'size' => 15 ] ],
                'font_weight' => [ 'default' => 400 ],
                'line_height' => [ 'default' => [ 'size' => 30 ] ],
            ],
            'condition'=>[
                'readmorestyle'=>'readmore-text',
            ],
        ]
    ); 

    $this->end_controls_section();

    //filter hover color
    $this->start_controls_section(
        'filter_btn_style', [
            'label' => esc_html__( 'Filter Button Style', 'ele-blog' ),
            'tab' => Controls_Manager::TAB_STYLE,
        ]
    );

    $this->add_control(
        'filter_btn_color',
        [
            'label' => esc_html__( 'Button  Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#4a4a4a',
            'selectors' => [
                '{{WRAPPER}} .eblog-masonry-btn .button' => 'color: {{VALUE}}',
            ],
        ]
    );

    $this->add_control(
        'filter_btn_bg_color',
        [
            'label' => esc_html__( 'Button bg Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#ddd',
            'selectors' => [
                '{{WRAPPER}} .eblog-masonry-btn .button' => 'background-color: {{VALUE}}',
            ],
        ]
    );

     $this->add_control(
        'filter_btn_active',
        [
            'label' => esc_html__( 'Active/Hover  Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#fff',
            'selectors' => [
                '{{WRAPPER}} .eblog-masonry-btn .button.is-checked,
                .eblog-masonry-btn .button:hover,.eblog-masonry-btn .button:hover' => 'color: {{VALUE}}',
            ],
        ]
    );

     $this->add_control(
        'filter_btn_bg_active',
        [
            'label' => esc_html__( 'Active/Hover bg Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#3d6bb7',
            'selectors' => [
                '{{WRAPPER}} .eblog-masonry-btn .button.is-checked,
                .eblog-masonry-btn .button:hover,.eblog-masonry-btn .button:hover' => 'background-color: {{VALUE}} !important',
            ],
        ]
    ); 

      //btn typography
    $this->add_group_control(
        Group_Control_Typography::get_type(),
        [
            'name'           => 'filter_btn_typography',
            'label'          => esc_html__( 'Typography', 'ele-blog' ),
            'selector'       => '{{WRAPPER}} .eblog-masonry-btn .button',
            'fields_options' => [
                // first mimic the click on Typography edit icon
                'typography'  => [ 'default' => 'yes' ],
                // then redifine the Elementor defaults
                'font_size'   => [ 'default' => [ 'size' => 16 ] ],
                'font_weight' => [ 'default' => 500 ],
                'line_height' => [ 'default' => [ 'size' => 40 ] ],
            ],
        ]
    ); 

    $this->end_controls_section();

        
    }

    protected function render() {

    $settings = $this->get_settings();

    $args  = array(
        'post_type'           => 'post',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => $settings['ppr'],
    );

    $args['orderby'] = $settings['orderby'];
    $args['order']   = $settings['order'];
    if ( ! empty( $settings['exclude_cat'] ) ) {
        $args['category__not_in'] = $settings['exclude_cat'];
    }


    if ( ! empty( $settings['select_cat'] ) ) {
        $catgory             = implode( ', ', $settings['select_cat'] );
        $args['tax_query'][] = array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => array_values( $settings['select_cat'] )
        );
    }

    if ( ! empty( $settings['select_tag'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'post_tag',
            'field'    => 'id',
            'terms'    => array_values( $settings['select_tag'] )
        );
    }

    $posts_query = new \WP_Query( $args );

    ?>

    <section class="eblog-area style-11">
        <div class="eblog-container">
             <div class="eblog-masonry-btn button-group filters-button-group">
                <button class="button is-checked" data-filter="*"><?php echo esc_html__( 'All', 'ele-blog' ); ?></button>
                <?php
                if( is_array( $settings[ 'select_cat' ] ) ) :
                   foreach ( $settings['select_cat'] as $term ) :
                ?>
                
                <button class="button" data-filter=".<?php echo strtolower(preg_replace('/[^a-zA-Z]+/', '-', eleblog_get_cat_slug( $term ) ) ); ?>"><?php echo get_cat_name( $term ); ?></button>
                <?php endforeach;
                else: 
                    $taxonomy = 'category';
                    $terms = get_terms($taxonomy); // Get all terms of a taxonomy
                    if ( $terms && !is_wp_error( $terms ) ) :
                        foreach ( $terms as $term ) { ?>
                <button class="button" data-filter=".<?php echo  strtolower(preg_replace('/[^a-zA-Z]+/', '-', $term->name)); ?>"><?php echo esc_html($term->name); ?></button>
                <?php } endif; endif; ?>

            </div>
            <div id="grid-container" style="height: 100%;" class="eblog-masonry-inner masonry js-masonry grid eblog-row">
                <?php 
                if ( $posts_query->have_posts() ):
                while ( $posts_query->have_posts() ): $posts_query->the_post();

                $img_id    = get_post_thumbnail_id( get_the_ID() );
                $image_alt = get_post_meta( $img_id, '_wp_attachment_image_alt', true );
                $src       = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full', false, '' );

                 $terms = get_the_terms( get_the_ID(), 'category' );
                ?>
                <article 
                class="<?php foreach ($terms as $term){ echo strtolower(preg_replace('/[^a-zA-Z]+/', '-', $term->slug )).' '; } ?>">
                    <div class="eblog-single-inner style-4">
						<div class="eblog-single-inner-wrap">
							<?php if( has_post_thumbnail() ) : ?>
								<div class="icon-img">
									<img src="<?php echo esc_url( ele_blog_img_resize( $src[0], 364, 223, true ) ); ?>"
									 alt="<?php echo esc_attr( $image_alt ); ?>">
								</div> 
							<?php endif; ?>
							<div class="content-box">

							<?php 

							//check if not hide title
							if( 'hide' != $settings[ 'dtitle' ] ) :

							?>
							<<?php echo esc_attr( $settings[ 'titltag' ] ); ?> class="inner-title">

							<a href="<?php the_permalink(); ?>">

								<?php
								 if( 'full' == $settings[ 'dtitle' ] ) :

								  the_title();

								  else :
									echo wp_trim_words( get_the_title(), $settings[ 'title_excerpt_length' ], '' );
								 endif;
								 ?>

							</a>

							</<?php echo esc_attr( $settings[ 'titltag' ] ); ?>>
                            <?php 
                            // check if not hide content
                            if( 'hide' != $settings[ 'dcontent' ] ) :

                              //check if excerpt enable
                              if( 'excerpt' == $settings[ 'dcontent' ] ) : ?>
                                <p><?php echo wp_trim_words( get_the_content(), $settings[ 'excerpt_length'] , '' ); ?></p>
                              <?php else : 
                                the_content();
                               endif;
                             endif;
                            ?>
							<?php endif; // end title  

							//check if read more enable
								if( 'yes' == $settings[ 'enable_readmore' ] ) :
								?>

								<a class="<?php echo esc_attr( $settings[ 'readmorestyle' ] ); ?>" href="<?php the_permalink( ); ?>">

										<?php echo esc_html( $settings[ 'btntext' ] ); ?>
										<?php if( 'yes' == $settings[ 'enable_btn_icon' ] ) : ?>
											<i class="<?php echo esc_attr( $settings[ 'icon' ] ); ?>"></i>
										<?php endif; ?>
									</a>
								<?php endif; ?>

							</div>
                        </div>
                    </div>
                </article>
                <?php endwhile; wp_reset_postdata(); endif; ?>
            </div>
        </div>
    </section>  
       
    <?php    
    global $pagenow;
    if($pagenow =='admin-ajax.php'){
     ?>
     <script>

        jQuery( function() {
          var $grid = jQuery('.grid').isotope({
            itemSelector: 'article'
          });

          // filter buttons
          jQuery('.filters-button-group').on( 'click', 'button', function() {
            var filterValue = jQuery( this ).attr('data-filter');
            $grid.isotope({ filter: filterValue });
          });
          jQuery('.button-group').each( function( i, buttonGroup ) {
            var $buttonGroup = jQuery( buttonGroup );
            $buttonGroup.on( 'click', 'button', function() {
              $buttonGroup.find('.is-checked').removeClass('is-checked');
              jQuery( this ).addClass('is-checked');
            });
          });
        });
     </script>
 <?php }

    }
    

}

plugin::instance()->widgets_manager->register_widget_type(new Ele_Blog_Style_Seven());