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

$page = "role";
$action = $_GET['action'];

if($_POST['submit']){
    $saveAry = array(
        "role_name"     =>  $_POST["role_name"],
        "status"        =>  $_POST["status"],
        "added_by"      =>  $_SESSION["admin"]["uid"],
    );

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

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

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

                <form method="post" >
                    
                <div class="form-group">
                    <label>Role Name <span class="mandatory">*</span></label>
                    <input type="text" name="role_name" value="<?php echo $role['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 }
                    else {
                        $allroles = $db->getRows("select * from role");
                        ?>

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

                        <table class="table table-striped table-sm">
                            <thead class="table-success">
                            <tr>
                                <th>S.No.</th>
                                <th>Role</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>
                                <?php foreach($allroles as $role) { $i++; ?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo $role['role_name']; ?></td>
                                    <td><?php echo $role['status']; ?></td>
                                    <td>
                                        <a href="<?php echo $page; ?>?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>
                                    </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();
                });
            }
        }
    </script>

</body>

</html>