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

if(isset($_POST['page'])){
    //Include pagination class file
    include('Pagination.php');
    
    //Include database configuration file
    include "../dbconnect.php";
    
    $client_id = '62';

    $city = 'all';
	$outlet = 'all';
	$ordtype = 'all';
	$dateran = '01/12/2017 - 31/12/2017';
	$operator = '>';
	$starnum = 2;

	$date = explode('-', $dateran);
	// print_r($date);
	$fst_date = trim($date[0], " ");
	$expdate1 = explode('/', $fst_date);
	$fdate = $expdate1[2]."-".$expdate1[1]."-".$expdate1[0];

	$lst_date = trim($date[1], " ");
	$expdate2 = explode('/', $lst_date);
	$ldate = $expdate2[2]."-".$expdate2[1]."-".$expdate2[0];

	if($city == 'all'){
		$city_sql = '';
	} else {
		$city_sql = 'AND fbr.`city_name` = \''.$city.'\'';
	}

	if($outlet == 'all'){
		$outlet_sql = '';
	} else {
		$outlet_sql = 'AND  fbr.`outlet_name` = \''.$outlet.'\'';
	}

	if($ordtype == 'all'){
		$ordtype_sql = '';
	} else {
		$ordtype_sql = 'AND fbr.`order_type` = \''.$ordtype.'\'';
	}

    $start = !empty($_POST['page'])?$_POST['page']:0;
    $limit = 3;

    //get number of rows
    $qry_pgn = mysql_query("SELECT count(*) AS pgnNum FROM `feedback_response` AS fbr WHERE (fbr.`sale_date` >= '$fdate1' AND fbr.`sale_date` <= '$ldate1') AND fbr.`client_id` = '$client_id' $city_sql $outlet_sql $ordtype_sql");
	$row_pgn = mysql_fetch_assoc($qry_pgn);
	$rowCount = $row_pgn['pgnNum'];
    
    //initialize pagination class
    $pagConfig = array('baseURL'=>'pageData.php', 'totalRows'=>$rowCount, 'currentPage'=>$start, 'perPage'=>$limit, 'contentDiv'=>'paginate-customer');
    $pagination =  new Pagination($pagConfig);
    
    
    //get rows
    // echo "SELECT (SUM(frd.`ans_wt`) / count(frd.`fb_no`)) AS rate, fbr.`guest_name`, fbr.`invoice_no`, fbr.`order_source_name`, fbr.`outlet_name`, fbr.`response_date_time`, fbr.`fb_comments`, fbr.`fb_no`, fbr.`outlet_id`, fbr.`invoice_id` FROM `feedback_response_details` AS frd INNER JOIN `feedback_response` AS fbr ON frd.`fb_no` = fbr.`fb_no` WHERE (fbr.`sale_date` >= '$fdate1' AND fbr.`sale_date` <= '$ldate1') AND fbr.`client_id` = '$client_id' $city_sql1 $outlet_sql1 $ordtype_sql1 group by frd.`fb_no` HAVING (SUM(frd.`ans_wt`) / count(frd.`fb_no`)) $operator1 '$starnum1' ORDER BY fbr.`response_date_time` DESC LIMIT $start,$limit";
	$qry1 = mysql_query("SELECT (SUM(frd.`ans_wt`) / count(frd.`fb_no`)) AS rate, fbr.`guest_name`, fbr.`invoice_no`, fbr.`order_source_name`, fbr.`outlet_name`, fbr.`response_date_time`, fbr.`fb_comments`, fbr.`fb_no`, fbr.`outlet_id`, fbr.`invoice_id` FROM `feedback_response_details` AS frd INNER JOIN `feedback_response` AS fbr ON frd.`fb_no` = fbr.`fb_no` WHERE (fbr.`sale_date` >= '$fdate' AND fbr.`sale_date` <= '$ldate') AND fbr.`client_id` = '$client_id' $city_sql $outlet_sql $ordtype_sql group by frd.`fb_no` HAVING (SUM(frd.`ans_wt`) / count(frd.`fb_no`)) $operator '$starnum' ORDER BY fbr.`response_date_time` DESC LIMIT $start,$limit");
	if (mysql_num_rows($qry1) > 0) {
		while ($row1 = mysql_fetch_array($qry1)) {	
			$date_time = $row1['response_date_time'];
			$fbno = $row1['fb_no'];
			/*$rate_qry = mysql_query("SELECT (SUM(`ans_wt`) / count(`fb_no`)) AS rate FROM `feedback_response_details` WHERE `fb_no` = '$fbno'");
			$rate_row = mysql_fetch_array($rate_qry);*/
			$avg_rate = number_format($row1['rate'], 1);
			?>
			<div class="view-customer">
				<div class="shadow"></div>
				<div class="row">
					<div class="col-lg-6">
						<p class="customer-name text-logs" style="font-weight: 800;"><?php echo $row1['guest_name']; ?></p>
					</div>
					<div class="col-lg-6 text-right">
						<p class="customer-name text-logs" style="font-weight: 500;"><?php echo $row1['invoice_no']; ?></p>
						<p class="customer-name text-logs" style="font-weight: 500;"><?php echo $row1['order_source_name']; ?></p>
						<p class="customer-name text-logs" style="font-weight: 500;"><?php echo $row1['outlet_name']; ?></p>
					</div>
				</div>
				<div class="row">
					<div class="col-lg-8">
						<p class="text-logs">Response On <span class="text-bold"><?php echo date('d-m-Y H:i:s', strtotime($row1['response_date_time'])); ?></span></p>
						<p class="text-logs">
							<?php
							for ($i=1; $i <= 5; $i++) { 
								
								if ($i <= $avg_rate) {
									$checked = "star-checked";
									$star = "fa-star";
								} else if( $avg_rate > $j AND $avg_rate < $i ) { 
									$checked = "star-checked";
									$star = "fa-star-half-o";
								} else {
									$checked = "";
									$star = "fa-star-o";
								}	
								$j = $i;

								echo '<i class="fa '.$star." ".$checked.'" aria-hidden="true"></i>';

							}
							?>
							<span class="text-bold">Rated <?php echo number_format($row1['rate'], 1); ?></span>
						</p>
						<p class="text-logs"><span class="help-block text-bold"><?php echo $row1['fb_comments']; ?></span></p>
					</div>
					<div class="col-lg-4">
						<button type="button" name="submit-btn" class="btn btn-outline-primary view-details float-right" onclick="customerViewDetails('details', <?php echo $row1['fb_no']; ?>, <?php echo $row1['outlet_id']; ?>, <?php echo $row1['invoice_id']; ?> )">View Details</button>
					</div>
				</div>
			</div>
			<?php
		}
		echo $pagination->createLinks();
	}
}
?>