<?php
/**
 * Plugin Deactivation Handler.
 *
 * @package 3D Scan & Show
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Handles plugin deactivation modal display.
 */
class Scan_Show_Deactivation {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_deactivation_assets' ) );
		add_action( 'admin_footer-plugins.php', array( $this, 'render_deactivation_modal' ) );
	}

	/**
	 * Enqueue CSS and JS for deactivation modal on plugins page.
	 *
	 * @param string $hook The current admin page hook.
	 * @return void
	 */
	public function enqueue_deactivation_assets( $hook ) {
		if ( 'plugins.php' !== $hook ) {
			return;
		}

		wp_enqueue_style(
			'scanshow-deactivation',
			plugin_dir_url( dirname( __FILE__ ) ) . 'admin/css/deactivation.css',
			array(),
			'1.0.0'
		);

		wp_enqueue_script(
			'scanshow-deactivation',
			plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/deactivation.js',
			array( 'jquery' ),
			'1.0.0',
			true
		);

		wp_localize_script(
			'scanshow-deactivation',
			'stwDeactivation',
			array(
				'restUrl' => rest_url( '3d-scan-and-show/v1/' ),
				'nonce'   => wp_create_nonce( 'wp_rest' ),
			)
		);
	}

	/**
	 * Output deactivation modal markup.
	 *
	 * @return void
	 */
	public function render_deactivation_modal() {
		$screen = get_current_screen();
		if ( ! $screen || 'plugins' !== $screen->id ) {
			return;
		}
		?>
		<div class="stw-deactivate-modal" id="stw-deactivate-modal">
			<div class="stw-modal-wrap">
			
				<div class="stw-modal-header">
					<h2><?php esc_html_e( 'Quick feedback', '3d-scan-and-show' ); ?></h2>
					<button class="stw-modal-cancel"><span class="dashicons dashicons-no-alt"></span></button>
				</div>

				<div class="stw-modal-body">
					<p class="stw-feedback-intro">
						<?php esc_html_e( 'Sorry to see you go! Before you leave, could you tell us why? Your feedback helps us improve.', '3d-scan-and-show' ); ?>
					</p>
					
					<div class="stw-feedback-form">
						<label class="stw-reason-option">
							<input type="radio" name="stw-deactivate-reason" value="setup">
							<span class="stw-reason-text"><?php esc_html_e( 'The plugin didn\'t set up properly', '3d-scan-and-show' ); ?></span>
						</label>
						
						<label class="stw-reason-option">
							<input type="radio" name="stw-deactivate-reason" value="scan_quality">
							<span class="stw-reason-text"><?php esc_html_e( 'Couldn\'t get a good scan', '3d-scan-and-show' ); ?></span>
						</label>
						
						<label class="stw-reason-option">
							<input type="radio" name="stw-deactivate-reason" value="too_complex">
							<span class="stw-reason-text"><?php esc_html_e( 'Too complicated to use', '3d-scan-and-show' ); ?></span>
						</label>
						
						<label class="stw-reason-option">
							<input type="radio" name="stw-deactivate-reason" value="pricing">
							<span class="stw-reason-text"><?php esc_html_e( 'Pricing doesn\'t fit my needs', '3d-scan-and-show' ); ?></span>
						</label>
						
						<label class="stw-reason-option">
							<input type="radio" name="stw-deactivate-reason" value="not_needed">
							<span class="stw-reason-text"><?php esc_html_e( 'No longer need 3D product views', '3d-scan-and-show' ); ?></span>
						</label>
						
						<label class="stw-reason-option">
							<input type="radio" name="stw-deactivate-reason" value="other">
							<span class="stw-reason-text"><?php esc_html_e( 'Other reason', '3d-scan-and-show' ); ?></span>
						</label>
						
						<textarea 
							id="stw-deactivate-other-text" 
							class="stw-other-textarea" 
							placeholder="<?php esc_attr_e( 'Please tell us more...', '3d-scan-and-show' ); ?>"
							maxlength="300"
							style="display: none;"
						></textarea>
						
						<p class="stw-feedback-error" style="display: none;"></p>
					</div>
					
					<p class="stw-subscription-note">
						<span class="dashicons dashicons-info"></span>
						<?php esc_html_e( 'Your STW subscription stays active. You can reactivate anytime!', '3d-scan-and-show' ); ?>
					</p>
				</div>

				<div class="stw-modal-footer">
					<button class="stw-modal-deactivate stw-btn stw-btn-primary" disabled>
						<?php esc_html_e( 'Submit & Deactivate', '3d-scan-and-show' ); ?>
					</button>
					<a class="stw-modal-skip" href="#"><?php esc_html_e( 'Skip & Deactivate', '3d-scan-and-show' ); ?></a>
					<a class="stw-modal-cancel-link" href="#"><?php esc_html_e( 'Cancel', '3d-scan-and-show' ); ?></a>
				</div>
				
			</div>
		</div>
		<?php
	}
}
