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

{% block title %} Tickets {% endblock title %}

<!-- Specific CSS goes HERE  -->
{% block stylesheets %}
    <link rel="stylesheet" href="{{ ASSETS_ROOT }}/css/select2-bootstrap.min.css">
{% endblock stylesheets %}

{% block content %}

    <!-- ### $App Screen Content ### -->
    <main class='main-content bgc-grey-100'>
      <div id='mainContent'>
        <div class="full-container">
          <div class="email-app">
            <div class="email-side-nav remain-height ov-h">
                <div class="h-100 layers">
                <div class="p-20 bgc-grey-100 layer w-100">
                    <button type="button" class="btn btn-danger c-white w-100" data-bs-toggle="modal" data-bs-target="#ticketModal">New Ticket</button>
                </div>
                <div class="scrollable pos-r bdT layer w-100 fxg-1">
                    <ul id="filterSidebar" class="p-20 nav flex-column">

                    {% if priorities %}

                        <li class="nav-item mT-15">
                            <h6>Priorities</h6>
                        </li>

                        <li id="filterPriorityAll" class="nav-item filter-priority">
                            <label for="filterPriority-all" class="nav-link c-grey-800 cH-blue-500 actived">
                                <div class="peers ai-c jc-sb">
                                    <div class="peer peer-greed">
                                        <input type="radio" id="filterPriority-all" name="filterPriorities" class="form-check-input me-2" checked="checked" value="all">
                                        <span>All</span>
                                    </div>
                                    <div class="peer">
                                        <span class="badge rounded-pill bgc-green-50 c-green-700">0</span>
                                    </div>
                                </div>
                            </label>
                        </li>

                        {% for priority in priorities %}

                            <li class="nav-item filter-priority">
                                <label for="filterPriority-{{ priority.uuid }}" class="nav-link c-grey-800 cH-blue-500 actived">
                                    <div class="peers ai-c jc-sb">
                                        <div class="peer peer-greed">
                                            <input type="radio" id="filterPriority-{{ priority.uuid }}" name="filterPriorities" class="form-check-input me-2" value="{{ priority.uuid }}">
                                            <span>{{ priority.title }}</span>
                                        </div>
                                        <div class="peer">
                                            <span class="badge rounded-pill" style="color: {{ priority.colour }}; background-color: {{ priority.backgroundcolour }};">0</span>
                                        </div>
                                    </div>
                                </label>
                            </li>

                        {% endfor %}

                    {% endif %}

                    {% if tags %}

                        <li class="nav-item mT-15">
                            <h6>Tags</h6>
                        </li>

                        {% for tag in tags %}

                            <li class="nav-item filter-tag">
                                <label for="filterTag-{{ tag.uuid }}" class="nav-link c-grey-800 cH-blue-500 actived">
                                    <div class="peers ai-c jc-sb">
                                        <div class="peer peer-greed">
                                            <input type="checkbox" id="filterTag-{{ tag.uuid }}" class="form-check-input me-2" value="{{ tag.uuid }}">
                                            <span>{{ tag.title }}</span>
                                        </div>
                                        <div class="peer">
                                            <span class="badge rounded-pill" style="color: {{ tag.colour }}; background-color: {{ tag.backgroundcolour }};">0</span>
                                        </div>
                                    </div>
                                </label>
                            </li>

                        {% endfor %}

                    {% endif %}

                    {% if departments %}

                        <li class="nav-item mT-15">
                            <h6>Departments</h6>
                        </li>

                        {% for department in departments %}

                            <li class="nav-item filter-department">
                                <label for="filterDepartment-{{ department.uuid }}" class="nav-link c-grey-800 cH-blue-500 actived">
                                    <div class="peers ai-c jc-sb">
                                        <div class="peer peer-greed">
                                            <input type="checkbox" id="filterDepartment-{{ department.uuid }}" class="form-check-input me-2" value="{{ department.uuid }}">
                                            <span>{{ department.title }}</span>
                                        </div>
                                        <div class="peer">
                                            <span class="badge rounded-pill bgc-green-50 c-green-700">0</span>
                                        </div>
                                    </div>
                                </label>
                            </li>

                        {% endfor %}

                    {% endif %}

                    </ul>
                </div>
                </div>
            </div>
            <div class="email-wrapper row remain-height bgc-white ov-h">
                <div class="email-list h-100 layers">
                <div class="layer w-100">
                    <div class="bgc-grey-100 peers ai-c p-20 fxw-nw">
                    <div class="peer me-auto">
                        <div class="btn-group" role="group">
                            <button type="button" class="email-side-toggle d-n@md+ btn bgc-white bdrs-2 mR-3 cur-p">
                                <i class="ti-menu"></i>
                            </button>
                            <button type="button" class="btn bgc-white bdrs-2 mR-3 cur-p">
                                <i class="ti-folder"></i>
                            </button>
                            <button type="button" class="btn bgc-white bdrs-2 mR-3 cur-p">
                                <i class="ti-tag"></i>
                            </button>
                            <div class="btn-group" role="group">
                                <button id="btnGroupDrop1" type="button" class="btn cur-p bgc-white no-after dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <i class="ti-more-alt"></i>
                                </button>
                                <ul class="dropdown-menu fsz-sm" aria-labelledby="btnGroupDrop1">
                                <li>
                                    <a href="" class="d-b td-n pY-5 pX-10 bgcH-grey-100 c-grey-700">
                                        <i class="ti-trash mR-10"></i>
                                        <span>Delete</span>
                                    </a>
                                </li>
                                <li>
                                    <a href="" class="d-b td-n pY-5 pX-10 bgcH-grey-100 c-grey-700">
                                        <i class="ti-alert mR-10"></i>
                                        <span>Mark as Spam</span>
                                    </a>
                                </li>
                                <li>
                                    <a href="" class="d-b td-n pY-5 pX-10 bgcH-grey-100 c-grey-700">
                                        <i class="ti-star mR-10"></i>
                                        <span>Star</span>
                                    </a>
                                </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="peer">
                        <div class="btn-group" role="group">
                        <button type="button" class="fsz-xs btn bgc-white bdrs-2 mR-3 cur-p">
                            <i class="ti-angle-left"></i>
                        </button>
                        <button type="button" class="fsz-xs btn bgc-white bdrs-2 mR-3 cur-p">
                            <i class="ti-angle-right"></i>
                        </button>
                        </div>
                    </div>
                    </div>
                </div>
                <div class="layer w-100">
                    <div class="bdT bdB">
                    <input type="text" class="form-control m-0 bdw-0 pY-15 pX-20 bdrs-0" placeholder="Search...">
                    </div>
                </div>

                <div id="ticketsContainer" class="layer w-100 fxg-1 scrollable pos-r"></div>

                </div>
                <div class="email-content h-100">
                <div class="h-100 scrollable pos-r">
                    <div class="bgc-grey-100 peers ai-c jc-sb p-20 fxw-nw d-n@md+">
                    <div class="peer">
                        <div class="btn-group" role="group">
                        <button type="button" class="back-to-mailbox btn bgc-white bdrs-2 mR-3 cur-p">
                            <i class="ti-angle-left"></i>
                        </button>
                        <button type="button" class="btn bgc-white bdrs-2 mR-3 cur-p">
                            <i class="ti-folder"></i>
                        </button>
                        <button type="button" class="btn bgc-white bdrs-2 mR-3 cur-p">
                            <i class="ti-tag"></i>
                        </button>
                        <div class="btn-group" role="group">
                            <button id="btnGroupDrop1" type="button" class="btn cur-p bgc-white no-after dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <i class="ti-more-alt"></i>
                            </button>
                            <ul class="dropdown-menu fsz-sm" aria-labelledby="btnGroupDrop1">
                            <li>
                                <a href="" class="d-b td-n pY-5 pX-10 bgcH-grey-100 c-grey-700">
                                <i class="ti-trash mR-10"></i>
                                <span>Delete</span>
                                </a>
                            </li>
                            <li>
                                <a href="" class="d-b td-n pY-5 pX-10 bgcH-grey-100 c-grey-700">
                                <i class="ti-alert mR-10"></i>
                                <span>Mark as Spam</span>
                                </a>
                            </li>
                            <li>
                                <a href="" class="d-b td-n pY-5 pX-10 bgcH-grey-100 c-grey-700">
                                <i class="ti-star mR-10"></i>
                                <span>Star</span>
                                </a>
                            </li>
                            </ul>
                        </div>
                        </div>
                    </div>
                    <div class="peer">
                        <div class="btn-group" role="group">
                        <button type="button" class="fsz-xs btn bgc-white bdrs-2 mR-3 cur-p">
                            <i class="ti-angle-left"></i>
                        </button>
                        <button type="button" class="fsz-xs btn bgc-white bdrs-2 mR-3 cur-p">
                            <i class="ti-angle-right"></i>
                        </button>
                        </div>
                    </div>
                    </div>
                    <div class="email-content-wrapper">
                    <!-- Header -->
                    <div class="peers ai-c jc-sb pX-40 pY-30">
                        <div class="peers peer-greed">
                        <div class="peer mR-20">
                            <img id="ticketAuthorImg" class="bdrs-50p w-3r h-3r" alt="" src="" style="display: none; object-fit: cover;">
                        </div>
                        <div class="peer">
                            <small id="ticketTimestamp"></small>
                            <h5 id="ticketAuthor" class="c-grey-900 mB-5"></h5>
                            <div id="ticketBadges" style="display: none;"></div>
                        </div>
                        </div>
                        <div class="peer">
                            <div class="btn-group" role="group">
                                <button id="btnGroupDrop2" class="btn btn-danger c-white bdrs-50p p-15 lh-0" style="display: none;" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <i class="ti-menu"></i>
                                </button>
                                <ul class="dropdown-menu fsz-sm" aria-labelledby="btnGroupDrop2">
                                    test
                                </ul>
                            </div>
                        </div>
                    </div>

                    <!-- Content -->
                    <div class="bdT pX-40 pY-30">
                        
                        <h4 id="ticketTitle"></h4>
                        <div id="ticketDesc"></div>
                        <!-- <h4>Title of this email goes here</h4>
                        <p>
                        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                        </p>
                        <p>
                        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                        </p>
                        <p>
                        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam
                        </p> -->
                    </div>
                    </div>
                </div>
                </div>
            </div>
          </div>
        </div>
      </div>
    </main>

    <div id="ticketModal" class="modal fade" aria-hidden="true">
        <div class="modal-dialog">
            <form method="post">

                {% csrf_token %}

                <div class="modal-content">
                    <div class="modal-header">
                        <h3 class="modal-title fs-5">New Ticket</h3>
                        <button type="button" class="btn-close" data-bs-toggle="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="newTitle" class="form-label">Subject</label>
                            <input type="text" name="newTitle" id="newTitle" class="form-control" placeholder="Subject">
                            <small class="text-muted">Briefly describe the subject of the ticket.</small>
                        </div>
                        <div class="mb-3">
                            <label for="newDesc" class="form-label">Description</label>
                            <div id="newDesc" class="form-control"></div>
                            <small class="text-muted">Describe your issue in detail here.</small>
                            <!-- class="form-control" -->
                        </div>
                        <div class="mb-3">
                            <label for="newPriority" class="form-label">Priority</label>
                            <select name="newPriority" id="newPriority" class="select-2">
                                {% for priority in priorities %}
                                    <option value="{{ priority.uuid }}">{{ priority.title }}</option>
                                {% endfor %}
                            </select>
                            <small class="text-muted">How important is this ticket?</small>
                        </div>
                        <div class="mb-3">
                            <label for="newTags" class="form-label">Tags</label>
                            <select name="newTags" id="newTags" class="select-2" multiple="multiple">
                                {% for tag in tags %}
                                    <option value="{{ tag.uuid }}">{{ tag.title }}</option>
                                {% endfor %}
                            </select>
                            <small class="text-muted">Use tags to categorize this ticket.</small>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-danger c-white">Submit</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

{% endblock content %}

<!-- Specific Page JS goes HERE  -->
{% block javascripts %}
<script>
    var displayedTicketID = -1;
        filters = {};
        editor = null;

    const formControls = [
        {
            id: "newTitle",
            validation: function(element) {
                const value = element.val();
                return (!element.attr("required") || value.trim() !== "")
            },
            errorMessage: function(element) {
                return "This field is required."
            }
        }
    ];

    $(document).ready(function() {
        // $(".email-list-item").on("click", function() {
        //     displayTicket(this);
        // });

        ClassicEditor
            .create( document.getElementById("newDesc"), {})
            .then( newEditor => {
                editor = newEditor;
            })
            .catch( error => {
                console.error(error)
            });

        // $("#filterPriorityAll").click(function() {
        //     delete filters[".filter-priority"];
        //     loadAllTickets();
        // });

        setupFilter("#filterSidebar .filter-department", "author__department__in");
        setupFilter("#filterSidebar .filter-tag", "tags__in")
        setupFilter("#filterSidebar .filter-priority", "priority__in")

        loadFilterCounts();
        loadAllTickets();
    });

    function setupFilter(selector, key) {
        $(selector).each(function () {
            var input = $(this).find("input[type=checkbox], input[type=radio]");
            var uuid = input.val();

            input.on("change", function () {
                if (input.is(":checkbox")) {
                    if ($(this).is(":checked")) {
                        filters[key] = filters[key] || [];
                        filters[key].push(uuid);
                    } else {
                        // filters[key].splice(filters[key].indexOf(uuid), 1);
                        filters[key] = filters[key].filter(id => id !== uuid);
                        if (filters[key].length === 0) {
                            delete filters[key];
                        }
                    }
                }
                else if (input.is(":radio") && input.is(":checked")) {
                    filters[key] = [uuid];
                }

                console.log(JSON.stringify(filters, null, 4));
                loadAllTickets();
            });
        });
    }

    function validateForm() {

        $("#ticketModal form").find(".form-control,.form-select").removeClass("is-valid is-invalid");
        $("#ticketModal form .invalid-feedback").text("");

        var valid = true;

        formControls.forEach(function(control) {
            var element = $("#" + control.id);
            if (!control.validation(element)) {
                element.addClass("is-invalid");
                element.siblings(".invalid-feedback").text(control.errorMessage(element));
                valid = false;
            }
            else {
                element.addClass("is-valid");
            }
        });

        return valid;
    }

    $("#ticketModal form").on("submit", function(event) {
        event.preventDefault();

        if (!validateForm()) {
            return;
        }

        $.ajax({
            url: "{% url 'ticket-new' %}",
            type: "POST",
            dataType: "json",
            data: {
                csrfmiddlewaretoken: "{{ csrf_token }}",
                title: $("#newTitle").val(),
                description: editor.getData(),
                author_id: "{{ request.user.uuid }}",
                priority_id: $("#newPriority").val(),
                tag_ids: $("#newTags").val()
            },
            success: function(data) {
                loadAllTickets();
                loadFilterCounts();
            },
            error: function(data) {
                alert(JSON.stringify(data, null, 4))
            }
        });
    });

    function getOrdinalSuffix(day) {
        if (day >= 11 && day <= 13) {
            return day + 'th';
        } else {
            switch (day % 10) {
                case 1: return day + 'st';
                case 2: return day + 'nd';
                case 3: return day + 'rd';
                default: return day + 'th';
            }
        }
    }

    function updateFilterCounts(filterType, data) {
        $("#filterSidebar .filter-" + filterType).each(function() {
            var uuid = $(this).find("input[type=checkbox],input[type=radio]").val();
            var count = data[filterType + '_counts'][uuid];
            $(this).find(".badge").text(count);
        });
    }

    function loadFilterCounts() {
        $.ajax({
            url: "{% url 'ticket-getfiltercounts' %}",
            type: "POST",
            dataType: "json",
            data: {
                csrfmiddlewaretoken: "{{ csrf_token }}"
            },
            success: function(data) {
                console.log(JSON.stringify(data, null, 4));

                updateFilterCounts('priority', data);
                updateFilterCounts('tag', data);
                updateFilterCounts('department', data);
                $("#filterPriorityAll .badge").text(data.ticket_count);

            },
            error: function(data) {
                alert(JSON.stringify(data, null, 4))
            }
        });
    }

    function loadAllTickets() {
        $("#ticketsContainer").empty();

        $.ajax({
            url: "{% url 'ticket-getmany' %}",
            type: "POST",
            dataType: "json",
            data: {
                csrfmiddlewaretoken: "{{ csrf_token }}",
                filters: JSON.stringify(filters)
            },
            success: function(data) {
                console.log(JSON.stringify(data, null, 4))

                data.tickets.forEach(function(ticket) {
                    var timestamp = new Date(ticket.timestamp);
                    var formattedTime;

                    if (ticket.was_yesterday) {
                        var day = getOrdinalSuffix(timestamp.getDate());
                        var month = timestamp.toLocaleString('en-GB', { month: 'short' });
                        var year = timestamp.toLocaleString('en-GB', { year: 'numeric' });
                        var time = timestamp.toLocaleString('en-GB', { hour: 'numeric', minute: 'numeric' });

                        // Formatting the final result
                        var formattedTime = time + ', ' + day + ' ' + month + ' ' + year;
                    }
                    else {
                        var hours = timestamp.getUTCHours();
                        var minutes = timestamp.getUTCMinutes();
                        formattedTime = hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0');
                    }

                    if (ticket.is_edited) {
                        formattedTime += " • edited";
                    }

                    var item = $(`
                        <div class="email-list-item peers fxw-nw p-20 bdB bgcH-grey-100 cur-p" data-ticket-id="${ticket.uuid}" data-author-icon="${ticket.author.icon}">
                            <div class="peer mR-10">
                                <img src="${ticket.author.icon}" alt="" class="w-2r h-2r bdrs-50p me-2" style="object-fit: cover;">
                            </div>
                            <div class="peer peer-greed ov-h">
                                <div class="peers ai-c">
                                    <div class="peer peer-greed">
                                        <h6 class="ticket-author">${ticket.author.forename} ${ticket.author.surname}</h6>
                                    </div>
                                    <div class="peer">
                                        <small class="ticket-timestamp">${formattedTime}</small>
                                    </div>
                                </div>
                                <h5 class="fsz-def tt-c c-grey-900 ticket-title">${ticket.title}</h5>
                                <span class="whs-nw w-100 ov-h tov-e d-b ticket-desc">${ticket.description}</span>
                            </div>
                        </div>
                    `);


                    $("#ticketsContainer").append(item);
                });

                $(".email-list-item").on("click", function(e) {
                    e.preventDefault();
                    displayTicket(this);
                    $('.email-content').toggleClass('open');
                });
            },
            error: function(data) {
                alert(JSON.stringify(data, null, 4))
            }
        });

    }

    function displayTicket(ticketElement) {
        ticket = $(ticketElement);
        ticketID = ticket.data("ticket-id");

        // $(".back-to-mailbox").off("click").on("click", function(event) {
        //     event.preventDefault();
        //     $('.email-content').toggleClass('open');
        //     displayTicket(ticketElement);
        // });
        
        
        if (displayedTicketID === ticketID) {
            // displayedTicketID = -1;
            return;
        }

        ticket.siblings().removeClass("bgc-grey-100");
        ticket.addClass("bgc-grey-100");

        $("#ticketTitle").text("")
        $("#ticketDesc").empty();
        $("#ticketAuthor").text("");
        $("#ticketAuthorImg").hide();
        $("#ticketAuthorImg").prop("src", "");
        $("#ticketTimestamp").text("");
        $("#btnGroupDrop2").hide();
        $("#ticketBadges").empty().hide();

        
        displayedTicketID = ticketID;

        $.ajax({
            url: `{% url 'ticket-getone' %}`,
            type: 'POST',
            dataType: 'json',
            data: {
                csrfmiddlewaretoken: '{{ csrf_token }}',
                ticket_uuid: ticketID
            },
            success: function (data) {
                console.log(JSON.stringify(data, null, 4));

                var ticket = data.ticket;
                var author = ticket.author;
                var department = author.department;
                var priority = ticket.priority;

                $("#ticketTitle").text(ticket.title);
                $("#ticketDesc").append($(`<div class="w-100">${ticket.description}</div>`));
                $("#ticketAuthor").text(`${author.forename} ${author.surname}`);
                $("#ticketAuthorImg").show();
                $("#ticketAuthorImg").prop("src", author.icon);
                $("#btnGroupDrop2").show();
                $("#ticketBadges").show();

                $("#ticketBadges").append($(`<div class="badge me-1" style="color: ${priority.colour}; background-color: ${priority.backgroundcolour};">${priority.title} Priority <i class="ti-control-record "></i></div>`));

                if (department != null) {
                    $("#ticketBadges").append($(`<div class="badge bgc-deep-purple-500 me-1">${department.title}</div>`));
                }

                ticket.tags.forEach(function(tag) {
                    $("#ticketBadges").append($(`<div class="badge me-1" style="color: ${tag.colour}; background-color: ${tag.backgroundcolour};">${tag.title} <i class="ti-tag"></i></div>`));
                });

                // timestamp
                var timestamp = new Date(ticket.timestamp);
                var formattedTime;

                if (ticket.was_yesterday) {
                    var options = { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' };
                    formattedTime = timestamp.toLocaleDateString('en-GB', options);
                }
                else {
                    var hours = timestamp.getUTCHours();
                    var minutes = timestamp.getUTCMinutes();
                    formattedTime = hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0');
                }

                if (ticket.is_edited) {
                    formattedTime += " • edited";
                }

                $("#ticketTimestamp").text(formattedTime);
            },
            error: function(message) {
                alert(JSON.stringify(message, null, 4));
            }
        });
    }
</script>
{% endblock javascripts %}
