<?php
namespace Elementor;

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


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

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

    public function get_title() {
        return esc_html__( 'Post Slider', 'ele-blog' );
    }

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

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

    protected function _register_controls() {


    // ----------------------------------------  content ------------------------------
     $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' => 4
            ]
        );


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

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

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

            ]
        );


        $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

        //meta settings
        $this->start_controls_section(
            'meta_settings',
            [
                'label' => esc_html__( 'Meta Settings', 'ele-blog' ),
            ]
        );

        $this->add_control(
            'enable_meta',
            [
                'label' => esc_html__( 'Meta', '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(
            'metaposition', [
                'label'   => esc_html__( 'Meta Position', 'ele-blog' ),
                'type'    => Controls_Manager::SELECT,
                'options' => array(
                    'before' => esc_html__( 'Before Title', 'ele-blog' ),
                    'after'  => esc_html__( 'After Title', 'ele-blog' ),
                ),
                'default' => 'before'

            ]
        );


        $this->add_control(
            'enable_author',
            [
                'label' => esc_html__( 'Display Author Name', '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',
                'condition'=>[
                    'enable_meta' => 'yes'
                ]
            ]
        );

        $this->add_control(
            'enable_date',
            [
                'label' => esc_html__( 'Display Date', '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',
                'condition'=>[
                    'enable_meta' => 'yes'
                ]
            ]
        );

        $this->add_control(
            'enable_comment',
            [
                'label' => esc_html__( 'Display Comment Number', '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' => 'no',
                'condition'=>[
                    'enable_meta' => 'yes'
                ]
            ]
        );

        $this->add_control(
            'enable_category',
            [
                'label' => esc_html__( 'Display Category', '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' => 'no',
                'condition'=>[
                    'enable_meta' => 'yes'
                ]
            ]
        );



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


        //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' => '#fff',
                '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();

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

    $this->add_control(
        'meta_color',
        [
            'label' => esc_html__( 'Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#fff',
            'selectors' => [
                '{{WRAPPER}} .eblog-meta-inner li, .eblog-meta-inner li a ' => 'color: {{VALUE}}',
            ],
        ]
    );

    $this->add_control(
        'meta_icon_color',
        [
            'label' => esc_html__( 'Icon Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#fff',
            'selectors' => [
                '{{WRAPPER}} .eblog-meta-inner li svg' => 'color: {{VALUE}}',
            ],
        ]
    );


    //meta typography
    $this->add_group_control(
        Group_Control_Typography::get_type(),
        [
            'name'           => 'meta_typography',
            'label'          => esc_html__( 'Meta Typography', 'ele-blog' ),
            'selector'       => '{{WRAPPER}} .eblog-meta-inner li',
            'fields_options' => [
                // first mimic the click on Typography edit icon
                'typography'  => [ 'default' => 'yes' ],
                // then redifine the Elementor defaults
                'font_size'   => [ 'default' => [ 'size' => 13 ] ],
                'font_weight' => [ 'default' => 400 ],
                'line_height' => [ 'default' => [ 'size' => 21 ] ],
            ],
        ]
    );

     $this->end_controls_section();

    //slider button color
    $this->start_controls_section(
        'slider_button_style', [
            'label' => esc_html__( 'Slider Button Style', 'ele-blog' ),
            'tab' => Controls_Manager::TAB_STYLE,
        ]
    );

    $this->add_control(
        'button_icon_color',
        [
            'label' => esc_html__( 'Icon Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#1368ab',
            'selectors' => [
                '{{WRAPPER}} .slider-control-base button' => 'color: {{VALUE}};border-color:{{VALUE}}',
            ],
        ]
    );  

    $this->add_control(
        'button_hover_color',
        [
            'label' => esc_html__( 'Hover Color', 'ele-blog' ),
            'type' => Controls_Manager::COLOR,
             'default' => '#1368ab',
            'selectors' => [
                '{{WRAPPER}} .slider-control-base button:hover' => 'background-color: {{VALUE}};',
            ],
        ]
    );


     $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-8">
        <div class="eblog-container">
            <div class="eblog-slider-1 slider-control-base owl-carousel">
                <?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, '' );

                ?>
                    <div class="item">
                        <div class="eblog-single-inner style-7">
                            <div class="icon-img">
                                <img src="<?php echo esc_url( ele_blog_img_resize( $src[0], 376, 396, false ) ); ?>"
                                 alt="<?php echo esc_attr( $image_alt ); ?>">
                            </div>
                            <div class="content-box">
                                
                            <?php 
                            //enable meta
                             if( 'yes' == $settings[ 'enable_meta' ] && 'before' == $settings[ 'metaposition' ] ):
                            ?>
                                <ul class="eblog-meta-inner">
                                    <?php if( 'yes' == $settings[ 'enable_author' ] ) : ?>
                                        <li><i class="fa fa-user"></i><?php the_author(); ?></li>
                                    <?php endif; ?>
                                    <?php if( 'yes' == $settings[ 'enable_date' ] ) : ?>
                                        <li><i class="fa fa-clock"></i>
                                            <?php the_time( get_option( 'date_format' ) ); ?>
                                        </li>
                                    <?php endif; ?>
                                     <?php if( 'yes' == $settings[ 'enable_comment' ]  ) : ?>
                                        <li><i class="fa fa-comment-alt"></i>
                                            <?php comments_popup_link(); ?>
                                        </li>
                                    <?php endif; ?>
                                    <?php if( 'yes' == $settings[ 'enable_category' ] ) : ?>
                                        <li><i class="fa fa-tag"></i><?php the_category(', '); ?></li>
                                    <?php endif; ?>
                                </ul>
                            <?php endif; ?>

                            <?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 endif; // end title 

                            //enable meta
                             if( 'yes' == $settings[ 'enable_meta' ] && 'after' == $settings[ 'metaposition' ] ):
                            ?>
                                <ul class="eblog-meta-inner">
                                    <?php if( 'yes' == $settings[ 'enable_author' ] ) : ?>
                                        <li><i class="fa fa-user"></i><?php the_author(); ?></li>
                                    <?php endif; ?>
                                    <?php if( 'yes' == $settings[ 'enable_date' ] ) : ?>
                                        <li><i class="fa fa-clock"></i>
                                            <?php the_time( get_option( 'date_format' ) ); ?>
                                        </li>
                                    <?php endif; ?>
                                     <?php if( 'yes' == $settings[ 'enable_comment' ]  ) : ?>
                                        <li><i class="fa fa-comment-alt"></i>
                                            <?php comments_popup_link(); ?>
                                        </li>
                                    <?php endif; ?>
                                    <?php if( 'yes' == $settings[ 'enable_category' ] ) : ?>
                                        <li><i class="fa fa-tag"></i><?php the_category(', '); ?></li>
                                    <?php endif; ?>
                                </ul>
                            <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endwhile; wp_reset_postdata(); endif; ?>
            </div>
        </div>
    </section> 

    <?php    

    }
    

}

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