/**
 * Precision Auto Care - Mechanic Shop Template
 * Custom JavaScript for RSL Template
 */

(function() {
    'use strict';

    /**
     * Initialize template-specific functionality
     */
    function init() {
        initSmoothScrolling();
        initFormValidation();
        initDatePicker();
    }

    /**
     * Smooth scrolling for anchor links
     */
    function initSmoothScrolling() {
        document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
            anchor.addEventListener('click', function(e) {
                var targetId = this.getAttribute('href');
                if (targetId === '#') return;

                var target = document.querySelector(targetId);
                if (target) {
                    e.preventDefault();
                    var navbarHeight = document.querySelector('.rsl-navbar')?.offsetHeight || 0;
                    var targetPosition = target.getBoundingClientRect().top + window.pageYOffset - navbarHeight - 20;

                    window.scrollTo({
                        top: targetPosition,
                        behavior: 'smooth'
                    });
                }
            });
        });
    }

    /**
     * Basic form validation for appointment form
     */
    function initFormValidation() {
        var form = document.getElementById('appointment-form');
        if (!form) return;

        form.addEventListener('submit', function(e) {
            e.preventDefault();

            // Get form data
            var formData = new FormData(form);
            var isValid = true;

            // Check required fields
            var requiredFields = form.querySelectorAll('[required]');
            requiredFields.forEach(function(field) {
                if (!field.value.trim()) {
                    isValid = false;
                    field.classList.add('error');
                } else {
                    field.classList.remove('error');
                }
            });

            if (!isValid) {
                alert('Please fill in all required fields.');
                return;
            }

            // In a real implementation, you would send this to a server
            // For demo purposes, we'll show a success message
            showFormSuccess();
        });

        // Remove error state on input
        form.querySelectorAll('input, select, textarea').forEach(function(field) {
            field.addEventListener('input', function() {
                this.classList.remove('error');
            });
        });
    }

    /**
     * Show form success message
     */
    function showFormSuccess() {
        var form = document.getElementById('appointment-form');
        if (!form) return;

        var formWrapper = form.closest('.form-wrapper');
        if (formWrapper) {
            formWrapper.innerHTML = '\
                <div class="form-success">\
                    <div class="success-icon"><i class="fas fa-check-circle"></i></div>\
                    <h2>Appointment Request Received!</h2>\
                    <p>Thank you for choosing Precision Auto Care. We\'ll confirm your appointment within 2 hours during business hours.</p>\
                    <p><strong>What happens next?</strong></p>\
                    <ul>\
                        <li>Our team will review your request</li>\
                        <li>We\'ll call or email to confirm the appointment</li>\
                        <li>You\'ll receive a reminder before your visit</li>\
                    </ul>\
                    <a href="index.html" class="btn btn-primary"><i class="fas fa-home"></i> Back to Home</a>\
                </div>\
            ';
        }
    }

    /**
     * Set minimum date for date picker to today
     */
    function initDatePicker() {
        var dateInput = document.getElementById('preferred-date');
        if (dateInput) {
            var today = new Date();
            var yyyy = today.getFullYear();
            var mm = String(today.getMonth() + 1).padStart(2, '0');
            var dd = String(today.getDate()).padStart(2, '0');
            var minDate = yyyy + '-' + mm + '-' + dd;

            dateInput.setAttribute('min', minDate);

            // Set default value to tomorrow
            var tomorrow = new Date(today);
            tomorrow.setDate(tomorrow.getDate() + 1);

            // Skip weekends
            while (tomorrow.getDay() === 0) { // Sunday
                tomorrow.setDate(tomorrow.getDate() + 1);
            }

            var tomorrowYyyy = tomorrow.getFullYear();
            var tomorrowMm = String(tomorrow.getMonth() + 1).padStart(2, '0');
            var tomorrowDd = String(tomorrow.getDate()).padStart(2, '0');
            dateInput.value = tomorrowYyyy + '-' + tomorrowMm + '-' + tomorrowDd;
        }
    }

    // Initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

})();
