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

include "../dbconnect.php";
include('pagination.php');
include"../header.php";

$limit = 10;

$queryNum = mysql_query("SELECT DISTINCT(goh.`GuestPhone`) as postNum FROM `guest_order_history` AS goh WHERE (goh.`OrderStatus` != 'Rejected' AND goh.`OrderStatus` != 'Logged' AND goh.`OrderStatus` != 'Manual' AND goh.`OrderStatus` != 'Cancelled')");
// $resultNum = $queryNum->fetch_assoc();
$rowCount = mysql_num_rows($queryNum);

$pagConfig = array(
    'totalRows' => $rowCount,
    'perPage' => $limit,
    'link_func' => 'searchFilter'
);
$pagination =  new Pagination($pagConfig);

$qry = mysql_query("SELECT goh.`GuestPhone`, goh.`FirstName`, goh.`LastName`, MAX(goh.`OrderDate`) AS OrderDate, goh.`OrderTime`, count(goh.`OrderDate`) AS ord, goh.`PointsRedeemed` FROM `guest_order_history` AS goh WHERE (goh.`OrderStatus` != 'Rejected' AND goh.`OrderStatus` != 'Logged' AND goh.`OrderStatus` != 'Manual' AND goh.`OrderStatus` != 'Cancelled') GROUP BY goh.`GuestPhone` LIMIT $limit");
?>
<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">
										Client Information
										<!-- <a href="../crm/manage_filter.php" class="float-right btn-segment">
								            <button type="button" class="btn btn-success text-md pull-right">
								               	Create Segment
								            </button>
								        </a>
								        <a href="uploads.php" class="float-right btn-segment">
								            <button type="button" class="btn btn-success text-md pull-right mr">
								               	Upload Data
								            </button>
								        </a> -->
									</div>
									<div class="card-body">
										<div class="row">
											<div class="col-lg-12">

												<div class="analytic-numbers">
								            		<div class="row mt-lg ml0 mr0">
													    <div class="col-lg-4 col-md-3">
													      	<div class="panel widget b b-primary-light">
														       	<div class="row row-table row-flush">
														          	<div class="col-xs-4 bg-primary-light text-center">
														            	Total Clients
														          	</div>
														          	<div class="col-xs-8">
														             	<div class="panel-body text-center">
														                	<h4 class="mt0 mb0 oic total-clients"><?php echo $rowCount; ?></h4>
														             	</div>
														          	</div>
														       	</div>
													    	</div>
													  	</div>
													  	<?php
													  	/*$male = 0;
													  	$female = 0;
													    $query1 = mysql_query("SELECT COUNT( DISTINCT(gd.`GuestPhone`) ) AS cnt, gd.`Gender` AS gender FROM `guest_details` AS gd LEFT JOIN `guest_order_history` AS goh ON gd.`GuestId` = goh.`GuestId` WHERE (goh.`OrderStatus` != 'Rejected' AND goh.`OrderStatus` != 'Logged' AND goh.`OrderStatus` != 'Manual' AND goh.`OrderStatus` != 'Cancelled') GROUP BY gd.`Gender`");
													    // $res1 = mysql_fetch_assoc($query1);
													    while ($res1 = mysql_fetch_assoc($query1)) {
													    	$gender = $res1['gender'];
													    	$cnt = $res1['cnt'];
													    	if ($gender == 'male') {
													    		$male += $cnt;
													    	}
													    	if ($gender == 'female') {
													    		$female += $cnt;
													    	}
													    }*/
													    ?>
													    <!-- <div class="col-lg-4 col-md-3">
													      	<div class="panel widget b b-success-light">
													       		<div class="row row-table row-flush">
														          	<div class="col-xs-4 bg-success-light text-center">
														           		Female
														          	</div>
														          	<div class="col-xs-8">
														             	<div class="panel-body text-center">
														                	<h4 class="mt0 mb0 oic total-female"><?php echo $female; ?></h4>
														             	</div>
														          	</div>
														       	</div>
													    	</div>
													  	</div>
												        <div class="col-lg-4 col-md-3">
												          	<div class="panel widget b b-info-light">
												         		<div class="row row-table row-flush">
														            <div class="col-xs-4 bg-info-light text-center">
														             	Male
														            </div>
														            <div class="col-xs-8">
														               	<div class="panel-body text-center">
														                  	<h4 class="mt0 mb0 oic total-male"><?php echo $male; ?></h4>
														               	</div>
														            </div>
														        </div>
													      	</div>
													  	</div> -->
								  
								  					</div>
												</div>

												<section style="min-height: 449px;">
												   	<div class="content-wrapper" style="overflow: initial;">

												      	<!-- <ul class="nav nav-pills mb pl heading-border">
												          	<li class="active ml">
												          		<a href="#customers" data-toggle="tab">All</a>
												          	</li>
												          	<li class="">
												          		<a href="#segments" data-toggle="tab">Segments</a>
												          	</li>
												       	</ul> -->

												       	<!-- <div class="tab-content"> -->
													    	<!-- start customers tab -->
													      	<div id="customers" class="tab-pane fade in active tab-custom" role="tabpanel">
													      		<div class="row mt-xl">
															        <div class="col-lg-11">
															            <input id="search-text" type="text" placeholder="Search by Name or Phone Number" class="input-lg form-control">
															        </div>
															        <div class="col-lg-1">
															        	<button type="button" class="btn btn-outline-primary float-right" onclick="searchFilter()">Search</button>
															        </div>
															    </div>
															    <div class="table-responsive mt-lg" id="posts_content">
															    	<table class="table table-hover customers-table">
															            <thead>
															               	<tr>
															                  	<th class="fw400">Image</th>
															                  	<th class="fw400">Name</th>
															                  	<th class="hidden-xs hidden-sm fw400">Phone Number</th>
															                  	<th class="hidden-xs hidden-sm fw400">Last Visited</th>
															                  	<th class="fw400">Visits</th>
															                  	<!-- <th class="fw400">Points</th> -->
															               	</tr>
															            </thead>
															            <tbody class="js-users_body">
															            	<?php
															            	while ($row = mysql_fetch_array($qry)) {
															            		?>
															            		<tr>
															            			<!-- <input type="hidden" name="guestid" value="<?php //echo $row['GuestId']; ?>"> -->
															            			<td>
																				      	<div class="media">
																				            <img src="../assets/img/avatars/user3.png" alt="Image" class="img-responsive img-circle">
																				        </div>
																				   	</td>
																				   	<td style="overflow-wrap: break-word;max-width: 157px;">
																				      	<a href="customers_information.php?p=<?php echo $row['GuestPhone']; ?>" class="js-name">
																				        	<?php echo $row['FirstName'].' '.$row['LastName']; ?>
																				        </a>
																				   	</td>
																				   	<td class="hidden-xs hidden-sm">
																				        <?php echo $row['GuestPhone']; ?>
																				   	</td>

																				   	<td class="hidden-xs hidden-sm"><?php echo date('d-m-Y', strtotime($row['OrderDate'])).', '.$row['OrderTime']; ?></td>
																				   	<td><?php echo $row['ord']; ?></td>
																				   	<!-- <td><?php //echo $row['PointsRedeemed']; ?></td> -->
																				</tr>
															            		<?php
															            	}
															            	?>
																		</tbody>
															        </table>
															        <?php echo $pagination->createLinks(); ?>
															    </div>
													      	</div>

													      	<!-- segments tab start -->
													      	<!-- <div id="segments" class="tab-pane fade in tab-custom" role="tabpanel">
													      		<div class="table-responsive mt-xl ml text-md">
														         	<table class="table table-hover">
														            	<thead>
														               		<tr>
															                  	<th class="col-md-2">Segment</th>
															                  	<th class="hidden-xs col-md-3">Rules</th>
															                  	<th class="" align="center">No. Customers</th>
															                  	<th class="hidden-xs ">Actions</th>
														               		</tr>
														            	</thead>
														            	<tbody>
														                    <tr id="segemnt-7524">
															                	<td>
															                      	<a href="segment-detail/7524">
															                        	New
															                      	</a>
															                	</td>
															                	<td class="hidden-xs">
															                        First visited less than 2 months
															                	</td>
															                	<td>2
															                  		<br>
															                  
															                	</td>
																                <td>
																                  	<a href="create-segment/7524" tabindex="0">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">&nbsp;<em class="fa fa-edit js-edit-campaign">&nbsp;</em> Edit</button>
																                  	</a>

																                  	<button type="button" data-id="7524" data="" class="mb-sm btn btn-default btn-xs js-delete-segment">
																                    &nbsp;<em class="fa fa-trash-o">&nbsp;</em> Delete
																                  	</button>

																                  	<a href="create-campaign?segment=7524" class="hidden">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">Send Message</button>
																                  	</a>
																                     <br>                                        
														                        </td>
														              		</tr>
														                    <tr id="segemnt-7525">
																                <td>
																                    <a href="segment-detail/7525">
																                        Lost Repeats
																                    </a>
																                </td>
																                <td class="hidden-xs">
																                    of the following: <br>
																                    Last visit is not within 2 months No. of visits are atleast 2
																                </td>
																                <td>0
																                  	<br>
																                </td>
																                <td>
																                  	<a href="create-segment/7525" tabindex="0">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">&nbsp;<em class="fa fa-edit js-edit-campaign">&nbsp;</em> Edit</button>
																                  	</a>

																                  	<button type="button" data-id="7525" data="" class="mb-sm btn btn-default btn-xs js-delete-segment">
																                    &nbsp;<em class="fa fa-trash-o">&nbsp;</em> Delete
																                  	</button>

																                  	<a href="create-campaign?segment=7525" class="hidden">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">Send Message</button>
																                  	</a>
																                    <br>
																                    </td>
																            </tr>
																            <tr id="segemnt-7526">
																                <td>
																                    <a href="segment-detail/7526">
																                        1 &amp; Done
																                    </a>
																                </td>
																                <td class="hidden-xs"> 
																                    of the following: <br>
														                            Last visit is not within 2 months No. of visits are atleast 2
														                		</td>
														                		<td>0
														                  			<br>
														                		</td>
														                		<td>
																                  	<a href="create-segment/7526" tabindex="0">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">&nbsp;<em class="fa fa-edit js-edit-campaign">&nbsp;</em> Edit</button>
																                  	</a>

																                  	<button type="button" data-id="7526" data="" class="mb-sm btn btn-default btn-xs js-delete-segment">
																                    &nbsp;<em class="fa fa-trash-o">&nbsp;</em> Delete
																                  	</button>

																                  	<a href="create-campaign?segment=7526" class="hidden">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">Send Message</button>
																                  	</a>
														                            <br>                                    
														                        </td>
													              			</tr>
														                    <tr id="segemnt-7527">
																                <td>
																                    <a href="segment-detail/7527">
																                        Lost
																                    </a>
																                </td>
																                <td class="hidden-xs">
																                    Last visit is not within 2 months
																                </td>
																                <td>0
																                  <br>
																                </td>
																                <td>
																                  	<a href="create-segment/7527" tabindex="0">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">&nbsp;<em class="fa fa-edit js-edit-campaign">&nbsp;</em> Edit</button>
																                  	</a>

																                  	<button type="button" data-id="7527" data="" class="mb-sm btn btn-default btn-xs js-delete-segment">
																                    &nbsp;<em class="fa fa-trash-o">&nbsp;</em> Delete
																                  	</button>

																                  	<a href="create-campaign?segment=7527" class="hidden">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">Send Message</button>
																                  	</a>
														                            <br>                                 
														                        </td>
														              		</tr>
														                    <tr id="segemnt-7528">
														                		<td>
														                      		<a href="segment-detail/7528">
														                        		Active
														                      		</a>
																                </td>
																                <td class="hidden-xs">
																                    Last visit is within 2 months
																                </td>
																                <td>2
																                  <br>
																                </td>
																                <td>
																                  	<a href="create-segment/7528" tabindex="0">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">&nbsp;<em class="fa fa-edit js-edit-campaign">&nbsp;</em> Edit</button>
																                  	</a>

																                  	<button type="button" data-id="7528" data="" class="mb-sm btn btn-default btn-xs js-delete-segment">
																                    &nbsp;<em class="fa fa-trash-o">&nbsp;</em> Delete
																                  	</button>

																                  	<a href="create-campaign?segment=7528" class="hidden">
																                    	<button type="button" class="mb-sm btn btn-default btn-xs">Send Message</button>
																                  	</a>
														                            <br>                              
														                        </td>
														              		</tr>
														                </tbody>
														        	</table>
														      	</div>
													      	</div> -->
													    <!-- </div> -->
												      	
												    </div>

												</section>

											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
<?php
include"../footer.php";
?>

<script type="text/javascript">
function searchFilter(page_num) {
    page_num = page_num?page_num:0;
    var keywords = $('#search-text').val();
    $.ajax({
        type: 'POST',
        url: 'page_data.php',
        data:'page='+page_num+'&keywords='+keywords,
        success: function (html) {
            $('#posts_content').html(html);
        }
    });
}
</script>