<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- admin view partial; local variables are not global
// phpcs:disable WordPress.Security.NonceVerification -- read-only admin view; capability check handled by admin menu registration

require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/scholar_sync_functions.php';

global $wpdb;

$author_id = isset( $_GET['author-id'] ) ? absint( wp_unslash( $_GET['author-id'] ) ) : 0;
if (empty($author_id)) {
    echo "<h1>Missing Author Id</h1>";
    die;
}

$table_name = SCHOLAR_SYNC_DB_AUTHORS;
$results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->prepare( 'SELECT * FROM %i WHERE author_id=%d', $table_name, $author_id) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- %i is the WordPress identifier placeholder (WP 6.2+)
);
if (count($results) != 1) {
    echo "<h1>Invalid Author Id</h1>";
    die;
}
$author_data = $results[0];
?>

<h1 class="top-header" style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px;">
    <span><?php echo esc_html($author_data->name) ?> Overview</span>
    <a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=scholar-sync-preview-slug&author-id=' . (int) $author_id ), 'scholar_sync_preview' ) ); ?>"
       target="wp-preview-author-<?php echo (int) $author_id; ?>"
       class="button button-secondary"
       style="font-size:13px;display:inline-flex;align-items:center;gap:6px;">
        <?php esc_html_e( 'Preview &amp; Shortcode', 'scholar-sync' ); ?> &#8599;
    </a>
</h1>
<hr class="wp-header-end">

<?php
if ( ! empty( $_GET['msg'] )
     && isset( $_GET['_wpnonce'] )
     && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'scholar_sync_notice' ) ) {
    $msg = new scholar_sync_message( sanitize_key( wp_unslash( $_GET['msg'] ) ) );
}

$save_error = sanitize_key( scholar_sync_get_param( 'save_error' ) );
if ( $save_error === 'missing_name' ) {
    echo '<div class="notice notice-error"><p>' . esc_html__( 'Full Name is required — your changes were not saved.', 'scholar-sync' ) . '</p></div>';
} elseif ( $save_error === 'save_failed' ) {
    echo '<div class="notice notice-error"><p>' . esc_html__( 'Could not save your changes — please check your input (e.g. a field may be too long) and try again.', 'scholar-sync' ) . '</p></div>';
}
?>

<ul class="nav nav-tabs">
    <?php
    $tabs = array(
        __("Author Details", "scholar-sync"),
        __("Articles", "scholar-sync"),
        __("Sync Details", "scholar-sync")
    );
    $active_tabs = array();
    if (!isset($_GET['tab'])) {
        $_GET['tab'] = 0;
    }
    $activeTab = absint( $_GET['tab'] );
    $params = array(
        'page'      => 'scholar-sync-authors-slug',
        'author-id' => $author_id,
    );
    foreach ($tabs as $index => $value) {
        $params['tab'] = $index;
        $query_result = http_build_query($params);
        $is_active = ( $activeTab == $index );
        array_push($active_tabs, $is_active ? ' active' : '');
        $ahref = '<a href="' . esc_url( admin_url( 'admin.php?' . $query_result ) ) . '">' . esc_html( $value ) . '</a>';
        echo '<li class="' . ( $is_active ? 'active' : '' ) . '">' . $ahref . '</li>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $ahref is already escaped above
    }
    ?>
</ul>

<div class="tab-content">
    <!-- TAB Author Details -->
    <div class="tab-pane<?php echo esc_attr( $active_tabs[0] ); ?>">
        <?php if (!empty($active_tabs[0])) {
            $action = 'update-author';
            $submit_label = 'Update Author';
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/author_edit_details_tab.php';
        }?>
    </div>
    <!-- TAB Articles -->
    <div class="tab-pane<?php echo esc_attr( $active_tabs[1] ); ?>">
        <?php if (!empty($active_tabs[1])) {
            if (scholar_sync_get_param('article-action') == 'add-article') {
                require_once plugin_dir_path(dirname(__FILE__)) . 'admin/author_edit_article_tab.php';
            } else {
                echo '<form action="' . esc_url( admin_url( 'admin.php' ) ) . '">';
                echo '<input type="hidden" name="page" value="scholar-sync-authors-slug" />';
                echo '<input type="hidden" name="author-id" value="' . esc_attr( absint( wp_unslash( $_GET['author-id'] ) ) ) . '" />';
                echo '<input type="hidden" name="tab" value="1" />';
                echo '<input type="hidden" name="article-action" value="add-article" />';
                echo '<input type="submit" id="search-submit" class="button" value="Add New Article">';
                echo '</form>';

                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/articles_list_view.php';

                $scholar_sync_articles_list_table = new ScholarSync_ArticlesListTableClass();
                $scholar_sync_articles_list_table->prepare_items();

                echo '<form method="post" name="frm_search_post" >';
                $scholar_sync_articles_list_table->search_box('Search Article(s)', 'search_post_id');
                echo '</form>';

                echo '<form id="bulk-actions" method="get">';
                echo '<input type="hidden" name="page" value="scholar-sync-actions-slug" />';
                echo '<input type="hidden" name="author-id" value="' . esc_attr( absint( wp_unslash( $_GET['author-id'] ) ) ) . '" />';
                echo '<input type="hidden" name="s" value="' . esc_attr( $scholar_sync_articles_list_table->get_search_term() ) . '" />';
                $scholar_sync_articles_list_table->display();
                echo '</form>';
            }
        }?>
    </div>
    <!-- TAB Sync -->
    <div class="tab-pane<?php echo esc_attr( $active_tabs[2] ); ?>">
        <?php if (!empty($active_tabs[2])) {
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/author_edit_sync_tab.php';
        }?>
    </div>
</div>
