
<?php
    session_start();
    include("include/Functions.php");   
    $db = new Functions();  
    $stations = $db->fetchAllStationsByCompanyId($_SESSION['companyfk'])['stations'];
    $companyfk = $_SESSION['companyfk'];
    if(empty($_SESSION['userid']))
        header('Location: index.php');

        if(!empty($_POST["companyfk"]) && !empty($_POST["username"]) && !empty($_POST["firstname"]) && !empty($_POST["lastname"]) && isset($_POST["station"]) && isset($_POST["password"])){

            $companyfk = $_POST["companyfk"];
            $username =  $_POST["username"];  
            $firstname = $_POST["firstname"];
            $lastname = $_POST["lastname"];
            $station = $_POST["station"];
            $password = $_POST["password"];


            $db->registerUser($companyfk, $username, $firstname, $lastname, $station, $password);
            $companyfk = ""; 
            $username = "";  
            $firstname = ""; 
            $lastname = ""; 
            $station = "";
            $password = "";

    }   





?>
<!DOCTYPE html>
<html lang="en">
<body class="fix-header fix-sidebar">
    <!-- Preloader - style you can find in spinners.css -->
    <div class="preloader">
        <svg class="circular" viewBox="25 25 50 50">
			<circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" /> </svg>
    </div>
    <!-- Main wrapper  -->
    <div id="main-wrapper">
        <?php
        include("include/head.html");   
        ?>
        <!-- End Left Sidebar  -->
        <!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                    <h3 class="text-primary">Dashboard</h3> </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Dashboard</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row justify-content-center">
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="form-validation">
                                    <form class="form-valide" action="" method="post">
                                    <input type="hidden" value="<?php echo  $_SESSION['companyfk'];?>" class="form-control" id="companyfk" name="companyfk">   
                                    <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Username <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-username" name="username" placeholder="Enter a username.."required>
                                            </div>
                                        </div>

                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">First Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-firstname" name="fname" placeholder="Enter first name.." required>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Last Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="val-lastname" name="lname" placeholder="Enter last name.." required>
                                            </div>
                                        </div>

                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-skill">Station <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <select class="form-control" id="val-station" name="station">
            
                                                       <?php foreach ($stations as $station) {?>
                                                         <option value="<?php echo  $station['stationid']; ?>"> <?php echo  $station['name']; ?></option>
                                                       <?php } ?>
                                                </select>
                                            </div>
                                        </div>

                        
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-password">Password <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="password" class="form-control" id="val-password" name="val-password" placeholder="Choose a safe one.." required>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-confirm-password">Confirm Password <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="password" class="form-control" id="val-confirm-password" name="val-confirm-password" placeholder="..and confirm it!" required>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                                <button type="submit" onclick="proccesForm()" class="btn btn-primary">Submit</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
            <!-- footer -->
            <footer class="footer"> © 2020 All rights reserved. Template designed by <a href="https://softcom">Meit Systems</a></footer>
            <!-- End footer -->
        </div>
        <!-- End Page wrapper  -->
    </div>
    <!-- End Wrapper -->

    <script>

function proccesForm(){
 var companyfk =document.getElementById("companyfk").value;
 var username =document.getElementById("val-username").value;
 var firstname =document.getElementById("val-firstname").value;
 var lastname =document.getElementById("val-lastname").value;
 var station  =document.getElementById("val-station").value;
 var password =document.getElementById("val-password").value;
 //alert(username+"|"+firstname+"|"+lastname+"|"+station+"|"+password+"|"+companyfk);
    $.ajax({
        type: "POST",
        url: "addusers.php",
        data: {companyfk:companyfk,username:username,firstname:firstname,lastname:lastname,station:station,password:password},
        complete: function(data){
           // alert("gone");
            //data contains the response from the php file.
            //u can pass it here to the javascript function
        }
});

   }
    </script>


    <!-- All Jquery -->
    <script src="js/lib/jquery/jquery.min.js"></script>
    <!-- Bootstrap tether Core JavaScript -->
    <script src="js/lib/bootstrap/js/popper.min.js"></script>
    <script src="js/lib/bootstrap/js/bootstrap.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="js/jquery.slimscroll.js"></script>
    <!--Menu sidebar -->
    <script src="js/sidebarmenu.js"></script>
    <!--stickey kit -->
    <script src="js/lib/sticky-kit-master/dist/sticky-kit.min.js"></script>


    <!-- Form validation -->
    <script src="js/lib/form-validation/jquery.validate.min.js"></script>
    <script src="js/lib/form-validation/jquery.validate-init.js"></script>
    <!--Custom JavaScript -->
    <script src="js/custom.min.js"></script>

</body>

</html>