<?php
 
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */
 
// DB table to use


//if user selects all date range than use all view, otherwise use 12 month view

	if(!isset($_COOKIE['job_search_daterange'])) {
		
			if ($_COOKIE['job_search_daterange'] == "allmonths"){

				$table =  $_COOKIE['phxcrmclient_id'] . "_jobs_all_view";
			
			}else{

				$table =  $_COOKIE['phxcrmclient_id'] . "_jobs_view";
			
			}
	
	}else{

			$table =  $_COOKIE['phxcrmclient_id'] . "_jobs_view";

	}

	

// Table's primary key


$primaryKey = 'st_id';
 
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'st_id_link', 'dt' => 0 ),
    array( 'db' => 'sales_date', 'dt' => 1 ),
    array( 'db' => 'comp_name',  'dt' => 2 ),
    array( 'db' => 'job_descr2',   'dt' => 3 ),
    array( 'db' => 'st_rego',     'dt' => 4 ),
	array( 'db' => 'client_order_no',     'dt' => 5 ),
    array( 'db' => 'status',     'dt' => 6 ),
	array( 'db' => 'job_date_start',     'dt' => 7 ),
	array( 'db' => 'job_date_end',     'dt' => 8 ),
	array( 'db' => 'job_ttl_quote',     'dt' => 9 )
);
 
// SQL server connection information
$sql_details = array(
    'user' => 'wealthhe_mybzu',
    'pass' => 'IuGaYVI@b%tD',
    'db'   => 'wealthhe_mybizdb',
    'host' => 'localhost'
);
 //echo "cookie".$_COOKIE['tradesjobsearchcriteria'];
 
 $search = "job_compid = " . $_COOKIE['current_user_outlet'];
 
	if(!isset($_COOKIE['client_job_search_id'])) {
		
		//if no search client selected

		if(!isset($_COOKIE['tradesjobsearchcriteria'])) {
			// no search client and no criteria
		//	$search = "";
		}else{
			// no search client and criteria
			if ($_COOKIE['tradesjobsearchcriteria'] == ""){
				//no search client and criteria empty
		//		$search = "";
			}else{
				//no search client and criteria is there
				$search = $search . " and " . stripslashes($_COOKIE['tradesjobsearchcriteria']);
			}
		}
  
	}else{
		//search client selected

		if(!isset($_COOKIE['tradesjobsearchcriteria'])) {
				//client search selected and criteria not set
			$search = $search . " and uid_client = " . $_COOKIE['client_job_search_id'];
		}else{
			//client search selected and search criteria is set
			if ($_COOKIE['tradesjobsearchcriteria'] == ""){
				$search = $search .  " and uid_client = " . $_COOKIE['client_job_search_id'];
				//client search selected and search criteria set but blank
//				$search = "";
			}else{
				$search = $search . " and uid_client = " . $_COOKIE['client_job_search_id'] . " and " . stripslashes($_COOKIE['tradesjobsearchcriteria']);
			}
		}
  
	}
 
// $search = "";
 
 

// $search = "comp_name like '%a%' and comp_name like '%v%'";
// $search = "status = 'Quote Sent'";
 
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
 
require( 'ssp.class_search.php' );

//echo $search . "<br>";
 
 	if ($search == ""){
		echo json_encode(
		SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
		);
	}else{
		echo json_encode(
		SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, stripslashes($search) )
		);		
	}
	