<?php require_once('header.php'); ?>
<?php if($allPermissions['userManagement']['create']=="1"){ ?>
<?php
if(isset($_POST['form1'])) {
   // var_dump($_POST);
	$valid = 1;

	if(empty($_POST['full_name'])) {
		$valid = 0;
		$error_message .= 'Name can not be empty<br>';
	}

	if(empty($_POST['email'])) {
		$valid = 0;
		$error_message .= 'email can not be empty<br>';
	}
	
		if(empty($_POST['phone'])) {
		$valid = 0;
		$error_message .= 'Phone No can not be empty<br>';
	}

		if(empty($_POST['status'])) {
		$valid = 0;
		$error_message .= 'Status Code can not be empty<br>';
	}
		if(empty($_POST['password'])) {
		$valid = 0;
		$error_message .= 'Password can not be empty<br>';
	}


    if($valid==1)
    {
    
		$statement = $pdo->prepare("UPDATE tbl_user SET full_name=?,email=?,gst_no=?,phone=?,address=?,location=?,status=?,password=?,city=?,state=?,pincode=?,role=?,role_id=?,vehicle_tracking_branch_id=? WHERE id=?");
		$statement->execute(array($_POST['full_name'],$_POST['email'],'',$_POST['phone'],'','',$_POST['status'],$_POST['password'],'','','','Admin',$_POST['role_id'],$_REQUEST['vehicle_tracking_branch_id'],$_REQUEST['id']));
			
		$success_message = 'Employee is added successfully!';

		unset($_POST['full_name']);
		unset($_POST['email']);
		unset($_POST['gst_no']);
		unset($_POST['phone']);
		unset($_POST['address']);
		unset($_POST['status']);
		unset($_POST['location']);
		unset($_POST['password']);
	    unset($_POST['city']);
		unset($_POST['state']);
		unset($_POST['pincode']);
    }
	
}
?>

<section class="content-header">
	<div class="content-header-left">
		<h1>Add Users</h1>
	</div>
	<div class="content-header-right">
		<a href="manageEditors.php" class="btn btn-primary btn-sm">View All</a>
	</div>
</section>


<section class="content">

	<div class="row">
		<div class="col-md-12">

			<?php if($error_message): ?>
			<div class="callout callout-danger">
				<p>
					<?php echo $error_message; ?>
				</p>
			</div>
			<?php endif; ?>

			<?php if($success_message): ?>
			<div class="callout callout-success">
				<p><?php echo $success_message; ?></p>
			</div>
			<?php endif; ?>
			
<?php
$statement = $pdo->prepare("SELECT * FROM tbl_user where id=?");
							$statement->execute(array($_REQUEST['id']));
							$results = $statement->fetchAll(PDO::FETCH_ASSOC);
                            foreach($results as $res)
                            { ?>
			
			<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
				<div class="box box-info">
					<div class="box-body">
					    	<div class="form-group">
							<label for="" class="col-sm-2 control-label">Vehicle Tracking Branch <span>*</span></label>
							<div class="col-sm-6">
								<select name="vehicle_tracking_branch_id" id="vehicle_tracking_branch_id" class="form-control">
															<?php 
							       $statement = $pdo->prepare("SELECT * FROM `tbl_barcode` where status=1");
        							$statement->execute();
        							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
            						 foreach($result as $rows) { ?>
																<option value="<?php echo $rows['id']; ?>" <?php  if($rows['id']==$res['vehicle_tracking_branch_id']){ echo "selected"; } ?>>
																	<?php echo $rows['location']; ?>
																</option>
																<?php } ?>
														</select>
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Employee Name <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="full_name" value="<?php if(isset($res['full_name'])){echo $res['full_name'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Email <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="email" value="<?php if(isset($res['email'])){echo $res['email'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Phone <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="phone" value="<?php if(isset($res['phone'])){echo $res['phone'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Role <span>*</span></label>
							<div class="col-sm-6">
								<select name="role_id" id="role_id" class="form-control">
															<?php 
							       $statement = $pdo->prepare("SELECT * FROM `tbl_role` WHERE id!=1 ORDER BY id ");
        							$statement->execute();
        							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
            						 foreach($result as $rows) { ?>
																<option value="<?php echo $rows['id']; ?>" <?php  if($rows['id']==$res['role_id']){ echo "selected"; } ?>>
																	<?php echo $rows['role_name']; ?>
																</option>
																<?php } ?>
														</select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Is Active? <span>*</span></label>
							<div class="col-sm-6">
								<select class="form-control" name="status">
								    <option value="Active" <?php  if($res['status']=='Active'){ echo "selected"; } ?>>Active</option>
								    <option value="Inactive" <?php  if($res['status']=='Inactive'){ echo "selected"; } ?>>Inactive</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Password <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="password" value="<?php if(isset($res['password'])){echo $res['password'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label"></label>
							<div class="col-sm-6">
								<button type="submit" class="btn btn-success pull-left" name="form1">Submit</button>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
<?php } ?>
</section>
<?php }else{ ?>
<center><h4 style="color:red;">You don't have permission to add user </h4></center>
<?php } ?>

<?php require_once('footer.php'); ?>