<?php
/**
 * Form for downloading a bundle from prod.
 *
 * @package EaLab\WP_Deployer
 */

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

use EaLab\WP_Deployer\vendor\Ealab\Deployer\Utility\Settings;

if ( ! function_exists( 'deploy_sync_content__download_prod_bundle' ) ) {
	/**
	 * Form for downloading a bundle from prod.
	 */
	function deploy_sync_content__download_prod_bundle(): void {
		$endpoint_url = esc_attr( rest_url( 'deploy-sync-content/v1/prod/deployment-bundle/download' ) );
		?>
		<deploy-sync-content--download-prod-bundle data-url="<?php echo esc_url( $endpoint_url ); ?>" class="form-deploy">
			<div class="row g-2 align-items-center">
				<div class="col-auto">
					<label for="datatoexport"><?php echo esc_html__( 'From Date', 'deploy-sync-content' ); ?></label>
				</div>
				<div class="col-auto">
					<input
						class="form-control form-control-lg"
						name="since"
						required
						step="1"
						type="datetime-local"
						value="">
				</div>
				<div class="col-auto"><label for="bundle_label" class="col-auto">Bundle label (optional) </label></div>
				<div class="col-auto">
					<input name="bundle_label" id="bundle_label" type="text" class="form-control form-control-lg"></div>
				<div class="col-auto">
					<button name="submit" id="submit" class="btn btn-lg btn-primary">
						<img class="arrow-icon"
							src="<?php echo esc_attr( plugins_url( 'admin/assets/icons/arrow-right.svg', DEPLOY_SYNC_CONTENT__PLUGIN_FILE ) ) ?>"
							width="27" height="27" alt="Import">
					</button>
				</div>
				<div class="col-auto"><p class="description status-text"></p></div>
			</div>
		</deploy-sync-content--download-prod-bundle>
		<?php

		/**
		 * Enqueue the assets.
		 */
		wp_enqueue_style(
			'deploy-sync-content--download-prod-bundle',
			plugins_url( '/admin/assets/css/download-prod-bundle.css', DEPLOY_SYNC_CONTENT__PLUGIN_FILE ),
			array(),
			DEPLOY_SYNC_CONTENT__VERSION
		);

		wp_enqueue_script(
			'deploy-sync-content--download-prod-bundle',
			plugins_url( '/admin/assets/js/download-prod-bundle.js', DEPLOY_SYNC_CONTENT__PLUGIN_FILE ),
			array(),
			DEPLOY_SYNC_CONTENT__VERSION,
			true
		);

		// Get the remote base URL.
		$remote_base_url = Settings::getInstance()->get( Settings::SETTINGS__URL_PROD, '' );

		if ( ! empty( $remote_base_url ) ) {
			$remote_base_url = trailingslashit( $remote_base_url );
		}

		// Attach the nonce to the script.
		wp_localize_script(
			'deploy-sync-content--download-prod-bundle',
			'deploy_sync_content__component_settings',
			array(
				'rest' => array(
					'nonce' => wp_create_nonce( 'wp_rest' ),
				),
				'text' => array(
					'error'             => esc_html__( 'An error occurred. Please check your network status or the basic Deploy & Sync Content settings, or try again later.', 'deploy-sync-content' ),
					'requestError'      => esc_html__( 'The request failed.', 'deploy-sync-content' ),
					'requestSuccessful' => esc_html__( 'Done.', 'deploy-sync-content' ),
					'inProgress'        => esc_html__( 'In progress…', 'deploy-sync-content' ),
					'error_notAllowed'  =>
						sprintf(
							// translators: %1$s is a URL.
							__(
								'Please tick the checkbox on the settings page of the <a href="%1$s" target="_blank">remote</a> instance to allow the request, then return to this form to retry.',
								'deploy-sync-content'
							),
							esc_attr( $remote_base_url . 'wp-admin/' . DEPLOY_SYNC_CONTENT__BASIC_URI . '&tab=settings' ),
						),
				),
			)
		);
	}
}
