<?php
	include("includes/dbconfig.php");
	$pagetitle = "Customers";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	if(isset($_GET['id'])){
		$customer_id = $_GET['id'];
	}else{
		$customer_id = NULL;
	}
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>Customers</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<?php
						if($_SESSION["connectedusergroup"] == 3 || $_SESSION["connectedusergroup"] == 4 || $_SESSION["connectedusergroup"] == 1)
						{
					?>
					<div class="row">
						<div class="col-md-12">
							<div id="servererrormessage">
							</div>
						</div>
					</div>

					<div class="row">
						<div class="col-md-12">
							<div style="">
                                <?php
                                if(isset($_POST['customer_name'])){
									$customer_id = trim($_POST['customer_id']);
                                    $customer_name = trim($_POST['customer_name']);
                                    $customer_phone = trim($_POST['customer_phone']);
                                    $customer_email = trim($_POST['customer_email']);
                                    $customer_claim = trim($_POST['customer_claim']);

                                    DB::$error_handler = false;
		                            DB::$throw_exception_on_error = true;
		
                                    try
                                    {
                                       
                                        $trxnConnextn = new MeekroDB();
                                        $trxnConnextn->startTransaction();

										$trxnConnextn->query("UPDATE customers SET name='{$customer_name}', phone='{$customer_phone}', email='{$customer_email}', claim='{$customer_claim}' where id = '{$customer_id}' ");

                                        $trxnConnextn->commit();
                                    }
                                    catch(MeekroDBException $e)
		                            {
                                        $err = $err.$e->getMessage()."<br>";
                                        $err = $err.$e->getQuery()."<br>";
                                        $trxnConnextn->rollback();
                                        $errormessage = "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>" . $err . "</div>";
                                        $server_response[] = array('issaved' => 0, 'message' => $errormessage);
                                        DB::$error_handler = 'meekrodb_error_handler';
                                        DB::$throw_exception_on_error = false;
                                        echo json_encode($server_response);
                                        exit();
                                    }
                                }
                                    
                                ?>
							</div>
						</div>
					</div>
				
					<div class="row">
						<div class="col-md-12">
							<div class="card card-primary" id="customerdetails" style="height: 500px;">
								<div class="card-body">
									<?php
														$customers = DB::query("SELECT id, name, phone, email, claim FROM customers where id = '{$customer_id}'");
														
														foreach ($customers as $customer)
														{
													?>
														<?php 
															$name = $customer['name']; 
															$phone = $customer['phone']; 
															$email = $customer['email']; 
															$claim = $customer['claim']; 
														?>
													<?php
														}
													?>
													
									
									<div>
										<div id="cartitemslist">
										<form method="post" action="customerinfo.php">
											<table class="table table-hover table-bordered "  id="">
												
												<tbody>
													
													<tr>
														<td><b>ID</b></td><td><input type="hidden" name="customer_id" value="<?php echo $customer_id; ?>" /><?php echo $customer_id; ?></td>
													</tr>
													<tr>
														<td><b>Name</b></td><td><input type="text" name="customer_name" value="<?php echo $name; ?>" /></td>
													</tr>
													<tr>
														<td><b>Phone</b></td><td><input type="text" name="customer_phone" value="<?php echo $phone; ?>" /></td>
													</tr>
													<tr>
														<td><b>Email</b></td><td><input type="text" name="customer_email" value="<?php echo $email; ?>" /></td>
													</tr>
													<tr>
														<td><b>Claim (%)</b></td><td><input type="text" name="customer_claim" value="<?php echo $claim; ?>" /></td>
													</tr>
													<tr>
														<td></td><td><input type="submit" name="submit" value="Update" /></td>
													</tr>
												</tbody>
											</table>
										</form>
										</div>
									</div>
								</div><!-- /.card-body -->
							</div><!-- /.card -->
						</div>
						
						
					</div>
					<?php
						}
						else
						{
					?>
					<div class="alert alert-danger alert-dismissable">
						<i class="fa fa-ban-circle"></i>
						Limited Access
					</div>
					<?php
						}
					?>
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			 
		</div><!-- ./wrapper -->
		
		
		<?php include ('includes/general_footer.php')?> 
		
		<script src="plugins/bert/jquery.price_format.min.js" type="text/javascript"></script>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/cashsalesregister.js" type="text/javascript"></script>
	</body>
</html>