{{set . "title" "Mahasiswa"}}
{{template "header.html" .}}

<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.0/font/bootstrap-icons.css" rel="stylesheet">


<style>
    .table {
        border-collapse: collapse;
        width: 100%;
    }

    .table th,
    .table td {
        border: 1px solid #dddddd;
        text-align: left;
        padding: 8px;
    }

    .table th {
        background-color: #fff176;
    }

    .table tr:nth-child(even) {
        background-color: #f2f2f2;
    }

    .table tr:nth-child(odd) {
        background-color: #ffffff;
    }

    .table th:first-child,
    .table td:first-child {
        border-left: 1px solid #dddddd;
    }

    .table th:last-child,
    .table td:last-child {
        border-right: 1px solid #dddddd;
    }

    .column {
        margin-top: 10px;
    }

    .header {
        padding: 10px;
        margin-bottom: 10px;

        border-bottom: 1px solid black;
    }

    .menu-bar {
        display: flex;
        align-items: center;
    }

    .sidebar {
        width: 250px;
        height: 100vh;
        background-color: #333;
        color: #fff;
        position: fixed;
        top: 0;
        left: -250px;
        transition: left 0.3s ease;
    }

    .menu-bar button {
        background: none;
        border: none;
        color: #fff;
        cursor: pointer;
        padding: 10px;
    }

    .menu-bar button:focus {
        outline: none;
    }

    .profile {
        display: flex;
        align-items: center;
        padding: 20px;
        margin-bottom: 20px;
    }




    .avatar {
        width: 40px;
        height: 40px;
        display: flex;
        border-radius: 50%;
        background-color: #ccc;
        margin-right: 10px;
        justify-content: center;
        border: 1px solid black;
        align-items: center;
    }

    avatar i {
        font-size: 40px;
        /* Ukuran ikon */
    }

    .menu {
        list-style-type: none;
        padding: 0;
    }

    .error {
        border-color: red !important;
    }

    .menu li {
        padding: 15px 20px;
        border-bottom: 1px solid #555;
    }

    .menu li a {
        color: #fff;
        text-decoration: none;
        display: block;
    }

    .menu-bar.active .sidebar {
        left: 0;
    }
</style>

<script>
    var admin = false;




    function logout() {
        window.location.href = "/adm";
    }


    function openModal() {

        $('#changePasswordModal').modal('show');
    }

    function toggleEye(id) {
        var newPassInput = document.getElementById(id);
        if (newPassInput.type == 'password') {
            newPassInput.type = 'text';
            this.innerHTML = '<i class="bi bi-eye"></i>';
        } else {
            newPassInput.type = 'password';
            this.innerHTML = '<i class="bi bi-eye-slash"></i>';
        }
    }



    function gantiPassword() {
        var newPassForm = document.getElementById('newpass');
        var confirmPassForm = document.getElementById('confirmpass');
        var newPass = newPassForm.value;
        var confirmPass = confirmPassForm.value;

        var newData = {
            Password: newPass,
        };

        if (newPass == confirmPass) {
            fetch('/adm/changepass', {
                method: 'PATCH',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(newData)
            })
                .then(response => {
                    alert("Sukses mengganti password");
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    window.location.reload();
                    return response.json();
                })
                .catch(error => {
                    console.error('There was an error!', error);
                });



            $('#changePasswordModal').modal('hide');
        } else {
            newPassForm.classList.add('error');
            confirmPassForm.classList.add('error');
            alert("Password tidak sesuai");

            newPassForm.value = "";
            confirmPassForm.value = "";
            return false;
        }


    }

    document.getElementById("newpass").addEventListener("input", function () {
        this.classList.remove('error');
    });
    document.getElementById("confirmpass").addEventListener("input", function () {
        this.classList.remove('error');
    });

    function toggleDropdown() {
        $('.menu-bar').toggleClass('active');
    }






    function setAsAdmin(id, isAdmin) {

        isAdmin = (isAdmin === 'true');
        admin = !isAdmin;


        var data = {
            IsAdmin: admin
        };


        fetch('/mahasiswa/admin/' + id, {
            method: 'PATCH',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
            .then(response => {

                if (!response.ok) {
                    alert("Gagal menambahkan akses admin");

                    throw new Error('Network response was not ok');
                } else {
                    if (admin) {
                        alert("Sukses menambahkan akses admin");
                    } else {
                        alert("Sukses menghapus akses admin");
                    }

                }
                window.location.reload();
                return response.json();
            })
            .catch(error => {
                console.error('There was an error!', error);
            });
    }



</script>



<div class="header" style=" padding: 10px;">

    <div class="header">
        <div class="modal fade" id="changePasswordModal" tabindex="-1" role="dialog"
            aria-labelledby="changePasswordModal" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Ganti Password</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form id="addForm">
                            <div class="form-group row">
                                <label for="newpass" class="col-sm-4 col-form-label">Password Baru: </label>
                                <div class="col-sm-6">
                                    <input type="password" class="form-control" id="newpass" name="newpass">
                                    <button class="btn btn-outline-secondary col-sm-2" onclick="toggleEye('newpass')" type="button">
                                        <i class="bi bi-eye-slash"></i>
                                    </button>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="confirmpass" class="col-sm-4 col-form-label">Konfirmasi Password: </label>
                                <div class="col-sm-6">
                                    <input type="password" class="form-control" id="confirmpass" name="confirmpass">
                                    <button class="btn btn-outline-secondary col-sm-2" onclick="toggleEye('confirmpass')" type="button">
                                        <i class="bi bi-eye-slash"></i>
                                    </button>
                                </div>
                            </div>
                            
                        
                        </form>


                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                        <button type="button" id="simpanButton" class="btn btn-success"
                            onclick="gantiPassword()">OK</button>
                    </div>
                </div>
            </div>
        </div>
        <button class="btn" type="button" id="toggleSidebar" onclick="toggleDropdown()">
            <span class="bi bi-list"></span>
        </button>

        <div class="menu-bar">
            <div class="sidebar">

                <div class="profile">
                    <div class="avatar"><i class="bi bi-person"></i></div>
                    <span>Admin</span>
                </div>
                <ul class="menu">
                    <li><a href="#">Mahasiswa</a></li>
                    <li><a href="#" onclick="logout()">Logout</a></li>
                    <li><a href="#" onclick="openModal()">Ganti Password</a></li>
                </ul>
            </div>
        </div>
    </div>

    <div class="container">
        <div class="row">
            <div class="column">
                <table class="table">
                    <thead>

                        <tr>
                            <th>ID</th>
                            <th>Nama</th>
                            <th>Email</th>
                            <th>Coin</th>
                            <th>Powerups</th>
                            <th>Is Admin</th>
                            <th>Action</th>
                        </tr>

                    </thead>
                    <tbody>
                        {{range .Mahasiswa}}
                        <tr>
                            <td>{{.ID}}</td>
                            <td>{{.Nama}}</td>
                            <td>{{.Email}}</td>

                            <td>{{.Coin}}</td>
                            <td>{{.Powerups}}</td>
                            <td>{{.IsAdmin}}</td>
                            <td>
                                <button type="button" class="btn btn-warning btn-sm"
                                    onclick="setAsAdmin('{{.ID}}', '{{.IsAdmin}}')">
                                    {{if .IsAdmin}}
                                    <span class="bi bi-check-circle-fill" style="color: green;"></span>
                                    {{else}}
                                    <span class="bi bi-x-circle-fill" style="color: red;"></span>
                                    {{end}}

                                    <span class="bi bi-key-fill"></span>
                                </button>

                            </td>
                        </tr>
                        {{end}}
                    </tbody>
                </table>
            </div>
        </div>

        {{template "footer.html" .}}