<?php

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

/**
 * Class Thim_Top_Sale_Products
 */
class Thim_Top_Sale_Products extends WP_Widget {

	function __construct() {
		parent::__construct(
			'thim_top_sale_products',
			esc_html__( 'Thim: Top Sale Products', 'cakeart' ),
			array(
				'description' => esc_html__( 'Display a list of your top sale products on your site.', 'cakeart' )
			)
		);
	}

	public function form( $instance ) {
		$default = array(
			'title'           => 'Top Sale Products',
			'number_products' => 3
		);

		$instance = wp_parse_args( (array) $instance, $default );

		$title           = $instance['title'];
		$number_products = $instance['number_products'];
		?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>"><?php esc_html_e( 'Title', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'number_products' ) ); ?>"><?php esc_html_e( 'Number of products to show', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number_products' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number_products' ) ); ?>" type="number" min="1" value="<?php echo esc_attr( $number_products ); ?>">
		</p>
	<?php
	}

	public function update( $new_instance, $old_instance ) {
		$instance                    = $old_instance;
		$instance['title']           = strip_tags( $new_instance['title'] );
		$instance['number_products'] = !empty( $new_instance['number_products'] ) ? strip_tags( $new_instance['number_products'] ) : 1;
		return $instance;
	}


	public function widget( $args, $instance ) {
		extract( $args, EXTR_SKIP );

		$title           = apply_filters( 'widget-title', $instance['title'] );
		$number_products = esc_html( $instance['number_products'] );

		echo ent2ncr( $args['before_widget'] );

		echo '<h3 class="widget-title">' . esc_html( $title ) . '</h3>';

		?>

		<div class="wc-top-sale-products">

		<?php

		global $post;
		$theme_options_data = theme_options_data();

		$query = array(
			'post_type'           => 'product',
			'post_status'         => 'publish',
			'ignore_sticky_posts' => 1,
			'posts_per_page'      => $number_products,
			'meta_key'            => 'total_sales',
			'orderby'             => 'meta_value_num',
			'meta_query'          => WC()->query->get_meta_query()
		);
		$loop  = new WP_Query( $query );
        $loop->in_the_loop = true;
		while ( $loop->have_posts() ) : $loop->the_post();

			?>

			<div class="row top-product">
				<div class="col-xs-6 product-thumb">
					<?php echo wp_get_attachment_image( get_post_thumbnail_id( $post->ID ) ); ?>
					<?php
					if ( isset( $theme_options_data['thim_woo_set_show_qv'] ) && $theme_options_data['thim_woo_set_show_qv'] == '1' ) {
						echo '<div class="quick-view" data-prod="' . esc_attr( get_the_ID() ) . '"><a href="javascript:;"><i class="fa fa-search"></i></a></div>';
					}
					?>
					<a href="<?php echo esc_url( get_the_permalink() ); ?>" class="link-images-product"></a>
				</div>
				<div class="col-xs-6 product-info">
					<a href="<?php echo esc_url( get_the_permalink() ); ?>" class="product-name"><?php echo esc_attr( get_the_title() ); ?></a><br />
					<?php woocommerce_template_loop_rating(); ?>
					<?php woocommerce_template_loop_price(); ?>
				</div>
			</div>

		<?php endwhile;

		wp_reset_postdata();

		echo '</div>';

		echo ent2ncr( $args['after_widget'] );
	}

}

add_action( 'widgets_init', 'thim_register_top_sale_products_widget' );

function thim_register_top_sale_products_widget() {
	register_widget( 'Thim_Top_Sale_Products' );
}