<?php
//namespace CrtAddons\Admin\Templates\Library;
use CrtAddons\Classes\Utilities;
use CrtAddons\Admin\Templates\Library\CRT_Templates_Data;

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

/**
 * CRT_Templates_Library_Pages setup
 *
 * @since 1.0
 */
class CRT_Templates_Library_Pages {

	/**
	** Constructor
	*/
	public function __construct() {

		// Template Library Popup
		add_action( 'wp_ajax_render_library_templates_pages', [ $this, 'render_library_templates_pages' ] );
		add_action( 'wp_ajax_render_library_templates_pages_grid_items', [ $this, 'render_library_templates_pages_grid_items' ] );

	}

	/**
	** Template Library Popup
	*/
	public static function render_library_templates_pages() {
		$license = !defined('CRT_ADDONS_PRO_VERSION') || !crt_fs()->can_use_premium_code() ? 'free' : 'premium';

		?>

		<div class="crt-tplib-sidebar" data-license="<?php echo esc_attr($license); ?>">
			<div class="crt-tplib-price" data-filter="mixed">
				<h3>
					<span><?php esc_html_e( 'Mixed', 'crt-manage' ); ?></span>
					<i class="fas fa-angle-down"></i>
				</h3>
				
				<div class="crt-tplib-price-list">
					<ul>
						<li data-filter="mixed"><?php esc_html_e( 'Mixed', 'crt-manage' ); ?></li>
						<li data-filter="free"><?php esc_html_e( 'Free', 'crt-manage' ); ?></li>
						<li data-filter="pro"><?php esc_html_e( 'Premium', 'crt-manage' ); ?></li>
					</ul>
				</div>
			</div>
			<div class="crt-tplib-search">
				<input type="text" placeholder="Search Template">
				<i class="eicon-search"></i>
			</div>
		</div>

		<div class="crt-tplib-template-gird elementor-clearfix">
			<div class="crt-tplib-template-gird-inner">

				<?php //CRT_Templates_Library_Pages::render_library_templates_pages_grid_items(); ?>

			</div>

		<div class="crt-tplib-template-gird-loading">
			<div class="crt-wave">
				<div class="crt-rect crt-rect1"></div>
				<div class="crt-rect crt-rect2"></div>
				<div class="crt-rect crt-rect3"></div>
				<div class="crt-rect crt-rect4"></div>
				<div class="crt-rect crt-rect5"></div>
			</div>
		</div>
		
		<?php

		wp_die();
	}

	/**
	** Template Library Grid Items
	*/
	public static function render_library_templates_pages_grid_items() {
		$kits = CRT_Templates_Data::get_available_kits_for_pages();

		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$kits_per_page = 10; // should be set to 10
		
		$start = ($page - 1) * $kits_per_page;
		$end = $start + $kits_per_page;
		
		$kits = array_slice($kits, $start, $kits_per_page, true);
		
		foreach( $kits as $kit => $data ) :
			
			foreach( $data['pages'] as $key => $page ) :

				$template_title = $data['name'] .' - '. str_replace('-', ' ', ucwords($page));
				$template_price = ('pro' === $data['price'] && (!defined('CRT_ADDONS_PRO_VERSION') || !crt_fs()->can_use_premium_code()) ) ? 'pro' : 'free';
				$template_class = 'pro' === $template_price ? ' crt-tplib-pro-wrap' : '';
				$preview_url = $data['preview'][$key];

		?>

			<div class="crt-tplib-template-wrap<?php echo esc_attr($template_class); ?>" style="position:absolute;top:9999999999px;" data-title="<?php echo esc_attr(strtolower($template_title)); ?>" data-price="<?php echo esc_attr($template_price); ?>">
				<div class="crt-tplib-template" data-slug="<?php echo esc_attr($page); ?>" data-kit="<?php echo esc_attr($kit); ?>" data-preview-type="iframe" data-preview-url="<?php echo esc_attr($preview_url); ?>">
					<div class="crt-tplib-template-media">
					    <?php
					    $url = 'https://royal-elementor-addons.com/library/templates-kit/'. $kit .'/'. $page .'.jpg';
					    ?>
						<img class="lazy" src="<?php echo esc_url(CRT_MANAGE_URI .'assets/img/icon-256x256.png'); ?>" data-src="<?php echo esc_url('https://royal-elementor-addons.com/library/templates-kit/'. $kit .'/'. $page .'.jpg'); ?>">
						<div class="crt-tplib-template-media-overlay">
							<i class="eicon-eye"></i>
						</div>
					</div>
					<div class="crt-tplib-template-footer elementor-clearfix">
						<h3>
							<span><?php echo esc_html($data['name']) ?></span>
							<span><?php echo '&nbsp;- '. esc_html(ucwords($page)); ?></span>
						</h3>

						<?php if ( 'pro' === $data['price'] && (!defined('CRT_ADDONS_PRO_VERSION') || !crt_fs()->can_use_premium_code()) ) : ?>
							<span class="crt-tplib-insert-template crt-tplib-insert-pro"><i class="eicon-star"></i> <span><?php esc_html_e( 'Go Pro', 'crt-manage' ); ?></span></span>
						<?php else : ?>
							<span class="crt-tplib-insert-template"><i class="eicon-file-download"></i> <span><?php esc_html_e( 'Insert', 'crt-manage' ); ?></span></span>
						<?php endif; ?>
					</div>
				</div>
			</div>

		<?php endforeach;

		endforeach;

		wp_die();
	}
}

new CRT_Templates_Library_Pages();