<?php 
include '../db.php';
$type = $_POST['type'];

if($type === "delete_module"){
    $id = $_POST['id'];
    $flgUp=$db->delete("modules","where id='$id' ");
    if($flgUp){
        echo "Deleted Successfully";
    } else {
        echo "Unable to delete. Please try again.";
    }
}

if($type === "delete_school"){
    $id = $_POST['id'];
    $flgUp=$db->delete("schools","where id='$id' ");
    $flg = $db->delete("users", " where school_id='$id' ");
    if($flgUp){
        echo "Deleted Successfully";
    } else {
        echo "Unable to delete. Please try again.";
    }

}

if($type === "delete_role"){
    $id = $_POST['id'];
    $flgUp=$db->delete("tbl_roll","where id='$id' ");
    if($flgUp){
        echo "Deleted Successfully";
    } else {
        echo "Unable to delete. Please try again.";
    }

}

if($type === "delete_user"){
    $id = $_POST['id'];
    $flgUp=$db->delete("users","where id='$id' ");
    if($flgUp){
        echo "Deleted Successfully";
    } else {
        echo "Unable to delete. Please try again.";
    }

}

if($type === "get_role_list"){
    ?>
    <select name="role" id="role_list">
            <option value="">--Select Role--</option>
            <?php $allroles = $db->getRows("select * from tbl_roll where school_id='".$_POST["school_id"]."' ");
                foreach ($allroles as $role) { ?>
                    <option value="<?php echo $role['id']; ?>"  <?php if($user['roll']==$role['id']) echo "selected"; ?>><?php echo $role['name']; ?></option>
                    <?php
                }
                ?>
        </select>
    <?php
}

if($type === "get_roles"){
    $school_id = $_POST['school_id'];
    
    $whereschool = "";
    if($school_id!=""){
        $whereschool = " where school_id='$school_id' ";
    }
    
    $allroles = $db->getRows("select * from tbl_roll $whereschool ");
    
    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 }
}


if($type === "get_users"){
    $school_id = $_POST['school_id'];
    
    $whereschool = "";
    if($school_id!=""){
        $whereschool = " where school_id='$school_id' ";
    }
    
    $allusers = $db->getRows("select * from users $whereschool");
    
    foreach($allusers as $user) { $i++; ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo $user['name']; ?></td>
            <td><?php echo $user['userid']; ?></td>
            <td><?php
            $pass = new password();
			$salt = "CrazyassLongSALTThatMakesYourUsersPasswordVeryLong123!!312567__asdSdas";
			$pwd = $pass->encrypt_decrypt("decrypt", $user["password"] , $salt, "a1b2c3a1b2c3a1b2"); 
            echo $pwd; ?></td>
            <td><?php echo $user['mobile']; ?></td>
            <td><?php echo $user['email']; ?></td>
            <td><?php echo $db->getVal("select school_name from schools where id='$user[school_id]' "); ?></td>
            <td>
                <a href="<?php echo $page; ?>.php?action=edit&id=<?php echo $user['id']; ?>">
                    <button class="btn btn-success edit-btn">Edit</button>
                </a>
                <button onclick="delete_user(<?php echo $user['id']; ?>)" class="btn btn-danger delete-btn">Delete</button>
            </td>
        </tr>
        
    <?php }
}

if($type === "delete_device"){
    $id = $_POST['id'];
    $flgUp=$db->delete("biometric_device","where id='$id' ");
    if($flgUp){
        echo "Deleted Successfully";
    } else {
        echo "Unable to delete. Please try again.";
    }

}

?>