HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/test.kaunokolegija.lt/kk_wp_content/plugins/events/admin/add-event.php
<?php
// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

global $wpdb;

$event = null;
$is_edit = false;
$message = '';
$error = '';

// Check if editing
if (isset($_GET['edit'])) {
    $event_id = intval($_GET['edit']);
    $events_table = $wpdb->base_prefix . 'kauno_events';
    $event = $wpdb->get_row($wpdb->prepare(
        "SELECT * FROM $events_table WHERE id = %d AND site_id = %d",
        $event_id,
        get_current_blog_id()
    ));
    $is_edit = $event ? true : false;
}

// Handle form submission
if ($_POST && wp_verify_nonce($_POST['kauno_events_nonce'], 'kauno_events_save')) {
    global $wpdb;

    $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : '';
    $description = isset($_POST['description']) ? wp_kses_post($_POST['description']) : '';
    $event_date = isset($_POST['event_date']) ? sanitize_text_field($_POST['event_date']) : '';
    $end_date = isset($_POST['end_date']) && !empty($_POST['end_date']) ? sanitize_text_field($_POST['end_date']) : null;
    $location = isset($_POST['location']) ? sanitize_text_field($_POST['location']) : '';
    $address = isset($_POST['address']) ? sanitize_textarea_field($_POST['address']) : '';
    $language = isset($_POST['language']) ? sanitize_text_field($_POST['language']) : '';
    $status = isset($_POST['status']) ? sanitize_text_field($_POST['status']) : 'active';
    $featured_image = isset($_POST['featured_image']) ? sanitize_url($_POST['featured_image']) : '';
    $gallery_images = isset($_POST['gallery_images']) && is_array($_POST['gallery_images']) 
        ? json_encode(array_map('sanitize_url', $_POST['gallery_images'])) 
        : '';
    $registration_required = isset($_POST['registration_required']) ? 1 : 0;
    $max_participants = isset($_POST['max_participants']) && !empty($_POST['max_participants']) ? intval($_POST['max_participants']) : null;
    $contact_email = isset($_POST['contact_email']) ? sanitize_email($_POST['contact_email']) : '';
    $contact_phone = isset($_POST['contact_phone']) ? sanitize_text_field($_POST['contact_phone']) : '';
    $price = isset($_POST['price']) ? floatval($_POST['price']) : 0.00;

    $events_table = $wpdb->base_prefix . 'kauno_events';

    // Validation
    if (empty($title)) {
        $error = __('Event title is required.', 'kauno-events');
    } elseif (empty($event_date)) {
        $error = __('Event date is required.', 'kauno-events');
    } elseif (empty($language)) {
        $error = __('Language is required.', 'kauno-events');
    } else {
        $data = array(
            'title' => $title,
            'description' => $description,
            'event_date' => $event_date,
            'end_date' => $end_date,
            'location' => $location,
            'address' => $address,
            'featured_image' => $featured_image,
            'gallery_images' => $gallery_images,
            'language' => $language,
            'status' => $status,
            'registration_required' => $registration_required,
            'max_participants' => $max_participants,
            'contact_email' => $contact_email,
            'contact_phone' => $contact_phone,
            'price' => $price,
            'site_id' => get_current_blog_id()
        );

        if ($is_edit) {
            // Do NOT update slug on edit
            $result = $wpdb->update($events_table, $data, array('id' => $event->id));

            if ($result !== false) {
                $message = __('Event updated successfully!', 'kauno-events');
                $event = $wpdb->get_row($wpdb->prepare("SELECT * FROM $events_table WHERE id = %d", $event->id));
            } else {
                $error = __('Error updating event. Please try again. Database error: ', 'kauno-events') . $wpdb->last_error;
            }
        } else {
            // Create unique event_slug only on new insert
            $slug = sanitize_title($title); // replaces spaces with dashes, makes lowercase
            $original_slug = $slug;
            $i = 1;
            while ($wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $events_table WHERE event_slug = %s", $slug))) {
                $slug = $original_slug . '-' . $i++;
            }

            $data['event_slug'] = $slug;

            $result = $wpdb->insert($events_table, $data);

            if ($result !== false) {
                $new_id = $wpdb->insert_id;
                $message = __('Event created successfully!', 'kauno-events');
                echo '<script>window.location.href="' . admin_url('admin.php?page=kauno-events-add&edit=' . $new_id . '&created=1') . '";</script>';
                exit;
            } else {
                $error = __('Error creating event. Please try again. Database error: ', 'kauno-events') . $wpdb->last_error;
            }
        }
    }
}




// Check for creation success message
if (isset($_GET['created']) && $_GET['created'] == '1') {
    $message = __('Event created successfully!', 'kauno-events');
}

// Safe gallery images handling
$gallery_images = array();
if ($event && !empty($event->gallery_images)) {
    $decoded_images = json_decode($event->gallery_images, true);
    $gallery_images = is_array($decoded_images) ? $decoded_images : array();
}
?>

<div class="wrap kauno-events-admin-modern">
    <!-- Header -->
    <div class="admin-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="admin-title">
                    <span class="title-icon">πŸ“…</span>
                    <?php echo $is_edit ? __('Edit Event', 'kauno-events') : __('Add New Event', 'kauno-events'); ?>
                </h1>
                <?php if ($is_edit): ?>
                    <div class="event-id">ID: <?php echo $event->id; ?></div>
                <?php endif; ?>
            </div>
            <div class="header-actions">
                <a href="<?php echo admin_url('admin.php?page=kauno-events'); ?>" class="btn btn-secondary">
                    ← Back to Events
                </a>
                <?php if ($is_edit): ?>
                    <a href="<?php echo home_url('/event/' . $event->id . '/'); ?>" class="btn btn-outline" target="_blank">
                        πŸ‘ Preview Event
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Messages -->
    <?php if ($message): ?>
        <div class="notice notice-success">
            <p><strong>βœ… <?php echo esc_html($message); ?></strong></p>
        </div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="notice notice-error">
            <p><strong>❌ <?php echo esc_html($error); ?></strong></p>
        </div>
    <?php endif; ?>

    <!-- Main Form -->
    <form method="post" class="event-form-modern" id="eventForm">
        <?php wp_nonce_field('kauno_events_save', 'kauno_events_nonce'); ?>
        
        <div class="form-layout">
            <!-- Left Column - Main Content -->
            <div class="form-main">
                <!-- Basic Information Card -->
                <div class="form-card">
                    <div class="card-header">
                        <h2>πŸ“ <?php _e('Basic Information', 'kauno-events'); ?></h2>
                        <p><?php _e('Enter the main details about your event', 'kauno-events'); ?></p>
                    </div>
                    <div class="card-content">
                        <div class="form-group">
                            <label for="title" class="form-label required"><?php _e('Event Title', 'kauno-events'); ?></label>
                            <input type="text" 
                                   id="title" 
                                   name="title" 
                                   class="form-input" 
                                   value="<?php echo $event ? esc_attr($event->title) : ''; ?>" 
                                   placeholder="<?php _e('Enter event title...', 'kauno-events'); ?>"
                                   required>
                            <div class="form-help"><?php _e('This will be the main heading for your event', 'kauno-events'); ?></div>
                        </div>

                        <div class="form-row">
                            <div class="form-group">
                                <label for="language" class="form-label required"><?php _e('Language', 'kauno-events'); ?></label>
                                <select id="language" name="language" class="form-select" required>
                                    <option value=""><?php _e('Select Language', 'kauno-events'); ?></option>
                                    <option value="lt" <?php echo ($event && $event->language === 'lt') ? 'selected' : ''; ?>><?php _e('πŸ‡±πŸ‡Ή Lithuanian', 'kauno-events'); ?></option>
                                    <option value="en" <?php echo ($event && $event->language === 'en') ? 'selected' : ''; ?>><?php _e('πŸ‡¬πŸ‡§ English', 'kauno-events'); ?></option>
                                </select>
                            </div>
                            
                            <div class="form-group">
                                <label for="status" class="form-label"><?php _e('Status', 'kauno-events'); ?></label>
                                <select id="status" name="status" class="form-select">
                                    <option value="active" <?php echo ($event && $event->status === 'active') ? 'selected' : ''; ?>><?php _e('βœ… Active', 'kauno-events'); ?></option>
                                    <option value="inactive" <?php echo ($event && $event->status === 'inactive') ? 'selected' : ''; ?>><?php _e('⏸️ Inactive', 'kauno-events'); ?></option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="description" class="form-label"><?php _e('Description', 'kauno-events'); ?></label>
                            <div class="wp-editor-container">
                                <?php
                                $editor_id = 'description';
                                $content = $event ? $event->description : '';
                                $settings = array(
                                    'textarea_name' => 'description',
                                    'textarea_rows' => 10,
                                    'editor_class' => 'event-description-editor',
                                    'media_buttons' => false,
                                    'teeny' => false,
                                    'quicktags' => array(
                                        'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close'
                                    ),
                                    'tinymce' => array(
                                        'toolbar1' => 'formatselect fontsizeselect bold italic underline strikethrough | bullist numlist blockquote | alignleft aligncenter alignright | link unlink | forecolor',
                                        'toolbar2' => 'undo redo | outdent indent | charmap | removeformat',
                                        'fontsize_formats' => '8px 10px 12px 14px 16px 18px 20px 24px 28px 32px 36px 48px',
                                        'font_formats' => 'Arial=arial,helvetica,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva',
                                        'plugins' => 'lists,paste,tabfocus,wplink,fullscreen',
                                        'content_css' => '',
                                        'body_class' => 'event-description-editor',
                                        'setup' => 'function(ed) {
                                            ed.on("init", function() {
                                                ed.theme.resizeTo("100%", 300);
                                            });
                                        }'
                                    ),
                                    'drag_drop_upload' => true,
                                    'wpautop' => false,
                                    'editor_height' => 300
                                );

                                wp_editor($content, $editor_id, $settings);
                                ?>
                            </div>
                            <div class="form-help"><?php _e('Provide a detailed description of the event. You can use the formatting toolbar to style your content.', 'kauno-events'); ?></div>
                        </div>
                    </div>
                </div>

                <!-- Date & Location Card -->
                <div class="form-card">
                    <div class="card-header">
                        <h2>πŸ“ <?php _e('Date & Location', 'kauno-events'); ?></h2>
                        <p><?php _e('When and where will your event take place?', 'kauno-events'); ?></p>
                    </div>
                    <div class="card-content">
                        <div class="form-row">
                            <div class="form-group">
                                <label for="event_date" class="form-label required"><?php _e('Start Date & Time', 'kauno-events'); ?></label>
                                <input type="datetime-local" 
                                       id="event_date" 
                                       name="event_date" 
                                       class="form-input"
                                       value="<?php echo $event ? date('Y-m-d\TH:i', strtotime($event->event_date)) : ''; ?>" 
                                       required>
                            </div>
                            
                            <div class="form-group">
                                <label for="end_date" class="form-label"><?php _e('End Date & Time', 'kauno-events'); ?></label>
                                <input type="datetime-local" 
                                       id="end_date" 
                                       name="end_date" 
                                       class="form-input"
                                       value="<?php echo ($event && $event->end_date) ? date('Y-m-d\TH:i', strtotime($event->end_date)) : ''; ?>">
                            </div>
                        </div>

                        <div class="form-row">
                            <div class="form-group">
                                <label for="location" class="form-label"><?php _e('Location Name', 'kauno-events'); ?></label>
                                <input type="text" 
                                       id="location" 
                                       name="location" 
                                       class="form-input" 
                                       value="<?php echo $event ? esc_attr($event->location) : ''; ?>" 
                                       placeholder="<?php _e('e.g., Conference Hall A', 'kauno-events'); ?>">
                            </div>
                            
                            <div class="form-group">
                                <label for="price" class="form-label"><?php _e('Price (€)', 'kauno-events'); ?></label>
                                <input type="number" 
                                       id="price" 
                                       name="price" 
                                       class="form-input" 
                                       step="0.01" 
                                       min="0" 
                                       value="<?php echo $event ? $event->price : '0.00'; ?>" 
                                       placeholder="0.00">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="address" class="form-label"><?php _e('Full Address', 'kauno-events'); ?></label>
                            <textarea id="address" 
                                      name="address" 
                                      class="form-textarea" 
                                      rows="3" 
                                      placeholder="<?php _e('Enter the complete address...', 'kauno-events'); ?>"><?php echo $event ? esc_textarea($event->address) : ''; ?></textarea>
                        </div>
                    </div>
                </div>

                <!-- Images Card -->
                <div class="form-card">
                    <div class="card-header">
                        <h2>πŸ–ΌοΈ <?php _e('Images', 'kauno-events'); ?></h2>
                        <p><?php _e('Add a main image and gallery images for your event', 'kauno-events'); ?></p>
                    </div>
                    <div class="card-content">
                        <!-- Featured Image -->
                        <div class="form-group">
                            <label class="form-label"><?php _e('Main Image', 'kauno-events'); ?></label>
                            <div class="image-upload-area" id="featuredImageArea">
                                <?php if ($event && $event->featured_image): ?>
                                    <div class="featured-image-preview">
                                        <img src="<?php echo esc_url($event->featured_image); ?>" alt="Featured image">
                                        <div class="image-overlay">
                                            <button type="button" class="btn-image-remove" onclick="removeFeaturedImage()">πŸ—‘οΈ</button>
                                            <button type="button" class="btn-image-change" onclick="selectFeaturedImage()">πŸ“</button>
                                        </div>
                                    </div>
                                <?php else: ?>
                                    <div class="upload-placeholder" onclick="selectFeaturedImage()">
                                        <div class="upload-icon">πŸ“·</div>
                                        <div class="upload-text"><?php _e('Click to upload main image', 'kauno-events'); ?></div>
                                        <div class="upload-hint"><?php _e('Recommended: 1200x600px', 'kauno-events'); ?></div>
                                    </div>
                                <?php endif; ?>
                                <input type="hidden" id="featured_image" name="featured_image" value="<?php echo $event ? esc_attr($event->featured_image) : ''; ?>">
                            </div>
                        </div>

                        <!-- Gallery Images -->
                        <div class="form-group">
                            <label class="form-label"><?php _e('Image Gallery', 'kauno-events'); ?></label>
                            <div class="gallery-container">
                                <div class="gallery-grid" id="galleryGrid">
                                    <?php foreach ($gallery_images as $index => $image): ?>
                                        <div class="gallery-item" data-index="<?php echo $index; ?>">
                                            <img src="<?php echo esc_url($image); ?>" alt="Gallery image">
                                            <div class="gallery-overlay">
                                                <button type="button" class="btn-gallery-remove" onclick="removeGalleryImage(this)">πŸ—‘οΈ</button>
                                            </div>
                                            <input type="hidden" name="gallery_images[]" value="<?php echo esc_attr($image); ?>">
                                        </div>
                                    <?php endforeach; ?>
                                    
                                    <div class="gallery-add-btn" onclick="selectGalleryImages()">
                                        <div class="add-icon">βž•</div>
                                        <div class="add-text"><?php _e('Add Images', 'kauno-events'); ?></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Right Column - Sidebar -->
            <div class="form-sidebar">
                <!-- Registration Settings Card -->
                <div class="form-card">
                    <div class="card-header">
                        <h2>🎫 <?php _e('Registration', 'kauno-events'); ?></h2>
                        <p><?php _e('Configure event registration', 'kauno-events'); ?></p>
                    </div>
                    <div class="card-content">
                        <div class="form-group">
                            <label class="checkbox-label">
                                <input type="checkbox" 
                                       name="registration_required" 
                                       value="1" 
                                       <?php echo ($event && $event->registration_required) ? 'checked' : ''; ?>
                                       onchange="toggleRegistrationFields(this)">
                                <span class="checkbox-custom"></span>
                                <?php _e('Registration Required', 'kauno-events'); ?>
                            </label>
                        </div>

                        <div class="registration-fields" style="<?php echo (!$event || !$event->registration_required) ? 'display: none;' : ''; ?>">
                            <div class="form-group">
                                <label for="max_participants" class="form-label"><?php _e('Max Participants', 'kauno-events'); ?></label>
                                <input type="number" 
                                       id="max_participants" 
                                       name="max_participants" 
                                       class="form-input" 
                                       min="1" 
                                       value="<?php echo $event ? $event->max_participants : ''; ?>" 
                                       placeholder="<?php _e('Unlimited', 'kauno-events'); ?>">
                            </div>
                            
                            <div class="form-group">
                                <label for="contact_email" class="form-label"><?php _e('Contact Email', 'kauno-events'); ?></label>
                                <input type="email" 
                                       id="contact_email" 
                                       name="contact_email" 
                                       class="form-input" 
                                       value="<?php echo $event ? esc_attr($event->contact_email) : ''; ?>" 
                                       placeholder="contact@example.com">
                            </div>
                            
                            <div class="form-group">
                                <label for="contact_phone" class="form-label"><?php _e('Contact Phone', 'kauno-events'); ?></label>
                                <input type="tel" 
                                       id="contact_phone" 
                                       name="contact_phone" 
                                       class="form-input" 
                                       value="<?php echo $event ? esc_attr($event->contact_phone) : ''; ?>" 
                                       placeholder="+370 600 00000">
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Quick Actions Card -->
                <div class="form-card">
                    <div class="card-header">
                        <h2>⚑ <?php _e('Actions', 'kauno-events'); ?></h2>
                    </div>
                    <div class="card-content">
                        <button type="submit" class="btn btn-primary btn-full">
                            <?php echo $is_edit ? __('πŸ’Ύ Update Event', 'kauno-events') : __('✨ Create Event', 'kauno-events'); ?>
                        </button>
                        
                        <?php if ($is_edit): ?>
                            <a href="<?php echo home_url('/event/' . $event->id . '/'); ?>" 
                               class="btn btn-outline btn-full" 
                               target="_blank">
                                πŸ‘ <?php _e('View Event', 'kauno-events'); ?>
                            </a>
                            
                            <button type="button" 
                                    class="btn btn-danger btn-full" 
                                    onclick="deleteEvent(<?php echo $event->id; ?>)">
                                πŸ—‘οΈ <?php _e('Delete Event', 'kauno-events'); ?>
                            </button>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Event Info Card -->
                <?php if ($is_edit): ?>
                    <div class="form-card">
                        <div class="card-header">
                            <h2>ℹ️ <?php _e('Event Info', 'kauno-events'); ?></h2>
                        </div>
                        <div class="card-content">
                            <div class="info-item">
                                <strong><?php _e('Created:', 'kauno-events'); ?></strong><br>
                                <?php echo date('M j, Y \a\t H:i', strtotime($event->created_at)); ?>
                            </div>
                            <div class="info-item">
                                <strong><?php _e('Last Updated:', 'kauno-events'); ?></strong><br>
                                <?php echo date('M j, Y \a\t H:i', strtotime($event->updated_at)); ?>
                            </div>
                            <div class="info-item">
                                <strong><?php _e('Status:', 'kauno-events'); ?></strong><br>
                                <span class="status-badge status-<?php echo $event->status; ?>">
                                    <?php echo ucfirst($event->status); ?>
                                </span>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </form>
</div>

<style>
/* WordPress Editor Styling */
.wp-editor-container {
    border: 1px solid #d1d5db;
    border-radius: 6px;
    overflow: hidden;
    margin-bottom: 1rem;
}

.wp-editor-container .wp-editor-area {
    border: none !important;
    border-radius: 0 !important;
    padding: 1rem !important;
    font-family: inherit !important;
    font-size: 14px !important;
    line-height: 1.6 !important;
    min-height: 300px !important;
}

.wp-editor-container .wp-editor-area:focus {
    outline: none !important;
    box-shadow: 0 0 0 3px rgba(13, 148, 136, 0.1) !important;
}

/* TinyMCE Toolbar Styling */
.mce-toolbar {
    background: #f8fafc !important;
    border-bottom: 1px solid #e2e8f0 !important;
}

.mce-toolbar .mce-btn {
    background: transparent !important;
    border: none !important;
    color: #374151 !important;
}

.mce-toolbar .mce-btn:hover {
    background: #e2e8f0 !important;
}

.mce-toolbar .mce-btn.mce-active {
    background: #0d9488 !important;
    color: white !important;
}

/* Editor Content Styling */
.event-description-editor {
    background: white !important;
    color: #374151 !important;
}

.event-description-editor p {
    margin-bottom: 1rem !important;
}

.event-description-editor h1,
.event-description-editor h2,
.event-description-editor h3,
.event-description-editor h4,
.event-description-editor h5,
.event-description-editor h6 {
    margin-top: 1.5rem !important;
    margin-bottom: 1rem !important;
    font-weight: 600 !important;
    color: #1e293b !important;
}

.event-description-editor ul,
.event-description-editor ol {
    margin-bottom: 1rem !important;
    padding-left: 1.5rem !important;
}

.event-description-editor blockquote {
    border-left: 4px solid #0d9488 !important;
    padding-left: 1rem !important;
    margin: 1rem 0 !important;
    font-style: italic !important;
    color: #64748b !important;
}

.event-description-editor a {
    color: #0d9488 !important;
    text-decoration: underline !important;
}

.event-description-editor a:hover {
    color: #0f766e !important;
}

/* Responsive Editor */
@media (max-width: 768px) {
    .wp-editor-container .wp-editor-area {
        min-height: 250px !important;
    }
    
    .mce-toolbar .mce-btn {
        padding: 4px !important;
    }
}
</style>

<script>
// WordPress Media Library Integration
let mediaUploader;

function selectFeaturedImage() {
    if (mediaUploader) {
        mediaUploader.open();
        return;
    }
    
    mediaUploader = wp.media({
        title: 'Select Featured Image',
        button: { text: 'Use This Image' },
        multiple: false,
        library: { type: 'image' }
    });
    
    mediaUploader.on('select', function() {
        const attachment = mediaUploader.state().get('selection').first().toJSON();
        document.getElementById('featured_image').value = attachment.url;
        
        const area = document.getElementById('featuredImageArea');
        area.innerHTML = `
            <div class="featured-image-preview">
                <img src="${attachment.url}" alt="Featured image">
                <input type="hidden" id="featured_image" name="featured_image" value="${attachment.url}">
                <div class="image-overlay">
                    <button type="button" class="btn-image-remove" onclick="removeFeaturedImage()">πŸ—‘οΈ</button>
                    <button type="button" class="btn-image-change" onclick="selectFeaturedImage()">πŸ“</button>

                </div>
            </div>
        `;
    });
    
    mediaUploader.open();
}

function removeFeaturedImage() {
    document.getElementById('featured_image').value = '';
    document.getElementById('featuredImageArea').innerHTML = `
        <div class="upload-placeholder" onclick="selectFeaturedImage()">
            <div class="upload-icon">πŸ“·</div>
            <div class="upload-text">Click to upload featured image</div>
            <div class="upload-hint">Recommended: 1200x600px</div>
        </div>
    `;
}

function selectGalleryImages() {
    const galleryUploader = wp.media({
        title: 'Select Gallery Images',
        button: { text: 'Add to Gallery' },
        multiple: true,
        library: { type: 'image' }
    });
    
    galleryUploader.on('select', function() {
        const attachments = galleryUploader.state().get('selection').toJSON();
        const grid = document.getElementById('galleryGrid');
        const addBtn = grid.querySelector('.gallery-add-btn');
        
        attachments.forEach(function(attachment) {
            const item = document.createElement('div');
            item.className = 'gallery-item';
            item.innerHTML = `
                <img src="${attachment.url}" alt="Gallery image">
                <div class="gallery-overlay">
                    <button type="button" class="btn-gallery-remove" onclick="removeGalleryImage(this)">πŸ—‘οΈ</button>
                </div>
                <input type="hidden" name="gallery_images[]" value="${attachment.url}">
            `;
            grid.insertBefore(item, addBtn);
        });
    });
    
    galleryUploader.open();
}

function removeGalleryImage(button) {
    button.closest('.gallery-item').remove();
}

function toggleRegistrationFields(checkbox) {
    const fields = document.querySelector('.registration-fields');
    if (checkbox.checked) {
        fields.style.display = 'block';
    } else {
        fields.style.display = 'none';
    }
}

function deleteEvent(eventId) {
    if (confirm('Are you sure you want to delete this event? This action cannot be undone.')) {
        window.location.href = `<?php echo admin_url('admin.php?page=kauno-events'); ?>&action=delete&id=${eventId}&_wpnonce=<?php echo wp_create_nonce('delete_event'); ?>`;
    }
}

// Form validation
document.getElementById('eventForm').addEventListener('submit', function(e) {
    const title = document.getElementById('title').value.trim();
    const eventDate = document.getElementById('event_date').value;
    const language = document.getElementById('language').value;
    
    if (!title) {
        alert('Please enter an event title');
        e.preventDefault();
        return false;
    }
    
    if (!eventDate) {
        alert('Please select an event date and time');
        e.preventDefault();
        return false;
    }
    
    if (!language) {
        alert('Please select a language');
        e.preventDefault();
        return false;
    }
    
    // Show loading state
    const submitBtn = this.querySelector('button[type="submit"]');
    const originalText = submitBtn.innerHTML;
    submitBtn.innerHTML = '⏳ Saving...';
    submitBtn.disabled = true;
    
    // Re-enable if there's an error (form won't submit)
    setTimeout(() => {
        submitBtn.innerHTML = originalText;
        submitBtn.disabled = false;
    }, 5000);
});
</script>