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

$client_id = '62';
$outlet_id = '1';
if ($_GET['act'] == 'takefb') {
	$outletid = $_GET['outletid'];
	$invoiceid = $_GET['invoiceid'];

	$feedback_details = '';
	$fbdt = array();
	
	// echo "SELECT * FROM `guest_order_history` WHERE `feedback_done` = 0 AND (OrderStatus != 'Rejected' AND OrderStatus != 'Cancelled') AND `UnitId` = '$outletid' AND `InvoiceId` = '$invoiceid'";
	$qry = mysql_query("SELECT * FROM `guest_order_history` WHERE `feedback_done` = 0 AND (OrderStatus != 'Rejected' AND OrderStatus != 'Cancelled') AND `UnitId` = '$outletid' AND `InvoiceId` = '$invoiceid'");
	while ($row = mysql_fetch_array($qry)) {
		$ord_src = $row['SourceOfOrder'];
		$UnitId = $row['UnitId'];

		$unit_sql = mysql_query("SELECT UnitName FROM unit_master WHERE UnitId=$UnitId");
		$unit_row = mysql_fetch_assoc($unit_sql);
		$unit_name = $unit_row['UnitName'];

		$feedback_details = '<script src="../plug/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script><p class="customer-name text-logs" style="font-weight: 800;">'.$row['FirstName'].' '.$row['LastName'].'</p><div class="row"><div class="col-lg-6 col-md-6 fb-detail-left"><p class="detail-left text-logs">'.$row['GuestPhone'].'</p><p class="detail-left text-logs">Sale Date <span class="text-bold">'.date('d-m-Y', strtotime($row['SaleDate'])).' '.$row['SaleTime'].'</span></p></div><div class="col-lg-6 col-md-6 fb-detail-right text-right"><p class="detail-right text-logs">Invoice No. - <span class="text-bold">'.$row['InvoiceNumber'].'</span></p><p class="detail-right text-logs">Order Source - <span class="text-bold">'.$row['SourceOfOrder'].'</span></p><p class="detail-right text-logs">Outlet Name - <span class="text-bold">'.$unit_name.'</span></p></div></div><div class="row"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><form id="frm_callback" name="frm"><input type="hidden" name="outletid" id="cboutletid" value="'.$outletid.'"><input type="hidden" name="invoiceid" id="cbinvoiceid" value="'.$invoiceid.'"> ';
		$i = 1;
	
		//		$que_qry = mysql_query("SELECT * FROM `feedback_ques` WHERE `fb_ques_enable` = 1 AND `client_id` = '$client_id' AND `outlet_id` = '$UnitId'");
		$que_qry = mysql_query("SELECT * FROM `feedback_ques` WHERE `fb_ques_enable` = 1 AND `client_id` = '$client_id' AND `outlet_id` = '$outlet_id'");
		while($que_row = mysql_fetch_array($que_qry)) {
			$ques_for = $que_row['fb_ques_for'];
			$fb_ques_id = $que_row['fb_ques_id'];
			$fb_ques = $que_row['fb_ques'];
			if($ord_src == 'Outlet') {
				if ($ques_for == 'Both' || $ques_for == 'Dine-In') {
					$feedback_details .= '<div class="form-group"><div class="row form-label"><div class="col-lg-6 col-md-4 col-sm-4 col-xs-12">';
					$feedback_details .= '<label id="fb_topic_name<?php if($fb_ques_id != 1 && $fb_ques_id != 2) { echo $fb_ques_id; }?>" style="font-weight: 800;">'.ucwords($fb_ques).'</label>';
					$feedback_details .= '</div></div><div class="row"><div class="cc-selector callback col-lg-12 col-md-12 col-sm-12 col-xs-12">';
					
					// echo "SELECT * FROM `feedback_ques_details` WHERE `outlet_id` = '$outletid'";
		//					$fb_qry = mysql_query("SELECT * FROM `feedback_ques_details` WHERE `outlet_id` = '$UnitId'");
					$fb_qry = mysql_query("SELECT * FROM `feedback_ques_details` WHERE `outlet_id` = 1");
					while($fb_row = mysql_fetch_array($fb_qry)) {
						
						$fbinputid = strtolower(str_replace(' ', '_', $que_row['fb_ques']).'_'.str_replace(' ', '_', $fb_row['fb_option_text']));
						$ques_value = strtolower(str_replace(' ', '_', $que_row['fb_ques'])).'#'.$que_row['fb_ques_id'].'-'.$fb_row['fb_option_wt'].'-'.str_replace(' ', '_', $fb_row['fb_option_text']);
						// $fbinputname = strtolower(str_replace(' ', '_', $que_row['fb_ques']));
						// $fb_option_text = $fb_row['fb_option_text'];
						$fb_option_image = $fb_row['fb_option_image'];
						$fboptiontext = ucfirst($fb_row['fb_option_text']);
						
						$feedback_details .= '<div class="selector col-lg-2 col-md-2 col-sm-2 col-xs-2"><input id="'.$fbinputid.'" type="radio" name="call_'.$i.'" value="'.$ques_value.'" /><label class="drinkcard-cc" for="'.$fbinputid.'"><img src="../img/emoji-img/'.$fb_option_image.'"></label><p class="text">'.$fboptiontext.'</p></div>';
						
					}
					$feedback_details .= '<input type="hidden" name="no_ques" value="'.$i.'">';
					$feedback_details .= '</div></div></div>';
				}
			} elseif ($ord_src != 'Outlet') {
				if ($ques_for == 'Both' || $ques_for == 'Delivery') {
					$feedback_details .= '<div class="form-group"><div class="row form-label"><div class="col-lg-6 col-md-4 col-sm-4 col-xs-12">';
					$feedback_details .= '<label id="fb_topic_name<?php if($fb_ques_id != 1 && $fb_ques_id != 2) { echo $fb_ques_id; }?>">'.ucwords($fb_ques).'</label>';
					$feedback_details .= '</div></div><div class="row"><div class="cc-selector callback col-lg-12 col-md-12 col-sm-12 col-xs-12">';
					
					//					$fb_qry = mysql_query("SELECT * FROM `feedback_ques_details` WHERE `outlet_id` = '$UnitId'");
					$fb_qry = mysql_query("SELECT * FROM `feedback_ques_details` WHERE `outlet_id` =1");
					while($fb_row = mysql_fetch_array($fb_qry)) {
						$fbinputid = strtolower(str_replace(' ', '_', $que_row['fb_ques']).'_'.str_replace(' ', '_', $fb_row['fb_option_text']));
						$ques_value = strtolower(str_replace(' ', '_', $que_row['fb_ques'])).'#'.$que_row['fb_ques_id'].'-'.$fb_row['fb_option_wt'].'-'.str_replace(' ', '_', $fb_row['fb_option_text']);
						// $fbinputname = strtolower(str_replace(' ', '_', $que_row['fb_ques']));
						// $fb_option_text = $fb_row['fb_option_text'];
						$fb_option_image = $fb_row['fb_option_image'];
						$fboptiontext = ucfirst($fb_row['fb_option_text']);

						$feedback_details .= '<div class="selector col-lg-2 col-md-2 col-sm-2 col-xs-2"><input id="'.$fbinputid.'" type="radio" name="call_'.$i.'" value="'.$ques_value.'" /><label class="drinkcard-cc" for="'.$fbinputid.'"><img src="../img/emoji-img/'.$fb_option_image.'"></label><p class="text">'.$fboptiontext.'</p></div>';
						
					}
					/*$feedback_details .= '<input type="hidden" name="no_ques" value="'.$i.'">';*/
					$feedback_details .= '</div></div></div>';
				}
			}
			$i++;
		}
		
		$no_ques = $i-1;
		$feedback_details .= '<input type="hidden" name="no_ques" id="no_ques_id" value="'.$no_ques.'">';
		$feedback_details .= "<div class=\"form-group\"><label>Email</label><input type=\"text\" name=\"cbemail\" id=\"cbemail\" class=\"form-control\" placeholder=\"Enter your email\"></div><div class=\"row\"><div class=\"col-lg-6\"><div class=\"form-group\"><label>Birthday</label><input type=\"text\" name=\"cbdob\" id=\"cbdob\" readonly class=\"form-control\"  placeholder=\"Select your birthday\"></div></div><div class=\"col-lg-6\"><div class=\"form-group\"><label>Anniversary</label><input type=\"text\" name=\"cbanniversary\" id=\"cbanniversary\" readonly class=\"form-control\"  placeholder=\"Select your anniversary date\"></div></div></div><div class=\"form-group\"><label>Comments</label><textarea class=\"form-control\" name=\"cbcomments\" id=\"cbcomments\" placeholder=\"Comments please...\" rows=\"5\"></textarea></div><div class=\"form-group\"><input type=\"hidden\" name=\"cbSubmit\" value=\"cbSubmit\"><input type=\"submit\" class=\"btn btn-success btn-save-setting\" name=\"cbSubmit\" id=\"btnsubmit\" value=\"Save Fedback\"></div></form></div></div>";
		
	}
	echo $feedback_details; 
	
}
 ?>

 <script type="text/javascript">
  
var request;

$("#frm_callback").submit(function(event){

    event.preventDefault();
    
    if (request) {
        request.abort();
    }

    var no = document.getElementById('no_ques_id').value;
		for(var i=1; i<=no; i++){
			var q = $('input[name=call_'+i+']:checked').val();
			if(q==undefined || q=='undefined'){
				// alert('Please select Question No. '+ i);
				alert('Please provide feedback for all questions.');
				return false;
			}
		}

    var $form = $(this);

    /*Let's select and cache all the fields*/
    var $inputs = $form.find("input, select, button, textarea");

    /*Serialize the data in the form*/
    var serializedData = $form.serialize();

   /* Let's disable the inputs for the duration of the Ajax request.
    Note: we disable elements AFTER the form data has been serialized.
    Disabled form elements will not be serialized.*/
    $inputs.prop("disabled", true);

    /*Fire off the request to /form.php*/
    request = $.ajax({
        url: "save_fb_settings_data.php",
        type: "post",
        data: serializedData
    });

    /*Callback handler that will be called on success*/
    request.done(function (response, textStatus, jqXHR){
        /*Log a message to the console*/
       /* console.log(response);
        console.log("Hooray, it worked!");*/
        var data = "<img src=\"../img/done.png\">";        
        $('#customer-cb-detail').html(data);	
        /* Auto Load Data */
        feedbackCallData('callback', document.getElementById('outlet').value, document.getElementById('callbackdatepick').value )
    });

    /*Callback handler that will be called on failure*/
    request.fail(function (jqXHR, textStatus, errorThrown){
        /*Log the error to the console*/
        alert("The following error occurred: "+
            textStatus, errorThrown);
        /*console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );*/
    });

    /*Callback handler that will be called regardless
    if the request failed or succeeded*/
    request.always(function () {
        /*Reenable the inputs*/
        $inputs.prop("disabled", false);
    });

});

 </script>
