<?php
/*
 * Elementor eCommerce Addon Subscribe Widget
 * Author & Copyright: NicheAddon
*/

namespace Elementor;

if (!isset(get_option( 'naeca_bw_settings' )['naeca_subscribe'])) { // enable & disable

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

class eCommerce_Elementor_Addon_Subscribe extends Widget_Base{

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

	/**
	 * Retrieve the widget title.
	*/
	public function get_title(){
		return esc_html__( 'Subscribe Form', 'ecommerce-addon' );
	}

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

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

	/**
	 * Register eCommerce Addon Subscribe widget controls.
	 * Adds different input fields to allow the user to change and customize the widget settings.
	*/
	protected function register_controls(){

		$this->start_controls_section(
			'section_subscribe',
			[
				'label' => esc_html__( 'Subscribe Options', 'ecommerce-addon' ),
			]
		);
		$this->add_control(
			'subscribe_form',
			[
				'label' => esc_html__( 'Subscribe Form', 'ecommerce-addon' ),
				'type' => Controls_Manager::TEXTAREA,
				'placeholder' => __( '[mc4wp_form id="40"]', 'ecommerce-addon' ),
				'label_block' => true,
			]
		);
		$this->add_responsive_control(
			'content_alignment',
			[
				'label' => esc_html__( 'Content Alignment', 'ecommerce-addon' ),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => esc_html__( 'Left', 'ecommerce-addon' ),
						'icon' => 'fa fa-align-left',
					],
					'center' => [
						'title' => esc_html__( 'Center', 'ecommerce-addon' ),
						'icon' => 'fa fa-align-center',
					],
					'right' => [
						'title' => esc_html__( 'Right', 'ecommerce-addon' ),
						'icon' => 'fa fa-align-right',
					],
				],
				'default' => 'center',
				'selectors' => [
					'{{WRAPPER}} .subscribe-wrap' => 'text-align: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

		// Section
			$this->start_controls_section(
				'section_box_style',
				[
					'label' => esc_html__( 'Section', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_responsive_control(
				'section_width',
				[
					'label' => esc_html__( 'Section Width', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1500,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .mc4wp-form-fields, {{WRAPPER}} .subscribe-wrap' => 'max-width:{{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'section_padding',
				[
					'label' => __( 'Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .mc4wp-form-fields' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_control(
				'section_bg_color',
				[
					'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .mc4wp-form-fields' => 'background-color: {{VALUE}};',
					],
				]
			);
			$this->add_control(
				'section_border_radius',
				[
					'label' => __( 'Border Radius', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .mc4wp-form-fields' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[
					'name' => 'section_box_border',
					'label' => esc_html__( 'Border', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .mc4wp-form-fields',
				]
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				[
					'name' => 'section_box_shadow',
					'label' => esc_html__( 'Image Box Shadow', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .mc4wp-form-fields',
				]
			);
			$this->end_controls_section();// end: Section

		// Form
			$this->start_controls_section(
				'section_form_style',
				[
					'label' => esc_html__( 'Form', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_responsive_control(
				'input_margin',
				[
					'label' => __( 'Text Field Margin', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'textarea_margin',
				[
					'label' => __( 'Textarea Field Margin', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe textarea' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'form_section_width',
				[
					'label' => esc_html__( 'Form Width', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1500,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe-form' => 'max-width:{{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'form_padding',
				[
					'label' => __( 'Form Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="text"],
						{{WRAPPER}} .naeca-subscribe input[type="email"],
						{{WRAPPER}} .naeca-subscribe input[type="date"],
						{{WRAPPER}} .naeca-subscribe input[type="time"],
						{{WRAPPER}} .naeca-subscribe input[type="number"],
						{{WRAPPER}} .naeca-subscribe input[type="password"], 
						{{WRAPPER}} .naeca-subscribe input[type="tel"], 
						{{WRAPPER}} .naeca-subscribe input[type="search"], 
						{{WRAPPER}} .naeca-subscribe input[type="url"], 
						{{WRAPPER}} .naeca-subscribe textarea,
						{{WRAPPER}} .naeca-subscribe select,
						{{WRAPPER}} .naeca-subscribe .form-control,
						{{WRAPPER}} .naeca-subscribe .nice-select' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'name' => 'form_typography',
					'selector' => '{{WRAPPER}} .naeca-subscribe input[type="text"],
					{{WRAPPER}} .naeca-subscribe input[type="email"],
					{{WRAPPER}} .naeca-subscribe input[type="date"],
					{{WRAPPER}} .naeca-subscribe input[type="time"],
					{{WRAPPER}} .naeca-subscribe input[type="number"],
					{{WRAPPER}} .naeca-subscribe input[type="password"], 
					{{WRAPPER}} .naeca-subscribe input[type="tel"], 
					{{WRAPPER}} .naeca-subscribe input[type="search"], 
					{{WRAPPER}} .naeca-subscribe input[type="url"], 
					{{WRAPPER}} .naeca-subscribe textarea,
					{{WRAPPER}} .naeca-subscribe select,
					{{WRAPPER}} .naeca-subscribe .form-control,
					{{WRAPPER}} .naeca-subscribe .nice-select',
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[
					'name' => 'form_border',
					'label' => esc_html__( 'Border', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .naeca-subscribe input[type="text"],
					{{WRAPPER}} .naeca-subscribe input[type="email"],
					{{WRAPPER}} .naeca-subscribe input[type="date"],
					{{WRAPPER}} .naeca-subscribe input[type="time"],
					{{WRAPPER}} .naeca-subscribe input[type="number"],
					{{WRAPPER}} .naeca-subscribe input[type="password"], 
					{{WRAPPER}} .naeca-subscribe input[type="tel"], 
					{{WRAPPER}} .naeca-subscribe input[type="search"], 
					{{WRAPPER}} .naeca-subscribe input[type="url"], 
					{{WRAPPER}} .naeca-subscribe textarea,
					{{WRAPPER}} .naeca-subscribe select,
					{{WRAPPER}} .naeca-subscribe .form-control,
					{{WRAPPER}} .naeca-subscribe .nice-select',
				]
			);
			$this->add_control(
				'placeholder_text_color',
				[
					'label' => __( 'Placeholder Text Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input:not([type="submit"])::-webkit-input-placeholder' => 'color: {{VALUE}} !important;',
						'{{WRAPPER}} .naeca-subscribe input:not([type="submit"])::-moz-placeholder' => 'color: {{VALUE}} !important;',
						'{{WRAPPER}} .naeca-subscribe input:not([type="submit"])::-ms-input-placeholder' => 'color: {{VALUE}} !important;',
						'{{WRAPPER}} .naeca-subscribe input:not([type="submit"])::-o-placeholder' => 'color: {{VALUE}} !important;',
						'{{WRAPPER}} .naeca-subscribe textarea::-webkit-input-placeholder' => 'color: {{VALUE}} !important;',
						'{{WRAPPER}} .naeca-subscribe textarea::-moz-placeholder' => 'color: {{VALUE}} !important;',
						'{{WRAPPER}} .naeca-subscribe textarea::-ms-input-placeholder' => 'color: {{VALUE}} !important;',
						'{{WRAPPER}} .naeca-subscribe textarea::-o-placeholder' => 'color: {{VALUE}} !important;',
					],
				]
			);
			$this->add_control(
				'text_color',
				[
					'label' => __( 'Text Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="text"],
						{{WRAPPER}} .naeca-subscribe input[type="email"],
						{{WRAPPER}} .naeca-subscribe input[type="date"],
						{{WRAPPER}} .naeca-subscribe input[type="time"],
						{{WRAPPER}} .naeca-subscribe input[type="number"],
						{{WRAPPER}} .naeca-subscribe input[type="password"], 
						{{WRAPPER}} .naeca-subscribe input[type="tel"], 
						{{WRAPPER}} .naeca-subscribe input[type="search"], 
						{{WRAPPER}} .naeca-subscribe input[type="url"], 
						{{WRAPPER}} .naeca-subscribe textarea,
						{{WRAPPER}} .naeca-subscribe select,
						{{WRAPPER}} .naeca-subscribe .form-control,
						{{WRAPPER}} .naeca-subscribe .nice-select' => 'color: {{VALUE}} !important;',
					],
				]
			);
			$this->add_control(
				'form_bg_color',
				[
					'label' => __( 'Background Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="text"],
						{{WRAPPER}} .naeca-subscribe input[type="email"],
						{{WRAPPER}} .naeca-subscribe input[type="date"],
						{{WRAPPER}} .naeca-subscribe input[type="time"],
						{{WRAPPER}} .naeca-subscribe input[type="number"],
						{{WRAPPER}} .naeca-subscribe input[type="password"], 
						{{WRAPPER}} .naeca-subscribe input[type="tel"], 
						{{WRAPPER}} .naeca-subscribe input[type="search"], 
						{{WRAPPER}} .naeca-subscribe input[type="url"], 
						{{WRAPPER}} .naeca-subscribe textarea,
						{{WRAPPER}} .naeca-subscribe select,
						{{WRAPPER}} .naeca-subscribe .form-control,
						{{WRAPPER}} .naeca-subscribe .nice-select' => 'background-color: {{VALUE}} !important;',
					],
				]
			);
			$this->add_control(
				'form_border_radius',
				[
					'label' => __( 'Border Radius', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="text"],
						{{WRAPPER}} .naeca-subscribe input[type="email"],
						{{WRAPPER}} .naeca-subscribe input[type="date"],
						{{WRAPPER}} .naeca-subscribe input[type="time"],
						{{WRAPPER}} .naeca-subscribe input[type="number"],
						{{WRAPPER}} .naeca-subscribe input[type="password"], 
						{{WRAPPER}} .naeca-subscribe input[type="tel"], 
						{{WRAPPER}} .naeca-subscribe input[type="search"], 
						{{WRAPPER}} .naeca-subscribe input[type="url"], 
						{{WRAPPER}} .naeca-subscribe textarea,
						{{WRAPPER}} .naeca-subscribe select,
						{{WRAPPER}} .naeca-subscribe .form-control,
						{{WRAPPER}} .naeca-subscribe .nice-select' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'field_height',
				[
					'label' => esc_html__( 'Fields Height', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1500,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="text"],
						{{WRAPPER}} .naeca-subscribe input[type="email"],
						{{WRAPPER}} .naeca-subscribe input[type="date"],
						{{WRAPPER}} .naeca-subscribe input[type="time"],
						{{WRAPPER}} .naeca-subscribe input[type="number"],
						{{WRAPPER}} .naeca-subscribe input[type="password"], 
						{{WRAPPER}} .naeca-subscribe input[type="tel"], 
						{{WRAPPER}} .naeca-subscribe input[type="search"], 
						{{WRAPPER}} .naeca-subscribe input[type="url"], 
						{{WRAPPER}} .naeca-subscribe select,
						{{WRAPPER}} .naeca-subscribe .form-control,
						{{WRAPPER}} .naeca-subscribe .nice-select' => 'height:{{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'tarea_height',
				[
					'label' => esc_html__( 'Textarea Height', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1500,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe textarea' => 'height:{{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->end_controls_section();// end: Section

		// Button
			$this->start_controls_section(
				'section_button_style',
				[
					'label' => esc_html__( 'Button', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'name' => 'button_typography',
					'selector' => '{{WRAPPER}} .naeca-subscribe input[type="submit"]',
				]
			);
			$this->add_responsive_control(
				'btn_width',
				[
					'label' => esc_html__( 'Width', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1000,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="submit"]' => 'min-width: {{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->add_control(
				'btn_margin',
				[
					'label' => __( 'Margin', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="submit"]' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_control(
				'button_border_radius',
				[
					'label' => __( 'Border Radius', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-subscribe input[type="submit"]' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->start_controls_tabs( 'button_style' );
				$this->start_controls_tab(
					'button_normal',
					[
						'label' => esc_html__( 'Normal', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'button_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-subscribe input[type="submit"]' => 'color: {{VALUE}};',
						],
					]
				);
				$this->add_control(
					'button_bg_color',
					[
						'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-subscribe input[type="submit"]' => 'background-color: {{VALUE}};',
						],
					]
				);
				$this->add_group_control(
					Group_Control_Border::get_type(),
					[
						'name' => 'button_border',
						'label' => esc_html__( 'Border', 'ecommerce-addon' ),
						'selector' => '{{WRAPPER}} .naeca-subscribe input[type="submit"]',
					]
				);
				$this->end_controls_tab();  // end:Normal tab

				$this->start_controls_tab(
					'button_hover',
					[
						'label' => esc_html__( 'Hover', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'button_hover_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-subscribe input[type="submit"]:hover' => 'color: {{VALUE}};',
						],
					]
				);
				$this->add_control(
					'button_bg_hover_color',
					[
						'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-subscribe input[type="submit"]:hover' => 'background-color: {{VALUE}};',
						],
					]
				);
				$this->add_group_control(
					Group_Control_Border::get_type(),
					[
						'name' => 'button_hover_border',
						'label' => esc_html__( 'Border', 'ecommerce-addon' ),
						'selector' => '{{WRAPPER}} .naeca-subscribe input[type="submit"]:hover',
					]
				);
				$this->end_controls_tab();  // end:Hover tab
			$this->end_controls_tabs(); // end tabs

			$this->end_controls_section();// end: Section

	}

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

		// Starts
		$output  = '<div class="naeca-subscribe naeca-form"><div class="subscribe-wrap">'.do_shortcode( $subscribe_form ).'</div></div>';

		echo $output;

	}

}
Plugin::instance()->widgets_manager->register_widget_type( new eCommerce_Elementor_Addon_Subscribe() );

} // enable & disable
