<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Local template scope variables.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$this->render_page_header(
    __( 'nnax User Files - General', 'nnax-frontend-user-files' ),
    __( 'Configure the core frontend settings and custom metadata fields.', 'nnax-frontend-user-files' ),
    'general'
);

// Ensure variables exist for static analysis and when template is included directly.
$custom_meta_fields = isset( $custom_meta_fields ) && is_array( $custom_meta_fields ) ? $custom_meta_fields : array();
$settings = isset( $settings ) && is_array( $settings ) ? $settings : array();
$yootheme_active = NNAXFUF_Helpers::is_yootheme_active();
?>
<form method="post" action="options.php" class="nnaxadmin-form">
    <?php settings_fields( 'nnaxfuf_settings_group' ); ?>
    <?php
    $main_headline = isset( $settings['main_headline'] ) ? trim( (string) $settings['main_headline'] ) : '';
    if ( '' === $main_headline ) {
        $main_headline = __( 'My Files', 'nnax-frontend-user-files' );
    }
    ?>

    <div class="nnax-grid nnax-grid-2">
        <?php $this->render_card_start( __( 'General options', 'nnax-frontend-user-files' ), __( 'Define the main frontend behavior and title.', 'nnax-frontend-user-files' ) ); ?>
            <label for="nnaxadmin-main-headline"><strong><?php esc_html_e( 'Frontend title', 'nnax-frontend-user-files' ); ?></strong></label>
            <p class="nnax-card__muted"><?php esc_html_e( 'Enter the title shown in the frontend file manager.', 'nnax-frontend-user-files' ); ?></p>
            <input
                id="nnaxadmin-main-headline"
                type="text"
                name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[main_headline]"
                value="<?php echo esc_attr( $main_headline ); ?>"
                class="regular-text"
                placeholder="<?php echo esc_attr__( 'My Files', 'nnax-frontend-user-files' ); ?>"
            />

            <hr>
            <label for="nnaxadmin-load-plugin-uikit">
                <strong><?php esc_html_e( 'Load bundled UIkit assets', 'nnax-frontend-user-files' ); ?></strong>
            </label>
            <p class="nnax-card__muted">
                <?php esc_html_e( 'Loads this plugin\'s bundled UIkit CSS and JS files on the file manager page. Turn it off only if your theme or another plugin already loads UIkit on the same page.', 'nnax-frontend-user-files' ); ?>
            </p>
            <?php if ( $yootheme_active ) : ?>
                <p class="nnax-card__muted">
                    <?php esc_html_e( 'YOOtheme is active, so the plugin will not load its own UIkit files to avoid duplicates.', 'nnax-frontend-user-files' ); ?>
                </p>
            <?php endif; ?>
            <label>
                <input
                    type="hidden"
                    name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[load_plugin_uikit]"
                    value="0"
                />
                <input
                    id="nnaxadmin-load-plugin-uikit"
                    type="checkbox"
                    name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[load_plugin_uikit]"
                    value="1"
                    <?php checked( ! empty( $settings['load_plugin_uikit'] ) ); ?>
                >
                <?php esc_html_e( 'Yes, load the bundled UIkit assets on the file manager page.', 'nnax-frontend-user-files' ); ?>
            </label>

            <hr>
            <label for="nnaxadmin-delete-all-on-uninstall">
                <strong><?php esc_html_e( 'Delete all plugin data on uninstall', 'nnax-frontend-user-files' ); ?></strong>
            </label>
            <p class="nnax-card__muted"><?php esc_html_e( 'If enabled, uninstall removes plugin options, activity table, all managed file/folder posts, and physical files in allowed storage paths.', 'nnax-frontend-user-files' ); ?></p>
            <label>
                <input
                    type="hidden"
                    name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[delete_all_on_uninstall]"
                    value="0"
                />
                <input
                    id="nnaxadmin-delete-all-on-uninstall"
                    type="checkbox"
                    name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[delete_all_on_uninstall]"
                    value="1"
                    <?php checked( ! empty( $settings['delete_all_on_uninstall'] ) ); ?>
                >
                <?php esc_html_e( 'Yes, remove everything on uninstall.', 'nnax-frontend-user-files' ); ?>
            </label>
        <?php $this->render_card_end(); ?>

        <?php $this->render_card_start( __( 'Frontend Shortcode', 'nnax-frontend-user-files' ), __( 'Place the file manager into a frontend page or builder block.', 'nnax-frontend-user-files' ) ); ?>
            <p class="nnax-card__muted">
                <?php echo wp_kses_post( __( 'Use <code>[nnax_user_file_manager]</code> on the page where the file manager should appear.', 'nnax-frontend-user-files' ) ); ?>
            </p>
            <p class="nnax-card__muted">
                <?php esc_html_e( 'If you use a page builder, place the shortcode in a text, shortcode, or HTML element that renders shortcodes.', 'nnax-frontend-user-files' ); ?>
            </p>
        <?php $this->render_card_end(); ?>

        <?php $this->render_card_start( __( 'Meta fields per file', 'nnax-frontend-user-files' ), __( 'Add, sort, and remove fields.', 'nnax-frontend-user-files' ) ); ?>
            <p class="nnax-card__muted">
                <?php esc_html_e( 'Reorder fields by dragging the handle to control how they appear in the backend.', 'nnax-frontend-user-files' ); ?>
            </p>
            <div class="nnaxadmin-table-wrap">
                <table class="nnax-schemes-table" id="nnaxadmin-custom-meta-table">
                    <thead>
                        <tr>
                            <th><?php esc_html_e( 'Sort', 'nnax-frontend-user-files' ); ?></th>
                            <th><?php esc_html_e( 'Label', 'nnax-frontend-user-files' ); ?></th>
                            <th><?php esc_html_e( 'Type', 'nnax-frontend-user-files' ); ?></th>
                            <th><?php esc_html_e( 'Meta Key', 'nnax-frontend-user-files' ); ?></th>
                            <th><?php esc_html_e( 'Action', 'nnax-frontend-user-files' ); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $custom_meta_fields as $idx => $field ) :
                            $label = isset( $field['label'] ) ? (string) $field['label'] : '';
                            $type  = isset( $field['type'] ) ? (string) $field['type'] : 'text';
                            $key   = isset( $field['key'] ) ? (string) $field['key'] : '';
                            ?>
                            <tr class="nnaxadmin-meta-row">
                                <td>
                                    <button type="button" class="nnaxadmin-drag-handle" draggable="true" title="<?php echo esc_attr__( 'Change order', 'nnax-frontend-user-files' ); ?>" aria-label="<?php echo esc_attr__( 'Change order', 'nnax-frontend-user-files' ); ?>">⋮⋮</button>
                                </td>
                                <td>
                                    <input type="text" class="regular-text"
                                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[custom_meta_fields][<?php echo esc_attr( $idx ); ?>][label]"
                                        value="<?php echo esc_attr( $label ); ?>"
                                        placeholder="<?php echo esc_attr__( 'e.g. Description', 'nnax-frontend-user-files' ); ?>">
                                </td>
                                <td>
                                    <select
                                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[custom_meta_fields][<?php echo esc_attr( $idx ); ?>][type]">
                                        <option value="text" <?php selected( $type, 'text' ); ?>><?php esc_html_e( 'Text', 'nnax-frontend-user-files' ); ?></option>
                                        <option value="textarea" <?php selected( $type, 'textarea' ); ?>><?php esc_html_e( 'Textarea', 'nnax-frontend-user-files' ); ?></option>
                                        <option value="date" <?php selected( $type, 'date' ); ?>><?php esc_html_e( 'Date', 'nnax-frontend-user-files' ); ?></option>
                                        <option value="number" <?php selected( $type, 'number' ); ?>><?php esc_html_e( 'Number', 'nnax-frontend-user-files' ); ?></option>
                                        <option value="email" <?php selected( $type, 'email' ); ?>><?php esc_html_e( 'Email', 'nnax-frontend-user-files' ); ?></option>
                                        <option value="url" <?php selected( $type, 'url' ); ?>><?php esc_html_e( 'URL', 'nnax-frontend-user-files' ); ?></option>
                                    </select>
                                </td>
                                <td>
                                    <input type="text" class="regular-text code"
                                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[custom_meta_fields][<?php echo esc_attr( $idx ); ?>][key]"
                                        value="<?php echo esc_attr( $key ); ?>"
                                        placeholder="_nnaxfuf_my_field">
                                </td>
                                <td>
                                    <button type="button" class="nnax-btn nnax-btn--danger nnax-btn--small nnaxadmin-remove-meta-row"><?php esc_html_e( 'Remove', 'nnax-frontend-user-files' ); ?></button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <p>
                <button type="button" class="nnax-btn nnax-btn--secondary nnax-btn--small" id="nnaxadmin-add-meta-row"><?php esc_html_e( 'Add field', 'nnax-frontend-user-files' ); ?></button>
            </p>
        <?php $this->render_card_end(); ?>
    </div>

    <p>
        <button type="submit" name="submit" class="nnax-btn nnax-btn--primary"><?php esc_html_e( 'Save changes', 'nnax-frontend-user-files' ); ?></button>
    </p>
</form>
<?php
$this->render_page_footer();
