<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">Ranks</h2>
            <button type="button" class="btn btn-<%= config.other.navColor[2] %> float-end" data-bs-toggle="modal"
                data-bs-target="#exampleModal">
                Add Rank
            </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">Rank</th>
                            <th data-field="flags">Flags</th>
                            <th data-field="minH">Minimum Hours</th>
                            <th data-field="data">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <% ranks.forEach(rank=>{%>
                            <tr id="tr-id-<%=rank.id%>" class="tr-class-<%=rank.id%>" data-title="<%=rank.id%>" title="<%=rank.label%>"
                                data-object='{"key": "value"}'>
                                <td id="td-id-<%=rank.id%>" class="td-class-<%=rank.id%>" data-title="<%=rank.id%>">
                                    <strong>
                                        <%=rank.label%>
                                    </strong>
                                </td>
                                <td data-text="<%= rank.manual == 1 ? 'manual rank' : 'None'%>">
                                    <% if(rank.manual == 1){%>
                                        <span class="badge bg-primary">Manual Rank</span>    
                                    <%}else{%>
                                        None
                                    <%}%>
                                </td>
                                <td data-text="<%= rank.minH%>">
                                    <%= rank.minH %>
                                </td>
                                <td data-text="Delete">
                                    <button class="btn btn-danger" onclick="remRank('<%=rank.id%>')"><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 Rank</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form method="POST" action="/admin/ranks/new" novalidate class="needs-validation">
                                <div class="mb-3 mt-1">
                                    <label class="form-label">Rank Name:
                                    <input class="form-control mt-1" id="newAirAirSel" name="name" required>
                                    <div class="valid-feedback">Looks Good!</div>
                                    <div class="invalid-feedback">Needs to be valid text!</div>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Rank Minimum Hours:
                                        <input class="form-control" id="newAirAirSel" required type="number" step="any" name="min">
                                        <div class="valid-feedback">Looks Good!</div>
                                        <div class="invalid-feedback">Needs to be a valid number!</div>
                                </div>
                                <div class="form-check mt-1 text-start">
                                    <input class="form-check-input" type="checkbox" value="yespls" name="manual" id="manualCheck">
                                    <label class="form-check-label" for="manualCheck">
                                        Manual Only
                                    </label>
                                </div>
                                <p class="text-muted mb-3">This means to achieve this rank, the pilot must have been manually ranked via the <a
                                        class="link link-primary" href="/admin/users">users page</a>.</p>
                                <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://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script src="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.js"></script>
            
        <script>
            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) {
                        console.log(this)
                        if (this.status == 200) {
                            const data = JSON.parse(this.responseText).result;
                            document.getElementById("newAirLivSel").innerHTML = "";
                            data.forEach(liv => {
                                const el = document.createElement("option")
                                el.value = liv.liveryID;
                                el.innerHTML = liv.liveryName;
                                document.getElementById("newAirLivSel").appendChild(el)
                            })
                        } else {
                            alert(this.status)
                        }
                    }
                });
                xhr.open("GET", `/getLivData?liv=${airID}`);
                xhr.send(data);
            }
            loadAircraftLivData()
            function remRank(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/ranks/remove`);
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send(data);
                setTimeout(() => {
                    window.location.reload();
                }, 1500);
            }
        </script>
</body>

</html>