{% extends BASE_TEMPLATE %}
{% load i18n %}
{% load static %}
{% block customMetaTag %}
    <meta name="csrf-token" content="{{ csrf_token }}">
{% endblock %}
{% block customCSS %}
    <link rel="stylesheet" type="text/css" href="{% static 'css/appt-common.css' %}"/>
    <link rel="stylesheet" type="text/css" href="{% static 'css/app_admin/admin.css' %}"/>
    <style>
        @media (max-width: 991px) {
            body {
                font-size: 15px; /* Adjust font size for tablets */
            }

            #calendar {
                max-width: 800px; /* Adjust calendar width for tablets */
            }
        }

        /* Mobile Styles */
        @media (max-width: 767px) {
            body {
                margin: 20px 5px; /* Reduce margins on mobile */
                font-size: 14px; /* Adjust font size for mobiles */
            }

            #calendar {
                max-width: 100%; /* Let the calendar take the full width on mobile */
            }

            .fc, .fc-toolbar-title, h2 {
                font-size: 15px !important; /* Adjust as needed */
                margin: 0; /* Remove any default margin */
            }
        }

        .calendar-wrapper {
            display: flex;
            transition: all 0.3s; /* smooth transition for resizing */
            width: 100%;
            flex-direction: column;
            margin-top: 25px;
        }

        #calendar {
            flex-grow: 1; /* Makes the calendar grow to occupy all available space */
            transition: all 0.3s; /* smooth transition for resizing */
            width: 100%;
        }

        #event-details {
            overflow-y: auto;
            width: 0; /* initially hidden */
            transition: all 0.3s; /* smooth transition for resizing */
            border-left: 1px solid #ccc;
            flex-shrink: 0; /* Prevents the div from shrinking beyond its content's size */
        }

        .modal-content {
            background-color: #fff;
            margin: 10% auto; /* Centering the modal vertically */
            width: 100%;
            position: relative; /* For positioning the close button */
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2); /* A little shadow for depth */
        }

        .modal-footer {
            text-align: right;
        }

        .close-button {
            position: absolute;
            top: 10px;
            right: 10px;
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
        }

        .body-blur {
            filter: blur(2px); /* This will blur the whole body when modal is open */
        }

        #eventDetailsModal .modal-content {
            background-color: #f4f4f4;
            border-radius: 10px;
        }

        #eventDetailsModal .modal-header,
        #eventDetailsModal .modal-footer {
            background-color: #2c3e50;
            color: #ecf0f1;
        }

        #eventDetailsModal .modal-title {
            font-weight: bold;
        }

        #eventDetailsModal label {
            display: block;
            margin-top: 10px;
        }

        #eventDetailsModal input {
            width: 100%;
            padding: 5px;
            margin: 5px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        #eventDetailsModal .btn {
            margin-right: 10px;
        }

        #serviceSelect, #staffSelect {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ccc;
            border-radius: 4px;
            background-color: #f9f9f9;
            font-size: 14px;
            transition: background-color 0.3s ease;
        }

        #serviceSelect:disabled, #staffSelect:disabled {
            background-color: #e9ecef;
        }

        #customContextMenu ul {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        #customContextMenu ul li a {
            padding: 5px 10px;
            display: block;
            text-decoration: none;
            color: black;
        }

        #customContextMenu ul li a:hover {
            background-color: #f0f0f0;
        }

        #customContextMenu {
            border: 1px solid #ccc;
            background-color: #fff;
            box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
            padding: 5px 0;
            position: absolute;
            z-index: 1000;
            display: none;
        }


        #eventDetailsModal input[type="checkbox"] {
            width: auto;
            margin: 0 5px 0 0;
        }

        /* General styles */
        .flex-container-appt {
            display: flex;
            align-items: center;
            margin-top: 5px;
        }

        .flex-container-appt label {
            flex: 0 0 auto; /* Do not grow, do not shrink, and base width on content */
            margin-right: 5px;
            white-space: nowrap; /* Prevent label from wrapping */
        }

        .flex-container-appt input,
        .flex-container-appt select {
            flex: 1 1 auto; /* Grow and shrink based on available space */
        }

        /* Larger Modal on Desktop */
        @media (min-width: 850px) {
            .modal-content {
                /* centering the modal horizontally */
                margin: 10% auto;
                left: 0;
                right: 0;
            }
        }

        /* Stack label and input on mobile */
        @media (max-width: 767px) {
            .flex-container-appt {
                flex-direction: column;
                align-items: flex-start;
            }

            .flex-container-appt label {
                margin-right: 0;
                margin-bottom: 5px;
            }
        }

        .highlight-weekend {
            background-color: rgba(155, 155, 155, 0.03); /* Light gray background for weekends */
        }

        .event-dot {
            height: 5px;
            width: 5px;
            border-radius: 50%;
            display: inline-block;
            margin: 2px;
        }

        /* Hide event list container on larger screens */
        @media (min-width: 768px) {
            #event-list-container {
                display: none; /* Hide the event list container */
            }
        }

        /* Show event list container on smaller screens */
        @media (max-width: 767px) {
            #event-list-container {
                /* Styles to display and position the event list container */
                display: block; /* Show the event list container */
                margin-top: 20px; /* Space from the calendar */
            }

            .event-list-item {
                /* Styles for individual event list items */
                padding: 10px;
                border-bottom: 1px solid #ccc;
            }
        }

        @media (max-width: 450px) {
            .fc-daygrid-event-harness {
                width: 5px;
            }

            .fc td, .fc th {

            }
        }

        .event-list-item-appt {
            /* pointer hand cursor */
            cursor: pointer;
        }
    </style>
{% endblock %}
{% block title %}
    {{ page_title }}
{% endblock %}
{% block description %}
    {{ page_description }}
{% endblock %}
{% block body %}
    <section class="content content-wrapper">
        <div class="container">
            <div class="calendar-wrapper">
                <div id="calendar" class="calendarbox"></div>
                <div id="event-list-container" class="event-list-container"></div>
            </div>

            {% include 'modal/event_details_modal.html' %}
            {% include 'modal/error_modal.html' %}

            <div class="messages" style="margin: 20px 0">
                {% if messages %}
                    {% for message in messages %}
                        <div class="alert alert-dismissible {% if message.tags %}alert-{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}danger{% else %}{{ message.tags }}{% endif %}{% endif %}"
                             role="alert">{{ message }}</div>
                    {% endfor %}
                {% endif %}
            </div>
        </div>
    </section>
    <div id="customContextMenu" style="display: none; position: absolute; z-index: 1000;">
        <ul>
            <li id="newAppointmentOption"><a href="#">New Appointment</a></li>
        </ul>
    </div>

    {% include 'modal/confirm_modal.html' %}
{% endblock %}


{% block customJS %}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.js"
            integrity="sha512-3CuraBvy05nIgcoXjVN33mACRyI89ydVHg7y/HMN9wcTVbHeur0SeBzweSd/rxySapO7Tmfu68+JlKkLTnDFNg=="
            crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.45/moment-timezone-with-data.min.js"
            integrity="sha512-t/mY3un180WRfsSkWy4Yi0tAxEDGcY2rAEx873hb5BrkvLA0QLk54+SjfYgFBBoCdJDV1H86M8uyZdJhAOHeyA=="
            crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/6.1.10/index.global.min.js"
            integrity="sha512-JCQkxdym6GmQ+AFVioDUq8dWaWN6tbKRhRyHvYZPupQ6DxpXzkW106FXS1ORgo/m3gxtt5lHRMqSdm2OfPajtg=="
            crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script>
        const timezone = "{{ timezone }}";
        const locale = "{{ locale }}";
        const availableSlotsAjaxURL = "{% url 'appointment:available_slots_ajax' %}";
        const requestNextAvailableSlotURLTemplate = "{% url 'appointment:request_next_available_slot' service_id=0 %}";
        const deleteAppointmentURLTemplate = "{% url 'appointment:delete_appointment_ajax' %}";
        const getNonWorkingDaysURL = "{% url 'appointment:get_non_working_days_ajax' %}";
        const serviceId = "{{ service.id }}";
        const serviceDuration = parseInt("{{ service.duration.total_seconds }}") / 60;
        let appointments = {{ appointments|safe }};
        const fetchServiceListForStaffURL = "{% url 'appointment:fetch_service_list_for_staff' %}";
        const fetchStaffListURL = "{% url 'appointment:fetch_staff_list' %}";
        const updateApptMinInfoURL = "{% url 'appointment:update_appt_min_info' %}";
        const updateApptDateURL = "{% url 'appointment:update_appt_date_time' %}";
        const validateApptDateURL = "{% url 'appointment:validate_appointment_date' %}";
        const isUserStaffAdminURL = "{% url 'appointment:is_user_staff_admin' %}";
        const isUserSuperUser = "{{ is_superuser }}" === "True";
    </script>
    <script>
        {# Text for translation #}
        const confirmDeletionTitleTxt = "{% trans 'Confirm Deletion' %}";
        const confirmDeletionTxt = "{% trans 'Are you sure you want to delete this appointment?' %}";
        const deleteBtnTxt = "{% trans 'Delete' %}";
        const eventsOnTxt = "{% trans 'Events on' %}";
        const noEventTxt = "{% trans 'No events for this day.' %}";
        const newEventTxt = "{% trans 'New Event' %}";
        const successTxt = "{% trans 'Success' %}";
        const errorTxt = "{% trans 'Error' %}";
        const updateApptErrorTitleTxt = "{% trans 'Error: Unable to delete appointment.' %}";
        const apptNotFoundTxt = "{% trans 'Appointment not found.' %}";
        const notStaffMemberTxt = "{% trans "You're not a staff member. Can't perform this action !" %}";
        const noServiceOfferedTxt = "{% trans "You don't offer any service. Add new service from your profile." %}";
        const noStaffMemberTxt = "{% trans "No staff members found." %}";
    </script>

    <script src="{% static 'js/modal/error_modal.js' %}"></script>
    <script src="{% static 'js/app_admin/staff_index.js' %}"></script>
    <script src="{% static 'js/modal/show_modal.js' %}"></script>
    <script src="{% static 'js/js-utils.js' %}"></script>

    <script>
        function createCommonInputFields(appointment, servicesDropdown, isEditMode, defaultStartTime, staffDropdown) {
            const startTimeValue = isEditMode ? moment(appointment.start_time).format('HH:mm:ss') : defaultStartTime;
            const disabledAttribute = isEditMode ? '' : 'disabled';

            let superuserInputField = '';
            if (isUserSuperUser) {
                superuserInputField = `
                    <div class="flex-container-appt">
                    <label>{% trans 'Staff Member' %}:</label>
                    ${staffDropdown.outerHTML}
                    </div>
                `;
            }

            return `
                ${superuserInputField}
                <div class="flex-container-appt">
                <label>{% trans 'Service Name' %}:</label>
                ${servicesDropdown.outerHTML}
                </div>
                <label for="clientName">{% trans 'Client Name' %}:</label>
                <input type="text" name="clientName" value="${appointment.client_name || ''}" ${disabledAttribute} id="clientName" placeholder="John Doe">
                <label for="clientEmail">{% trans 'Client Email' %}:</label>
                <input type="email" name="clientEmail" value="${appointment.client_email || ''}" ${disabledAttribute} id="clientEmail" placeholder="john.doe@example.com">
                <span id="emailError" style="display:none;"></span>
                <label for="clientPhone">{% trans 'Phone Number' %}:</label>
                <input type="tel" name="clientPhone" value="${appointment.client_phone || ''}" ${disabledAttribute} id="clientPhone" placeholder="+12392350799">
                <label for="clientAddress">{% trans 'Client address' %}:</label>
                <input type="text" name="clientAddress" value="${appointment.client_address || ''}" ${disabledAttribute} id="clientAddress" placeholder="Naples, Florida">
                <div class="flex-container-appt">
                <label for="want_reminder">{% trans 'Wants reminder' %}:</label>
                <input type="checkbox" name="want_reminder" id="want_reminder" value="true" ${appointment.want_reminder ? 'checked' : ''}  ${disabledAttribute}>
                </div>
                <label for="additional_info">{% trans 'Additional Information' %}:</label>
                <input type="text" name="additional_info" value="${appointment.additional_info || ''}" ${disabledAttribute} placeholder="{% trans 'Client wants this and that' %}" id="additional_info">
                <div class="flex-container-appt">
                <label for="startTime">{% trans 'Start time' %}:</label>
                <input type="time" name="startTime" value="${startTimeValue}" ${disabledAttribute}>
                </div>
            `;
        }

        function generateModalContent(appointment, servicesDropdown, isEditMode, staffDropdown) {
            const startTimeValue = moment(appointment.start_time).format('HH:mm:ss');
            const commonInputs = createCommonInputFields(appointment, servicesDropdown, isEditMode, startTimeValue, staffDropdown);
            return `
                ${commonInputs}
                <div class="flex-container-appt">
                <label for="endTime">{% trans 'End time' %}:</label>
                <input type="time" name="endTime" value="${moment(appointment.end_time).format('HH:mm:ss')}" ${!isEditMode ? 'disabled' : ''}>
                </div>
            `;
        }

        function prepareCreateAppointmentModalContent(servicesDropdown, staffDropdown, defaultStartTime, formattedDate) {
            const appointment = {client_name: '', client_email: '', client_phone: '', client_address: ''};
            const commonInputs = createCommonInputFields(appointment, servicesDropdown, true, defaultStartTime, staffDropdown);

            return `
                ${commonInputs}
                <label for="date" style="display: none"></label>
                <input type="hidden" name="date" value="${formattedDate}">
            `;
        }
    </script>
{% endblock %}
