<?php
/*
 * Elementor Confin Service Widget
 * Author & Copyright: themexshaper
*/

namespace Elementor;

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

class Confin_Service extends Widget_Base{

	/**
	 * Retrieve the widget name.
	*/
	public function get_name(){
		return 'tmx-confin_service';
	}

	/**
	 * Retrieve the widget title.
	*/
	public function get_title(){
		return esc_html__( 'Service', 'confin-core' );
	}

	/**
	 * Retrieve the widget icon.
	*/
	public function get_icon() {
		return 'eicon-post-content';
	}

	/**
	 * Retrieve the list of categories the widget belongs to.
	*/
	public function get_categories() {
		return ['themexshaper-category'];
	}

	/**
	 * Retrieve the list of scripts the Confin Service widget depended on.
	 * Used to set scripts dependencies required to run the widget.
	*/
	public function get_script_depends() {
		return ['tmx-confin_service'];
	}
	
	/**
	 * Register Confin Service widget controls.
	 * Adds different input fields to allow the user to change and customize the widget settings.
	*/
	protected function register_controls(){


		$posts = get_posts( 'post_type="service"&numberposts=-1' );
    $PostID = array();
    if ( $posts ) {
      foreach ( $posts as $post ) {
        $PostID[ $post->ID ] = $post->ID;
      }
    } else {
      $PostID[ __( 'No ID\'s found', 'confin' ) ] = 0;
    }
		
		
		$this->start_controls_section(
			'section_service',
			[
				'label' => esc_html__( 'Service Options', 'confin-core' ),
			]
		);
		$this->add_control(
			'service_style',
			[
				'label' => esc_html__( 'Service Style', 'confin-core' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'style-one' => esc_html__( 'Style One', 'confin-core' ),
					'style-two' => esc_html__( 'Style Two', 'confin-core' ),
				],
				'default' => 'style-one',
				'description' => esc_html__( 'Select your service style.', 'confin-core' ),
			]
		);
		$this->end_controls_section();// end: Section

		$this->start_controls_section(
			'section_service_listing',
			[
				'label' => esc_html__( 'Listing Options', 'confin-core' ),
			]
		);
		$this->add_control(
			'service_limit',
			[
				'label' => esc_html__( 'Service Limit', 'confin-core' ),
				'type' => Controls_Manager::NUMBER,
				'min' => 1,
				'max' => 100,
				'step' => 1,
				'default' => 3,
				'description' => esc_html__( 'Enter the number of items to show.', 'confin-core' ),
			]
		);
		$this->add_control(
			'service_order',
			[
				'label' => __( 'Order', 'confin-core' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'ASC' => esc_html__( 'Asending', 'confin-core' ),
					'DESC' => esc_html__( 'Desending', 'confin-core' ),
				],
				'default' => 'DESC',
			]
		);
		$this->add_control(
			'service_orderby',
			[
				'label' => __( 'Order By', 'confin-core' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'none' => esc_html__( 'None', 'confin-core' ),
					'ID' => esc_html__( 'ID', 'confin-core' ),
					'author' => esc_html__( 'Author', 'confin-core' ),
					'title' => esc_html__( 'Title', 'confin-core' ),
					'date' => esc_html__( 'Date', 'confin-core' ),
				],
				'default' => 'date',
			]
		);
		$this->add_control(
			'service_show_category',
			[
				'label' => __( 'Certain Categories?', 'confin-core' ),
				'type' => Controls_Manager::SELECT2,
				'default' => [],
				'options' => Controls_Helper_Output::get_terms_names( 'service_category'),
				'multiple' => true,
			]
		);
		$this->add_control(
			'service_show_id',
			[
				'label' => __( 'Certain ID\'s?', 'confin-core' ),
				'type' => Controls_Manager::SELECT2,
				'default' => [],
				'options' => $PostID,
				'multiple' => true,
			]
		);
		$this->end_controls_section();// end: Section

		// Section
		$this->start_controls_section(
			'service_section_style',
			[
				'label' => esc_html__( 'Section', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
				'condition' => [
					'service_style' => array('style-one'),
				],
			]
		);
		$this->add_control(
			'service_section_color',
			[
				'label' => esc_html__( 'Background Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .services-section .service-grids > .grid:nth-child(even)' => 'background-color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'service_section_count_color',
			[
				'label' => esc_html__( 'Count Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .services-section .count' => 'color: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

		// Title
		$this->start_controls_section(
			'service_section_title_style',
			[
				'label' => esc_html__( 'Title', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__( 'Typography', 'confin-core' ),
				'name' => 'service_confin_title_typography',
				'selector' => '{{WRAPPER}} .confin-service .grid h3 a',
			]
		);
		$this->add_control(
			'service_section_title_color',
			[
				'label' => esc_html__( 'Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .confin-service .grid h3 a' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'service_section_title_padding',
			[
				'label' => esc_html__( 'Title Padding', 'confin-core' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .confin-service .grid h3 a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

		// Content
		$this->start_controls_section(
			'service_section_content_style',
			[
				'label' => esc_html__( 'Content', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__( 'Typography', 'confin-core' ),
				'name' => 'service_confin_content_typography',
				'selector' => '{{WRAPPER}} .confin-service .grid p',
			]
		);
		$this->add_control(
			'service_section_content_color',
			[
				'label' => esc_html__( 'Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .confin-service .grid p' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'service_section_content_padding',
			[
				'label' => esc_html__( 'Title Padding', 'confin-core' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .confin-service .grid p' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();// end: Section
		
		
	}

	/**
	 * Render Service widget output on the frontend.
	 * Written in PHP and used to generate the final HTML.
	*/
	protected function render() {
		$settings = $this->get_settings_for_display();	
		$service_style = !empty( $settings['service_style'] ) ? $settings['service_style'] : '';
		$service_limit = !empty( $settings['service_limit'] ) ? $settings['service_limit'] : '';
		$service_order = !empty( $settings['service_order'] ) ? $settings['service_order'] : '';
		$service_orderby = !empty( $settings['service_orderby'] ) ? $settings['service_orderby'] : '';
		$service_show_category = !empty( $settings['service_show_category'] ) ? $settings['service_show_category'] : [];
		$service_show_id = !empty( $settings['service_show_id'] ) ? $settings['service_show_id'] : [];

		// Turn output buffer on
		ob_start();

		// Pagination
		global $paged;
		if( get_query_var( 'paged' ) )
		  $my_page = get_query_var( 'paged' );
		else {
		  if( get_query_var( 'page' ) )
			$my_page = get_query_var( 'page' );
		  else
			$my_page = 1;
		  set_query_var( 'paged', $my_page );
		  $paged = $my_page;
		}

    if ($service_show_id) {
			$service_show_id = json_encode( $service_show_id );
			$service_show_id = str_replace(array( '[', ']' ), '', $service_show_id);
			$service_show_id = str_replace(array( '"', '"' ), '', $service_show_id);
      $service_show_id = explode(',',$service_show_id);
    } else {
      $service_show_id = '';
    }

		$args = array(
		  // other query params here,
		  'paged' => $my_page,
		  'post_type' => 'service',
		  'posts_per_page' => (int)$service_limit,
		  'category_name' => implode(',', $service_show_category),
		  'orderby' => $service_orderby,
		  'order' => $service_order,
      'post__in' => $service_show_id,
		);

	$confin_service = new \WP_Query( $args );
	$count = 0;
	if ( $service_style == 'style-one' ) { ?>
	 <div class="confin-service services-section">
	    <div class="row">
	        <div class="service-grids">
	           <?php 
							if ( $confin_service->have_posts()) : while ( $confin_service->have_posts()) : $confin_service->the_post();
							$count++;
			        if ( $count < 10 ) {
			          $main_count = '0'.$count;
			        } else {
			          $main_count = $count;
			        }

			        $service_options = get_post_meta( get_the_ID(), 'service_options', true );
			        $service_excerpt = isset($service_options['service_excerpt']) ? $service_options['service_excerpt'] : '';
			       ?>
	           <div class="grid">
		           	<?php if( $main_count ) { echo '<span class="count">'.esc_html( $main_count ).'.</span>'; } ?>
		           	<h3>
		           		<a href="<?php echo esc_url( get_permalink() ); ?>">
		           			<?php echo esc_html( get_the_title() ); ?>
		           		</a>
		           	</h3>
			          <p><?php echo wp_kses_post( $service_excerpt ); ?></p>
	            </div>
             <?php 
						  endwhile;
						  endif;
						  wp_reset_postdata();
						 ?>
	        </div>
	    </div>
		</div>
		<?php } else { ?>
		<div class="confin-service services-section-s2">
	    <div class="row">
	        <div class="service-grids services-slider clearfix">
	        	<?php 
							if ( $confin_service->have_posts()) : while ( $confin_service->have_posts()) : $confin_service->the_post();

							$count++;
			        if ( $count < 10 ) {
			          $main_count = '0'.$count;
			        } else {
			          $main_count = $count;
			        }

							$service_options = get_post_meta( get_the_ID(), 'service_options', true );
			        $service_title = isset($service_options['service_title']) ? $service_options['service_title'] : '';
			        $short_desc = isset($service_options['short_desc']) ? $service_options['short_desc'] : '';
			        $service_image = isset($service_options['service_image']) ? $service_options['service_image'] : '';
			        $image_url = wp_get_attachment_url( $service_image );
			        $image_alt = get_post_meta( $service_image , '_wp_attachment_image_alt', true);

							?>
	           <div class="grid">
	                <div class="img-holder">
	                   <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $image_alt ); ?>">
	                </div>
	                <div class="details">
	                    <h3><a href="<?php echo esc_url( get_permalink() ); ?>"><span><?php echo esc_html( $main_count ); ?>.</span>
	                    	<?php echo esc_html( get_the_title() ); ?></a>
	                    </h3>
	                    <p><?php echo wp_kses_post( $short_desc ); ?></p>
	                </div>
	            </div>
	          <?php 
					  endwhile;
					  endif;
					  wp_reset_postdata();
					 ?>
	        </div>
	    </div>
		</div>
		<?php } 
			// Return outbut buffer
			echo ob_get_clean();	
		}
	/**
	 * Render Service widget output in the editor.
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	*/
	
	//protected function _content_template(){}
	
}
Plugin::instance()->widgets_manager->register( new Confin_Service() );