<?php  
include('includes/config/config.php');
include('includes/auth/auth.php');
include('includes/functions.php');include('access.php');
// error_reporting(E_ERROR | E_PARSE);
include('log_entry.php');
?>
<?php  

// Connect to mysqli database
$page = 1; // The current page
$sortname = 'comm_date  '; // Sort column
$sortorder = 'desc'; // Sort order
$qtype = ''; // Search column
$query = ''; // Search string
$searchSql=""; 
// Get posted data
if (isset($_POST['page'])) {
        $page = @mysqli_real_escape_string($conn,$_POST['page']);
}
if (isset($_POST['sortname'])) {
        $sortname = @mysqli_real_escape_string($conn,$_POST['sortname']);
}
if (isset($_POST['sortorder'])) {
        $sortorder = @mysqli_real_escape_string($conn,$_POST['sortorder']);
}
if (isset($_POST['qtype'])) {
        $qtype = @mysqli_real_escape_string($conn,$_POST['qtype']);
}
if (isset($_POST['query'])) {
        $query = @mysqli_real_escape_string($conn,$_POST['query']);
}
if (isset($_POST['rp'])) {
        $rp = @mysqli_real_escape_string($conn,$_POST['rp']);
}
// Setup sort and search SQL using posted data
$hasReadAccess = isAccessible($_SESSION['RoleId'],$menu_key,'R');
$hasWriteAccess = isAccessible($_SESSION['RoleId'],$menu_key,'W');
$hasExecuteAccess = isAccessible($_SESSION['RoleId'],$menu_key,'E');
 if($qtype=='comm_date'){
    
     $searchSql = " where date_format(date(comm_date),'%Y-%m-%d')=str_to_date('$query','%d/%m/%Y') ";
}else if($qtype=='ambulance_date_between'){
    $query = explode ("-", $query);
    $searchSql = ($qtype != '' && $query != '') ? "where  upper(waste_gen_date) between  STR_TO_DATE('".trim ( $query[0] )."','%d/%m/%Y') and STR_TO_DATE('".trim ( $query[1] )."','%d/%m/%Y')"  : '';
    
}else if($qtype=='ambulance_from'){
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
}else if($qtype=='ambulance_to'){
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
}else if($qtype=='ambulance_used'){
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
}else if($qtype=='ambulance_duty'){
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
}else{
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
    
}
$sortSql = "order by $sortname $sortorder";
$searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%$query%')" : '';
// Get total count of records
$sql = "select count(*) from covid_questionaire   ";
$result = @mysqli_query($conn,$sql);
$total = @mysqli_num_rows($result);
// Setup paging
if(!isSet($rp)){
	$rp=10;
}
$pageStart = ($page-1)*$rp;
$limitSql = "limit $pageStart, $rp";
// Return JSON data
$data = array();
$data['page'] = $page;
$data['total'] = $total;
$data['rows'] = array();
$sql_covidquestion = "select * from covid_questionaire ";
//echo $sql_communication;
$results_covidquestion = @mysqli_query($conn,$sql_covidquestion );
$count=($page-1)*$rp+1;

//echo $;
//echo $access_level;

while ($row = @mysqli_fetch_assoc($results_covidquestion)) {
	$modified_by=$row['modified_by'];
	$question_id=$row['question_id'];
	$question_hindi=$row['question_hindi'];
	$answer_status=$row['answer_status'];
    $score=$row['score'];

	$sql_covid= "select * from covid_questionaire where question_id ='".$question_id."' ";

	$results_covid= @mysqli_query($conn,$sql_covid );
//echo $sql_communication_details;

while ($row_details = @mysqli_fetch_assoc($results_covid)) {
	// if(isset($row_details['details']) && $row_details['details']!='' ){
	// 	$communication_info.= '<b>'.getTableFieldValue('communication','com_item','communication_id',$row_details['communication_id']).'</b>::'.$row_details['details'].'<br>';
	// }
}

//echo $;
$view_link="";
$edit_link="";
$delete_link="";
$links="";


// $current_date = date("Y-m-d H:i:s");
// error_log("current date".$current_date);
// $timestamp1 = strtotime($current_date);
// error_log("time 1 date".$timestamp1 );
// error_log("comm_date ".$comm_date);
// $timestamp2 = strtotime($comm_date);
// error_log("time 2 date".$timestamp2  );
// $tim_diff = round(($timestamp1-$timestamp2)/3600,1);

// error_log("Actusal hhhhhhhhhhhhhhhh".$tim_diff);




if($hasReadAccess)
{
	//echo "";
$view_link="<a  href=\"#\"class=\"green\"  onclick=\"open_covid_questionaire('".$question_id."','V');\"><i class=\"ace-icon fa fa-search-plus bigger-130\"></i></a>";
}

if($hasWriteAccess )
{
$edit_link="<a href=\"#\" class=\"blue\" onclick=\"open_covid_questionaire('".$question_id."','E');\"><i class=\"ace-icon fa fa-pencil bigger-130\"></i></a>";
    
   }

if($hasExecuteAccess)
{
$delete_link="<a href=\"#\" class=\"blue\" onclick=\"delete_covid('".$question_id."');\"><i class=\"ace-icon fa fa-trash-o bigger-130\"></i></a>";
}

$space="&nbsp;&nbsp;&nbsp;";
$links = $assign_link.$space.$view_link.$space.$edit_link.$space.$delete_link;

$data['rows'][] = array(
			'question_id' => $row ['question_id'],
			'cell' => array (
			        
					$count ++,
					
					$question_hindi,$answer_status,$score,$links 

                  )					
					
			) ;
			
}
// error_log(print_r($data, true));
echo json_encode($data);
?>