<?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 tab; capability check handled by admin menu registration
// phpcs:disable WordPress.Security.ValidatedSanitizedInput -- GET params cast to int or sanitized in callers

    $button_label = isset($_GET['article-id']) ? 'Update' : 'Add';
    $cite_value   = isset($_GET['cite']) ? (int) $_GET['cite'] : 0;
    $year_value   = isset($_GET['year']) ? (int) $_GET['year'] : (int) gmdate('Y');

    $created_value      = '';
    $last_updated_value = '';
    $image_value        = scholar_sync_get_param( 'article-image' );
    $cite_sync          = 1;
    if ( ! empty( $_GET['article-id'] ) ) {
        global $wpdb;
        $table_articles = SCHOLAR_SYNC_DB_ARTICLES;
        $row = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- %i is the WordPress identifier placeholder (WP 6.2+)
                'SELECT created, last_updated, image_url, cited_by_value_sync FROM %i WHERE article_id = %d',
                $table_articles,
                (int) $_GET['article-id']
            )
        );
        if ( $row ) {
            $created_value      = date_i18n( 'Y-m-d H:i', strtotime( $row->created ) );
            $last_updated_value = date_i18n( 'Y-m-d H:i', strtotime( $row->last_updated ) );
            $cite_sync          = (int) $row->cited_by_value_sync;
            if ( '' === $image_value && ! empty( $row->image_url ) ) {
                $image_value = $row->image_url;
            }
        }
    }

?>

    <form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
        <?php wp_nonce_field('scholar_sync_add_article'); ?>
        <table class="form-table" role="presentation">
            <tbody>
                <input type="hidden" name="page" value="scholar-sync-actions-slug" />
                <input type="hidden" name="action" value="add-article" />
                <input type="hidden" name="tab" value="<?php echo esc_attr(scholar_sync_get_param('tab')) ?>" />
                <input type="hidden" name="author-id" value="<?php echo esc_attr(scholar_sync_get_param('author-id')) ?>" />
                <input type="hidden" name="article-id" value="<?php echo esc_attr(scholar_sync_get_param('article-id')) ?>" />
            <tr class="form-field form-required <?php echo esc_attr(scholar_sync_get_param('invalid_title'))  ?>">
                <th><label for="title">Title (required)</label></th>
                <td><input type="text" name="title" id="title" class="regular-text" value="<?php echo esc_attr(scholar_sync_get_param('title')) ?>" ></td>
            </tr>

            <tr>
                <th><label for="link">Link</label></th>
                <td><input type="text" name="link" id="link" value="<?php echo esc_attr(scholar_sync_get_param('link')) ?>" class="regular-text form-required"></td>
            </tr>

            <tr>
                <th><label for="authors">Authors</label></th>
                <td><input type="text" name="authors" id="authors" value="<?php echo esc_attr(scholar_sync_get_param('authors')) ?>" class="regular-text"></td>
            </tr>

            <tr>
                <th><label for="publication">Publication</label></th>
                <td><input type="text" name="publication" id="publication" value="<?php echo esc_attr(scholar_sync_get_param('publication')) ?>" class="regular-text"></td>
            </tr>

            <tr>
                <th><label for="article_image"><?php esc_html_e( 'Article image', 'scholar-sync' ); ?></label></th>
                <td>
                    <input type="text" name="article-image" id="article_image" value="<?php echo esc_attr( $image_value ); ?>" class="regular-text">
                    <button type="button" class="button" id="scholar-sync-select-article-image"><?php esc_html_e( 'Select Image', 'scholar-sync' ); ?></button>
                    <p class="description"><?php esc_html_e( 'e.g. graphical abstract or poster', 'scholar-sync' ); ?></p>
                    <img id="scholar-sync-article-image-preview" src="<?php echo esc_url( $image_value ); ?>" alt="<?php esc_attr_e( 'Article image preview', 'scholar-sync' ); ?>" <?php echo empty( $image_value ) ? 'hidden' : ''; ?>>
                </td>
            </tr>

            <tr class="form-field form-required <?php echo esc_attr(scholar_sync_get_param('invalid_cite'))  ?>">
                <th><label for="cite">Citations</label></th>
                <td>
                    <input type="number" step="1" min="0" name="cite" id="cite" value="<?php echo esc_attr($cite_value) ?>" class="form-field  small-text">
                    <?php if ( ! empty( $_GET['article-id'] ) && 0 === $cite_sync ) : ?>
                        <span class="scholar-sync-sync-badge" tabindex="0" role="button">Not synced</span>
                        <div class="scholar-sync-sync-explain" hidden>
                            <?php esc_html_e( "This field was edited manually, so sync won't overwrite it.", 'scholar-sync' ); ?>
                            <label>
                                <input type="checkbox" name="reenable_sync[]" value="cited_by_value">
                                <?php esc_html_e( 'Re-enable sync (takes effect on the next sync, not immediately — click Update to apply)', 'scholar-sync' ); ?>
                            </label>
                        </div>
                    <?php endif; ?>
                </td>
            </tr>

            <tr class="form-field form-required <?php echo esc_attr(scholar_sync_get_param('invalid_year')) ?>">
                <th><label for="year">Year</label></th>
                <td><input type="number" step="1" min="1500" name="year" id="year" value="<?php echo esc_attr($year_value) ?>" class="form-field  small-text"></td>
            </tr>

            <tr>
                <td class="scholar-sync-form-actions">
                    <input type="submit" value="<?php echo esc_attr($button_label) ?> Article" class="button-primary" />
                    <a href="<?php echo esc_url( add_query_arg( array( 'page' => 'scholar-sync-authors-slug', 'author-id' => scholar_sync_get_param('author-id'), 'tab' => '1' ), admin_url('admin.php') ) ); ?>"
                       class="button" style="margin-left:8px;"><?php esc_html_e( 'Cancel', 'scholar-sync' ); ?></a>
                </td>
                <?php if ( $created_value ) : ?>
                <td style="padding-top:4px;">
                    <span style="color:#666;font-size:12px;margin-right:16px;">
                        <?php esc_html_e( 'Created:', 'scholar-sync' ); ?>
                        <input type="text" readonly value="<?php echo esc_attr( $created_value ); ?>"
                               style="width:130px;background:#f0f0f1;border:1px solid #ddd;color:#666;font-size:12px;padding:2px 6px;">
                    </span>
                    <span style="color:#666;font-size:12px;">
                        <?php esc_html_e( 'Last updated:', 'scholar-sync' ); ?>
                        <input type="text" readonly value="<?php echo esc_attr( $last_updated_value ); ?>"
                               style="width:130px;background:#f0f0f1;border:1px solid #ddd;color:#666;font-size:12px;padding:2px 6px;">
                    </span>
                </td>
                <?php endif; ?>
            </tr>
            </tbody>
        </table>
    </form>

    <?php if ( $button_label === 'Add' ) : ?>
    <ul style="margin-top: 16px; color: #555; list-style: disc; padding-left: 20px;">
        <li><?php esc_html_e( 'Manually added articles are stored locally and are not synchronized with the Scholar Sync service. To update them, return to this form and edit the entry directly.', 'scholar-sync' ); ?></li>
    </ul>
    <?php endif; ?>
