<?php
include('../include/header.php');
?>

 <!-- 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">create user</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" method="post">
									  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">User Type <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <select type="text" class="form-control" id="type" name="type" required>
												<option>Select</option>
												<?php
												 $sql2 = "select *from tbl_user_type where id!='1' and id!='2'";
						$result2 = $objhr->viewdata($sql2);
							
							while($rows2 = $result2->fetch_assoc()){
												?>
												<option value="<?php echo $rows2['id']; ?>"><?php echo strtoupper($rows2['user_type']); ?></option>
												<?php } ?>
												</select>
                                            </div>
                                        </div>
										
										 <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Department<span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                               <select type="text" class="form-control" id="dept" name="dept" required>
												<option>Select</option>
												<?php
												 $sql2 = "select *from tbl_department";
						$result2 = $objhr->viewdata($sql2);
							
							while($rows2 = $result2->fetch_assoc()){
												?>
												<option value="<?php echo $rows2['id']; ?>"><?php echo strtoupper($rows2['department']); ?></option>
												<?php } ?>
												</select>
                                            </div>
                                        </div>
										
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="name" name="name" required>
                                            </div>
                                        </div>
										
										  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Phone <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="phone" name="phone" required>
                                            </div>
                                        </div>
										
										  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Address <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="address" name="address" required>
                                            </div>
                                        </div>
										
										  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">User Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="u_name" name="u_name" onblur="chk()" required>
                                            </div>
                                        </div>
										
										  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Password <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="password" name="password" required>
                                            </div>
                                        </div>
                                        
                                        <div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                                <button type="submit" class="btn btn-primary" name="save">Submit</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
			
<?php
if(isset($_POST['save'])){
$ipaddress = $_SERVER['REMOTE_ADDR'];

 $sql = "INSERT INTO `tbl_user_mstr` (`user_type_id`, `dept_id`, `name`, `phone`, `address`, `user_name`, `password`,`ip_address`, `user_id`, `active_status`) VALUES ('".$_POST['type']."', '".$_POST['dept']."', '".$_POST['name']."', '".$_POST['phone']."', '".$_POST['address']."', '".$_POST['u_name']."', '".$_POST['password']."', '$ipaddress', '$user_id', '1');";
						$result = $objhr->viewdata($sql);
						echo "<script>alert('added');</script>";
}
?>
<?php
include('../include/footer.php');
?>
<script>
function chk(){
var u_name = $('#u_name').val();
$.ajax({
		type: "POST",
		url: "chk_user_name.php",
		data: "user="+u_name,
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var res = data[0];
				
				if(res==1){
				alert('User Name Already Exist.');
				$('#u_name').val('');
				$('#u_name').focus();
				}
				
				
				
			}
			});
   

}
</script>