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

$current_user_id = isset( $current_user_id ) ? (int) $current_user_id : (int) get_current_user_id();

$nnaxfuf_bulk_move_targets = get_posts(
    array(
        'post_type'      => NNAXFUF_Plugin::CPT_FOLDER,
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'orderby'        => 'title',
        'order'          => 'ASC',
    )
);

$nnaxfuf_bulk_move_targets = array_values(
    array_filter(
        $nnaxfuf_bulk_move_targets,
        function ( $post ) use ( $current_user_id ) {
            return NNAXFUF_Helpers::user_can_manage_item( (int) $current_user_id, (int) $post->ID );
        }
    )
);

$nnaxfuf_bulk_move_target_entries = array();
foreach ( $nnaxfuf_bulk_move_targets as $target_folder ) {
    $folder_id     = (int) $target_folder->ID;
    $ancestor_ids  = array_reverse( array_map( 'intval', get_post_ancestors( $folder_id ) ) );
    $path_segments = array();

    foreach ( $ancestor_ids as $ancestor_id ) {
        $ancestor_title = trim( (string) NNAXFUF_Helpers::get_raw_item_title( $ancestor_id ) );
        if ( $ancestor_title !== '' ) {
            $path_segments[] = $ancestor_title;
        }
    }

    $folder_title = trim( (string) NNAXFUF_Helpers::get_raw_item_title( $folder_id ) );
    if ( $folder_title === '' ) {
        /* translators: %d: folder ID. */
        $folder_title = sprintf( __( 'Folder #%d', 'nnax-frontend-user-files' ), $folder_id );
    }

    $path_segments[] = $folder_title;
    $folder_path     = implode( ' / ', $path_segments );

    $nnaxfuf_bulk_move_target_entries[] = array(
        'id'   => $folder_id,
        'path' => $folder_path,
    );
}

usort(
    $nnaxfuf_bulk_move_target_entries,
    function ( $a, $b ) {
        return strcasecmp( (string) $a['path'], (string) $b['path'] );
    }
);
?>
<!-- BULK MOVE MODAL -->
<div id="nnaxfuf-bulk-move-modal" class="nnaxfuf-modal uk-flex-top" uk-modal>
    <div class="uk-modal-dialog uk-modal-body uk-margin-auto-vertical uk-width-1-1 uk-width-1-2@m">

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

        <h3 class="uk-modal-title"><?php esc_html_e( 'Move Selection', 'nnax-frontend-user-files' ); ?></h3>

        <p class="uk-margin-small">
            <?php esc_html_e( 'Selected items:', 'nnax-frontend-user-files' ); ?> <strong id="nnaxfuf_bulk_move_count">0</strong>
        </p>

        <form method="post" action="<?php echo esc_url( $frontend_form_action_url ); ?>" id="nnaxfuf_bulk_move_form" class="uk-margin-remove">
            <?php wp_nonce_field( 'nnaxfuf_bulk_move', 'nnaxfuf_nonce' ); ?>
            <input type="hidden" name="action" value="nnaxfuf_bulk_move">
            <input type="hidden" name="nnaxfuf_bulk_items" id="nnaxfuf_bulk_move_items" 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_bulk_move_target_folder_id"><?php esc_html_e( 'Target folder', 'nnax-frontend-user-files' ); ?></label>
                <div class="uk-form-controls">
                    <select class="uk-select" name="nnaxfuf_target_folder_id" id="nnaxfuf_bulk_move_target_folder_id">
                        <option value="0"><?php esc_html_e( 'Root (My Files)', 'nnax-frontend-user-files' ); ?></option>
                        <?php foreach ( $nnaxfuf_bulk_move_target_entries as $entry ) : ?>
                            <option value="<?php echo esc_attr( (int) $entry['id'] ); ?>">
                                <?php echo esc_html( (string) $entry['path'] ); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </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( 'Move selection', 'nnax-frontend-user-files' ); ?>
                </button>
            </div>
        </form>
    </div>
</div>
