/**
 * Admin JavaScript
 * Handles admin area functionality
 */

(function($) {
    'use strict';
    
    $(document).ready(function() {
        const admin = {
            init: function() {
                this.bindEvents();
                this.initMetaBoxes();
            },
            
            bindEvents: function() {
                // Course meta box functionality
                this.initCourseMetaBox();
                this.initTestimonialMetaBox();
                
                // Image upload functionality
                $('.upload-image-button').on('click', this.handleImageUpload.bind(this));
                
                // Form validation
                $('#post').on('submit', this.validateForm.bind(this));
            },
            
            initCourseMetaBox: function() {
                // Course level validation
                $('#course_level').on('blur', function() {
                    const value = $(this).val();
                    if (value && !/^[A-Za-z\s]+$/.test(value)) {
                        alert('Course level should contain only letters and spaces.');
                        $(this).focus();
                    }
                });
                
                // Price validation
                $('#course_price').on('blur', function() {
                    const value = $(this).val();
                    if (value && !/^[\d\.,\s]+$/.test(value)) {
                        alert('Price should contain only numbers, commas, and periods.');
                        $(this).focus();
                    }
                });
            },
            
            initTestimonialMetaBox: function() {
                // Rating validation
                $('#testimonial_rating').on('change', function() {
                    const value = parseInt($(this).val());
                    if (value < 1 || value > 5) {
                        alert('Rating must be between 1 and 5.');
                        $(this).val(5);
                    }
                });
                
                // Name validation
                $('#testimonial_name').on('blur', function() {
                    const value = $(this).val();
                    if (value && !/^[A-Za-z\s]+$/.test(value)) {
                        alert('Name should contain only letters and spaces.');
                        $(this).focus();
                    }
                });
            },
            
            handleImageUpload: function(e) {
                e.preventDefault();
                
                const button = $(e.target);
                const targetInput = button.siblings('input[type="hidden"]');
                const preview = button.siblings('.image-preview');
                
                // Create media frame
                const frame = wp.media({
                    title: 'Select Image',
                    button: {
                        text: 'Use this image'
                    },
                    multiple: false
                });
                
                // Handle selection
                frame.on('select', function() {
                    const attachment = frame.state().get('selection').first().toJSON();
                    targetInput.val(attachment.id);
                    preview.html('<img src="' + attachment.sizes.thumbnail.url + '" style="max-width: 150px; height: auto;">');
                });
                
                // Open frame
                frame.open();
            },
            
            validateForm: function(e) {
                let isValid = true;
                const errors = [];
                
                // Course validation
                if ($('#post_type').val() === 'course') {
                    const level = $('#course_level').val();
                    const duration = $('#course_duration').val();
                    const price = $('#course_price').val();
                    
                    if (!level) {
                        errors.push('Course level is required.');
                        isValid = false;
                    }
                    
                    if (!duration) {
                        errors.push('Course duration is required.');
                        isValid = false;
                    }
                    
                    if (!price) {
                        errors.push('Course price is required.');
                        isValid = false;
                    }
                }
                
                // Testimonial validation
                if ($('#post_type').val() === 'testimonial') {
                    const name = $('#testimonial_name').val();
                    const country = $('#testimonial_country').val();
                    const rating = $('#testimonial_rating').val();
                    
                    if (!name) {
                        errors.push('Testimonial name is required.');
                        isValid = false;
                    }
                    
                    if (!country) {
                        errors.push('Testimonial country is required.');
                        isValid = false;
                    }
                    
                    if (!rating) {
                        errors.push('Testimonial rating is required.');
                        isValid = false;
                    }
                }
                
                if (!isValid) {
                    e.preventDefault();
                    alert('Please fix the following errors:\n\n' + errors.join('\n'));
                }
            }
        };
        
        // Initialize admin
        admin.init();
    });
    
})(jQuery);
