<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- template file, variables are local to included scope

// variables defined in author_edit_view.php
/** @var array $author_data */
/** @var string $action */
/** @var string $submit_label */

$author_created_value      = ! empty( $author_data->created )
    ? date_i18n( 'Y-m-d H:i', strtotime( $author_data->created ) ) : '';
$author_last_updated_value = ! empty( $author_data->last_updated )
    ? date_i18n( 'Y-m-d H:i', strtotime( $author_data->last_updated ) ) : '';

?>

    <form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
        <?php wp_nonce_field('scholar_sync_update_author'); ?>
        <table class="form-table" role="presentation">
            <tbody>
            <input type="hidden" name="page" value="scholar-sync-actions-slug" />
            <input type="hidden" name="action" value="<?php echo esc_attr($action) ?>" />
            <input type="hidden" name="author-id" value="<?php echo esc_attr(scholar_sync_get_param('author-id')) ?>" />

            <tr class="form-field form-required <?php echo esc_attr(scholar_sync_get_param('invalid_name'))  ?>">
                <th><label for="name">Full Name (required)</label></th>
                <td><input type="text" name="name" id="name" class="regular-text" value="<?php echo esc_attr($author_data->name)  ?>" ></td>
            </tr>

            <tr>
                <th><label for="position"><?php esc_html_e( 'Position / Title', 'scholar-sync' ); ?></label></th>
                <td>
                    <input type="text" name="position" id="position" value="<?php echo esc_attr( $author_data->position ) ?>" class="regular-text" placeholder="<?php esc_attr_e( 'e.g. Associate Professor', 'scholar-sync' ); ?>">
                </td>
            </tr>

            <tr>
                <th><label for="affiliations">Affiliations</label></th>
                <td><input type="text" name="affiliations" id="affiliations" value="<?php echo esc_attr($author_data->affiliations) ?>" class="regular-text"></td>
            </tr>

            <tr>
                <th><label for="thumbnail">Thumbnail Image</label></th>
                <td>
                    <input type="text" name="thumbnail" id="thumbnail" value="<?php echo esc_attr($author_data->thumbnail) ?>" class="regular-text">
                    <button type="button" class="button" id="scholar-sync-select-image" style="margin-left: 4px;"><?php esc_html_e('Select Image', 'scholar-sync'); ?></button>
                    <br>
                    <img id="scholar-sync-thumbnail-preview"
                         src="<?php echo esc_url( ! empty( $author_data->thumbnail ) ? $author_data->thumbnail : SCHOLAR_SYNC_PLUGIN_URL . 'includes/images/blank_profile.png' ); ?>"
                         alt="<?php esc_attr_e( 'Thumbnail preview', 'scholar-sync' ); ?>"
                         data-default="<?php echo esc_url( SCHOLAR_SYNC_PLUGIN_URL . 'includes/images/blank_profile.png' ); ?>">
                </td>
            </tr>

            <tr>
                <th><label for="interests">Interests</label></th>
                <td>
                    <textarea type="text" name="interests" id="interests" rows="3" class="regular-text"><?php echo esc_textarea($author_data->interests) ?></textarea>
                    <?php if ( (int) $author_data->interests_sync === 0 ) : ?>
                        <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="interests">
                                <?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">
                <th><label for="cite">Citations Total</label></th>
                <td>
                    <input type="number" step="1" min="0" name="cite" id="cite" value="<?php echo esc_attr($author_data->citations_all) ?>" class="regular-text">
                    <?php if ( (int) $author_data->citations_all_sync === 0 ) : ?>
                        <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="citations_all">
                                <?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">
                <th><label for="h_index">h-index</label></th>
                <td>
                    <input type="number" step="1" min="0" name="h_index" id="h_index" value="<?php echo esc_attr($author_data->h_index_all) ?>" class="regular-text">
                    <?php if ( (int) $author_data->h_index_all_sync === 0 ) : ?>
                        <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="h_index_all">
                                <?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">
                <th><label for="i10_index">i10-index</label></th>
                <td>
                    <input type="number" step="1" min="0" name="i10_index" id="i10_index" value="<?php echo esc_attr($author_data->i10_index_all) ?>" class="regular-text">
                    <?php if ( (int) $author_data->i10_index_all_sync === 0 ) : ?>
                        <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="i10_index_all">
                                <?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>
                <th><label for="directory_link"><?php esc_html_e( 'Author Page URL', 'scholar-sync' ); ?></label></th>
                <td>
                    <input type="url" name="directory_link" id="directory_link" value="<?php echo esc_attr( $author_data->directory_link ) ?>" class="regular-text">
                </td>
            </tr>

            <tr>
                <td>
                    <input type="submit" id="update-author" class="button" value="<?php echo esc_attr($submit_label) ?>" />
                </td>
                <?php if ( $author_created_value ) : ?>
                <td style="padding-top:4px;">
                    <span class="scholar-sync-meta-label">
                        <?php esc_html_e( 'Created:', 'scholar-sync' ); ?>
                        <input type="text" readonly class="scholar-sync-meta-input" value="<?php echo esc_attr( $author_created_value ); ?>">
                    </span>
                    <span class="scholar-sync-meta-label">
                        <?php esc_html_e( 'Last updated:', 'scholar-sync' ); ?>
                        <input type="text" readonly class="scholar-sync-meta-input" value="<?php echo esc_attr( $author_last_updated_value ); ?>">
                    </span>
                </td>
                <?php endif; ?>
            </tr>
            </tbody>
        </table>
    </form>

    <?php if ( $action === 'add-author' ) : ?>
    <ul style="margin-top: 16px; color: #555; list-style: disc; padding-left: 20px;">
        <li><?php esc_html_e( 'You can add unlimited manual authors. Since they are not synced with our service, their articles must also be added manually.', 'scholar-sync' ); ?></li>
        <li><?php esc_html_e( 'Author Page URL: the public profile link shown in group grids. Leave blank, it auto-fills when you publish the author\'s page from the Preview &amp; Publish screen.', 'scholar-sync' ); ?></li>
    </ul>
    <?php endif; ?>
