<?php

namespace XIMSAPLUGIN\Element;

use Elementor\Controls_Manager;
use Elementor\Controls_Stack;
use Elementor\Group_Control_Typography;
use Elementor\Scheme_Typography;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Border;
use Elementor\Repeater;
use Elementor\Widget_Base;
use Elementor\Utils;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Plugin;

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

class crypto_blog_section extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * Retrieve Elementor widget name.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'crypto-blg-section';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'Crypto Blog & FAQ Section', 'ximsa' );
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve Elementor widget icon.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'gl-custom-icon';
	}

	/**
	 * Get widget categories.
	 *
	 * Retrieve the list of categories the Elementor widget belongs to.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'ximsa' ];
	}

	/**
	 * Register Elementor widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 * @access protected
	 */
	 
	
	protected function register_controls() {
        $this->start_controls_section(
            'crypto_post_options',
            [
                'label' => __( 'Crypto Post Options', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
			'bl_sub_title',
			[
				'label' => esc_html__( 'Feature Sub Title', 'ximsa' ),
				'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,
                'default' => esc_html__( 'Who We Are?', 'ximsa' ),
				'placeholder' => esc_html__( 'Type feature Sub Title Here', 'ximsa' ),
			]
		);
        $this->add_control(
			'bl_title',
			[
				'label' => esc_html__( 'Feature Title', 'ximsa' ),
				'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,
                'default' => esc_html__( 'Create your crypto portfolio today', 'ximsa' ),
				'placeholder' => esc_html__( 'Type feature Title Here', 'ximsa' ),
			]
		);

        $repeater = new \Elementor\Repeater();
        $repeater->add_control(
            'post_id', [
                'label'       => esc_html__( 'Select Post', 'ximsa' ),
                'type'        => Controls_Manager::SELECT,
                'label_block' => true,
                'options'     => $this->select_param_posts(),
            ]
        );
        
        $this->add_control(
			'postsitems',
			[
				'label' => esc_html__( 'Post List', 'ximsa' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
			]
		);
        $this->add_control(
			'view_all',
			[
				'label' => esc_html__( 'View Label', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $this->add_control(
			'view_link',
			[
				'label' => esc_html__( 'View Link', 'ximsa' ),
				'type' => Controls_Manager::URL,
                'label_block' => true,
			]
		);
        $this->end_controls_section();
        
        $this->start_controls_section(
            'faq_options',
            [
                'label' => __( 'FAQ Options', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
			'fq_sub_title',
			[
				'label' => esc_html__( 'Feature Sub Title', 'ximsa' ),
				'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,
                'default' => esc_html__( 'Who We Are?', 'ximsa' ),
				'placeholder' => esc_html__( 'Type feature Sub Title Here', 'ximsa' ),
			]
		);
        $this->add_control(
			'fq_title',
			[
				'label' => esc_html__( 'Feature Title', 'ximsa' ),
				'type' => Controls_Manager::TEXTAREA,
                'label_block' => true,
                'default' => esc_html__( 'Create your crypto portfolio today', 'ximsa' ),
				'placeholder' => esc_html__( 'Type feature Title Here', 'ximsa' ),
			]
		);
        $repeater = new \Elementor\Repeater();
        $repeater->add_control(
			'is_active',
			[
				'label' => esc_html__( 'Is Active', 'ximsa' ),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__( 'YES', 'ximsa' ),
				'label_off' => esc_html__( 'No', 'ximsa' ),
				'return_value' => 'yes',
				'default' => 'yes',
			]
		);
        $repeater->add_control(
			'title',
			[
				'label' => esc_html__( 'Title', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $repeater->add_control(
			'content',
			[
				'label' => esc_html__( 'Contetn', 'ximsa' ),
				'type' => Controls_Manager::WYSIWYG,
                'label_block' => true,
			]
		);
        
        $this->add_control(
			'tabsitems',
			[
				'label' => esc_html__( 'Tab Item', 'ximsa' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'title_field' => '{{{ title }}}',
			]
		);
        $this->add_control(
			'fqview_all',
			[
				'label' => esc_html__( 'View Label', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $this->add_control(
			'fqview_link',
			[
				'label' => esc_html__( 'View Link', 'ximsa' ),
				'type' => Controls_Manager::URL,
                'label_block' => true,
			]
		);
		$this->end_controls_section();


        $this->start_controls_section(
            'feature_settings_style',
            [
                'label' => __( 'feature Settings', 'ximsa' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'se__sub_title_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Sub Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );        
        $this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'sub_bgbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .sec-title-two .title',
			]
		);
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'se_sth_typography',
                'label'          => esc_html__( 'Sub Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .sec-title-two .title',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'se_title_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'se_ht_color',
            [
                'label'     => esc_html__( 'Heading Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sec-title-two h2' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'se_th_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .sec-title-two h2',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        
        $this->add_control(
            'ps_list_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Post Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'fs_list_cl',
            [
                'label'     => esc_html__( 'List Txt Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-news-block .inner-box h6 a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'ab_list_bg',
            [
                'label'     => esc_html__( 'Title Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-news-block .inner-box h6 a:hover' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'ps_title_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .crypto-news-block .inner-box h6 a',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'pss_meta_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Post Meta Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'post_meta_clor',
            [
                'label'     => esc_html__( 'Post Meta Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-news-block .inner-box .post-date' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'post_meta_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .crypto-news-block .inner-box .post-date',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'view_more_btn_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'View More Button Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'view_more_clor',
            [
                'label'     => esc_html__( 'View More Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-blog-section .all-faq, .crypto-blog-section .view-blog' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_control(
            'view_more_hover_clor',
            [
                'label'     => esc_html__( 'View More Hover Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-blog-section .all-faq:hover, .crypto-blog-section .view-blog:before, .crypto-blog-section .view-blog:hover, .crypto-blog-section .all-faq:before' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'view_more_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .crypto-blog-section .all-faq, .crypto-blog-section .view-blog',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'ct_title_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Counter Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'ct_title_cl',
            [
                'label'     => esc_html__( 'Counter Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-fact-counter .counter-column .inner .counter-title' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'ct_title_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .crypto-fact-counter .counter-column .inner .counter-title',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'tab_title_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Tab Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'tab_title_cl',
            [
                'label'     => esc_html__( 'Tab Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-tabs .tab-btns .tab-btn' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'tb_title_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .crypto-tabs .tab-btns .tab-btn',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'tab_content_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Tab Contetnt Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'tab_cont_title_cl',
            [
                'label'     => esc_html__( 'Tab Content Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-tabs .tabs-content h5' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'tb_content_title_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .crypto-tabs .tabs-content h5',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'tab_cont__cl',
            [
                'label'     => esc_html__( 'Tab Content Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crypto-tabs .tabs-content p' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'tb_content__typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .crypto-tabs .tabs-content p',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
		$this->end_controls_section();
	}
		


	protected function render() {
		
		
		$settings = $this->get_settings_for_display(); ?>
		<!-- Crypto Blog Section -->
        <section class="crypto-blog-section">
            <div class="auto-container">
                <div class="row clearfix">
                    
                    <!-- Column -->
                    <div class="column col-lg-6 col-md-12 col-sm-12">
                        <div class="sec-title-two">
                            <div class="title"><?php echo esc_html($settings['bl_sub_title']);?></div>
                            <h2><?php echo esc_html($settings['bl_title']);?></h2>
                        </div>
                        
                        <?php foreach($settings['postsitems'] as $index => $box): ?>
                        <?php  if( $box['post_id'] ) : ?>
                        <!-- Crypto News Block -->
                        <div class="crypto-news-block">
                            <div class="inner-box">
                                <div class="image">
                                    <a href="<?php echo get_the_permalink($box['post_id']);?>"><img src="<?php echo esc_url(get_the_post_thumbnail_url($box['post_id']));?>" alt="" /></a>
                                </div>
                                <div class="post-date"><?php echo get_the_date()?></div>
                                <h6><a href="<?php echo get_the_permalink($box['post_id']);?>"><?php echo get_the_title($box['post_id']);?></a></h6>
                            </div>
                        </div>
                        <?php endif; endforeach;?>
                        <a class="view-blog" href="<?php echo esc_html($settings['view_link']['url']);?>"><?php echo esc_html($settings['view_all']);?></a>
                        
                    </div>
                    <!-- Column -->
                    <div class="column col-lg-6 col-md-12 col-sm-12">
                        <div class="sec-title-two">
                            <div class="title"><?php echo esc_html($settings['fq_sub_title']);?></div>
                            <h2><?php echo esc_html($settings['fq_title']);?></h2>
                        </div>
                        
                        <!-- Crypto Info Tabs -->
                        <div class="crypto-info-tabs">
                            <!-- Crypto Tabs -->
                            <div class="crypto-tabs tabs-box">
                                
                                <!-- Tab Btns -->
                                <ul class="tab-btns tab-buttons clearfix">
                                    <?php foreach($settings['tabsitems'] as $item):?>
                                    <li data-tab="#prod-<?php echo esc_attr($item['_id']);?>" class="tab-btn <?php if($item['is_active'] == 'yes'){echo esc_attr('active-btn');}?> "><?php echo esc_html($item['title']);?></li>
                                    <?php endforeach;?>
                                </ul>
                                
                                <!-- Tabs Container -->
                                <div class="tabs-content">
                                    
                                    <!-- Tab / Active Tab -->
                                    <?php foreach($settings['tabsitems'] as $item):?>
                                    <div class="tab <?php if($item['is_active'] == 'yes'){echo esc_attr('active-tab');}?> " id="prod-<?php echo esc_attr($item['_id']);?>">
                                        <div class="content">
                                            <?php echo $item['content'];?>
                                        </div>
                                    </div>
                                    <?php endforeach;?>
                                </div>
                            </div>
                        </div>
                        <a class="all-faq" href="<?php echo esc_html($settings['fqview_link']['url']);?>"><?php echo esc_html($settings['fqview_all']);?></a>
                        
                    </div>
                    
                </div>
            </div>
        </section>
        <!-- End Crypto Blog Section -->
		<?php 
      }
	
      protected function select_param_posts() {
        $args = wp_parse_args( [
            'post_type'   => 'post',
            'numberposts' => -1,
            'orderby'     => 'title',
            'order'       => 'ASC',
        ] );
    
        $query_query = get_posts( $args );
    
        $posts = [];
        if ( $query_query ) {
            foreach ( $query_query as $query ) {
                $posts[$query->ID] = $query->post_title;
            }
        }
    
        return $posts;
    }

}

