<?php
error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();  
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload		=	new autoloadClass(); 
$mecon			=	new condbClass();
$requestData	=	$_REQUEST; 
$columns = array(
	0	=>	'Id',
	1	=>	'Id',
	2	=>	'Project',
	3	=>	'Model',
	4	=>	'Promo',
	5	=>	'Amount',
	6	=>	'StartDate',
	7	=>	'EndDate',  
	8	=>	'PromoStatus' 
); 






$mainQuery 		= "SELECT Id, Project, Model, Promo, StartDate, EndDate, Amount, Conditions, PromoStatus, PromoDetails, Status FROM promo";
$sql			= $mainQuery; 
$mecon->query($sql);
$result			= $mecon->resultset(); 
$totalData		= count($result);
$totalFiltered	=	$totalData;
$sql			=	$mainQuery." WHERE 1=1";
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (Id LIKE '%".$ind."%' ";
		$sql.=" OR Project LIKE '%".$ind."%' ";
		$sql.=" OR Model LIKE '%".$ind."%' ";
		$sql.=" OR Promo LIKE '%".$ind."%' ";
		$sql.=" OR UPPER(DATE_FORMAT(StartDate,'%m/%d/%Y')) LIKE '%".$ind."%' ";
		$sql.=" OR UPPER(DATE_FORMAT(EndDate,'%m/%d/%Y')) LIKE '%".$ind."%' ";
		$sql.=" OR Amount LIKE '%".$ind."%' ";
		$sql.=" OR Conditions LIKE '%".$ind."%' ";
		$sql.=" OR PromoStatus LIKE '%".$ind."%' ";
		$sql.=" OR PromoDetails LIKE '%".$ind."%' ";
		$sql.=" OR Status  LIKE '%".$ind."%' )";
	}
}
$mecon->query($sql);
$result = $mecon->resultset(); 
$totalFiltered = count($result); 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   "; 
$mecon->query($sql);
$data = array();
$result = $mecon->resultset();
			 for($a=0; $a<count($result); $a++){
			 $nestedData=array();
				
				 
					$nestedData[]	= '<a href="index.php?pg=promo&type=promo&action=edit&id='.$result[$a]['Id'].'"><span class="btn btn-xs btn-primary glyphicon glyphicon-pencil" title="Update Promo"></span></a>&nbsp;&nbsp;<a href="report/promo_print.php?id='.$result[$a]['Id'].'"><span class="btn btn-xs btn-warning glyphicon glyphicon-print" title="Print Promo"></span></a>&nbsp;&nbsp;<span onclick="deleteTransaction('.$result[$a]['Id'].')" class="btn btn-xs btn-danger glyphicon glyphicon-trash" title="Delete Promo"></span>'; 
				$nestedData[]	= $a+1; 
				$nestedData[] = '<a href="index.php?pg=promo&type=promo&action=view&id='.$result[$a]['Id'].'">'.strtoupper(trim(html_entity_decode(stripslashes($result[$a]['Project'])))).'</a>';
				$nestedData[] = strtoupper(trim(html_entity_decode(stripslashes($result[$a]['Model']))));
				$nestedData[] = strtoupper(trim(html_entity_decode(stripslashes($result[$a]['Promo']))));
				$nestedData[] = number_format($result[$a]['Amount'],2); 
				
				$nestedData[] = (($result[$a]['StartDate']=='1970-01-01') || ($result[$a]['StartDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['StartDate']));
				
				$nestedData[] = (($result[$a]['EndDate']=='1970-01-01') || ($result[$a]['EndDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['EndDate']));

				$nestedData[] = strtoupper(trim(html_entity_decode(stripslashes($result[$a]['PromoStatus']))));
				$data[] = $nestedData;
			} 
$json_data = array(
			"draw"            => intval( $requestData['draw'] ),
			"recordsTotal"    => intval( $totalData ),
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);
echo json_encode($json_data);
exit();
ob_end_flush();