<?php
/**
 * Customizer za sortiranje Experience Journeyev
 */

// Dodaj Customizer section in control
function grilctours_customize_journey_order($wp_customize) {
    // Add section
    $wp_customize->add_section('experience_journey_order_section', array(
        'title' => __('Experience Journey Order', 'grilctours'),
        'priority' => 200,
    ));

    // Add setting
    $wp_customize->add_setting('experience_journey_order', array(
        'default' => '',
        'sanitize_callback' => 'sanitize_experience_journey_order',
        'transport' => 'refresh',
    ));

    // Add control
    $wp_customize->add_control(new WP_Customize_Sortable_Posts_Control($wp_customize, 'experience_journey_order', array(
        'section' => 'experience_journey_order_section',
        'label' => __('Drag & Drop to Reorder', 'grilctours'),
        'description' => __('Arrange the order of Experience Journeys on the homepage.', 'grilctours'),
    )));
}
add_action('customize_register', 'grilctours_customize_journey_order');

// Sanitizacijska funkcija za vrstni red
function sanitize_experience_journey_order($input) {
    $order = explode(',', $input);
    $sanitized = array();
    foreach ($order as $post_id) {
        $post_id = absint($post_id);
        if ($post_id && get_post_status($post_id)) {
            $sanitized[] = $post_id;
        }
    }
    return implode(',', $sanitized);
}

// Prilagodljiv control class
class WP_Customize_Sortable_Posts_Control extends WP_Customize_Control {
    public $type = 'sortable_posts';
    public $post_type = 'experience_journey';

    public function render_content() {
        $saved_order = (array) $this->value();
        $all_posts = get_posts(array(
            'post_type' => $this->post_type,
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'fields' => 'ids',
        ));

        // Split into saved and new posts
        $ordered_posts = array();
        $new_posts = array();

        // Keep valid saved posts
        foreach ($saved_order as $post_id) {
            if (in_array($post_id, $all_posts)) {
                $ordered_posts[] = $post_id;
            }
        }

        // Add new posts not in saved order
        foreach ($all_posts as $post_id) {
            if (!in_array($post_id, $ordered_posts)) {
                $new_posts[] = $post_id;
            }
        }

        $ordered_posts = array_merge($ordered_posts, $new_posts);
        ?>
        <label>
            <span class="customize-control-title"><?php echo esc_html($this->label); ?></span>
            <span class="description customize-control-description"><?php echo esc_html($this->description); ?></span>
        </label>
        <ul class="sortable-posts-list">
            <?php foreach ($ordered_posts as $post_id) : 
                $post = get_post($post_id);
                if (!$post) continue;
            ?>
                <li data-post-id="<?php echo esc_attr($post_id); ?>">
                    <span class="dashicons dashicons-menu"></span>
                    <?php echo esc_html($post->post_title); ?>
                </li>
            <?php endforeach; ?>
        </ul>
        <input type="hidden" <?php $this->link(); ?> value="<?php echo esc_attr(implode(',', $ordered_posts)); ?>" />
        <?php
    }
}

// Enqueue scripts
function grilctours_customize_controls_enqueue_scripts() {
    wp_enqueue_script('grilctours-customizer-sortable', get_template_directory_uri() . '/js/customizer-sortable.js', array('jquery', 'jquery-ui-sortable', 'customize-controls'), '', true);
    wp_enqueue_style('grilctours-customizer-sortable', get_template_directory_uri() . '/css/customizer-sortable.css');
}
add_action('customize_controls_enqueue_scripts', 'grilctours_customize_controls_enqueue_scripts');

// Preview script
function grilctours_customize_preview_js() {
    wp_enqueue_script('grilctours-customizer-preview', get_template_directory_uri() . '/js/customizer-preview.js', array('customize-preview'), '', true);
}
add_action('customize_preview_init', 'grilctours_customize_preview_js'); 