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

$this->render_page_header(
    __( 'nnax User Files - Upload', 'nnax-frontend-user-files' ),
    __( 'Configure upload security rules, limits and quotas.', 'nnax-frontend-user-files' ),
    'upload'
);

$enforce_mime_check = ! empty( $settings['enforce_mime_check'] );
$max_file_size_mb   = isset( $settings['max_file_size_mb'] ) ? (float) $settings['max_file_size_mb'] : 0;
$blocked_types      = isset( $settings['blocked_types'] ) ? (string) $settings['blocked_types'] : '';
$allowed_types_list = NNAXFUF_Helpers::normalize_safe_upload_extension_list( $allowed );
$blocked_types_list = array_filter( array_map( 'trim', explode( ',', (string) $blocked_types ) ) );
$blocked_types_list = array_map(
    function ( $ext ) {
        $ext = ltrim( strtolower( $ext ), '.' );
        return preg_replace( '/[^a-z0-9]/', '', $ext );
    },
    $blocked_types_list
);
$types_conflict = array_values(
    array_intersect(
        array_unique( array_filter( $allowed_types_list ) ),
        array_unique( array_filter( $blocked_types_list ) )
    )
);
?>
<form method="post" action="options.php" class="nnaxadmin-form">
    <?php settings_fields( 'nnaxfuf_settings_group' ); ?>

    <div class="nnax-grid nnax-grid-2">
        <?php $this->render_card_start( __( 'Upload Rules', 'nnax-frontend-user-files' ), '' ); ?>
            <div class="nnax-grid nnax-grid-1">
                <div class="nnax-card">
                    <label for="nnaxadmin-allowed-types"><strong><?php esc_html_e( 'Allowed file types', 'nnax-frontend-user-files' ); ?></strong></label>
                    <p class="nnax-card__muted"><?php esc_html_e( 'Comma-separated list of file extensions without dots. Executable and active-content types are always rejected.', 'nnax-frontend-user-files' ); ?></p>
                    <input
                        id="nnaxadmin-allowed-types"
                        type="text"
                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[allowed_types]"
                        value="<?php echo esc_attr( implode( ',', $allowed_types_list ) ); ?>"
                        class="regular-text"
                        placeholder="<?php echo esc_attr__( 'e.g. pdf,jpg,jpeg,png', 'nnax-frontend-user-files' ); ?>"
                    />
                </div>

                <div class="nnax-card">
                    <label>
                        <input
                            type="hidden"
                            name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[enforce_mime_check]"
                            value="0"
                        />
                        <input
                            type="checkbox"
                            name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[enforce_mime_check]"
                            value="1"
                            <?php checked( $enforce_mime_check ); ?>
                        />
                        <strong><?php esc_html_e( 'Enable MIME check', 'nnax-frontend-user-files' ); ?></strong>
                    </label>
                    <p class="nnax-card__muted"><?php esc_html_e( 'Checks whether file extension and detected MIME type match during upload.', 'nnax-frontend-user-files' ); ?></p>
                </div>

                <div class="nnax-card">
                    <label for="nnaxadmin-max-file-size-mb"><strong><?php esc_html_e( 'Maximum file size per file (MB)', 'nnax-frontend-user-files' ); ?></strong></label>
                    <p class="nnax-card__muted"><?php esc_html_e( '0 disables this additional limit.', 'nnax-frontend-user-files' ); ?></p>
                    <input
                        id="nnaxadmin-max-file-size-mb"
                        type="number"
                        min="0"
                        step="0.1"
                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[max_file_size_mb]"
                        value="<?php echo esc_attr( $max_file_size_mb ); ?>"
                        style="width: 120px;"
                    />
                </div>

                <div class="nnax-card">
                    <label for="nnaxadmin-blocked-types"><strong><?php esc_html_e( 'Blocked file extensions', 'nnax-frontend-user-files' ); ?></strong></label>
                    <p class="nnax-card__muted"><?php esc_html_e( 'Comma-separated list of blocked file extensions without dots.', 'nnax-frontend-user-files' ); ?></p>
                    <input
                        id="nnaxadmin-blocked-types"
                        type="text"
                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[blocked_types]"
                        value="<?php echo esc_attr( $blocked_types ); ?>"
                        class="regular-text"
                        placeholder="<?php echo esc_attr__( 'e.g. svg,php,exe', 'nnax-frontend-user-files' ); ?>"
                    />
                </div>

                <div class="nnax-card">
                    <label for="nnaxadmin-items-per-page"><strong><?php esc_html_e( 'Items per page', 'nnax-frontend-user-files' ); ?></strong></label>
                    <p class="nnax-card__muted"><?php esc_html_e( 'Number of files/folders shown per page in the frontend file manager.', 'nnax-frontend-user-files' ); ?></p>
                    <input
                        id="nnaxadmin-items-per-page"
                        type="number"
                        min="1"
                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[items_per_page]"
                        value="<?php echo esc_attr( isset( $settings['items_per_page'] ) ? (int) $settings['items_per_page'] : 20 ); ?>"
                        style="width: 120px;"
                    />
                </div>

                <?php if ( ! empty( $types_conflict ) ) : ?>
                    <p class="notice notice-warning inline">
                        <?php
                        echo esc_html(
                            sprintf(
                                /* translators: %s: comma-separated list of extensions configured in both allow and block lists */
                                __( 'Note: These extensions are set in both allowed and blocked lists: %s. Blocked entries take precedence.', 'nnax-frontend-user-files' ),
                                implode( ', ', $types_conflict )
                            )
                        );
                        ?>
                    </p>
                <?php endif; ?>
            </div>
        <?php $this->render_card_end(); ?>
        <?php $this->render_card_start( __( 'Upload limits', 'nnax-frontend-user-files' ), '' ); ?>
            <p class="nnax-card__muted"><?php esc_html_e( 'Use 0 for unlimited values.', 'nnax-frontend-user-files' ); ?></p>
            <table class="nnax-schemes-table nnaxadmin-quota-table">
                <thead>
                    <tr>
                        <th><?php esc_html_e( 'Role', 'nnax-frontend-user-files' ); ?></th>
                        <th><?php esc_html_e( 'Max files', 'nnax-frontend-user-files' ); ?></th>
                        <th><?php esc_html_e( 'Max storage (MB)', 'nnax-frontend-user-files' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $roles as $role_key => $role_data ) :
                        $quota = isset( $settings['quota'][ $role_key ] )
                            ? $settings['quota'][ $role_key ]
                            : array( 'max_files' => 0, 'max_mb' => 0 );
                        ?>
                        <tr>
                            <td><?php echo esc_html( $role_data['name'] ); ?></td>
                            <td>
                                <input type="number" min="0"
                                    name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[quota][<?php echo esc_attr( $role_key ); ?>][max_files]"
                                    value="<?php echo esc_attr( $quota['max_files'] ); ?>"
                                    style="width: 120px;">
                            </td>
                            <td>
                                <input type="number" min="0" step="0.1"
                                    name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[quota][<?php echo esc_attr( $role_key ); ?>][max_mb]"
                                    value="<?php echo esc_attr( $quota['max_mb'] ); ?>"
                                    style="width: 120px;">
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?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();
