<?php
/** WooCommerce products Elementor widget. */
defined( 'ABSPATH' ) || exit;

class Shabnam_Elementor_Shop extends Shabnam_Elementor_Widget_Base {
	public function get_name() { return 'shabnam-muchwow-shop'; }
	public function get_title() { return __( 'فروشگاه MuchWOW', 'shabnam-muchwow' ); }
	public function get_icon() { return 'eicon-products'; }

	protected function register_controls() {
		$this->start_controls_section( 'content', array( 'label' => __( 'محتوا', 'shabnam-muchwow' ) ) );
		$this->add_control( 'label', array( 'label' => __( 'برچسب بالای عنوان', 'shabnam-muchwow' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'فروشگاه' ) );
		$this->add_control( 'title', array( 'label' => __( 'عنوان', 'shabnam-muchwow' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'محصولات منتخب' ) );
		$this->add_control( 'subtitle', array( 'label' => __( 'توضیح کوتاه', 'shabnam-muchwow' ), 'type' => \Elementor\Controls_Manager::TEXTAREA, 'default' => 'محصولات تازه و پیشنهادهای فروشگاه را یک‌جا ببینید.' ) );
		$this->add_control( 'count', array( 'label' => __( 'تعداد محصولات', 'shabnam-muchwow' ), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 2, 'max' => 12, 'step' => 1, 'default' => 4 ) );
		$this->add_control( 'columns', array( 'label' => __( 'تعداد ستون‌ها', 'shabnam-muchwow' ), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => '3', 'options' => array( '2' => '۲', '3' => '۳', '4' => '۴' ) ) );
		$this->add_control( 'button_text', array( 'label' => __( 'متن دکمه', 'shabnam-muchwow' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'مشاهده همه محصولات' ) );
		$this->shabnam_add_url_control( 'button_url', __( 'لینک دکمه', 'shabnam-muchwow' ), function_exists( 'shabnam_shop_url' ) ? shabnam_shop_url() : home_url( '/' ) );
		$this->end_controls_section();
		$this->shabnam_register_common_style_controls();
	}

	protected function render() {
		if ( ! class_exists( 'WooCommerce' ) ) {
			if ( function_exists( 'shabnam_elementor_is_editor_request' ) && shabnam_elementor_is_editor_request() ) { echo '<div class="shabnam-section-card p-4">' . esc_html__( 'برای نمایش این سکشن، ووکامرس باید فعال باشد.', 'shabnam-muchwow' ) . '</div>'; }
			return;
		}
		$s       = $this->get_settings_for_display();
		$count   = max( 2, min( 12, absint( $s['count'] ) ) );
		$columns = max( 2, min( 4, absint( $s['columns'] ) ) );
		$orderby = function_exists( 'shabnam_get_setting' ) ? sanitize_key( (string) shabnam_get_setting( 'shop_home_orderby', 'date' ) ) : 'date';
		$order   = function_exists( 'shabnam_get_setting' ) ? strtoupper( sanitize_text_field( (string) shabnam_get_setting( 'shop_home_order', 'DESC' ) ) ) : 'DESC';
		$hide_oos = function_exists( 'shabnam_get_setting' ) ? (bool) shabnam_get_setting( 'shop_hide_out_of_stock', false ) : false;
		$orderby = in_array( $orderby, array( 'date', 'modified', 'title', 'menu_order' ), true ) ? $orderby : 'date';
		$order   = in_array( $order, array( 'ASC', 'DESC' ), true ) ? $order : 'DESC';
		$query   = new WP_Query( array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $count, 'no_found_rows' => true, 'orderby' => $orderby, 'order' => $order, 'meta_query' => $hide_oos ? array( array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => '!=' ) ) : array() ) );
		if ( ! $query->have_posts() ) { return; }
		foreach ( array( 'label', 'title', 'subtitle', 'button_text' ) as $key ) { $this->add_inline_editing_attributes( $key, 'none' ); }
		$this->add_link_attributes( 'button_url', $s['button_url'] );
		?>
		<section class="shabnam-home-shop section panel overflow-hidden woocommerce shabnam-elementor-section" id="shop"><div class="section-outer panel py-6 sm:py-8 lg:py-9 xl:py-10"><div class="container sm:max-w-md xl:max-w-lg">
		<div class="section-header panel sm:max-w-800px mx-auto mb-5 sm:mb-7 text-center"><div class="category"><div class="-rotate-2"><span class="fs-5 xl:fs-4 text-secondary fw-bold" <?php echo $this->get_render_attribute_string( 'label' ); ?>><?php echo esc_html( $s['label'] ); ?></span></div></div><h2 class="h4 sm:h3 xl:h2 mb-0 mt-2 text-white" <?php echo $this->get_render_attribute_string( 'title' ); ?>><?php echo esc_html( $s['title'] ); ?></h2><?php if ( $s['subtitle'] ) : ?><p class="fs-5 mt-2 text-white text-opacity-70" <?php echo $this->get_render_attribute_string( 'subtitle' ); ?>><?php echo esc_html( $s['subtitle'] ); ?></p><?php endif; ?></div>
		<ul class="products shabnam-home-product-grid columns-<?php echo esc_attr( $columns ); ?>" style="--shcd-shop-columns:<?php echo esc_attr( $columns ); ?>"><?php while ( $query->have_posts() ) : $query->the_post(); $GLOBALS['product'] = wc_get_product( get_the_ID() ); if ( $GLOBALS['product'] ) { wc_get_template_part( 'content', 'product' ); } endwhile; ?></ul>
		<div class="section-footer panel d-flex justify-center mt-5 sm:mt-6"><a class="btn btn-md sm:btn-xl px-4 border border-3 border-black bg-gradient-45n from-secondary to-tertiary contrast-shadow -rotate-2 hover:rotate-0" <?php echo $this->get_render_attribute_string( 'button_url' ); ?>><span class="text-black" <?php echo $this->get_render_attribute_string( 'button_text' ); ?>><?php echo esc_html( $s['button_text'] ); ?></span></a></div>
		</div></div></section>
		<?php
		wp_reset_postdata();
		if ( function_exists( 'wc_reset_loop' ) ) { wc_reset_loop(); }
		unset( $GLOBALS['product'] );
	}
}
