<?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 5 - SEO
 * Variables: $post, $meta, $schema, $global
 */

$p = TNC_FLIPBOOK_3D_PREFIX;
?>
<div class="tncfb-seo-tab">

    <p class="tncfb-section-desc"><?php esc_html_e( 'Control how this flipbook appears in search engines and social media. Leave fields blank to use defaults.', 'tnc-flipbook-3d-lite' ); ?></p>

    <!-- Search Engine -->
    <div class="tncfb-section-divider"><h4><?php esc_html_e( 'Search Engine', 'tnc-flipbook-3d-lite' ); ?></h4></div>

    <table class="form-table">
        <tr>
            <th><label for="tncfb3d_seo_title"><?php esc_html_e( 'SEO Title', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'seo_title' ); ?></th>
            <td>
                <input type="text" id="tncfb3d_seo_title" name="<?php echo esc_attr( $p ); ?>seo_title"
                       value="<?php echo esc_attr( isset( $meta['seo_title'] ) ? $meta['seo_title'] : '' ); ?>"
                       class="large-text" placeholder="<?php echo esc_attr( get_the_title( $post->ID ) ); ?>" />
                <p class="description"><?php esc_html_e( 'Custom title for search engines. Leave blank to use post title. Recommended: under 60 characters.', 'tnc-flipbook-3d-lite' ); ?></p>
            </td>
        </tr>
        <tr>
            <th><label for="tncfb3d_seo_desc"><?php esc_html_e( 'Meta Description', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'seo_description' ); ?></th>
            <td>
                <textarea id="tncfb3d_seo_desc" name="<?php echo esc_attr( $p ); ?>seo_description"
                          class="large-text" rows="3" maxlength="160"
                          placeholder="<?php esc_attr_e( 'Brief description for search results...', 'tnc-flipbook-3d-lite' ); ?>"
                ><?php echo esc_textarea( isset( $meta['seo_description'] ) ? $meta['seo_description'] : '' ); ?></textarea>
                <p class="description"><?php esc_html_e( 'Shown in search results below the title. Recommended: 50-160 characters.', 'tnc-flipbook-3d-lite' ); ?></p>
            </td>
        </tr>
        <tr>
            <th><label><?php esc_html_e( 'Search Visibility', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'seo_visibility' ); ?></th>
            <td>
                <?php $robots = isset( $meta['seo_robots'] ) ? $meta['seo_robots'] : 'index'; ?>
                <select name="<?php echo esc_attr( $p ); ?>seo_robots">
                    <option value="index" <?php selected( $robots, 'index' ); ?>><?php esc_html_e( 'Visible to search engines (index)', 'tnc-flipbook-3d-lite' ); ?></option>
                    <option value="noindex" <?php selected( $robots, 'noindex' ); ?>><?php esc_html_e( 'Hidden from search engines (noindex)', 'tnc-flipbook-3d-lite' ); ?></option>
                </select>
            </td>
        </tr>
        <tr>
            <th><label><?php esc_html_e( 'Canonical URL', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'seo_canonical' ); ?></th>
            <td>
                <input type="url" name="<?php echo esc_attr( $p ); ?>seo_canonical"
                       value="<?php echo esc_attr( isset( $meta['seo_canonical'] ) ? $meta['seo_canonical'] : '' ); ?>"
                       class="large-text" placeholder="<?php echo esc_url( get_permalink( $post->ID ) ); ?>" />
                <p class="description"><?php esc_html_e( 'Leave blank to use the default permalink. Set a custom URL if this content also exists elsewhere.', 'tnc-flipbook-3d-lite' ); ?></p>
            </td>
        </tr>
    </table>

    <!-- Social Sharing -->
    <div class="tncfb-section-divider"><h4><?php esc_html_e( 'Social Sharing', 'tnc-flipbook-3d-lite' ); ?></h4></div>

    <table class="form-table">
        <tr>
            <th><label><?php esc_html_e( 'Social Image', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'seo_og_image' ); ?></th>
            <td>
                <?php
                $og_img_id  = absint( isset( $meta['seo_og_image_id'] ) ? $meta['seo_og_image_id'] : 0 );
                $og_img_url = $og_img_id ? wp_get_attachment_image_src( $og_img_id, 'medium' ) : null;
                ?>
                <div class="tncfb-settings-media" data-media-type="image">
                    <input type="hidden" name="<?php echo esc_attr( $p ); ?>seo_og_image_id" value="<?php echo esc_attr( $og_img_id ); ?>" class="tncfb-media-input" />
                    <div class="tncfb-media-preview <?php echo $og_img_id ? '' : 'tncfb-hidden'; ?>">
                        <?php if ( $og_img_url ) : ?><img src="<?php echo esc_url( $og_img_url[0] ); ?>" alt="" class="tncfb-media-thumb" /><?php endif; ?>
                        <span class="tncfb-media-filename"><?php echo esc_html( $og_img_id ? get_the_title( $og_img_id ) : '' ); ?></span>
                        <button type="button" class="tncfb-media-remove tncfb-btn-pill tncfb-btn-pill--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>
                    <button type="button" class="tncfb-media-upload button"><?php esc_html_e( 'Choose Image', 'tnc-flipbook-3d-lite' ); ?></button>
                    <p class="description"><?php esc_html_e( 'Image shown when shared on Facebook, Twitter, LinkedIn, etc. Recommended: 1200x630px. Falls back to featured image.', 'tnc-flipbook-3d-lite' ); ?></p>
                </div>
            </td>
        </tr>
    </table>

    <!-- Addon hook: SEO addon can inject additional sections here -->
    <?php do_action( 'tncfb3d_seo_admin_fields', $post, $meta ); ?>


</div><!-- .tncfb-seo-tab -->
