<?php session_start();
include '../db.php';

$page = "role";
$action = $_GET['action'];
 
if($_POST['submit']){

    $saveAry = array( 
        "name"      =>  $_POST["name"],
        "status"    =>  $_POST["status"],
        "school_id" =>  $_POST["school_id"],
        "added_by"  =>  $_SESSION["superadmin"]["uid"],
    );

    if($action == "permission"){
        foreach($_POST["name"] as $name){
            $all=0;$none=0;
            if($_POST["permission-".$name] == "1")$all=1;
			elseif($_POST["permission-".$name] == "0")$none=1;
			
			$aryData=array(	'page_name'	=>	trim($name),
							'a'	=>	$all,
							'n'	=>	$none,
							'roll_id'=>	$_GET["id"]	,
							'school_id' => $_GET["school_id"]
			);
			
			$is_check=$db->getVal("select id from tbl_privilages where school_id='".$_GET["school_id"]."' and page_name='".$name."' and roll_id='".$_GET["id"]."'");
			if($is_check!=""){
				$flgIn=$db->updateAry("tbl_privilages",$aryData," where school_id='".$_GET["school_id"]."' and page_name='".$name."' and roll_id='".$_GET["id"]."'");
			}else{
                $aryData["added_by"]=$_SESSION["superadmin"]["uid"];
				$flgIn=$db->insertAry("tbl_privilages",$aryData);
			}
			
        }
            echo "<script>alert('Permission saved successfully.')</script>";
            redirect("$page.php");

    }

    if($action=="add"){
        $flgIn = $db->insertAry("tbl_roll", $saveAry);
        if($flgIn > 0){
            echo "<script>alert('Role added successfully.')</script>";
            redirect("$page.php");
        }else {
            echo "<script>alert('Something went wrong. Please try again.')</script>";
        }
    
    }

    if($action == "edit" && $_GET['id']!=''){
        $flgUp = $db->updateAry("tbl_roll", $saveAry, "where id='$_GET[id]' ");
        echo "<script>alert('Updated successfully.')</script>";
        redirect("$page.php?action=view");
    }

}

?>
<html>
    <head>
        <?php include 'meta.php'; ?>        
    </head>

<body>
    <?php include 'header.php'; ?>

    <div class="container-fluid">

    <div class="row">

        <div class="col-md-2 col-3">
            <?php include 'sidebar.php'; ?>
        </div>
        <div class="col-md-10 col-9">
            
            <div class="inner-div">

                <?php if(($action == "add") || ($action =="edit" && $_GET['id']!="" ) ){
                    if($action =="edit" && $_GET['id']!="" ){
                        $role = $db->getRow("select * from tbl_roll where id=$_GET[id] ");
                    }
                ?>

                <h2 class="top-heading"><?php echo $page; ?> Details</h2>

                <form method="post" >
                    
                <div class="form-group">
                    <label>School<span class="mandatory">*</span></label>
                    <select name="school_id" required>
                        <option value="">--Select School--</option>
                        <?php $allschools = $db->getRows("select * from schools");
                            foreach ($allschools as $school) { ?>
                                <option value="<?php echo $school['id']; ?>" <?php if($role['school_id']==$school['id']) echo "selected"; ?>><?php echo $school['school_name']; ?></option>
                                <?php
                            }
                            ?>
                    </select>
                </div>

                <div class="form-group">
                    <label>Role Name <span class="mandatory">*</span></label>
                    <input type="text" name="name" value="<?php if($role['name']==""){ echo "Super Admin"; } else echo $role['name']; ?>" required />
                </div>
                    
                <div class="form-group">
                    <label>Status <span class="mandatory">*</span></label>
                    <select name="status" id="status">
                        <option value="1" <?php if($role['status']===1)echo "selected"; ?>>Active</option>
                        <option value="0" <?php if($role['status']===0)echo "selected"; ?>>Inactive</option>
                    </select>
                </div>
                
                <div class="text-center">
                    <input type="submit" name="submit" value="Submit" />
                </div>

                </form>
                <?php }
                elseif($action =="permission" && $_GET['id']!="" ){
                    if($action =="edit" && $_GET['id']!="" ){
                        $role = $db->getRow("select * from tbl_roll where id=$_GET[id] ");
                    }
                    
                    $allmodules = $db->getRows("select * from modules");
                ?> 

                <h2 class="top-heading"><?php echo $page; ?> Details</h2>
                
                <h3 class="text-center text-success border-bottom mb-4"><?php echo $db->getVal("select school_name from schools where id='".$_GET['school_id']."' "); ?></h3>

                <form method="post" >
                    
                    <table class="table table-sm table-striped">
                        <thead>
                            <tr>
                                <th>S.No.</th>
                                <th>Module</th>
                                <th>All</th>
                                <th>No Access</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($allmodules as $module) { $i++;
                            $is_check = $db->getRow("select * from tbl_privilages where school_id='".$_GET["school_id"]."' and page_name='".$module['name']."' and roll_id='".$_GET["id"]."'");
                            ?>
                            <input type="hidden" name="name[]" value="<?php echo $module['name']; ?>" />
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo $module['name']; ?></td>
                                    <td>
                                        <input type="radio" name="permission-<?php echo $module['name']; ?>" value="1" <?php if($is_check['a']==1)echo "checked"; ?> />
                                    </td>
                                    <td>
                                        <input type="radio" name="permission-<?php echo $module['name']; ?>" value="0" <?php if($is_check['n']==1)echo "checked"; ?> />
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>

                <div class="text-center">
                    <input type="submit" name="submit" value="Submit" />
                </div>

                </form>
                <?php }
                    else {
                        $allroles = $db->getRows("select * from tbl_roll");
                        ?>

                        <h2 class="top-heading"><?php echo $page; ?> List
                            <a href="<?php echo $page ?>.php?action=add" class="btn btn-success">Add <?php echo $page; ?></a>
                        </h2>

                        <div style="width:300px;">
                            <select name="school_id" onchange="getRoleList(this.value)" required>
                                <option value="">--Select School--</option>
                                <?php $allschools = $db->getRows("select * from schools");
                                    foreach ($allschools as $school) { ?>
                                        <option value="<?php echo $school['id']; ?>" <?php if($user['school_id']==$school['id']) echo "selected"; ?>><?php echo $school['school_name']; ?></option>
                                        <?php
                                    }
                                    ?>
                            </select>
                        </div>
                        <hr>

                        <table class="table table-striped table-sm">
                            <thead class="table-success">
                            <tr>
                                <th>S.No.</th>
                                <th>Role</th>
                                <th>School</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                            <tbody id="roletable">
                                <?php foreach($allroles as $role) { $i++; ?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo $role['name']; ?></td>
                                    <td><?php echo $db->getVal("select school_name from schools where id='".$role['school_id']."' "); ?></td>
                                    <td><?php echo $role['status']; ?></td>
                                    <td>
                                        <a href="<?php echo $page; ?>.php?action=edit&id=<?php echo $role['id']; ?>">
                                            <button class="btn btn-success edit-btn">Edit</button>
                                        </a>
                                        <button onclick="delete_role(<?php echo $role['id']; ?>)" class="btn btn-danger delete-btn">Delete</button>
                                        <a href="<?php echo $page; ?>.php?action=permission&id=<?php echo $role['id']; ?>&school_id=<?php echo $role['school_id']; ?>">
                                            <button class="btn btn-secondary" style="font-weight:600;color:white;">Permission</button>
                                        </a>
                                    </td>
                                </tr>
                                
                                <?php } ?>

                            </tbody>
                        </table>
                        
                <?php
                    }
                
                ?>

            </div>

        </div>

    </div>

    </div>
    <?php include 'footer.php'; ?>

    <script>
        function delete_role(id){
            if (confirm("Are you sure want to delete this role?") == true) {
                $.post( "ajax.php", { type: "delete_role", id: id  } , function( data ){			
                    alert(data);
                    location.reload();
                });
            }
        }
        
        function getRoleList(val){
            
            $.post( "ajax.php", { type: "get_roles", school_id: val  } , function( data ){
                $("#roletable").html(data);
            });
        
        }
        
    </script>

</body>

</html>