<?php

use LDAP\Result;

    include_once('include/connection.php');

    session_start();
    $uID=$_SESSION['uID'];
    if(empty($uID))
	{
		header("location: index.php");
	}
    
    $query = "SELECT * FROM client_tbl";
    $result = $link->query($query);

    $query1 = "SELECT * FROM device_tbl where status='available'";
    $result1 = $link->query($query1);

    $query2 = "SELECT * FROM simcard_tbl where store='available'";
    $result2 = $link->query($query2);
     
?>

<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Internal Data|Fleet record</title>

        <!-- Bootstrap CSS -->
        <link href="library/bootstrap-5/bootstrap.min.css" rel="stylesheet" />
        <script src="library/bootstrap-5/bootstrap.bundle.min.js"></script>
        <script src="library/dselect.js"></script>
        <link type="text/css" href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
        
    </head>
    <body>

        <div class="container-fluID">
        <center>
                	<div class="alert alert-success" role="alert">
                    	<h2><span class="badge badge-secondary">CREATE Fleet </span></h2>
                        	DASHBOARD
                	</div>
            	</center>  

                <div class="form-group">
        
			<form method="POST" action="">
			   
            <a href="fleet_view.php" style="margin: 24.5%;"> <button class="btn btn-info " type="button">VIEW FLEET</button></a>
            <br>
            <br>
            <div class="row">
                <div class="col-md-3">&nbsp;</div>
                <div class="col-md-6">
                    <tr><td><label for="client" class="form-label">Client </label></td></tr>
                    <tr><td><select name="select_box" class="form-select" id="select_box">
                        <option value="">Select client</option>
                        <?php 
                        foreach($result as $row)
                        {
                            echo '<option value="'.$row["clientID"].'">'.$row["clientName"].'</option>';
                        }
                        ?>  
                    </select></td></tr>
                </div>
            </div>      
            <div class="row">
                <div class="col-md-3">&nbsp;</div>
                <div class="col-md-6">
                <tr></td><label for="device" class="form-label">DeviceID </label></tr>
                <tr></td><select name="select_box1" class="form-select" id="select_box1">
                    <option value="">Select device</option>
                        <?php 
                        foreach($result1 as $row1)
                        {
                            echo '<option value="'.$row1["dID"].'">'.$row1["dID"].'</option>';
                        }
                        ?>  
                    </select></td></tr>
                </div>
            </div>
            <!--<div class="row">-->
            <!--    <div class="col-md-3">&nbsp;</div>-->
            <!--    <div class="col-md-6">-->
            <!--    <label for="devicename" class="form-label">Device name </label>-->
            <!--        <input type="dname" class="form-control" name="dname" value="" />-->
            <!--    </div>-->
            <!--</div>  -->
            <div class="row">
                <div class="col-md-3">&nbsp;</div>
                <div class="col-md-6">
                <tr></td><label for="simcard" class="form-label">Simcard </label></tr>
                <tr></td><select name="select_box2" class="form-select" id="select_box2">
                    <option value="">Select simcard</option>
                        <?php 
                        foreach($result2 as $row2)
                        {
                            echo '<option value="'.$row2["simID"].'">'.$row2["simID"].'</option>';
                        }
                        ?>  
                    </select></td></tr>
                </div>
            </div>  
            <div class="row">
                <div class="col-md-3">&nbsp;</div>
                <div class="col-md-6">
                <label for="email" class="form-label">plateNo </label>
                    <input type="plateNo" class="form-control"  name="plateNo" value="" />
                </div>
            </div>  
            <div class="row">
                <div class="col-md-3">&nbsp;</div>
                <div class="col-md-6">
                <label for="email" class="form-label">Cost </label>
                    <input type="integer" class="form-control" name="cost" value="" />
                </div>
            </div>  
            <div class="row">
                <div class="col-md-3">&nbsp;</div>
                <div class="col-md-6">
                <label for="email" class="form-label">InstallDate </label>
                    <input type="date" class="form-control" name="installDate" required value=""/>
                </div>
            </div>  
            <div class="row">
                <div class="col-md-3">&nbsp;</div>
                <div class="col-md-6">
					<input type="submit" class="btn btn-primary" name="submit" value="add" />
                </div>
            </div> 
        
            </div>
         
            </form>

			</div>
            <?php

            if(isset($_POST['submit'])){
            
            $clientID=$_POST['select_box'];
            $dID=$_POST['select_box1'];
            // $dname=$_POST['dname'];
            $simID=$_POST['select_box2'];
            $plateNo=$_POST['plateNo'];
            $cost=$_POST['cost'];
            $installDate=$_POST['installDate'];

            if($clientID!='' && $dID!='' && $simID!='' && $installDate!=''){
                $query="INSERT INTO fleet_tbl (clientID,dID,simID,plateNo,cost,installDate) values ('$clientID','$dID','$simID','$plateNo','$cost','$installDate') ";
                $result=mysqli_query($link,$query);
            }
            if($result){
                $query3="UPDATE simcard_tbl SET store='used' where simID='$simID'";
                $result3=mysqli_query($link,$query3);
            }
            if($result3){
                $query4="UPDATE device_tbl SET status='used' where dID='$dID'";
                $result4=mysqli_query($link,$query4);
            
            }
            
            }

            ?>

        </div>
    </body>
</html>

<script>

    var select_box_element = document.querySelector('#select_box');

    dselect(select_box_element, {
        search: true
    });

</script>
<script>

    var select_box_element1 = document.querySelector('#select_box1');

    dselect(select_box_element1, {
        search: true
    });

</script>
<script>

    var select_box_element2 = document.querySelector('#select_box2');

    dselect(select_box_element2, {
        search: true
    });

</script>
