<?php
	include("includes/dbconfig.php");
	$pagetitle = "Customers";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>Search for Customer</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">
							<span style="padding: 10px; margin: 3px; background-color: #3C8DBC;" >
							<a href="createcustomer.php" style="color: #fff;"><i class="fa fa-plus"></i> New Customer</a>
							</span>
						</div>
					</div>
					<div>&nbsp;</div>
					
					<div class="card card-info">
              <div class="card-header">
                <h3 class="card-title">Search for customer</h3>
              </div>
              <div class="card-body">
			  <form action="searchforcustomer.php" method="post">
                
                <div class="row">
				  <div class="col-4">
                   <input type="text" name="customer_search" placeholder="Customer number or name" class="form-control" style="" />
						
                  </div>
                  <div class="col-4">
                    <input type="submit" value="Search" class="form-control btn-info" style="" />
				
                  </div>
				 
                   
                </div>
				 </form>
				 
				 <div class="row">
					 
					 
					 
							<div class="card card-primary  col-md-12" id="customerdetails" style="">
								 	 
											
												<div class="card-body  " id="cartitemslist">	
								 
								
								<table id="example1"   class="table  table-bordered table-striped  ">
												<thead>
													<tr>
														<th ></th>
														<th>Customer Name</th>
														<th class="text-right">Phone</th>
														<th class="text-right">Email</th>
														<th class="text-right">Claim (%)</th>
														<th class="text-right">Points</th>
														<th class="text-right">Work shop</th>
														<th class="text-right">Credid info</th>
														<th class="text-right">Shoping history</th>
														<th class="text-right">Action</th>
													</tr>
												</thead>
												<tbody>
													
													
													<?php
													if(isset($_POST['customer_search'])){
														$customer_search = $_POST['customer_search'];
														$customers = DB::query("SELECT id, name, phone, email, claim FROM customers where name like '%{$customer_search}%'");
														
														foreach ($customers as $customer)
														{
															
															//get total points remaining
															//get points of customer
															 
															 
															 $items = DB::query("SELECT id, customer_name, value_date, bill, claim FROM cash_receipt where customer_id = '".$customer['id']."'");
																				
															$total_bills = 0;
															$total_claims = 0;
															
															foreach ($items as $item)
															{
																$total_claims += ($item['claim']/100)*($item['bill']);
															}

															
															
															//get previous claims
															$claims = DB::query("SELECT id, customer_id, claim from customer_claims where customer_id = '".$customer['id']."'");
															$previous_claims = 0;
															
															foreach ($claims as $claim)
															{
																$previous_claims +=   $claim['claim'];
																 
															}
															//echo '<tr> <td>'.$customer['id'].' </td><td> Claimed '.$previous_claims.' </td><td> Redeemed'.$total_claims.' </td></tr>';
															
															$remaining_points = $total_claims - $previous_claims;
													?>
													<tr>
														<td ><?php echo $customer['id']; ?></td>
														<td><a href="customerinfo.php?id=<?php echo $customer['id']; ?>"><?php echo $customer['name']; ?></a></td>
														<td class="text-right"><?php echo $customer['phone']; ?></td>
														<td class="text-right"><?php echo $customer['email']; ?></td>
														<td class="text-right"><?php echo $customer['claim']; ?></td>
														<td class="text-right"><?php echo $remaining_points; ?></td>
														<td class="text-right"><a href="<?php echo $base_url; ?>workshopcushistory/<?php echo $customer['id']; ?>"> History</a></td>

														<td class="text-right"><a href="credit_info.php?id=<?php echo $customer['id']; ?>"> Credit Info</a></td>
														
														<td class="text-right"><a href="shoping_history.php?id=<?php echo $customer['id']; ?>"> Shoping History</a></td>
												
														<td class="text-right">[<a href="makeclaims.php?customer_id=<?php echo $customer['id']; ?>">Make Claim</a>]</td>
														 
													</tr>
													<?php
														}
														
														$customers = DB::query("SELECT id, name, phone, email, claim FROM customers where id = '{$customer_search}'");
														foreach ($customers as $customer)
														{
															//get points of customer
															 
															 
															 $items = DB::query("SELECT id, customer_name, value_date, bill, claim FROM cash_receipt where customer_id = '".$customer['id']."'");
																				
															$total_bills = 0;
															$total_claims = 0;

															 
															foreach ($items as $item)
															{
																$total_claims = $total_claims + ($item['claim']/100)*($item['bill']);
															}
															
															//get previous claims
															$claims = DB::query("SELECT id, customer_id, claim from customer_claims where customer_id = '".$customer['id']."'");
															$previous_claims = 0;
															
															foreach ($claims as $claim)
															{
																$previous_claims += $previous_claims + $claim['claim'];
															}
															
															$remaining_points = $total_claims - $previous_claims;
													?>
													<tr>
														<td ><?php echo $customer['id']; ?></td>
														<td><a href="customerinfo.php?id=<?php echo $customer['id']; ?>"><?php echo $customer['name']; ?></a></td>
														<td class="text-right"><?php echo $customer['phone']; ?></td>
														<td class="text-right"><?php echo $customer['email']; ?></td>
														<td class="text-right"><?php echo $customer['claim']; ?></td>
														<td class="text-right"><?php echo $remaining_points; ?></td>
														<td class="text-right"><a href="credit_info.php?id=<?php echo $customer['id']; ?>"> Credit Info</a></td>
														
														<td class="text-right"><a href="shoping_history.php?id=<?php echo $customer['id']; ?>"> Shoping History</a></td>
												
														<td class="text-right">[<a href="makeclaims.php?customer_id=<?php echo $customer['id']; ?>">Make Claim</a>]</td>
													  
												
													</tr>
													<?php
														}
													}
													?>
												</tbody>
											</table>
										 
									 
								</div><!-- /.card-body -->
							 
						</div>
						
						
					</div>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
					
					<?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 -->

			<footer class="main-footer text-center">
				<strong>Copyright &copy; <?php echo date("Y"); ?> BSMPOS.</strong> All rights reserved.
			</footer>
		</div><!-- ./wrapper -->
		
		
		
		<!-- jQuery 2.1.3 -->
		<script src="plugins/jQuery/jQuery-2.1.3.min.js"></script>
		<!-- Bootstrap 3.3.2 JS -->
		<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
		<!-- SlimScroll -->
		<script src="plugins/slimScroll/jquery.slimScroll.min.js" type="text/javascript"></script>
		<!-- FastClick -->
		<script src='plugins/fastclick/fastclick.min.js'></script>
		<!-- DATA TABES SCRIPT -->
		<script src="plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
		<script src="plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>
		<!-- AdminLTE App -->
		<script src="dist/js/app.min.js" type="text/javascript"></script>
		
		<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>