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

$settings = isset( $settings ) ? $settings : NNAXFUF_Helpers::get_settings();
$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 = isset( $share_options['default_notification_msg'] ) ? $share_options['default_notification_msg'] : $default_msg;
?>
<!-- SHARE-MODAL -->
<div id="nnaxfuf-share-modal" class="nnaxfuf-modal uk-flex-top" uk-modal="focus: false">
    <div class="uk-modal-dialog uk-modal-body uk-margin-auto-vertical nnaxfuf-share-modal-dialog">

        <button class="uk-modal-close-default" type="button" uk-close></button>

        <h3 class="uk-modal-title" id="nnaxfuf_share_modal_title"><?php esc_html_e( 'Share Item', 'nnax-frontend-user-files' ); ?></h3>

        <form method="post" action="<?php echo esc_url( $frontend_form_action_url ); ?>" class="nnaxfuf-share-modal-form" autocomplete="off" data-file-share-nonce="<?php echo esc_attr( wp_create_nonce( 'nnaxfuf_share_file' ) ); ?>" data-folder-share-nonce="<?php echo esc_attr( wp_create_nonce( 'nnaxfuf_share_folder' ) ); ?>">
            <?php wp_nonce_field('nnaxfuf_share_file', 'nnaxfuf_nonce'); ?>
            <input type="hidden" name="action" id="nnaxfuf_share_action" value="nnaxfuf_share_file">
            <input type="hidden" name="nnaxfuf_file_id" id="nnaxfuf_share_file_id" value="">
            <input type="hidden" name="nnaxfuf_folder_id" id="nnaxfuf_share_folder_id" value="">
            <input type="hidden" name="nnaxfuf_redirect_to" value="<?php echo esc_url( $frontend_redirect_url ); ?>">
            <input type="hidden" name="nnaxfuf_notice_key" value="<?php echo esc_attr( $frontend_notice_key ); ?>">

            <div class="uk-margin">
                <label class="uk-form-label" for="nnaxfuf_recipient_search"><?php esc_html_e( 'Search person', 'nnax-frontend-user-files' ); ?></label>
                <div class="uk-form-controls uk-flex">
                    <input
                        class="uk-input"
                        type="text"
                        id="nnaxfuf_recipient_search"
                        name="nnaxfuf_share_recipient_search"
                        placeholder="<?php echo esc_attr__( 'Enter name, username or email', 'nnax-frontend-user-files' ); ?>"
                        autocomplete="new-password"
                        autocapitalize="off"
                        autocorrect="off"
                        spellcheck="false">
                    <?php if ( $allow_role_sharing ) : ?>
                    <button type="button" class="uk-button uk-button-default uk-button-small uk-margin-small-left" id="nnaxfuf_load_roles_btn" title="<?php echo esc_attr__( 'Show available user roles', 'nnax-frontend-user-files' ); ?>" uk-tooltip>
                        <span uk-icon="icon: users"></span>
                    </button>
                    <?php endif; ?>
                </div>
                <small class="uk-text-muted uk-visible@s"><?php esc_html_e( 'Type to search, then select a user from the list.', 'nnax-frontend-user-files' ); ?></small>
            </div>

            <div class="uk-margin-small">
                <div id="nnaxfuf_user_search_results" class="uk-margin-small-top"></div>
            </div>

            <div class="uk-margin">
                <h5 class="uk-heading-bullet uk-margin-small"><?php esc_html_e( 'Shared with', 'nnax-frontend-user-files' ); ?></h5>
                <p class="uk-text-muted uk-text-small uk-margin-small-bottom">
                    <?php esc_html_e( 'Permissions are set per person. Removed people lose access after saving.', 'nnax-frontend-user-files' ); ?>
                </p>
                <?php if ( $allow_time_limit || $allow_owner_rights ) : ?>
                    <button
                        uk-toggle="target: #nnaxfuf-limit-infos; cls: nnaxfuf-limit-infos-open"
                        type="button"
                        class="uk-button uk-button-text uk-margin-small-bottom uk-hidden@s">
                        <?php esc_html_e( 'Read more', 'nnax-frontend-user-files' ); ?>
                    </button>
                    <div id="nnaxfuf-limit-infos" class="nnaxfuf-limit-infos uk-text-muted uk-text-small uk-margin-remove">
                        <p class="uk-text-muted uk-text-small uk-margin-remove">
                            <?php esc_html_e( 'For each person, you can choose permissions:', 'nnax-frontend-user-files' ); ?>
                        </p>
                        <ul class="uk-text-muted uk-text-small uk-margin-remove">
                            <li><strong><?php esc_html_e( 'Normal', 'nnax-frontend-user-files' ); ?></strong> <?php esc_html_e( 'means view and download only.', 'nnax-frontend-user-files' ); ?></li>
                            <?php if ( $allow_owner_rights ) : ?>
                                <li><strong><?php esc_html_e( 'Manager', 'nnax-frontend-user-files' ); ?></strong> <?php esc_html_e( 'also allows editing, uploading, managing subfolders and sharing with others.', 'nnax-frontend-user-files' ); ?></li>
                                <?php if ( $allow_shared_folder_file_delete ) : ?>
                                    <li><strong><?php esc_html_e( 'Admin', 'nnax-frontend-user-files' ); ?></strong> <?php esc_html_e( 'is available for folders and additionally allows deleting files inside the shared folder.', 'nnax-frontend-user-files' ); ?></li>
                                <?php endif; ?>
                            <?php endif; ?>
                            <?php if ( $allow_time_limit ) : ?>
                                <li><?php esc_html_e( 'Use', 'nnax-frontend-user-files' ); ?> <strong><?php esc_html_e( 'From', 'nnax-frontend-user-files' ); ?></strong> <?php esc_html_e( 'and', 'nnax-frontend-user-files' ); ?> <strong><?php esc_html_e( 'Until', 'nnax-frontend-user-files' ); ?></strong> <?php esc_html_e( 'to define the access period.', 'nnax-frontend-user-files' ); ?></li>
                            <?php endif; ?>
                        </ul>
                    </div>
                <?php endif; ?>
                <div class="nnaxfuf-share-list-head uk-text-muted uk-text-small">
                    <span><?php esc_html_e( 'Person', 'nnax-frontend-user-files' ); ?></span>
                    <?php if ( $allow_owner_rights ) : ?>
                        <span><?php esc_html_e( 'Role', 'nnax-frontend-user-files' ); ?></span>
                    <?php endif; ?>
                    <?php if ( $allow_time_limit ) : ?>
                        <span><?php esc_html_e( 'Valid from / until', 'nnax-frontend-user-files' ); ?></span>
                    <?php endif; ?>
                    <span class="nnaxfuf-share-list-head-action"><?php esc_html_e( 'Action', 'nnax-frontend-user-files' ); ?></span>
                </div>
                <ul id="nnaxfuf_selected_users" class="uk-list uk-list-divider uk-margin-small"></ul>
            </div>

            <!-- Hier erzeugt das JS versteckte <input name="nnaxfuf_share_user_ids[]> -->
            <div id="nnaxfuf_selected_users_hidden"></div>
            
            <div class="uk-margin-small">
                <label>
                    <input class="uk-checkbox" type="checkbox" name="nnaxfuf_send_notification" id="nnaxfuf_send_notification" value="1" checked>
                    <?php esc_html_e( 'Send notification email to newly added or re-enabled people', 'nnax-frontend-user-files' ); ?>
                </label>
                <div id="nnaxfuf_notification_msg_container" class="uk-margin-small-top">
                    <textarea 
                        name="nnaxfuf_notification_msg" 
                        id="nnaxfuf_notification_msg" 
                        class="uk-textarea" 
                        rows="6"
                    ><?php echo esc_textarea( $default_notification_msg ); ?></textarea>
                </div>
            </div>

            <div class="uk-margin-top uk-text-right">
                <button class="uk-button uk-button-default uk-modal-close" type="button">
                    <?php esc_html_e( 'Cancel', 'nnax-frontend-user-files' ); ?>
                </button>
                <button class="uk-button uk-button-primary" type="submit">
                    <?php esc_html_e( 'Save', 'nnax-frontend-user-files' ); ?>
                </button>
            </div>
        </form>
    </div>
</div>
