<?php
session_start();
if(!isset($_SESSION['user'])) {
	header("location: ../index.php");
	exit;
}

include "../dbconnect.php";
 
include"../header.php";

$phone = $_GET['p'];
// $gid = $_GET['gid'];
$qry = mysql_query("SELECT goh.`FirstName`, goh.`LastName`, goh.`GuestPhone`, gd.`Gender`, goh.`Locality`, goh.`Zone`, goh.`Address`, gd.`Email`, gd.`dob`, gd.`anniversary`, MIN(goh.`OrderDate`) AS dateJoin, MAX(goh.`OrderDate`) AS lastOrder, COUNT(goh.`OrderDate`) AS ord, SUM(goh.`TotalAmt`) AS TotalAmt, AVG(goh.`TotalAmt`) AS AvgAmt, goh.`PointsRedeemed` FROM `guest_order_history` AS goh INNER JOIN `guest_details` AS gd ON goh.`GuestId` = gd.`GuestId` WHERE goh.`GuestPhone` = '$phone' AND (goh.`OrderStatus` != 'Rejected' AND goh.`OrderStatus` != 'Logged' AND goh.`OrderStatus` != 'Manual' AND goh.`OrderStatus` != 'Cancelled') GROUP BY goh.`GuestPhone`");
$row = mysql_fetch_array($qry);

$locality = $row['Locality'];
$zone = $row['Zone'];

$loc_qry = mysql_query("SELECT `locality_name` FROM `locality` WHERE `licality_id` = '$locality'");
$loc_row = mysql_fetch_assoc($loc_qry);
$locality_name = $loc_row['locality_name'];

$zon_qry = mysql_query("SELECT `zone_name` FROM `zone_master` WHERE `zone_id` = '$zone'");
$zon_row = mysql_fetch_assoc($zon_qry);
$zone_name = $zon_row['zone_name'];
?>

<link href="../assets/css/analysis.css" rel="stylesheet">
						<div class="row">
							<div class="col-lg-12">

								<div class="card">
									<div class="card-header admin-feed">
										Customers
										<a href="customers.php" class="btn btn-default float-right btn-back"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> Back</a>
									</div>
									<div class="card-body">
										<div class="row">
											<div class="col-lg-4 col-md-6 col-sm-12">
									            <div class="panel widget section-border ml-md">
									                <div class="panel-body">
									                  	<div class="media p mt0">
									                     	<div class="pull-left">
									                     		<?php
									                     		if ($row['Gender'] == 'female') {
									                     			?>
									                     			<img src="../assets/img/avatars/female-icon.png" alt="Image" class="media-object img-circle thumb96">
									                     			<?php
									                     		} else if ($row['Gender'] == 'male') {
									                     			?>
									                     			<img src="../assets/img/avatars/male-icon.png" alt="Image" class="media-object img-circle thumb96">
									                     			<?php
									                     		} else {
									                     			?>
									                     			<img src="../assets/img/avatars/user3.png" alt="Image" class="media-object img-circle thumb96">
									                     			<?php
									                     		}
									                     		?>
									                            
									                        </div>
									                     	<div class="media-body">
									                        	<div class="media-heading">
									                          		
								                            		<h4 class="mt0 js-name user-name" style="display:inline-block;">
		                                                              	<?php echo $row['FirstName'].' '.$row['LastName']; ?>
		                                                          	</h4>
									                          
										                          	<ul class="list-unstyled">
										                              	<li class="mb-sm js-address-box">
										                                 	<em class="fa fa-envelope fa-fw"></em> <span class="js-address"><?php echo $row['Address1'].' '.$locality_name.' '.$zone_name; ?></span>
										                              	</li>
										                              	<li class="mb-sm js-phone">
										                                 	<em class="fa fa-phone-square fa-fw"></em> <?php echo $row['GuestPhone']; ?>
										                              	</li>
										                              	<?php
										                              	if ($row['Email'] != '') {
										                              		?>
										                              		<li class="mb-sm js-email-box">
											                                 	<em class="fa fa-envelope fa-fw"></em> <span class="js-email"><?php echo $row['Email']; ?></span>
											                              	</li>
										                              		<?php
										                              	}
										                              	?>
										                              	<!-- <li class="mb-sm js-birthday-box">
										                                 	<em class="fa fa-calendar-o fa-fw"></em> Birthday: <span class="js-birthday">1990-10-18</span>
										                              	</li>
										                              	<li class="mb-sm js-anniversary-box" style="display:none;">
										                                 	<em class="fa fa-calendar-o fa-fw"></em> Anniversary: <span class="js-anniversary"></span>
										                              	</li>
										                              	<li class="mb-sm js-address-box" style="display:none;">
										                                 	<em class="fa fa-envelope fa-fw"></em> <span class="js-address"></span>
										                              	</li> -->
										                              	<?php
										                              	if ($row['Gender'] != '') {
										                              		?>
										                              		<li class="mb-sm js-gender-box">
											                                 	<em class="fa fa-user fa-fw"></em> <span class="js-gender"><?php echo ucfirst($row['Gender']); ?></span>
											                              	</li>
										                              		<?php
										                              	}

										                              	if ($row['dob'] != '' || $row['dob'] != '0000-00-00') {
										                              		?>
										                              		<li class="mb-sm js-birthday-box">
											                                 	<em class="fa fa-calendar-o fa-fw"></em> <span class="js-birthday"><?php echo $row['dob']; ?></span>
											                              	</li>
										                              		<?php
										                              	}

										                              	if ($row['anniversary'] != '' || $row['anniversary'] != '0000-00-00') {
										                              		?>
										                              		<li class="mb-sm js-anniversary-box">
											                                 	<em class="fa fa-calendar-o fa-fw"></em> <span class="js-anniversary"><?php echo $row['anniversary']; ?></span>
											                              	</li>
										                              		<?php
										                              	}
										                              	?>
										                              	<!-- <li class="mb-sm">
										                                 	<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#customersDetails">Details</button>
										                              	</li> -->
										                              	<!-- <li class="mb-sm hidden">
										                                 	<em class="fa fa-wheelchair fa-fw"></em> Age range: 15-24
										                              	</li>

										                              	<li class="mb-sm">
										                                	<div class="seg_draw">
										                                		<div class="label" style="background: #000000;display: inline-block;">New</div>&nbsp;
										                                	</div>

										                               		 <div data-toggle="modal" data-target="#segmentAssignModal">
										                                  		<a href="#" class="small">Modify Segments</a>
										                                	<div>
										                              	</li> -->
										                          	</ul>
									                        	</div>
									                     	</div>
									                  	</div>
									               	</div>
									            </div>

									            <div class="panel widget section-border ml-md">
									              	<div class="row row-table row-flush">
										                <div class="col-xs-6 bb br">
										                  	<div class="row row-table row-flush">
											                    <div class="col-xs-4 text-center text-info">
											                       	<em class="fa fa-plus-square-o fa-2x"></em>
											                    </div>
											                    <div class="col-xs-8">
											                       	<div class="panel-body text-center">
											                          	<h4 class="mt0 js_current_points flush-num"><?php echo $row['PointsRedeemed']; ?></h4>
											                          	<p class="mb0 text-muted flush-text">POINTS</p>
											                       	</div>
											                    </div>
										                  	</div>
										                </div>
										                <div class="col-xs-6 bb">
										                  	<div class="row row-table row-flush">
											                    <div class="col-xs-4 text-center text-danger">
											                       	<em class="fa fa-location-arrow fa-2x"></em>
											                    </div>
											                    <div class="col-xs-8">
											                       	<div class="panel-body text-center">
											                          	<h4 class="mt0 js_vists_count flush-num"><?php echo $row['ord']; ?></h4>
											                          	<p class="mb0 text-muted flush-text">VISITS</p>
											                       	</div>
											                    </div>
										                  	</div>
										                </div>
									              	</div>

									                <div class="row row-table row-flush">
										                <div class="col-xs-6 bb br">
										                  	<div class="row row-table row-flush">
											                    <div class="col-xs-4 text-center text-danger">
											                       	<em class="fa fa-money fa-2x"></em>
											                    </div>
											                    <div class="col-xs-8">
											                       	<div class="panel-body text-center">
											                          	<h4 class="mt0 js_total_spent flush-num"><?php echo round($row['TotalAmt']); ?></h4>
											                          	<p class="mb0 text-muted flush-text">TOTAL SPEND</p>
											                       	</div>
											                    </div>
											                </div>
										                </div>

										                <div class="col-xs-6 bb">
										                  	<div class="row row-table row-flush">
											                    <div class="col-xs-4 text-center text-info">
											                       	<em class="fa fa-money fa-2x"></em>
											                    </div>
											                    <div class="col-xs-8">
											                       	<div class="panel-body text-center">
											                          	<h4 class="mt0 js_average_spent flush-num"><?php echo round($row['AvgAmt']); ?></h4>
											                          	<p class="mb0 text-muted flush-text">AVG. SPEND</p>
											                       	</div>
											                    </div>
										                  	</div>
										                </div>
									              	</div>
									              
									              	<div class="row row-table row-flush">
										                <div class="col-xs-6 br">
										                   	<div class="row row-table row-flush">
										                      	<div class="col-xs-4 text-center text-inverse">
										                         	<em class="fa fa-gift fa-2x"></em>
										                      	</div>
										                      	<div class="col-xs-8">
										                         	<div class="panel-body text-center">
										                            	<h4 class="mt0 flush-num">0</h4>
										                            	<p class="mb0 text-muted flush-text">REWARDS</p>
										                         	</div>
										                      	</div>
										                   	</div>
										                </div>
										                <div class="col-xs-6">
										                   	<div class="row row-table row-flush">
										                      	<div class="col-xs-4 text-center text-success">
										                         	<em class="fa fa-calendar-o fa-2x"></em>
										                      	</div>
										                      	<div class="col-xs-8">
										                         	<div class="panel-body text-center">
										                            	<h4 class="mt0 flush-num"><?php echo date('d M Y', strtotime($row['dateJoin'])); ?></h4>
										                            	<p class="mb0 text-muted flush-text">JOINED</p>
										                         	</div>
										                      	</div>
										                   	</div>
										                </div>
									              	</div>
									            </div>
									        </div>

											<div class="col-lg-8 col-md-6 col-sm-12">
									            <div class="panel panel-default">
									               	<div class="panel-body">
									                  	<h3 class="table-header">Order History</h3>
									                  	<div class="table-responsive">
									                     	<table id="table-ext-1" class="table table-hover mv">
									                        	<thead>
									                        		<tr>
									                        			<th class="fw400">Outlet</th>
									                        			<th class="fw400">Order Id</th>
									                        			<th class="fw400">Oreder Date/Time</th>
									                        			<th class="fw400">Amount</th>
									                        			<th class="fw400">Order Source</th>
									                        			<th class="fw400"></th>
									                        		</tr>
									                        	</thead>
									                        	<tbody class="js_history_tbody">
									                        		<?php
									                        		$qry2 = mysql_query("SELECT `UnitId`, `OrderId`, `OrderDate`, `OrderTime`, `InvoiceNumber`, `SaleDate`, `TotalAmt`, `SourceOfOrder` FROM `guest_order_history` WHERE `GuestPhone` = '$phone' AND (`OrderStatus` != 'Rejected' AND `OrderStatus` != 'Logged' AND `OrderStatus` != 'Manual' AND `OrderStatus` != 'Cancelled') ORDER BY `OrderDate` DESC");
									                        		while ($row2 = mysql_fetch_array($qry2)) {

									                        			$unitid = $row2['UnitId'];

									                        			$unit_qry = mysql_query("SELECT `UnitId`, `UnitName` FROM `unit_master` WHERE `UnitId` = '$unitid'");
									                        			while ($unit_row = mysql_fetch_array($unit_qry)) {
									                        				?>
										                        			<tr>
																			    <td class=""><?php echo $unit_row['UnitName']; ?></em></td>
																			  	<td><?php echo $row2['OrderId']; ?></td>
																			  	<td class="hidden-xs"><?php echo date('d-m-Y', strtotime($row2['OrderDate'])).' '.$row2['OrderTime']; ?></td>
																			  	<td class="hidden-xs" style="text-align: center;"><?php echo round($row2['TotalAmt']); ?></td>
																			  	<td class="hidden-xs"><?php echo $row2['SourceOfOrder']; ?></td>
																			  	<td class="hidden-xs"><button type="button" class="btn btn-primary" onclick="viewDetailsClientOrders('vieworder', <?php echo $row2['OrderId']; ?>, <?php echo $unit_row['UnitId']; ?>, <?php echo $row2['InvoiceNumber']; ?>, '<?php echo $row2['OrderDate']; ?>')">View Details</button></td>
																			</tr>
										                        			<?php
									                        			}
									                        		}
									                        		?>
									                            </tbody>
									                     	</table>
									                  	</div>
									                  
									               	</div>
									            </div>
									        </div>
										</div>
									</div>
								</div>

							</div>
						</div>


		                <div class="modal fade show" id="viewOrderDetails" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		                    <div class="modal-dialog modal-primary modal-lg" role="document">
		                        <div class="modal-content">
		                        	<div class="modal-header" style="padding: 0 15px 0 0;">
		                                <h4 class="modal-title"></h4>
		                                <button type="button" class="close" id="close-btn" data-dismiss="modal" aria-label="Close">
		                                    <span aria-hidden="true">×</span>
		                                </button>
		                            </div>
		                        	<div class="modal-body orderDetail">
		                        	</div>
		                        </div>
		                    </div>
		                </div>
<?php
include"../footer.php";
?>

<script type="text/javascript">
$(document).ready(function(){
	$("#close-btn").click(function(){
		$("#viewOrderDetails").hide();
	});
});
</script>