<?php
/**
 * Modern Preview Card View
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$cloudi7_icg_extra_data = json_decode( $cloudi7_icg_card->extra_data, true );
?>

<div class="cloudi7-icg-preview-container">
    
    <!-- Card Toolbar -->
    <div class="cloudi7-icg-card-toolbar no-print">
        <div class="cloudi7-icg-toolbar-left">
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=cloudi7-icg-all-cards' ) ); ?>" class="button">
                <span class="dashicons dashicons-arrow-left-alt2" style="margin-top:4px;"></span>
                <?php esc_html_e( 'Back to All Cards', 'cloudi7-id-card-generator' ); ?>
            </a>
        </div>
        <div class="cloudi7-icg-toolbar-right" style="display: flex; gap: 10px;">
            <button type="button" class="button button-primary" onclick="event.preventDefault(); window.cloudi7_icg_download_card('.cloudi7-icg-card-downloadable', 'ID-Card-<?php echo esc_js($cloudi7_icg_card->id_number); ?>');">
                <span class="dashicons dashicons-download" style="margin-top:4px;"></span>
                <?php esc_html_e( 'Download Card', 'cloudi7-id-card-generator' ); ?>
            </button>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=cloudi7-icg-create-card&id=' . $cloudi7_icg_card->id ) ); ?>" class="button">
                <span class="dashicons dashicons-edit" style="margin-top:4px;"></span>
                <?php esc_html_e( 'Edit Card', 'cloudi7-id-card-generator' ); ?>
            </a>
        </div>
    </div>

    <!-- Centered Card Display -->
    <div class="cloudi7-icg-card-display-grid card-print-area">
        <?php 
        $cloudi7_icg_skin        = get_option( 'cloudi7_icg_template_skin', 'modern' );
        $cloudi7_icg_front_file  = Cloudi7_ICG_PLUGIN_DIR . 'templates/' . $cloudi7_icg_skin . '/front.php';
        $cloudi7_icg_back_file   = Cloudi7_ICG_PLUGIN_DIR . 'templates/' . $cloudi7_icg_skin . '/back.php';
        
        if ( file_exists( $cloudi7_icg_front_file ) ) {
            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
            $card = $cloudi7_icg_card; // Temporary for templates compatibility, might need to update templates too
            echo '<div class="cloudi7-icg-card-preview-wrapper">';
            echo '<div class="cloudi7-icg-card-downloadable">';
            echo '<div class="cloudi7-icg-card-front-wrap">';
            include $cloudi7_icg_front_file;
            echo '</div>';
            if ( file_exists( $cloudi7_icg_back_file ) ) {
                echo '<div class="cloudi7-icg-card-back-wrap">';
                include $cloudi7_icg_back_file;
                echo '</div>';
            }
            echo '</div>';
            echo '</div>';
        } else {
            echo '<div class="notice notice-error"><p>' . esc_html__( 'Template not found.', 'cloudi7-id-card-generator' ) . '</p></div>';
        }
        ?>
    </div>
</div>
