<?php
session_start();
error_reporting('0');
if(isset($_SESSION['username'])){
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>
        .card-title a {
            font-size: 16px;
            border-bottom: 1px solid #707478;
            color: #707478;
        }
        .card-title a:hover{
            border-bottom: 1px solid #1A2229;
            color: #1A2229;
        }
        .btn-app:first-child{
            margin-left:0 !important;
        }
    </style>


    <?php include 'includes/styles.php'; ?>
    <title> Admin Privileges | <?php echo $school_name; ?> </title>
    <?php

    //allow only super admin access to pin management
    $sql = "SELECT type FROM admin WHERE username =:username";
    $conn->query($sql);
    $conn->bind(":username", $_SESSION['username']);
    $user_type = $conn->fetchColumn();

    if($user_type != 'superadmin'){
        $redirect_link = $conn->base_url();
        ?>
        <script>
            alert('You are not permitted to view this page');
            window.location.replace("<?php echo $redirect_link; ?>");
        </script>

        <?php


    }

    ?>


</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

    <!-- Navbar -->
    <?php include 'includes/notifications.php'; ?>
    <!-- /.navbar -->

    <!-- Main Sidebar Container -->
    <?php include "includes/sidebar.php";?>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0">Set Admin Privileges </h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="<?php echo $conn->base_url();?>">Home</a></li>
                            <li class="breadcrumb-item active">Set Admin Privileges </li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">

                <div class="row">
                    <!--          <section class="col-lg-12 connectedSortable">-->
                    <!--           <a href="add-class"><button class="btn btn-outline-success">Add a class</button></a>-->
                    <!--          </section>-->
                    <section class="col-lg-12 connectedSortable">
                        <div class="card card-default">
                            <div class="card-header">
                                <h3 class="card-title"> Select all the privileges applicable to an admin </h3>

                                <div class="card-tools">
                                    <button type="button" class="btn btn-tool" data-card-widget="collapse">
                                        <i class="fas fa-minus"></i>
                                    </button>
                                </div>
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body">
                                <div class="row">

                                    <div class="col-12 col-sm-12">
                                        <div class="form-group">
                                            <form action="" method="post" id="update-privilege">
                                                <label for="privilege">Make Selections</label>
                                                <div class="select2-purple">
                                                    <?php
                                                    $sql = "SELECT * FROM admin_privileges";
                                                    $conn->query($sql);
                                                    $row_count = $conn->rowCount();
                                                    if($row_count > 0) {
                                                        $result = $conn->fetchMultiple();
                                                        $module_name = [];
                                                        foreach ($result as $privilege) {
                                                            $module_name[] = $privilege->module_name;
                                                            $module_id = $privilege->id;
                                                        }
                                                    }
                                                    ?>
                                                    <select name='privilege[]' id="privilege" class="select2" multiple="multiple" data-placeholder="Select privileges" data-dropdown-css-class="select2-purple" style="width: 100%;">
                                                        <?php
                                                        //fetch data that has not been added to the above table yet.
                                                        $sql = "SELECT * FROM all_privileges";
                                                        $conn->query($sql);
                                                        $row_count = $conn->rowCount();
                                                        if ($row_count > 0) {
                                                            $result_set = $conn->fetchMultiple();
                                                            foreach ($result_set as $new_privilege){
                                                                $new_module_name = $new_privilege->module_name;
                                                                $new_module_id = $new_privilege->id;
                                                                ?>
                                                                <option value="<?php echo $new_module_name; ?>"
                                                                    <?php
                                                                    if(in_array($new_module_name, $module_name)){
                                                                        echo 'selected';
                                                                    }else{
                                                                        echo '';
                                                                    }
                                                                    ?>
                                                                ><?php echo $new_module_name; ?></option>
                                                                <?php
                                                            }
                                                        }else{
                                                            ?>
                                                            <option value="">No data found </option> <?php
                                                        }

                                                        ?>
                                                    </select>
                                                </div>
                                                <div class="form-group" style="margin-top: 20px;">
                                                    <input type="submit" value="Update Selection" class="btn btn-success" id="submit">
                                                </div>
                                            </form>
                                        </div>
                                        <!-- /.form-group -->
                                    </div>
                                    <!-- /.col -->
                                </div>
                                <!-- /.row -->
                            </div>
                            <!-- /.card-body -->
                        </div>


                    </section>
                </div>

                <div id="success-msg">

                </div>
                <!-- /.row (main row) -->
            </div><!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "includes/footer.php";?>


    <script>
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();
        });

        //Initialize Select2 Elements
        $('.select2').select2()
    </script>

    <script>
        $('#update-privilege').parsley();
        $('#update-privilege').on('submit', function (event) {
            event.preventDefault();
            if($('#update-privilege').parsley().isValid()){
                $.ajax({
                    url: 'update-admin-privilege.php',
                    method: 'POST',
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData: false,
                    beforeSend: function () {
                        $('#submit').attr('disabled', 'disabled');
                        $('#submit').val('Updating selections, pls wait.....');
                    },
                    success: function (data) {
                        $('#update-privilege').parsley().reset();
                        $('#submit').attr('disabled', false);
                        $('#submit').val('Update Selections');
                        $('#success-msg').html(data);
                    }
                });
            }

        });
    </script>

    <?php
    }else{
    ?>
    <!DOCTYPE html>
    <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, maximum-scale=1, shrink-to-fit=no">
        <title>Login to continue |  <?php echo $school_name; ?> </title>

        <?php include 'includes/styles.php'; ?>

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

    ?>
