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

$client_id = $_SESSION['client_id'];
$outlet_id = $_SESSION['DeafultUnitId'];

// Data and graph of custom date from feedback dashboard
if ($_GET['act'] == 'dd') 
{
	$city = $_GET['city'];
	$outlet = $_GET['outlet'];
	$ordtype = $_GET['ordtype'];
	$ordsrc = $_GET['ordsrc'];
	$dateran = $_GET['dateran'];
	
	$date = explode('-', $dateran);
	$fst_date = trim($date[0], " ");
	$expdate1 = explode('/', $fst_date);
	$fdate = $expdate1[2]."-".$expdate1[1]."-".$expdate1[0];
	// $ftdate = new DateTime($expdate1[2]."-".$expdate1[1]."-".$expdate1[0]);
	// $fdate = $ftdate->format("Y-m-d");
	$fst_mnth = date('d M Y',strtotime($fdate));

	$lst_date = trim($date[1], " ");
	$expdate2 = explode('/', $lst_date);
	$ldate = $expdate2[2]."-".$expdate2[1]."-".$expdate2[0];
	// $ltdate = new DateTime($expdate2[2]."-".$expdate2[1]."-".$expdate2[0]);
	// $ldate = $ltdate->format("Y-m-d");
	$last_mnth = date('d M Y',strtotime($ldate));

	$mnth_yr = $fst_mnth." - ".$last_mnth;
	//$days = date('d-m-Y',strtotime($ldate))-date('d-m-Y',strtotime($fdate));

	$crdate = array();
	$respond = array();
	$respond_all_star = array();
	$qlty_star = array();
	$package_star = array();
	$visit_star = array();
	
	// filter sql for graph and all question details
	$city_sql = '';
	$outlet_sql = 'AND  fbr.`outlet_id` = \'25\'';
	$ordtype_sql = '';
	$ordsrc_sql = '';
	
	// filter sql for feedback sent and received
	$city_sql1 = '';
	$outlet_sql1 = 'AND  goh.`UnitId` = \'25\'';
	$ordtype_sql1 = '';
	$ordsrc_sql1 = '';
	
	$qry = mysql_query("SELECT DISTINCT(`sale_date`) FROM `feedback_response` AS fbr WHERE (`sale_date` >= '$fdate' AND `sale_date` <= '$ldate') AND `client_id` = '$client_id' $city_sql $outlet_sql $ordtype_sql $ordsrc_sql");
	while ($row = mysql_fetch_array($qry)) 
	{
		$fbdate = $row['sale_date'];

		$frd_qry = mysql_query("SELECT (SUM(  frd.`ans_wt` ) / COUNT( DISTINCT(frd.`fb_no`) )) AS avg FROM `feedback_response_details` as frd RIGHT JOIN `feedback_response` as fbr ON frd.`fb_no` = fbr.`fb_no` AND fbr.`sale_date` = '$fbdate' AND frd.`fb_ques_id` = '1'");
		$frd_row = mysql_fetch_array($frd_qry);
		
		$respond[] = $frd_row['avg'];
		$crdate[] = date('d-m-Y', strtotime($fbdate));
	}

	
	$frd_qry2 = mysql_query("SELECT (SUM(  frd.`ans_wt` ) / COUNT( DISTINCT(frd.`fb_no`) )) AS avg_all FROM `feedback_response_details` as frd RIGHT JOIN `feedback_response` as fbr ON frd.`fb_no` = fbr.`fb_no` AND frd.`fb_ques_id` = '1' AND (fbr.`sale_date` >= '$fdate' AND fbr.`sale_date` <= '$ldate') $city_sql $outlet_sql $ordtype_sql $ordsrc_sql");
	$frd_row2 = mysql_fetch_array($frd_qry2);
	$respond_all = number_format($frd_row2['avg_all'], 1);
	for ($i=1; $i <= 5; $i++) 
	{ 
		
		if ($i <= $respond_all) {
			$checked = "star-checked";
			$star = "fa-star";
			
		} else if( $respond_all > $j AND $respond_all < $i ) { 
			$checked = "star-checked";
			$star = "fa-star-half-o";
		} else {
			$checked = "";
			$star = "fa-star-o";
		}	
		$j = $i;

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

	}


	$total_sent = 0;
	$sentData = array();
	$sentDate = array();
	if($ordtype == 'all' OR $ordtype == 'HomeDelivery') {
		$sqry11 = mysql_query("SELECT DISTINCT(goh.`SaleDate`) AS saledate, count(goh.`feedback_sent`) as sent FROM `guest_order_history` AS goh WHERE (goh.`SaleDate` >= '$fdate' AND goh.`SaleDate` <= '$ldate') $city_sql1 $outlet_sql1 $ordtype_sql1 $ordsrc_sql1 AND goh.`feedback_sent` = '1' GROUP BY goh.`SaleDate`");
		while ($srow11 = mysql_fetch_array($sqry11)) {
			$total_sent += $srow11['sent'];
			$sdate = $srow11['saledate'];
			$sentData[] = $srow11['sent'];
			$sentDate[] = date('d-m-Y', strtotime($sdate));
		}
	}

	// feedback received for delivery
	$total_done = 0;
	$recData = array();
	$recDate = array();

	if($ordtype == 'all' OR $ordtype == 'HomeDelivery') {
		$dqry = mysql_query("SELECT count(`fb_no`) as done, `sale_date` FROM `feedback_response` AS fbr WHERE (`sale_date` >= '$fdate' AND `sale_date` <= '$ldate') AND `order_type` != 'Dine_In' $city_sql $outlet_sql $ordsrc_sql GROUP BY `sale_date`");
		while ($drow = mysql_fetch_array($dqry)) {
			$total_done += $drow['done'];
			$ddate = $drow['sale_date'];
			$recData[] = $drow['done'];
			$recDate[] = date('d-m-Y', strtotime($ddate));
		}
	}

	// feedback received for dine_in
	$total_dine_done = 0;
	$dine_recData = array();
	$dine_recDate = array();
	if($ordtype == 'all' OR $ordtype == 'Dine_In') {
		$dqry1 = mysql_query("SELECT count(`fb_no`) as done, `sale_date` FROM `feedback_response` AS fbr WHERE (`sale_date` >= '$fdate' AND `sale_date` <= '$ldate') AND `order_type` = 'Dine_In' $city_sql $outlet_sql $ordsrc_sql GROUP BY `sale_date`");
		while ($drow1 = mysql_fetch_array($dqry1)) {
			$total_dine_done += $drow1['done'];
			$ddate1 = $drow1['sale_date'];
			$dine_recData[] = $drow1['done'];
			$dine_recDate[] = date('d-m-Y', strtotime($ddate1));
		}
	}

	$nqry = mysql_query("SELECT count(`feedback_done`) as notinterst FROM `guest_order_history` AS goh WHERE (`SaleDate` >= '$fdate' AND `SaleDate` <= '$ldate') $city_sql1 $outlet_sql1 $ordtype_sql1 $ordsrc_sql1 AND `feedback_sent` = '1' AND `feedback_done` = '2'");
	while ($nrow = mysql_fetch_array($nqry)) {
		$total_notint = $nrow['notinterst'];
	}

	$percentData = array();
	$percentDate = array();
	$per_qry = mysql_query("SELECT DISTINCT(goh.`SaleDate`) AS saledate, ((SUM(goh.`feedback_done`) / SUM(goh.`feedback_sent`))*100) AS percent FROM `guest_order_history` AS goh WHERE (goh.`SaleDate` >= '$fdate' AND goh.`SaleDate` <= '$ldate') $city_sql1 $outlet_sql1 $ordtype_sql1 $ordsrc_sql1 AND goh.`feedback_done` = '1' GROUP BY `SaleDate`");
	while ($per_row = mysql_fetch_array($per_qry)) {
		$per_date = $per_row['saledate'];
		$percentData[] = $per_row['percent'];
		$percentDate[] = date('d-m-Y', strtotime($per_date));
	}
	
	if ($total_sent == 0) {
		$total_done_per = 0;
	} else {
		$total_done_per = number_format(($total_done/$total_sent)*100, 2)."%";
	}
	


	// dynamic for feedback question details	
	$dy_ques_data = array();
	$dy_qry = mysql_query("SELECT `fb_ques_id`, `fb_ques` FROM `feedback_ques` WHERE `fb_ques_id` != 1 and fb_ques_enable=1 GROUP BY `fb_ques_id`");
	while ($dy_row = mysql_fetch_array($dy_qry)) {
		$ques_id = $dy_row['fb_ques_id'];
		$ques_name = $dy_row['fb_ques'];
		$expquesname = explode('_', $ques_name);
		$fdqlty_fbno = 0;
		$ans_wt = 0;
		$fdqlty_five_star11 = 0;
		$fdqlty_four_star11 = 0;
		$fdqlty_three_star11 = 0;
		$fdqlty_two_star11 = 0;
		$fdqlty_one_star11 = 0;
		$fdqlty_five_per11 = 0;
		$fdqlty_four_per11 = 0;
		$fdqlty_three_per11 = 0;
		$fdqlty_two_per11 = 0;
		$fdqlty_one_per11 = 0;
		$frd_qry121 = mysql_query("SELECT COUNT( DISTINCT(frd.`fb_no`) ) AS fdqlty_fbno, `ans_wt` FROM `feedback_response_details` as frd RIGHT JOIN `feedback_response` as fbr ON frd.`fb_no` = fbr.`fb_no` AND frd.`fb_ques_id` = '$ques_id' AND (fbr.`sale_date` >= '$fdate' AND fbr.`sale_date` <= '$ldate') $city_sql $outlet_sql $ordtype_sql $ordsrc_sql GROUP BY frd.`fb_no`");
		while ($frd_row121 = mysql_fetch_array($frd_qry121)) {

			$fdqlty_fbno = $frd_row121['fdqlty_fbno'];
			$ans_wt = $frd_row121['ans_wt'];
			if ($ans_wt == 5) {
				$fdqlty_five_star11 += $fdqlty_fbno;
				if ($total_done == 0) {
					$fdqlty_five_per11 = 0;
				} else {
					$fdqlty_five_per11 = ($fdqlty_five_star11/$total_done)*100;
				}
			}
			if ($ans_wt == 4) {
				$fdqlty_four_star11 += $fdqlty_fbno;
				if ($total_done == 0) {
					$fdqlty_four_per11 = 0;
				} else {
					$fdqlty_four_per11 = ($fdqlty_four_star11/$total_done)*100;
				}
			}
			if ($ans_wt == 3) {
				$fdqlty_three_star11 += $fdqlty_fbno;
				if ($total_done == 0) {
					$fdqlty_three_per11 = 0;
				} else {
					$fdqlty_three_per11 = ($fdqlty_three_star11/$total_done)*100;
				}
			}
			if ($ans_wt == 2) {
				$fdqlty_two_star11 += $fdqlty_fbno;
				if ($total_done == 0) {
					$fdqlty_two_per11 = 0;
				} else {
					$fdqlty_two_per11 = ($fdqlty_two_star11/$total_done)*100;
				}
			}
			if ($ans_wt == 1) {
				$fdqlty_one_star11 += $fdqlty_fbno;
				if ($total_done == 0) {
					$fdqlty_one_per11 = 0;
				} else {
					$fdqlty_one_per11 = ($fdqlty_one_star11/$total_done)*100;
				}
			}
		}
		$qlty_star_rate = array();
		$frd_qry12 = mysql_query("SELECT (SUM(  frd.`ans_wt` ) / COUNT( DISTINCT(frd.`fb_no`) )) AS avg_all12 FROM `feedback_response_details` as frd RIGHT JOIN `feedback_response` as fbr ON frd.`fb_no` = fbr.`fb_no` AND frd.`fb_ques_id` = '$ques_id' AND (fbr.`sale_date` >= '$fdate' AND fbr.`sale_date` <= '$ldate') $city_sql $outlet_sql $ordtype_sql $ordsrc_sql");
		while ($frd_row12 = mysql_fetch_array($frd_qry12)) {
			$respond_all12 = number_format($frd_row12['avg_all12'], 1);
			for ($i=1; $i <= 5; $i++) { 
																	
				if ($i <= $respond_all12) {
					$checked = "star-checked";
					$star = "fa-star";
					
				} else if( $respond_all12 > $j AND $respond_all12 < $i ) { 
					$checked = "star-checked";
					$star = "fa-star-half-o";
				} else {
					$checked = "";
					$star = "fa-star-o";
				}	
				$j = $i;

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

			}
			
			$dy_ques_data[] = '<div class="col-sm-6 col-lg-4"><div class="social-box facebook"><div class="box-title py-2"><div class="title">'.ucwords(implode(' ', $expquesname)).'</div><p class="dash-rating-count">'.$respond_all12.'</p><p class="star-rating fb-fdqlty-star">'.implode('',$qlty_star_rate).'</p></div><div class="custom-star-progress"><div class="row"><div class="col-xs-12 col-md-12"><div class="row fd-rating-desc"><div class="col-xs-3 col-md-3 col-lg-3">5 <span class="fa fa-star star-checked"></span></div><div class="col-xs-6 col-md-6 col-lg-6"><div class="progress progress-sm mt-2 progress-custom"><div class="progress-bar bg-success" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: '.$fdqlty_five_per11.'%"></div></div></div><div class="col-xs-2 col-md-2 col-lg-2"><span class="sr-only">'.$fdqlty_five_star11.'</span></div><div class="col-xs-3 col-md-3 col-lg-3">4 <span class="fa fa-star star-checked"></span></div><div class="col-xs-6 col-md-6 col-lg-6"><div class="progress progress-sm mt-2 progress-custom"><div class="progress-bar bg-success" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: '.$fdqlty_four_per11.'%"></div></div></div><div class="col-xs-2 col-md-2 col-lg-2"><span class="sr-only">'.$fdqlty_four_star11.'</span></div><div class="col-xs-3 col-md-3 col-lg-3">3 <span class="fa fa-star star-checked"></span></div><div class="col-xs-6 col-md-6 col-lg-6"><div class="progress progress-sm mt-2 progress-custom"><div class="progress-bar bg-success" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: '.$fdqlty_three_per11.'%"></div></div></div><div class="col-xs-2 col-md-2 col-lg-2"><span class="sr-only">'.$fdqlty_three_star11.'</span></div><div class="col-xs-3 col-md-3 col-lg-3">2 <span class="fa fa-star star-checked"></span></div><div class="col-xs-6 col-md-6 col-lg-6"><div class="progress progress-sm mt-2 progress-custom"><div class="progress-bar bg-success" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: '.$fdqlty_two_per11.'%"></div></div></div><div class="col-xs-2 col-md-2 col-lg-2"><span class="sr-only">'.$fdqlty_two_star11.'</span></div><div class="col-xs-3 col-md-3 col-lg-3">1 <span class="fa fa-star star-checked"></span></div><div class="col-xs-6 col-md-6 col-lg-6"><div class="progress progress-sm mt-2 progress-custom"><div class="progress-bar bg-success" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: '.$fdqlty_one_per11.'%"></div></div></div><div class="col-xs-2 col-md-2 col-lg-2"><span class="sr-only">'.$fdqlty_one_star11.'</span></div></div></div></div></div></div></div>';
		}
	}

	$data = array('date'=>$crdate, 'fbvalue'=>$respond, 'mtyr'=>$mnth_yr, 'overall'=>$respond_all, 'overallstar'=>$respond_all_star, 'fbsent'=>$total_sent, 'sentData'=>$sentData, 'sentDate'=>$sentDate, 'recData'=>$recData, 'recDate'=>$recDate, 'fbdone'=>$total_done, 'dine_recData'=>$dine_recData, 'dine_recDate'=>$dine_recDate, 'dine_fbdone'=>$total_dine_done, 'notinterest' => $total_notint, 'fbdoneper'=>$total_done_per, 'percentData'=>$percentData, 'percentDate'=>$percentDate, 'dyquesdata'=>$dy_ques_data);
	echo json_encode($data);
}

?>