<?php
/**
 * Settings form view template
 *
 * @package FinalPOS
 */

if ( ! defined( 'ABSPATH' ) ) {
	die( 'Direct access is not allowed.' );
}

if ( ! is_admin() ) {
	return;
}

$company_name    = FinalPOS_Common::get_parsed_jwt_data( 'company' )['name'] ?? 'Your Company';
$consumer_key    = get_option( 'finalpos_consumer_key' );
$consumer_secret = get_option( 'finalpos_consumer_secret' );
$current_stage   = get_option( 'finalpos_current_stage' );

// Check site status for warnings.
$site_status = FinalPOS_Common::get_site_status_warnings();
$can_proceed = $site_status['can_proceed'];
$warnings    = $site_status['warnings'];
?>

<div class="finalpos-settings-section">
	<div class="last-step-header">
	<?php
	// Display site status warnings.
	if ( ! empty( $warnings ) ) :
		foreach ( $warnings as $warning ) :
			?>
			<div class="notice notice-<?php echo 'error' === $warning['type'] ? 'error' : 'warning'; ?>">
				<p><?php echo wp_kses_post( $warning['message'] ); ?></p>
			</div>
			<?php
		endforeach;
	endif;

	// Display error message if present (passed via redirect from admin-post.php).
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a display-only page showing redirect error messages.
	if ( isset( $_GET['error'] ) ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$error_message = sanitize_text_field( wp_unslash( $_GET['error'] ) );
		echo '<div class="notice notice-error">';
		echo '<p>' . esc_html( $error_message ) . '</p>';
		$hub_link = '<a href="' . esc_url( 'https://hub.finalpos.com' ) . '" target="_blank" rel="noopener noreferrer">hub.finalpos.com</a>';
		echo '<p>' . wp_kses_post(
			sprintf(
				/* translators: %s: link to hub.finalpos.com */
				__( 'Please make sure the WooCommerce extension is installed and configured in your FinalPOS account at %s.', 'finalpos' ),
				$hub_link
			)
		) . '</p>';
		echo '</div>';
	}
	?>

		<h2 class="finalpos-title-large"><?php esc_html_e( 'Connection ready!', 'finalpos' ); ?></h2>
		<p class="finalpos-text-large">
			<?php
			/* translators: %s: Company name */
			printf( esc_html__( 'Configure your sync preferences to complete setup with %s.', 'finalpos' ), '<span class="company-name-bold">' . esc_html( $company_name ) . '</span>' );
			?>
		</p>


		<div class="finalpos-settings-content">
			<main class="finalpos-settings-main">
				<div class="finalpos-settings-form">
				</div>
				<div class="finalposchoose-categories">
					<p class="finalpos-text-heading">
						<?php esc_html_e( 'Choose the categories you want to sync from your WooCommerce store to Final POS:', 'finalpos' ); ?>
					</p>

					<div class="finalpos-sync-categories">
						<div class="finalpos-sync-category" data-sync-type="products">
							<img src="<?php echo esc_url( FINALPOS_PLUGIN_URL . 'assets/img/sync-products-icon.svg' ); ?>"
								alt="Sync Products Icon">
							<div class="finalpos-sync-details">
								<p class="finalpos-text-medium">
									<?php esc_html_e( 'Products', 'finalpos' ); ?>
								</p>
								<p class="finalpos-text-small">
									<?php esc_html_e( 'Syncing all product categories.', 'finalpos' ); ?>
								</p>
								<a href="#" class="finalpos-sync-terms"
									data-sync-type="products"><?php esc_html_e( 'Configure your sync terms', 'finalpos' ); ?></a>
							</div>
							<label class="finalpos-toggle">
								<input type="checkbox" name="sync_products" class="sync-toggle"
									data-sync-type="products" checked <?php echo $can_proceed ? '' : 'disabled'; ?>>
								<span class="finalpos-toggle-slider"></span>
							</label>
						</div>

						<div class="finalpos-sync-category" data-sync-type="orders">
							<img src="<?php echo esc_url( FINALPOS_PLUGIN_URL . 'assets/img/sync-orders-icon.svg' ); ?>"
								alt="Sync Orders Icon">
							<div class="finalpos-sync-details">
								<p class="finalpos-text-medium">
									<?php esc_html_e( 'Orders', 'finalpos' ); ?>
								</p>
								<p class="finalpos-text-small">
									<?php esc_html_e( 'Syncing orders from the last 30 days.', 'finalpos' ); ?>
								</p>
								<a href="#" class="finalpos-sync-terms"
									data-sync-type="orders"><?php esc_html_e( 'Configure your sync terms', 'finalpos' ); ?></a>
							</div>
							<label class="finalpos-toggle">
								<input type="checkbox" name="sync_orders" class="sync-toggle" data-sync-type="orders"
									checked <?php echo $can_proceed ? '' : 'disabled'; ?>>
								<span class="finalpos-toggle-slider"></span>
							</label>
						</div>

						<div class="finalpos-sync-category" data-sync-type="customers">
							<img src="<?php echo esc_url( FINALPOS_PLUGIN_URL . 'assets/img/sync-customers-icon.svg' ); ?>"
								alt="Sync Customers Icon">
							<div class="finalpos-sync-details">
								<p class="finalpos-text-medium">
									<?php esc_html_e( 'Registered Customers', 'finalpos' ); ?>
								</p>
								<p class="finalpos-text-small">
									<?php esc_html_e( 'Syncing all registered customers.', 'finalpos' ); ?>
								</p>
							</div>
							<label class="finalpos-toggle">
								<input type="checkbox" name="sync_customers" class="sync-toggle"
									data-sync-type="customers" checked <?php echo $can_proceed ? '' : 'disabled'; ?>>
								<span class="finalpos-toggle-slider"></span>
							</label>
						</div>
					</div>
				</div>

				<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"
					class="finalpos-sync-form">
					<?php wp_nonce_field( 'final_settings_action', 'final_settings_nonce' ); ?>
					<input type="hidden" name="action" value="finalpos_complete_setup">

					<!-- Hidden fields for sync settings -->
					<input type="hidden" name="products_sync_enabled" id="products_sync_enabled" value="1">
					<input type="hidden" name="products_sync_categories" id="products_sync_categories" value="">

					<input type="hidden" name="orders_sync_enabled" id="orders_sync_enabled" value="1">
					<input type="hidden" name="orders_sync_from_date" id="orders_sync_from_date" value="">

					<input type="hidden" name="customers_sync_enabled" id="customers_sync_enabled" value="1">

					<button type="submit" id="finalpos-sync-button"
						class="finalpos-button" <?php echo $can_proceed ? '' : 'disabled'; ?>><?php esc_html_e( 'Start Sync', 'finalpos' ); ?></button>
				</form>

			</main>

			<div class="finalpos-settings-image-container">
				<img src="<?php echo esc_url( FINALPOS_PLUGIN_URL . 'assets/img/orders-customers-products.png' ); ?>"
					alt="Orders, Customers, Products Illustration" class="finalpos-settings-image">


			</div>

		</div>


		<div class="finalpos-details">
			<p class="finalpos-text-footer-title">
				<?php esc_html_e( 'How is my synced data being used?', 'finalpos' ); ?>
			</p>
			<p class="finalpos-text-footer">
				<?php esc_html_e( 'Rest assured, your WooCommerce data is secure and used exclusively to power the integration with Final POS. We do not sell or share your data with any third parties. For more information, please see our', 'finalpos' ); ?>
				<a href="https://finalpos.com/privacy-policy"
					target="_blank"><?php esc_html_e( 'privacy policy', 'finalpos' ); ?></a>.
			</p>
		</div>
	</div>


</div>

<?php
// Include modals.
require_once FINALPOS_PLUGIN_DIR . 'includes/admin/wizard/views/products-sync-modal.php';
require_once FINALPOS_PLUGIN_DIR . 'includes/admin/wizard/views/orders-sync-modal.php';
?>
