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


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

if($_POST['submit']){
    
    $pass = new password();
	$_POST["password"]=$pass->password($_POST["password"]);

    $school_name = $db->getVal("select school_name from schools where id='$_POST[school_id]' ");
    $saveAry = array(
        "school_id"     =>  $_POST["school_id"],
        "userid"        =>  $_POST["userid"],
        "password"      =>  $_POST["password"],
        "name"          =>  $_POST["name"],
        "mobile"        =>  $_POST["mobile"],
        "email"         =>  $_POST["email"],
        "roll"          =>  $_POST["role"],
        "status"        =>  $_POST["status"],
        "added_by"      =>  $_SESSION["superadmin"]["uid"],
    );

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

    if($_GET['action'] == "edit" && $_GET['id']!=''){
        $flg = $db->updateAry("users", $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']!="" ){
                        $user = $db->getRow("select * from users 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" 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>

                <div class="form-group">
                    <label>Assign Role<span class="mandatory">*</span></label>
                    <select name="role" id="role_list">
                        <option value="">--Select Role--</option>
                        <?php  $allroles = $db->getRows("select * from tbl_roll");
                            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>
                </div>

                <div class="form-group">
                    <label>User Id <span class="mandatory">*</span></label>
                    <input type="text" name="userid" value="<?php 
                    if($action === "add"){
                        $userid = $db->getVal("select userid from users order by cast(userid as signed) DESC limit 1 ")+1;
                        echo trim($userid);
                    } else {
                    echo $user['userid']; } ?>" required readonly />
                </div>

                <div class="form-group">
                    <label>Password <span class="mandatory">*</span></label>
                    <input type="text" name="password" value="<?php
                        $pass = new password();
    					$salt = "CrazyassLongSALTThatMakesYourUsersPasswordVeryLong123!!312567__asdSdas";
    					$pwd = $pass->encrypt_decrypt("decrypt", $user["password"] , $salt, "a1b2c3a1b2c3a1b2");
                    echo $pwd; ?>" required />
                </div>

                <div class="form-group">
                    <label>Name</label>
                    <input type="text" name="name" value="<?php echo $user['name']; ?>" />
                </div>
                
                <div class="form-group">
                    <label>Mobile No.</label>
                    <input type="text" name="mobile" value="<?php echo $user['mobile']; ?>" />
                </div>

                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" value="<?php echo $user['email']; ?>" />
                </div>

                <div class="form-group">
                    <label style="margin-top:15px;float:left;">Address</label>
                    <textarea name="address"><?php echo $user['address']; ?></textarea>
                </div>

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


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

                        <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="getUserList(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>Name</th>
                                    <th>User Id</th>
                                    <th>Password</th>
                                    <th>Mobile</th>
                                    <th>Email</th>
                                    <th>Role</th>
                                    <th>School Name</th>                                
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody id="usertable">
                                <?php 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 name from tbl_roll where id='$user[roll]' "); ?></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 } ?>

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

            </div>

        </div>

    </div>

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

    <script>
        function delete_user(id){
            if (confirm("Are you sure want to delete this school?") == true) {
                $.post( "ajax.php", { type: "delete_user", id: id  } , function( data ){
                    // alert(data);
                    location.reload();
                });
            }
        }
        
        
        function getRoleList(id){
            $.post( "ajax.php", { type: "get_role_list", school_id: id  } , function( data ){
                $("#role_list").html(data);
                // alert(data);
                // location.reload();
            });
        }
        
        function getUserList(val){
            
            $.post( "ajax.php", { type: "get_users", school_id: val  } , function( data ){
                $("#usertable").html(data);
            });
        
        }
    </script>

</body>

</html>