<?php

/**
 * Checkout Widget.
 *
 * @since 1.10.0
 */

use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Typography;
use \Elementor\Repeater;

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

class Sina_Ext_Pro_Checkout_Widget extends Widget_Base{

	/**
	 * Get widget name.
	 *
	 * @since 1.10.0
	 */
	public function get_name() {
		return 'sina_ext_pro_checkout';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.10.0
	 */
	public function get_title() {
		return esc_html__( 'Sina Checkout', 'sina-ext-pro' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.10.0
	 */
	public function get_icon() {
		return 'eicon-checkout';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.10.0
	 */
	public function get_categories() {
		return [ 'sina-ext-pro-woocommerce' ];
	}

	/**
	 * Get widget keywords.
	 *
	 * Retrieve the list of keywords the widget belongs to.
	 *
	 * @since 1.10.0
	 */
	public function get_keywords() {
		return [ 'sina shop', 'sina product', 'sina checkout', 'woocommerce shop', 'woocommerce checkout', 'woocommerce product' ];
	}

	/**
	 * Get widget styles.
	 *
	 * Retrieve the list of styles the widget belongs to.
	 *
	 * @since 1.10.0
	 */
	public function get_style_depends() {
		return [
			'sina-ext-pro-widgets',
		];
	}

	/**
	 * Register widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.10.0
	 */
	protected function register_controls() {
		// Start Checkout Content
		// ========================
			$this->start_controls_section(
				'wc_checkout_content',
				[
					'label' => esc_html__( 'Content', 'sina-ext-pro' ),
					'tab' => Controls_Manager::TAB_CONTENT,
				]
			);

			$this->add_control(
				'wc_checkout_apply',
				[
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div class="elementor-update-preview" style="background: rgba(0,0,0,0);margin: 0;"><div class="elementor-update-preview-title">Refresh?</div><div class="elementor-update-preview-button-wrapper"><button class="elementor-update-preview-button elementor-button elementor-button-success">Yes</button></div></div>',
				]
			);

			$this->end_controls_section();
		// End Checkout Content
		// ======================


		// Start Title Styles
		// ===================
			Sina_Pro_Woo_Common_Data::checkout_title_styles($this);
		// End Title Styles
		// =================


		// Start Label Styles
		// ===================
			$this->start_controls_section(
				'wc_label_style',
				[
					'label' => esc_html__( 'Label', 'sina-ext-pro' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'name' => 'wc_label_typography',
					'fields_options' => [
						'typography' => [ 
							'default' =>'custom', 
						],
						'font_weight' => [
							'default' => '400',
						],
						'font_size'   => [
							'default' => [
								'size' => '14',
							],
						],
						'line_height'   => [
							'default' => [
								'size' => '22',
							],
						],
						'text_transform' => [
							'default' => 'none',
						],
					],
					'selector' => '{{WRAPPER}} .sina-pro-wc-checkout label',
				]
			);
			$this->add_control(
				'wc_label_color',
				[
					'label' => esc_html__( 'Color', 'sina-ext-pro' ),
					'type' => Controls_Manager::COLOR,
					'default' => '#222',
					'selectors' => [
						'{{WRAPPER}} .sina-pro-wc-checkout label' => 'color: {{VALUE}};',
					],
				]
			);
			$this->add_responsive_control(
				'wc_label_margin',
				[
					'label' => esc_html__( 'Margin', 'sina-ext-pro' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em', '%' ],
					'default' => [
						'top' => '0',
						'right' => '0',
						'bottom' => '10',
						'left' => '0',
						'isLinked' => false,
					],
					'selectors' => [
						'{{WRAPPER}} .sina-pro-wc-checkout label' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->end_controls_section();
		// End Label Styles
		// =================


		// Start Fields Styles
		// ======================
			$this->start_controls_section(
				'wc_fields_style',
				[
					'label' => esc_html__( 'Fields', 'sina-ext-pro' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);

			$this->add_control(
				'wc_fields_color',
				[
					'label' => esc_html__( 'Color', 'sina-ext-pro' ),
					'type' => Controls_Manager::COLOR,
					'default' => '#aaa',
					'selectors' => [
						'{{WRAPPER}} .sina-pro-wc-checkout .woocommerce-additional-fields textarea, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-shipping-fields input, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-billing-fields input' => 'color: {{VALUE}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Background::get_type(),
				[
					'name' => 'wc_fields_bg',
					'types' => [ 'classic', 'gradient' ],
					'fields_options' => [
						'background' => [ 
							'default' =>'classic', 
						],
						'color' => [
							'default' => '#fff',
						],
					],
					'selector' => '{{WRAPPER}} .sina-pro-wc-checkout .woocommerce-additional-fields textarea, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-shipping-fields input, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-billing-fields input',
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[
					'name' => 'wc_fields_border',
					'fields_options' => [
						'border' => [
							'default' => 'solid',
						],
						'color' => [
							'default' => '#ccc',
						],
						'width' => [
							'default' => [
								'top' => '1',
								'right' => '1',
								'bottom' => '1',
								'left' => '1',
								'isLinked' => true,
							]
						],
					],
					'selector' => '{{WRAPPER}} .sina-pro-wc-checkout .woocommerce-billing-fields .select2-container .select2-selection--single, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-additional-fields textarea, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-shipping-fields input, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-billing-fields input',
				]
			);
			$this->add_responsive_control(
				'wc_fields_radius',
				[
					'label' => esc_html__( 'Radius', 'sina-ext-pro' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em', '%' ],
					'default' => [
						'top' => '4',
						'right' => '4',
						'bottom' => '4',
						'left' => '4',
						'isLinked' => true,
					],
					'selectors' => [
						'{{WRAPPER}} .sina-pro-wc-checkout .woocommerce-additional-fields textarea, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-shipping-fields input, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-billing-fields input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'wc_fields_padding',
				[
					'label' => esc_html__( 'Padding', 'sina-ext-pro' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em', '%' ],
					'default' => [
						'top' => '8',
						'right' => '12',
						'bottom' => '8',
						'left' => '12',
						'isLinked' => false,
					],
					'selectors' => [
						'{{WRAPPER}} .sina-pro-wc-checkout .woocommerce-additional-fields textarea, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-shipping-fields input, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-billing-fields input' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'wc_fields_margin',
				[
					'label' => esc_html__( 'Margin', 'sina-ext-pro' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em', '%' ],
					'default' => [
						'top' => '0',
						'right' => '0',
						'bottom' => '8',
						'left' => '0',
						'isLinked' => false,
					],
					'selectors' => [
						'{{WRAPPER}} .sina-pro-wc-checkout .woocommerce-shipping-fields input, {{WRAPPER}} .sina-pro-wc-checkout .woocommerce-billing-fields input' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->end_controls_section();
		// End Fields Styles
		// =====================


		// Start Order Button Styles
		// ==========================
			$this->start_controls_section(
				'wc_order_button_style',
				[
					'label' => esc_html__( 'Order Button', 'sina-ext-pro' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);

			Sina_Pro_Woo_Common_Data::buttons_styles($this, '.sina-pro-wc-checkout button[type="submit"]', 'wc_order_button_');

			$this->end_controls_section();
		// End Order Button Styles
		// ========================
	}


	protected function render() {
		$data = $this->get_settings_for_display();

		if ( class_exists( 'WooCommerce' ) ):
			?>
			<div class="sina-pro-wc-checkout">
				<?php echo do_shortcode("[woocommerce_checkout]"); ?>
			</div>
			<?php
		else:
			printf('<h3>%s</h3>', esc_html__('Please install and activate the WooCommerce plugin to use this feature!', 'sina-ext-pro') );
		endif;
	}


	protected function content_template() {

	}
}
