<?php
/**
 *
 *
 * @var int             $current_page       Which page relative to the total of entries we are processing now.
 * @var int             $form_id            Which form we are dealing with.
 * @var array           $entries            Which entries we are modifying.
 * @var array           $selected_fields    Which fields were selected.
 * @var string          $view               Which view are in.
 * @var mixed           $form               Form settings.
 * @var ActionInterface $bulk_action_object Which bulk action rendered this modal.
 */

use \GravityKit\GravityActions\Assets;
use \GravityKit\GravityActions\Actions\EmailAction;
use GravityKit\GravityActions\Admin;
use \GravityKit\GravityActions\Actions\ActionInterface;

$back_button   = get_submit_button(
	esc_html__( 'Close', 'gk-gravityactions' ),
	'secondary featherlight-close',
	'submit',
	false,
	[
		'data-js'         => 'gk-gravityactions-modal-submit',
		'data-modal-view' => 'email/step-2',
	]
);
$cancel_button = get_submit_button(
	esc_html__( 'Cancel', 'gk-gravityactions' ),
	'secondary',
	'submit',
	false,
	[
		'data-js'         => 'gk-gravityactions-modal-submit',
		'data-modal-view' => 'email/step-complete',
	]
);

$done = ( $current_page + 1 )  * $per_page;
if ( ! empty( $email_data ) && is_array( $email_data ) ) {
	$done += count( $email_data );
}
$percentage_done = min( ( $done / count( $entries ) ) * 100, 100 );
$percentage_done_format = number_format( $percentage_done, 1 );
?>
<form
	class="gk-gravityactions"
	data-js="gk-gravityactions-modal-form"
>
	<?php $this->render( 'components/modal-fields', [ 'bulk_action' => EmailAction::get_key() ] ); ?>
	<?php $this->render( 'components/header' ); ?>
	<input type="hidden" name="current_page" value="<?php echo absint( $current_page ); ?>" />

	<div class="gk-gravityactions-modal-body">

		<div class="gk-gravityactions-modal-content">

			<div class="gk-gravityactions-modal-column gk-gravityactions-modal-column-center gk-gravityactions-modal-column-full-width">

				<img width="337" height="161" src="<?php echo Assets::get_url( 'src/assets/images/floaty-processing.svg' ); ?>" alt=""/>

				<span class="gk-gravityactions-modal-help-title">
					<?php esc_html_e( 'Sending your emails...', 'gk-gravityactions' ); ?>
				</span>

				<p class="gk-gravityactions-modal-help-subtitle">
					<?php _e( 'We\'re working hard on sending these emails, don\'t close this page until we are done.', 'gk-gravityactions' ); ?>
				</p>

				<div class="gk-gravityactions-modal-progress">
					<div class="gk-gravityactions-modal-progress-bar" style="width:<?php echo esc_attr( $percentage_done_format ); ?>%">
						<p class="gk-gravityactions-modal-progress-percent"><?php echo esc_html( $percentage_done_format ); ?>%</p>
					</div>
				</div>

				<?php if ( count( $entries ) > $done ) { ?>
				<script>
					GravityKit.GravityActions.Modal.currentTimeoutRequest = setTimeout(
						GravityKit.GravityActions.Actions.Email.handleNextPage,
						1000,
						<?php echo $form_id; ?>,
						<?php echo wp_json_encode( $entries ); ?>,
						'<?php echo $bulk_action_object::get_key(); ?>',
						'<?php echo wp_create_nonce( Admin::$modal_nonce_action ); ?>',
						'<?php echo Admin::$modal_nonce_name; ?>',
						'<?php echo $current_page + 1; ?>',
						<?php echo wp_json_encode( $field_values ); ?>
					);
				</script>
				<?php } else { ?>
				<script>
					GravityKit.GravityActions.Modal.currentTimeoutRequest = setTimeout(
						GravityKit.GravityActions.Actions.Email.onCompleteUpdating,
						3000,
						<?php echo $form_id; ?>,
						<?php echo wp_json_encode( $entries ); ?>,
						'<?php echo $bulk_action_object::get_key(); ?>',
						'<?php echo wp_create_nonce( Admin::$modal_nonce_action ); ?>',
						'<?php echo Admin::$modal_nonce_name; ?>'
					);
				</script>
				<?php } ?>
			</div>

		</div>

	</div>
	<?php $this->render( 'components/footer', [
		'total_steps'      => false,
		'left_navigation'  => $back_button,
		'right_navigation' => null,
	] ); ?>
</form>
