<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Template-scope variables and single-post admin meta reads, not globals or slow queries.
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Tab 1 - Content Source
 *
 * Variables: $post, $meta, $schema, $global
 */

$p           = TNC_FLIPBOOK_3D_PREFIX;
$source_type = isset( $meta['source_type'] ) ? $meta['source_type'] : 'pdf';
$pdf_id      = isset( $meta['pdf_id'] )      ? absint( $meta['pdf_id'] ) : 0;
$pdf_ids     = isset( $meta['pdf_ids'] )     ? (array) $meta['pdf_ids'] : array();
$image_ids   = isset( $meta['image_ids'] )   ? (array) $meta['image_ids'] : array();

$is_pro = function_exists( 'tnc_flipbook_is_pro' ) && tnc_flipbook_is_pro();

$source_types = array(
    'pdf'       => array(
        'icon'  => '<svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>',
        'title' => __( 'Single PDF', 'tnc-flipbook-3d-lite' ),
        'desc'  => __( 'Upload or select one PDF file. Each page becomes a flipbook page.', 'tnc-flipbook-3d-lite' ),
        'pro'   => false,
    ),
    'images'    => array(
        'icon'  => '<svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>',
        'title' => __( 'Image Gallery', 'tnc-flipbook-3d-lite' ),
        'desc'  => __( 'Use individual images (JPG, PNG, WebP) as flipbook pages.', 'tnc-flipbook-3d-lite' ),
        'pro'   => false,
    ),
);
?>
<div class="tncfb-content-tab">
    <h3 class="tncfb-section-title"><?php esc_html_e( 'Choose Your Content Source', 'tnc-flipbook-3d-lite' ); ?><?php tncfb3d_tip( 'source_type' ); ?></h3>
    <p class="tncfb-section-desc"><?php esc_html_e( 'Select how you want to provide the content for your flipbook.', 'tnc-flipbook-3d-lite' ); ?></p>

    <!-- Source type card selector -->
    <div class="tncfb-source-cards">
        <?php foreach ( $source_types as $type_key => $type ) :
            $is_gated = ! $is_pro && ! empty( $type['pro'] );
            $card_classes = 'tncfb-source-card';
            if ( $source_type === $type_key ) { $card_classes .= ' is-active'; }
            if ( $is_gated )                  { $card_classes .= ' tncfb-pro-gated'; }
        ?>
            <label class="<?php echo esc_attr( $card_classes ); ?>"
                   data-type="<?php echo esc_attr( $type_key ); ?>">
                <input type="radio"
                       name="<?php echo esc_attr( $p ); ?>source_type"
                       value="<?php echo esc_attr( $type_key ); ?>"
                       <?php checked( $source_type, $type_key ); ?>
                       <?php echo $is_gated ? 'disabled' : ''; ?> />
                <?php if ( $is_gated ) : ?>
                    
                <?php endif; ?>
                <span class="tncfb-card-icon"><?php echo $type['icon']; // phpcs:ignore -- SVG markup ?></span>
                <strong class="tncfb-card-title"><?php echo esc_html( $type['title'] ); ?></strong>
                <span class="tncfb-card-desc"><?php echo esc_html( $type['desc'] ); ?></span>
            </label>
        <?php endforeach; ?>
    </div>

    <?php
    // Track which source types have data (for JS-driven notice)
    $has_data = array(
        'pdf'       => $pdf_id > 0,
        'multi_pdf' => ! empty( array_filter( $pdf_ids ) ),
        'images'    => ! empty( array_filter( $image_ids ) ),
    );
    $source_labels = array(
        'pdf'       => __( 'Single PDF', 'tnc-flipbook-3d-lite' ),
        'multi_pdf' => __( 'Multiple PDFs', 'tnc-flipbook-3d-lite' ),
        'images'    => __( 'Image Gallery', 'tnc-flipbook-3d-lite' ),
    );
    ?>
    <div class="tncfb-banner is-warn" id="tncfb-source-notice" style="margin-top:12px;display:none;">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>
        <span id="tncfb-source-notice-text"></span>
    </div>
    <?php ob_start(); ?>
    (function(){
        var hasData = <?php echo wp_json_encode( $has_data ); ?>;
        var labels  = <?php echo wp_json_encode( $source_labels ); ?>;
        var notice  = document.getElementById('tncfb-source-notice');
        var text    = document.getElementById('tncfb-source-notice-text');

        function updateNotice() {
            var active = document.querySelector('input[name="<?php echo esc_js( $p ); ?>source_type"]:checked');
            if (!active) return;
            var current = active.value;
            var others = [];
            for (var key in hasData) {
                if (key !== current && hasData[key]) {
                    others.push('<strong>' + labels[key] + '</strong>');
                }
            }
            if (others.length > 0) {
                text.innerHTML = '<?php echo esc_js( __( 'You also have content in', 'tnc-flipbook-3d-lite' ) ); ?> ' + others.join(', ') + '. <?php echo esc_js( __( 'Only the selected source type above will be used.', 'tnc-flipbook-3d-lite' ) ); ?>';
                notice.style.display = 'flex';
            } else {
                notice.style.display = 'none';
            }
        }

        document.querySelectorAll('input[name="<?php echo esc_js( $p ); ?>source_type"]').forEach(function(r) {
            r.addEventListener('change', updateNotice);
        });

        updateNotice();
    })();
    <?php wp_add_inline_script( 'tncfb3d-admin', ob_get_clean() ); ?>

    <!-- Single PDF section -->
    <div class="tncfb-source-section" id="tncfb-source-pdf"
         <?php echo 'pdf' !== $source_type ? 'style="display:none"' : ''; ?>>
        <div class="tncfb-source-inner">
            <?php
            $pdf_title = $pdf_id ? get_the_title( $pdf_id ) : '';
            $pdf_meta  = $pdf_id ? wp_get_attachment_metadata( $pdf_id ) : null;
            $pdf_pages = '';
            if ( $pdf_meta && ! empty( $pdf_meta['filesize'] ) ) {
                $pdf_pages = size_format( $pdf_meta['filesize'], 1 );
            } elseif ( $pdf_id ) {
                $file_path = get_attached_file( $pdf_id );
                if ( $file_path && file_exists( $file_path ) ) {
                    $pdf_pages = size_format( filesize( $file_path ), 1 );
                }
            }
            ?>
            <input type="hidden"
                   id="<?php echo esc_attr( $p ); ?>pdf_id"
                   name="<?php echo esc_attr( $p ); ?>pdf_id"
                   value="<?php echo esc_attr( $pdf_id ); ?>" />

            <?php if ( $pdf_id && $pdf_title ) : ?>
                <!-- PDF attached: show file card -->
                <div class="tncfb-pdf-card" id="tncfb-pdf-card">
                    <div class="tncfb-pdf-card-icon">
                        <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="#e74c3c" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                    </div>
                    <div class="tncfb-pdf-card-info">
                        <strong class="tncfb-pdf-card-name"><?php echo esc_html( $pdf_title ); ?></strong>
                        <?php if ( $pdf_pages ) : ?>
                            <span class="tncfb-pdf-card-meta"><?php echo esc_html( $pdf_pages ); ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="tncfb-pdf-card-actions">
                        <button type="button" class="tncfb-media-upload tncfb-pdf-card-btn" data-media-type="application/pdf">
                            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
                            <?php esc_html_e( 'Replace', 'tnc-flipbook-3d-lite' ); ?>
                        </button>
                        <button type="button" class="tncfb-media-remove tncfb-pdf-card-btn tncfb-pdf-card-btn-danger">
                            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
                            <?php esc_html_e( 'Remove', 'tnc-flipbook-3d-lite' ); ?>
                        </button>
                    </div>
                </div>
            <?php else : ?>
                <!-- No PDF: show upload prompt -->
                <div class="tncfb-pdf-dropzone tncfb-media-upload" id="tncfb-pdf-dropzone" data-media-type="application/pdf">
                    <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#2271b1" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                    <p class="tncfb-pdf-dropzone-text"><?php esc_html_e( 'Select PDF', 'tnc-flipbook-3d-lite' ); ?></p>
                    <span class="tncfb-pdf-dropzone-hint"><?php esc_html_e( 'Upload or choose from Media Library', 'tnc-flipbook-3d-lite' ); ?></span>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Image gallery section -->
    <div class="tncfb-source-section" id="tncfb-source-images"
         <?php echo 'images' !== $source_type ? 'style="display:none"' : ''; ?>>
        <div class="tncfb-source-inner">
            <h4><?php esc_html_e( 'Image Gallery', 'tnc-flipbook-3d-lite' ); ?></h4>
            <p class="tncfb-field-desc"><?php esc_html_e( 'Each image will be one flipbook page. Drag to reorder.', 'tnc-flipbook-3d-lite' ); ?></p>
            <div class="tncfb-image-gallery" id="tncfb-image-gallery">
                <?php foreach ( $image_ids as $img_id ) :
                    $img_id  = absint( $img_id );
                    $img_src = wp_get_attachment_image_src( $img_id, 'thumbnail' );
                ?>
                    <div class="tncfb-gallery-thumb" data-id="<?php echo esc_attr( $img_id ); ?>">
                        <input type="hidden"
                               name="<?php echo esc_attr( $p ); ?>image_ids[]"
                               value="<?php echo esc_attr( $img_id ); ?>" />
                        <?php if ( $img_src ) : ?>
                            <img src="<?php echo esc_url( $img_src[0] ); ?>" alt="" />
                        <?php endif; ?>
                        <button type="button" class="tncfb-gallery-remove">
                            <span class="dashicons dashicons-no-alt"></span>
                        </button>
                    </div>
                <?php endforeach; ?>
            </div>
            <button type="button" class="button" id="tncfb-add-images-btn">
                <span class="dashicons dashicons-plus-alt2"></span>
                <?php esc_html_e( 'Add / Edit Images', 'tnc-flipbook-3d-lite' ); ?>
            </button>
        </div>
    </div>
</div>
