<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Eversync</title>
    {% load static %}
    <link rel="icon" href="{% static 'favicon.ico' %}">
    <link rel="stylesheet" href="{% static 'index-style.css' %}">
    <link rel="stylesheet" href="{% static 'register-style.css' %}">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <script src='https://cdn.jsdelivr.net/npm/fullcalendar@6.1.17/index.global.min.js'></script>

    <style>
            #event-form input, #event-form button {
                border: 1px solid #ccc;
                padding: 8px;
                margin: 5px;
                border-radius: 4px;
            }

            #event-form input:focus {
                border-color: #888;
                outline: none;
            }
    </style>

</head>
<body>   
    <div class="header">
        <div class="header-content">
            <a href="/"><img src="{% static 'eversync2.png' %}" alt="Eversync Logo" style="height: 80px; margin-right: 10px; display: flex; align-items: center; gap: 5px;"></a>
            <a href="/" class="logo" >eversync</a>
            <div class="nav-links" style="position: relative;">
                <div class="dropdown">
                    <button class="dropdown-toggle" style="background: none; border: none; color: white; font-size: 16px; cursor: pointer;">
                        Welcome, {{ user.username }} <i class="fas fa-caret-down"></i>
                    </button>
                    <div class="dropdown-menu" style="display: none; position: absolute; right: 0; background-color: #333; border: 1px solid #444; border-radius: 4px; padding: 10px; width: 184px;">
                        
                        <form action="{% url 'manage' %}" method="post" style="margin: 0;">
                            {% csrf_token %}
                            <button type="submit" class="logout-button" style="background-color: transparent; color: white; border: none; cursor: pointer;">Manage Account</button>
                        </form>
                        
                        <form action="{% url 'logoutz' %}" method="post" style="margin: 0;">
                            {% csrf_token %}
                            <button type="submit" class="logout-button" style="background-color: transparent; color: white; border: none; cursor: pointer;">Log Out</button>
                        </form>
                        
                    </div>
                </div>
            </div>
        </div>
    </div> 
    <h1 style="text-align: center;">Calendar</h1>

    <form id="event-form" style="text-align: center;">
        <h3>Create Event</h3>
        <input type="text" id="title" placeholder="Title" required>
        <input type="datetime-local" id="start" required>
        <input type="datetime-local" id="end" required>
        <input type="color" id="color" required>
        <button type="submit">Add Event</button>
      </form>

    <div class="calendar-container">
        <div id="calendar"></div>
    </div>
    <style>
        .fc-button-group {
            margin-left: 100px;
            display: flex;
            flex-direction: row;  
        }

        @media (max-width: 768px) {  
            .fc-button-group {
                margin-left: 0;  
                flex-direction: column;  
                align-items: center; 
            }
        }

        .calendar-container {
            max-width: 800px; 
            margin: 0 auto;   
            padding: 20px;    
        }
    </style>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const calendarEl = document.getElementById('calendar');
            const calendar = new FullCalendar.Calendar(calendarEl, {
                headerToolbar: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'dayGridMonth,timeGridWeek,timeGridDay'
                },
                initialView: 'dayGridMonth',
                events: '/calendar/events/',
                editable: true,  
                droppable: true,
                eventClick: function(info) {
                    // Set the modal content
                    document.getElementById('event-title').textContent = info.event.title;
                    document.getElementById('event-start').textContent = info.event.start ? info.event.start.toLocaleString() : '';
                    document.getElementById('event-end').textContent = info.event.end ? info.event.end.toLocaleString() : '';
                    document.getElementById('event-id').textContent = info.event.id;

                    
                    // Display the modal
                    document.getElementById('event-modal').style.display = 'flex';
                },
            });
            calendar.render();

            // Close modal when the 'Close' button is clicked
            document.getElementById('close-modal').addEventListener('click', function () {
                document.getElementById('event-modal').style.display = 'none';
            });

            // Close modal if clicked outside of the modal content
            document.getElementById('event-modal').addEventListener('click', function (e) {
                if (e.target === document.getElementById('event-modal')) {
                    document.getElementById('event-modal').style.display = 'none';
                }
            });
        });
    </script>

<script>
    document.getElementById('event-form').addEventListener('submit', function (e) {
        e.preventDefault();

        const title = document.getElementById('title').value;
        const start = document.getElementById('start').value;
        const end = document.getElementById('end').value;
        const color = document.getElementById('color').value;
        
        fetch('/calendar_event_create/', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-CSRFToken': getCookie('csrftoken'),
            },
            body: new URLSearchParams({
                title: title,
                start_time: start,
                end_time: end,
                color: color,
            })
        })
        .then(res => res.json())
        .then(data => {
            if (data.message) {
                alert('Event created!');
                location.reload();
                calendar.refetchEvents();
                document.getElementById('event-form').reset();
            } else {
                alert('Error creating event.');
            }
        });
    });

    // CSRF helper
    function getCookie(name) {
        let cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            const cookies = document.cookie.split(';');
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].trim();
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
</script>

<script>
    document.getElementById('color').addEventListener('input', function (e) {
        e.target.style.backgroundColor = e.target.value;
    });
</script>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const toggle = document.querySelector('.dropdown-toggle');
        const menu = document.querySelector('.dropdown-menu');
        toggle.addEventListener('click', function (e) {
            e.stopPropagation(); 
            menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
        });
        document.addEventListener('click', function(e) {
            if (!toggle.contains(e.target) && !menu.contains(e.target)) {
                menu.style.display = 'none';
            }
        });
    });
</script>
</body>
    <div id="event-modal" style="display:none;">
        <div class="modal-content">
            <h3>Event Details</h3>
            <p><strong>Title:</strong> <span id="event-title"></span></p>
            <p><strong>Start:</strong> <span id="event-start"></span></p>
            <p><strong>End:</strong> <span id="event-end"></span></p>
            <button id="close-modal">Close</button>
            <span id="event-id" style="display: none;"></span>
            <button id="delete-event" style="background-color: red; color: white; border: none; cursor: pointer; padding: 10px 20px; border-radius: 4px; font-size: 16px; margin-top: 10px;">Delete</button>
        </div>
    </div>

    <style>
        #event-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 999;
        }

        .modal-content {
            background: white;
            padding: 20px;
            border-radius: 8px;
            width: 300px;
            text-align: center;
            box-shadow: 0px 2px 12px rgba(0, 0, 0, 0.2);
        }

        #close-modal {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 4px;
            font-size: 16px;
            margin-top: 10px;
        }

        #close-modal:hover {
            background-color: #0056b3;
        }
    </style>


<script>
    document.getElementById('delete-event').addEventListener('click', function () {
        const title = document.getElementById('event-title').textContent;
        const eventId = document.getElementById('event-id').textContent;

        fetch(`/calendar_event_delete/${eventId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-CSRFToken': getCookie('csrftoken'),
            },
            body: new URLSearchParams({
                id: eventId,
            })
        })
        .then(res => res.json())
        .then(data => {
            if (data.message) {
                alert('Event deleted!');
                document.getElementById('event-modal').style.display = 'none';
                calendar.refetchEvents();
                location.reload();
            } else {
                alert('Event deleted!');
                location.reload();
            }
        });
    });
</script>

</html>    