<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        Admin || <%= config.name %>
    </title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.css">
    <%- include("./../components/head.ejs")%>
</head>

<body class="w-100 bg-light-light h-auto">
    <%- include("./../components/nav.ejs")%>
        <div class="w-100 content bg-light-light px-3 px-md-5 py-3" id="content">
            <h2 class="d-inline-block">Aircraft</h2>
            <button type="button" class="btn btn-<%= config.other.navColor[2] %> float-end" data-bs-toggle="modal"
                data-bs-target="#exampleModal">
                Add Aircraft
            </button>
            <div class="table-responsive">
                <table data-toggle="table" data-search="true" data-show-columns="true" id="mainTable">
                    <thead>
                        <tr class="tr-class-2">
                            <th data-field="title">Vehicle</th>
                            <th data-field="data">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <% aircraft.forEach(vic=>{%>
                            <tr id="tr-id-<%=vic.livID%>" class="tr-class-<%=vic.livID%>" data-title="<%=vic.livID%>"
                                title="<%=vic.livID%>" data-object='{"key": "value"}'>
                                <td id="td-id-<%=vic.livID%>" class="td-class-<%=vic.livID%>" data-title="<%=vic.publicName%>">
                                    <strong>
                                        <%=vic.publicName%>
                                    </strong>
                                </td>
                                <td data-text="Delete">
                                    <button class="btn btn-danger" onclick="remCraft('<%=vic.livID%>')"><i
                                            class="bi bi-x-circle"></i></button>
                                </td>
                            </tr>
                            <%})%>
                    </tbody>
                </table>
            </div>
            
            <br>
            <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Add Aircraft</h5>
                        </div>
                        <div class="modal-body">
                            <form method="POST" action="/admin/aircraft/new">
                                <label class="form-label">Aircraft
                                    <select class="form-select" id="newAirAirSel" name="airID" onchange="loadAircraftLivData()">
                                        <% listCraft.forEach(obj=>{%>
                                            <option value='<%=obj.id%>'>
                                                <%= obj.name %>
                                            </option>
                                            <%})%>
                                    </select>
                                </label><br>
                                <label class="form-label">Livery
                                    <select class="form-select" id="newAirLivSel" name="livID">
                                        <option value="">Waiting Selection</option>
                                    </select>
                                </label><br>
                                <button type="submit" class="btn btn-<%= config.other.navColor[2] %>">Submit</button>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <%- include('./../components/footer.ejs') %>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
            integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
            crossorigin="anonymous"></script>
            <script src="https://code.jquery.com/jquery-3.6.0.min.js"
                integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
            <script src="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script>
            function sort(id){
                var sel = $(`#${id}`);
                var selected = sel.val(); // cache selected value, before reordering
                var opts_list = sel.find('option');
                opts_list.sort(function(a, b) { return $(a).text() > $(b).text() ? 1 : -1; });
                sel.html('').append(opts_list);
                sel.val(selected); // set cached selected value
                document.getElementById(id).selectedIndex = 0;
            }
            sort('newAirAirSel');
            function loadAircraftLivData() {
                const airID = document.getElementById("newAirAirSel").value;
                document.getElementById("newAirLivSel").innerHTML = "<option selected value='' disabled>Loading...</option>"
                const data = null;
                const xhr = new XMLHttpRequest();
                xhr.addEventListener("readystatechange", function () {
                    if (this.readyState === this.DONE) {
                        if (this.status == 200) {
                            const data = JSON.parse(this.responseText);
                            document.getElementById("newAirLivSel").innerHTML = "";
                            data.livery.forEach(liv => {
                                const el = document.createElement("option")
                                el.value = liv.id;
                                el.innerHTML = liv.name;
                                document.getElementById("newAirLivSel").appendChild(el)
                            })
                            sort('newAirLivSel');
                        } else {
                            alert(this.status)
                        }
                    }
                });
                xhr.open("GET", `/getLivData?liv=${airID}`);
                xhr.send(data);
            }
            loadAircraftLivData()
            function remCraft(id) {
                    const data = `id=${id}`;
                    const xhr = new XMLHttpRequest();
                    xhr.withCredentials = true;
                    xhr.addEventListener("readystatechange", function () {
                        console.log(this)
                        if (this.readyState === this.DONE) {
                            console.log(this.responseText);
                        }
                    });
                    xhr.open("DELETE", `/admin/aircraft/remove`);
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xhr.send(data);
                    setTimeout(() => {
                        window.location.reload();
                    }, 1500);
                }
        </script>
</body>

</html>