<?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.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Local template-scope variables, not true globals.
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Admin view; per-line escaping verified at output sites.
// phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment -- Admin labels with simple placeholders documented in adjacent strings.
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only admin views; nonces enforced on action handlers.
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Embed & Preview metabox - shortcode generator, quick actions.
 * Rendered in the main content area with a 2-column layout.
 * Variables: $post, $meta
 */

$p      = TNC_FLIPBOOK_3D_PREFIX;
$width  = isset( $meta['width'] )  ? $meta['width']  : '100%';
$height = isset( $meta['height'] ) ? $meta['height'] : '600px';
$is_new = 'auto-draft' === $post->post_status;
$pid    = absint( $post->ID );
$permalink = get_permalink( $post );
$viewer_url = add_query_arg( 'tncfb3d_viewer', $pid, home_url( '/' ) );
?>
<div class="tnc-flipbook-admin tncfb-embed-box">

    <?php if ( $is_new ) : ?>
        <div class="tncfb-embed-empty">
            <svg width="20" height="20" 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"/></svg>
            <p><?php esc_html_e( 'Save the FlipBook first to get your shortcode and preview link.', 'tnc-flipbook-3d-lite' ); ?></p>
        </div>
    <?php else : ?>

        <div class="tncfb-embed-inner">
            <div class="tncfb-embed-card-shortcode">
                <div class="tncfb-embed-card-body">
                    <!-- Type selector -->
                    <div class="tncfb-embed-sc-top">
                        <select id="tncfb-shortcode-type" class="tncfb-embed-select">
                            <option value="embed"><?php esc_html_e( 'Embed (iframe)', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="link"><?php esc_html_e( 'Text / Button Link', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="image-link"><?php esc_html_e( 'Image Link', 'tnc-flipbook-3d-lite' ); ?></option>
                        </select>
                    </div>

                    <!-- Options (simplified — full control lives in Gutenberg/page-builder blocks) -->
                    <div class="tncfb-sc-options-body">
                            <!-- All types -->
                            <label class="tncfb-sc-field">
                                <span><?php esc_html_e( 'Start Page', 'tnc-flipbook-3d-lite' ); ?></span>
                                <input type="text" id="tncfb-sc-page" class="tncfb-sc-opt" data-attr="page" placeholder="1" style="width:50px;" />
                            </label>

                            <!-- Embed: container size -->
                            <div class="tncfb-sc-group" data-for="embed">
                                <label class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Width', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <input type="text" name="<?php echo esc_attr( $p ); ?>width" class="tncfb-sc-opt" data-attr="width" value="<?php echo esc_attr( $width ); ?>" placeholder="100%" style="width:70px;" />
                                </label>
                                <label class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Height', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <input type="text" name="<?php echo esc_attr( $p ); ?>height" class="tncfb-sc-opt" data-attr="height" value="<?php echo esc_attr( $height ); ?>" placeholder="600px" style="width:70px;" />
                                </label>
                            </div>

                            <!-- Embed: full-screen link toggle -->
                            <div class="tncfb-sc-group" data-for="embed">
                                <span class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Full-Screen Link', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <label class="tncfb-switcher-wrap tncfb-switcher-wrap-sm">
                                        <input type="checkbox" id="tncfb-sc-fs-link" class="tncfb-sc-opt" data-attr="fullscreen_link" data-val="true" />
                                        <span class="tncfb-switcher-slider tncfb-switcher-small"></span>
                                    </label>
                                </span>
                            </div>

                            <!-- Link-only -->
                            <div class="tncfb-sc-group" data-for="link" style="display:none;">
                                <label class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Link Text', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <input type="text" id="tncfb-sc-text" class="tncfb-sc-opt" data-attr="text" value="Read FlipBook" />
                                </label>
                                <label class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Mode', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <select id="tncfb-sc-mode" class="tncfb-sc-opt" data-attr="mode">
                                        <option value="popup"><?php esc_html_e( 'Popup', 'tnc-flipbook-3d-lite' ); ?></option>
                                        <option value="new_tab"><?php esc_html_e( 'New Tab', 'tnc-flipbook-3d-lite' ); ?></option>
                                        <option value="same_tab"><?php esc_html_e( 'Same Tab', 'tnc-flipbook-3d-lite' ); ?></option>
                                    </select>
                                </label>
                                <label class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Style', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <select id="tncfb-sc-style" class="tncfb-sc-opt" data-attr="style">
                                        <option value="text"><?php esc_html_e( 'Text Link', 'tnc-flipbook-3d-lite' ); ?></option>
                                        <option value="button" selected><?php esc_html_e( 'Button', 'tnc-flipbook-3d-lite' ); ?></option>
                                    </select>
                                </label>
                            </div>

                            <!-- Image-link-only -->
                            <div class="tncfb-sc-group" data-for="image-link" style="display:none;">
                                <label class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Image', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <input type="text" id="tncfb-sc-image" class="tncfb-sc-opt" data-attr="image" placeholder="<?php esc_attr_e( 'Auto (cover)', 'tnc-flipbook-3d-lite' ); ?>" style="width:140px;" />
                                    <button type="button" class="button button-small tncfb-sc-upload-btn"><?php esc_html_e( 'Browse', 'tnc-flipbook-3d-lite' ); ?></button>
                                </label>
                                <label class="tncfb-sc-field">
                                    <span><?php esc_html_e( 'Mode', 'tnc-flipbook-3d-lite' ); ?></span>
                                    <select id="tncfb-sc-img-mode" class="tncfb-sc-opt" data-attr="mode">
                                        <option value="popup"><?php esc_html_e( 'Popup', 'tnc-flipbook-3d-lite' ); ?></option>
                                        <option value="new_tab"><?php esc_html_e( 'New Tab', 'tnc-flipbook-3d-lite' ); ?></option>
                                        <option value="same_tab"><?php esc_html_e( 'Same Tab', 'tnc-flipbook-3d-lite' ); ?></option>
                                    </select>
                                </label>
                            </div>
                    </div>

                    <!-- Generated shortcode + copy -->
                    <div class="tncfb-embed-sc-output">
                        <code class="tncfb-embed-code" id="tncfb-shortcode-text"></code>
                        <button type="button" class="tncfb-embed-copy-btn tncfb-copy-shortcode-btn" data-clipboard="">
                            <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>
                            <?php esc_html_e( 'Copy', 'tnc-flipbook-3d-lite' ); ?>
                        </button>
                    </div>
                    <div class="tncfb-sc-promo" style="margin:12px -12px -12px;padding:10px 16px;background:#f0f6fc;border-top:1px solid #e2e8f0;text-align:center;font-size:12px;color:#6b7280;border-radius:0 0 4px 4px;">
                        <?php printf(
                            /* translators: 1: Gutenberg blocks link 2: page builder addons link */
                            esc_html__( 'For more options, use the %1$s or get %2$s.', 'tnc-flipbook-3d-lite' ),
                            '<a href="' . esc_url( admin_url( 'post-new.php?post_type=page' ) ) . '" style="color:#2271b1;text-decoration:none;">' . esc_html__( 'Gutenberg blocks', 'tnc-flipbook-3d-lite' ) . '</a>',
                            '<a href="' . esc_url( tncfb3d_utm( 'https://tncflipbook.com/addons/', 'addons', 'flipbook-sidebar' ) ) . '" target="_blank" style="color:#2271b1;text-decoration:none;">' . esc_html__( 'page builder addons', 'tnc-flipbook-3d-lite' ) . '</a>'
                        ); ?>
                    </div>
                </div>
                <input type="hidden" id="tncfb-sc-post-id" value="<?php echo $pid; ?>" />
            </div>
        </div>

    <?php endif; ?>

</div>
