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


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

if($_POST){
    // print_r($_POST);
    // die();

    if($action == "add"){
        $flg = $db->insertAry("biometric_device", $_POST);
        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("biometric_device", $_POST, "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']!="" ){
                        $device = $db->getRow("select * from biometric_device where id=$_GET[id] ");
                    }
                ?>

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

                <form method="post" >

                <div class="form-group">
                    <label>Device Id</label>
                    <input type="text" name="device_id" value="<?php echo $device['device_id']; ?>" />
                </div>
                
                <div class="form-group">
                    <label>Device Serial No.</label>
                    <input type="text" name="serial_no" value="<?php echo $device['serial_no']; ?>" />
                </div>
                
                <div class="form-group">
                    <label>Device Name</label>
                    <input type="text" name="device_name" value="<?php echo $device['device_name']; ?>" />
                </div>
                
                <div class="form-group"> 
                    <label>School URL</label>
                    <input type="text" name="school_url" value="<?php echo $device['school_url']; ?>" placeholder="https://avp.savarnacloud.com" />
                </div>
                
                <div class="form-group">
                    <label>School Name</label>
                    <input type="text" name="school_name" value="<?php echo $device['school_name']; ?>" />
                </div>

                <div class="form-group">
                    <label>Out Duration</label>
                    <input type="text" name="duration" value="<?php echo $device['duration']; ?>" />
                </div>

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


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

                        <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>

                        <table class="table table-striped table-sm">
                            <thead class="table-success">
                                <tr>
                                    <th>S.No.</th>
                                    <th>Devide Id</th>
                                    <th>Serial No.</th>
                                    <th>Device Name</th>
                                    <th>School URL</th>
                                    <th>School Name</th>
                                    <th>Out Duration</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody id="usertable">
                                <?php foreach($alldevice as $device) { $i++; ?>
                                <tr> 
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo $device['device_id']; ?></td>
                                    <td><?php echo $device['serial_no']; ?></td>
                                    <td><?php echo $device['device_name']; ?></td>
                                    <td><?php echo $device['school_url']; ?></td>
                                    <td><?php echo $device['school_name']; ?></td>
                                    <td><?php echo $device['duration']; ?></td>
                                    <td>
                                        <a href="<?php echo $page; ?>.php?action=edit&id=<?php echo $device['id']; ?>">
                                            <button class="btn btn-success edit-btn">Edit</button>
                                        </a>
                                        <button onclick="delete_device(<?php echo $device['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_device(id){
            if (confirm("Are you sure want to delete this device?") == true) {
                $.post( "ajax.php", { type: "delete_device", 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>