<?php

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

function eapsgt_tab_template_settings( $object ) {
	wp_nonce_field( basename( __FILE__ ), "nonce3" );
	$templates = EAPSGT_AnimatedPopupConfig::templates();
	?>
    <div class="eapsgt-logo-list">
        <div class="eapsgt-logo-template">
			<?php
			foreach ( $templates as $index => $template ) {
				$config = json_decode( file_get_contents( EAPSGT_PLUGIN_DIR . '/includes/templates/jsons/' . $template . '.json' ) );
				?>
                <div class="eapsgt-item" id="eapsgt-item<?php echo esc_attr( $index + 1 ); ?>"
                     data-json="<?php echo htmlspecialchars( json_encode( $config ) ); ?>">
                    <h3 class="eapsgt-name"><?php esc_html_e( $config->name, 'easy-animated-popup' ); ?></h3>
                    <img class="eapsgt-img" id="eapsgt-img<?php echo esc_attr($template); ?>"
                         src="<?php echo esc_url(EAPSGT_PLUGIN_URL . $config->thumbnail) ?>" alt="preview"
                         width="200" height="267">
                </div>
				<?php
			}
			?>
        </div>
        <div class="eapsgt-logo-preview">
            <img id="eapsgt-preview"
                 src='<?php echo ( ( esc_attr( get_post_meta( get_the_ID(), 'eapsgt-template-popup', true ) ) == "" ) ?
                         esc_url( EAPSGT_PLUGIN_URL . 'assets/img/popup_01' ) :
                         esc_url( EAPSGT_PLUGIN_URL . 'assets/img/popup_0' . esc_attr( get_post_meta( get_the_ID(), 'eapsgt-template-popup', true ) ) ) )
			                     . '.png' ?>'
                 alt="img-preview"
                 width="400" height="534">
            <input type="hidden" id="eapsgt-template-popup" name="eapsgt-template-popup"
                   value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'eapsgt-template-popup', true ) ) ?>">
        </div>
    </div>
	<?php
}

add_filter( 'eapsgt_tab_template', 'eapsgt_tab_template_settings' );
