{% extends "layouts/base.html" %}

{% block title %}Company Meetings{% endblock %}
{% block extra_css %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.css" />
<style>
    body {
        background: #f5f7fb;
    }

    .meetings-container {
        padding: 32px 24px 80px;
        max-width: 1200px;
        margin: 0 auto;
    }

    .header-section {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 16px;
        margin-bottom: 24px;
    }

    .header-section h1 {
        margin: 0 0 6px;
        font-size: 30px;
        font-weight: 700;
        color: #0f172a;
    }

    .header-section p {
        margin: 0;
        color: #475569;
        font-size: 15px;
    }

    .calendar-card {
        background: #ffffff;
        border-radius: 18px;
        box-shadow: 0 35px 65px rgba(15, 23, 42, 0.06);
        border: 1px solid rgba(148, 163, 184, 0.12);
        overflow: hidden;
    }

    .calendar-toolbar {
        padding: 18px 24px;
        border-bottom: 1px solid rgba(148, 163, 184, 0.18);
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .calendar-toolbar h3 {
        margin: 0;
        font-size: 18px;
        font-weight: 600;
        color: #0f172a;
    }

    #calendar {
        padding: 24px;
    }

    .fc .fc-toolbar-title {
        font-size: 20px;
        font-weight: 600;
        color: #0f172a;
    }

    .fc .fc-button {
        border-radius: 12px;
        border: none;
        background: #e2e8f0;
        color: #0f172a;
        font-weight: 600;
        padding: 8px 14px;
        box-shadow: none;
        text-transform: capitalize;
    }

    .fc .fc-button-primary:not(:disabled).fc-button-active,
    .fc .fc-button-primary:not(:disabled):active {
        background: #2563eb;
        color: #fff;
        border: none;
    }

    .fc .fc-button-primary:hover {
        background: #cbd5f5;
    }

    .fc .fc-daygrid-day-number {
        font-weight: 600;
        color: #0f172a;
    }

    .fc .fc-daygrid-day.fc-day-today {
        background: rgba(37, 99, 235, 0.08);
    }

    .fc .fc-event {
        border-radius: 10px;
        padding: 6px 8px;
        font-size: 13px;
        background: linear-gradient(135deg, #1d4ed8, #2563eb);
        border: none;
        box-shadow: 0 8px 16px rgba(37, 99, 235, 0.25);
    }

    .fc-theme-standard .fc-scrollgrid,
    .fc td,
    .fc th {
        border-color: rgba(148, 163, 184, 0.18);
    }

    .custom-modal {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(15, 23, 42, 0.45);
    }

    .custom-modal-content {
        background-color: #ffffff;
        margin: 10% auto;
        padding: 28px;
        border-radius: 18px;
        width: 90%;
        max-width: 460px;
        box-shadow: 0 40px 80px rgba(15, 23, 42, 0.25);
        border: 1px solid rgba(148, 163, 184, 0.12);
    }

    .custom-modal-content h3 {
        margin: 0 0 16px;
        font-size: 22px;
        font-weight: 600;
        color: #0f172a;
    }

    .close {
        color: #94a3b8;
        float: right;
        font-size: 24px;
        font-weight: bold;
        cursor: pointer;
        transition: color 0.2s ease;
    }

    .close:hover,
    .close:focus {
        color: #2563eb;
        text-decoration: none;
    }

    #meetingDetails {
        line-height: 1.6;
        color: #334155;
        font-size: 15px;
    }

    #meetingDetails strong {
        color: #0f172a;
    }

    @media (max-width: 768px) {
        .meetings-container {
            padding: 24px 18px 60px;
        }

        #calendar {
            padding: 16px;
        }

        .calendar-toolbar {
            flex-direction: column;
            align-items: flex-start;
            gap: 16px;
        }

        .fc .fc-toolbar.fc-header-toolbar {
            flex-direction: column;
            gap: 12px;
        }

        .custom-modal-content {
            margin: 20% auto;
        }
    }
</style>
{% endblock %}
{% block content %}
<div class="meetings-container">
    <div class="header-section">
        <div>
            <h1>Meetings Calendar</h1>
            <p>Stay on top of upcoming appointments and customer conversations.</p>
        </div>
    </div>
    <div class="calendar-card">
        <div class="calendar-toolbar">
            <h3>Schedule Overview</h3>
        </div>
        <div id="calendar"></div>
    </div>
</div>
<script type="application/json" id="meetings-data">{{ meetings|tojson|safe }}</script>

<!-- Custom Modal -->
<div id="meetingModal" class="custom-modal">
    <div class="custom-modal-content">
        <span class="close">&times;</span>
        <h3>Meeting Details</h3>
        <p id="meetingDetails"></p>
    </div>
</div>
{% endblock %}
{% block extra_js %}
<script src="https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        var calendarEl = document.getElementById('calendar');
        var modal = document.getElementById('meetingModal');
        var span = document.getElementsByClassName('close')[0];

        if (calendarEl) {
            var meetingsDataElement = document.getElementById('meetings-data');
            var meetingsData = [];

            if (meetingsDataElement) {
                try {
                    meetingsData = JSON.parse(meetingsDataElement.textContent || '[]') || [];
                } catch (error) {
                    meetingsData = [];
                }
            }

            var meetingEvents = meetingsData.map(function (meeting) {
                return {
                    title: 'Meeting with ' + meeting[1],
                    start: meeting[2] + 'T' + meeting[3],
                    extendedProps: {
                        customerEmail: meeting[1]
                    }
                };
            });

            var calendar = new FullCalendar.Calendar(calendarEl, {
                initialView: 'dayGridMonth',
                events: meetingEvents,
                eventClick: function (info) {
                    var details = 'Meeting with ' + info.event.extendedProps.customerEmail + '<br>' +
                        '<strong>Date</strong>: ' + info.event.start.toLocaleDateString() + '<br>' +
                        '<strong>Time</strong>: ' + info.event.start.toLocaleTimeString();
                    document.getElementById('meetingDetails').innerHTML = details;
                    modal.style.display = 'block';
                }
            });
            calendar.render();
        }

        // Close the modal
        span.onclick = function () {
            modal.style.display = 'none';
        };
        window.onclick = function (event) {
            if (event.target == modal) {
                modal.style.display = 'none';
            }
        };
    });
</script>
{% endblock %}