<?php
namespace ElementPack\Modules\Countdown;

use ElementPack\Base\Element_Pack_Module_Base;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Module extends Element_Pack_Module_Base {

	public function __construct() {
		parent::__construct();

		add_action('wp_ajax_element_pack_countdown_end', [$this, 'countdown_end']);
		add_action('wp_ajax_nopriv_element_pack_countdown_end', [$this, 'countdown_end']);
		
	}

	public function get_name() {
		return 'countdown';
	}

	public function get_widgets() {

		$widgets = [
			'Countdown',
		];

		return $widgets;
	}


	public function countdown_end(){
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'element-pack-site' ) ) {
			wp_die( 'Security check failed' );
		}

		$wp_current_time = current_time( 'timestamp' ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
		$end_time = isset($_POST['endTime']) ? (int) $_POST['endTime'] : 0;

		if( $wp_current_time > $end_time ){
			echo "ended";
		}
		wp_die();

	}
 
}
 