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

$this->render_page_header(
    __( 'nnax User Files - Sharing', 'nnax-frontend-user-files' ),
    __( 'Configure sharing behavior and role-based targets.', 'nnax-frontend-user-files' ),
    'sharing'
);

$share_options = isset( $settings['share_options'] ) && is_array( $settings['share_options'] )
    ? $settings['share_options']
    : array();
$allow_time_limit = ! empty( $share_options['allow_time_limit'] );
$allow_owner_rights = ! empty( $share_options['allow_owner_rights'] );
$allow_shared_folder_file_delete = ! empty( $share_options['allow_shared_folder_file_delete'] );
$allow_role_sharing = ! empty( $share_options['allow_role_sharing'] );

$default_msg = __( "Hello,\n\nI have shared \"{item_title}\" with you.\n\nYou can now find it in the file manager on the website: {site_url}\n\n{owner_name}", 'nnax-frontend-user-files' );
$default_notification_msg = ! empty( $share_options['default_notification_msg'] ) ? $share_options['default_notification_msg'] : $default_msg;
?>
<form method="post" action="options.php" class="nnaxadmin-form">
    <?php settings_fields( 'nnaxfuf_settings_group' ); ?>

    <div class="nnax-grid nnax-grid-2">
        <section class="nnax-card nnax-card--primary">
            <h2><?php esc_html_e( 'Sharing options', 'nnax-frontend-user-files' ); ?></h2>
            <p><?php esc_html_e( 'Controls which advanced sharing options are available in the frontend.', 'nnax-frontend-user-files' ); ?></p>
            <input type="hidden"
                name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[share_options][_submitted]"
                value="1">

            <div class="nnax-grid nnax-grid-1">
                <div class="nnax-card">
                    <label>
                        <input type="checkbox"
                            name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[share_options][allow_time_limit]"
                            value="1"
                            <?php checked( $allow_time_limit ); ?>>
                        <?php esc_html_e( 'Allow time period when sharing', 'nnax-frontend-user-files' ); ?>
                    </label>
                    <p class="nnax-card__muted">
                        <?php esc_html_e( 'Users can set an expiry date. After that date, access is automatically blocked.', 'nnax-frontend-user-files' ); ?>
                    </p>
                </div>

                <div class="nnax-card">
                    <label>
                        <input type="checkbox"
                            name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[share_options][allow_owner_rights]"
                            value="1"
                            <?php checked( $allow_owner_rights ); ?>>
                        <?php esc_html_e( 'Allow owner rights when sharing', 'nnax-frontend-user-files' ); ?>
                    </label>
                    <p class="nnax-card__muted">
                        <?php esc_html_e( 'Enables role-based sharing: Normal (view/download) and Manager (edit/upload/manage subfolders).', 'nnax-frontend-user-files' ); ?>
                    </p>
                </div>

                <div class="nnax-card">
                    <label>
                        <input type="checkbox"
                            name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[share_options][allow_shared_folder_file_delete]"
                            value="1"
                            <?php checked( $allow_shared_folder_file_delete ); ?>>
                        <?php esc_html_e( 'Allow shared users to delete files in shared folders', 'nnax-frontend-user-files' ); ?>
                    </label>
                    <p class="nnax-card__muted">
                        <?php esc_html_e( 'Adds the Admin role in folder sharing. Admin includes Manager rights plus deleting files in shared folders (no restore/permanent delete).', 'nnax-frontend-user-files' ); ?>
                    </p>
                </div>

                <div class="nnax-card">
                    <h3><label class="nnax-label" for="nnaxadmin_default_notification_msg"><?php esc_html_e( 'Default notification message', 'nnax-frontend-user-files' ); ?></label></h3>
                    <textarea
                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[share_options][default_notification_msg]"
                        id="nnaxadmin_default_notification_msg"
                        class="widefat"
                        rows="10"
                    ><?php echo esc_textarea( $default_notification_msg ); ?></textarea>
                    <p class="nnax-card__muted">
                        <?php esc_html_e( 'The template for the email notification when an item is shared. This can be edited by the user in the share dialog. Available placeholders:', 'nnax-frontend-user-files' ); ?>
                        <code>{owner_name}</code>, <code>{item_title}</code>, <code>{site_url}</code>
                    </p>
                </div>
            </div>
        </section>

        <section class="nnax-card">
            <h2><?php esc_html_e( 'Role-based targets', 'nnax-frontend-user-files' ); ?></h2>
            <p><?php esc_html_e( 'Define which target roles each role can share with.', 'nnax-frontend-user-files' ); ?></p>

            <p>
                <label>
                    <input type="checkbox"
                        name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[share_options][allow_role_sharing]"
                        value="1"
                        <?php checked( $allow_role_sharing ); ?>>
                    <?php esc_html_e( 'Allow sharing with user roles', 'nnax-frontend-user-files' ); ?>
                </label>
                <p class="nnax-card__muted">
                    <?php esc_html_e( 'Enables the option to share files and folders with entire WordPress user roles.', 'nnax-frontend-user-files' ); ?>
                </p>
            </p>

            <?php foreach ( $roles as $role_key => $role_data ) :
                $allowed_target_roles = isset( $share_options['allowed_target_roles'][ $role_key ] ) ? $share_options['allowed_target_roles'][ $role_key ] : array();
                ?>
                <details class="nnax-card nnax-toggle-card">
                    <summary class="nnax-toggle-card__summary">
                        <h4 class="nnax-toggle-card__title">
                            <?php echo esc_html( $role_data['name'] ); ?>
                            <small class="nnax-toggle-card__slug">(<?php echo esc_html( $role_key ); ?>)</small>
                        </h4>
                    </summary>

                    <div class="nnax-toggle-card__content">
                        <?php foreach ( $roles as $target_slug => $target_data ) : ?>
                            <label class="nnax-check nnax-check--block">
                                <input
                                    type="checkbox"
                                    name="<?php echo esc_attr( NNAXFUF_Plugin::OPTION_SETTINGS ); ?>[share_options][allowed_target_roles][<?php echo esc_attr( $role_key ); ?>][]"
                                    value="<?php echo esc_attr( $target_slug ); ?>"
                                    <?php checked( in_array( $target_slug, $allowed_target_roles, true ) ); ?>
                                >
                                <span><?php echo esc_html( $target_data['name'] ); ?></span>
                            </label>
                        <?php endforeach; ?>

                        <small class="description"><?php esc_html_e( 'Empty = Default (Own & lower cap roles)', 'nnax-frontend-user-files' ); ?></small>
                    </div>
                </details>
            <?php endforeach; ?>
        </section>
    </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();
