<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
require_once SIFENCY_ADDONS_DIR . 'includes/elementor/site/ajax.php';
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Registration_Form extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-registration-form',
			'title'          => 'Sifency Registration Form',
			'icon'           => 'eicon-lock-user',
			'category'       => array( 'sifency-advance-widgets' ),
			'script_depends' => array( 'sifency-register-script' ),
			'style_depends'  => array( 'sifency-register-style', 'sifency-typo-style', 'sifency-icons' ),
			'keywords'       => array( 'registration form', 'user registration', 'register widget', 'register button', 'user account' ),
		);}
	protected function register_controls() {

		// --- CONFIG SECTION ---
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Form Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'card',
				'options' => array(
					'card'     => esc_html__( '1. Classic Card', 'sifency-addons' ),
					'minimal'  => esc_html__( '2. Minimal', 'sifency-addons' ),
					'floating' => esc_html__( '3. Floating Labels', 'sifency-addons' ),
					'glass'    => esc_html__( '4. Glassmorphism', 'sifency-addons' ),
					'split'    => esc_html__( '5. Split Screen', 'sifency-addons' ),
					'neon'     => esc_html__( '6. Dark / Neon', 'sifency-addons' ),
					'outline'  => esc_html__( '7. Modern Outline', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'split_image',
			array(
				'label'     => esc_html__( 'Side Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array( 'url' => \Elementor\Utils::get_placeholder_image_src() ),
				'condition' => array( 'layout_type' => 'split' ),
			)
		);
		$this->add_control(
			'show_labels',
			array(
				'label'     => 'Show Labels',
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'layout_type!' => 'floating' ),
			)
		);

		$this->add_control(
			'redirect_after',
			array(
				'label'       => 'Redirect After Register',
				'type'        => Controls_Manager::TEXT,
				'placeholder' => home_url(),
			)
		);
		$this->add_control(
			'auto_login',
			array(
				'label'   => 'Auto Login After Register',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->end_controls_section();

		// --- FIELDS SECTION ---
		$this->start_controls_section( 'section_fields', array( 'label' => 'Form Fields' ) );

		$this->add_control(
			'show_first_name',
			array(
				'label'   => 'First Name',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_last_name',
			array(
				'label'   => 'Last Name',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_control(
			'user_label',
			array(
				'label'   => 'Username Label',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Username',
			)
		);
		$this->add_control(
			'email_label',
			array(
				'label'   => 'Email Label',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Email Address',
			)
		);
		$this->add_control(
			'pass_label',
			array(
				'label'   => 'Password Label',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Password',
			)
		);

		$this->add_control(
			'btn_text',
			array(
				'label'   => 'Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Register',
			)
		);
		$this->add_control(
			'login_text',
			array(
				'label'   => 'Login Link Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Already have an account? Login',
			)
		);
		$this->add_control(
			'login_url',
			array(
				'label' => 'Login Page URL',
				'type'  => Controls_Manager::URL,
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_fields',
			array(
				'label' => esc_html__( 'Input Fields', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => 'field_typography',
				'label'    => esc_html__( 'Input Typography', 'sifency-addons' ),
				'selector' => '{{WRAPPER}} .sifency-reg-input',
			)
		);

		$this->start_controls_tabs( 'tabs_field_state' );

		$this->start_controls_tab(
			'tab_field_normal',
			array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) )
		);
		$this->add_control(
			'field_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-reg-input' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'field_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-reg-input' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => 'field_border',
				'selector' => '{{WRAPPER}} .sifency-reg-input',
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'field_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-reg-input',
			)
		);

		$this->end_controls_tab();

		// --- Focus State ---
		$this->start_controls_tab(
			'tab_field_focus',
			array( 'label' => esc_html__( 'Focus', 'sifency-addons' ) )
		);
		$this->add_control(
			'field_bg_focus',
			array(
				'label'     => esc_html__( 'Background (Focus)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-reg-input:focus' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'field_text_focus',
			array(
				'label'     => esc_html__( 'Text Color (Focus)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-reg-input:focus' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => 'field_border_focus',
				'selector' => '{{WRAPPER}} .sifency-reg-input:focus',
			)
		);

		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'field_box_shadow_focus',
				'selector' => '{{WRAPPER}} .sifency-reg-input:focus',
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		// --- Spacing & Radius ---
		$this->add_responsive_control(
			'field_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-reg-input' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'field_margin',
			array(
				'label'      => esc_html__( 'Margin', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-reg-input' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'field_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-reg-input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'field_spacing',
			array(
				'label'     => esc_html__( 'Spacing Between Fields', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-field-group' => 'margin-bottom: {{SIZE}}{{UNIT}};' ),
			)
		);

		// --- Placeholder Color ---
		$this->add_control(
			'placeholder_color',
			array(
				'label'     => esc_html__( 'Placeholder Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .sifency-reg-input::-webkit-input-placeholder' => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-reg-input::-moz-placeholder'          => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-reg-input:-ms-input-placeholder'      => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-reg-input::placeholder'               => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'form_item_',
				'label'    => esc_html__( 'Form Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-reg-form-wrap .sifency-registration-form',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'image_item_',
				'label'     => esc_html__( 'Image Wrapper', 'sifency-addons' ),
				'selector'  => '.sifency-reg-image',
				'condition' => array( 'layout_type' => 'split' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'form_link_',
				'label'    => esc_html__( 'Link Text', 'sifency-addons' ),
				'selector' => '.sifency-login-link a',
				'exclude'  => array( 'alignment', 'transition', 'shadow' ),
				'exclude'  => array( 'gradient' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'form_label_',
				'label'    => esc_html__( 'Label', 'sifency-addons' ),
				'selector' => '.sifency-reg-label',
				'exclude'  => array( 'alignment', 'transition', 'shadow' ),
				'exclude'  => array( 'gradient' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'          => 'button_',
				'label'           => esc_html__( 'Button Style', 'sifency-addons' ),
				'selector'        => '.sifency-reg-submit',
				'show_typography' => true,
				'show_dimensions' => true,
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		if ( is_user_logged_in() && ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$current_user = wp_get_current_user();
			echo '<div class="sifency-reg-logged-in">';
			echo '<p>' . sprintf( /* translators: You are already registered */ esc_html__( 'You are already registered as %s', 'sifency-addons' ), '<strong>' . esc_html( $current_user->display_name ) . '</strong>' ) . '</p>';
			echo '<a href="' . esc_url( wp_logout_url( home_url() ) ) . '" class="sifency-reg-submit">' . esc_html__( 'Logout', 'sifency-addons' ) . '</a>';
			echo '</div>';
			return;
		}

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-reg-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-reg-layout-' . $settings['layout_type'] );

		$this->add_render_attribute( 'form', 'class', 'sifency-registration-form' );
		$this->add_render_attribute( 'form', 'action', esc_url( admin_url( 'admin-post.php' ) ) );
		$this->add_render_attribute( 'form', 'method', 'post' );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<div class="sifency-reg-container">
				
				<?php if ( 'split' === $settings['layout_type'] ) : ?>
					<div class="sifency-reg-image" style="background-image: url(<?php echo esc_url( $settings['split_image']['url'] ); ?>);"></div>
				<?php endif; ?>

				<div class="sifency-reg-form-wrap">
					<form <?php echo wp_kses_post( $this->get_render_attribute_string( 'form' ) ); ?>>
						
						<input type="hidden" name="action" value="sifency_register_user">
						<?php wp_nonce_field( 'sifency_reg_nonce', 'security' ); ?>
						<input type="hidden" name="redirect_to" value="<?php echo ! empty( $settings['redirect_after'] ) ? esc_url( $settings['redirect_after'] ) : esc_url( home_url() ); ?>">
						<input type="hidden" name="auto_login" value="<?php echo esc_attr( $settings['auto_login'] ); ?>">

						<!-- Username -->
						<div class="sifency-field-group">
							<?php if ( 'yes' === $settings['show_labels'] && 'floating' !== $settings['layout_type'] ) : ?>
								<label class="sifency-reg-label" for="reg_user"><?php echo esc_html( $settings['user_label'] ); ?></label>
							<?php endif; ?>
							<input type="text" name="username" id="reg_user" class="sifency-reg-input" placeholder="<?php echo 'floating' === $settings['layout_type'] ? ' ' : esc_attr( $settings['user_label'] ); ?>" required>
							<?php if ( 'floating' === $settings['layout_type'] ) : ?>
								<label class="sifency-reg-label floating" for="reg_user"><?php echo esc_html( $settings['user_label'] ); ?></label>
							<?php endif; ?>
						</div>

						<!-- Email -->
						<div class="sifency-field-group">
							<?php if ( 'yes' === $settings['show_labels'] && 'floating' !== $settings['layout_type'] ) : ?>
								<label class="sifency-reg-label" for="reg_email"><?php echo esc_html( $settings['email_label'] ); ?></label>
							<?php endif; ?>
							<input type="email" name="email" id="reg_email" class="sifency-reg-input" placeholder="<?php echo 'floating' === $settings['layout_type'] ? ' ' : esc_attr( $settings['email_label'] ); ?>" required>
							<?php if ( 'floating' === $settings['layout_type'] ) : ?>
								<label class="sifency-reg-label floating" for="reg_email"><?php echo esc_html( $settings['email_label'] ); ?></label>
							<?php endif; ?>
						</div>

						<!-- Name Fields -->
						<?php if ( 'yes' === $settings['show_first_name'] ) : ?>
							<div class="sifency-field-group">
								<?php if ( 'yes' === $settings['show_labels'] && 'floating' !== $settings['layout_type'] ) : ?>
									<label class="sifency-reg-label" for="reg_fname"><?php esc_html_e( 'First Name', 'sifency-addons' ); ?></label>
								<?php endif; ?>
								<input type="text" name="fname" id="reg_fname" class="sifency-reg-input" placeholder="<?php echo 'floating' === $settings['layout_type'] ? ' ' : 'First Name'; ?>">
								<?php if ( 'floating' === $settings['layout_type'] ) : ?>
									<label class="sifency-reg-label floating" for="reg_fname"><?php esc_html_e( 'First Name', 'sifency-addons' ); ?></label>
								<?php endif; ?>
							</div>
						<?php endif; ?>

						<?php if ( 'yes' === $settings['show_last_name'] ) : ?>
							<div class="sifency-field-group">
								<?php if ( 'yes' === $settings['show_labels'] && 'floating' !== $settings['layout_type'] ) : ?>
									<label class="sifency-reg-label" for="reg_lname"><?php esc_html_e( 'Last Name', 'sifency-addons' ); ?></label>
								<?php endif; ?>
								<input type="text" name="lname" id="reg_lname" class="sifency-reg-input" placeholder="<?php echo 'floating' === $settings['layout_type'] ? ' ' : 'Last Name'; ?>">
								<?php if ( 'floating' === $settings['layout_type'] ) : ?>
									<label class="sifency-reg-label floating" for="reg_lname"><?php esc_html_e( 'Last Name', 'sifency-addons' ); ?></label>
								<?php endif; ?>
							</div>
						<?php endif; ?>

						<!-- Password -->
						<div class="sifency-field-group">
							<?php if ( 'yes' === $settings['show_labels'] && 'floating' !== $settings['layout_type'] ) : ?>
								<label class="sifency-reg-label" for="reg_pass"><?php echo esc_html( $settings['pass_label'] ); ?></label>
							<?php endif; ?>
							<div class="sifency-password-wrap">
								<input type="password" name="password" id="reg_pass" class="sifency-reg-input" placeholder="<?php echo 'floating' === $settings['layout_type'] ? ' ' : esc_attr( $settings['pass_label'] ); ?>" required>
								<span class="sifency-pass-toggle"><i class="eicon-preview-medium"></i></span>
							</div>
							<?php if ( 'floating' === $settings['layout_type'] ) : ?>
								<label class="sifency-reg-label floating" for="reg_pass"><?php echo esc_html( $settings['pass_label'] ); ?></label>
							<?php endif; ?>
						</div>

						<!-- Actions -->
						<div class="sifency-reg-actions">
							<button type="submit" class="sifency-reg-submit sifency-button-style sifency-addons-primary-button">
								<span class="sifency-btn-text"><?php echo esc_html( $settings['btn_text'] ); ?></span>
								<span class="sifency-btn-loader" style="display:none;"><i class="eicon-loading eicon-animation-spin"></i></span>
							</button>
						</div>

						<div class="sifency-reg-message" style="display:none; margin-top:15px;"></div>

						<?php if ( ! empty( $settings['login_url']['url'] ) ) : ?>
							<div class="sifency-login-link">
								<a href="<?php echo esc_url( $settings['login_url']['url'] ); ?>"><?php echo esc_html( $settings['login_text'] ); ?></a>
							</div>
						<?php endif; ?>

					</form>
				</div>
			</div>

		</div>
		<?php
	}
}
