<?php

// error_reporting(E_ALL);
// ini_set('display_errors', 1);

    //open connection to mysql db
include ('../config/phpConfig.php');
    //fetch table rows from mysql db
	
//create an array
    $dataArray = array();
	
	$table = "order_header";
	$offset="";
	$records="";
	$filter="";
	$orderBy = "";
	$offset = $_POST['start'];
	$records = $_POST['length'];
	$filter = $_POST['filter'];
	$orderBy = $_POST['sort'];
	$orderIndex = $_POST['order'][0]['column'];
	$columnName = $_POST['columns'][$orderIndex]['data']; // Column name
	$orderDirection = $_POST['order'][0]['dir']; // asc or desc
	$iFilteredTotal = 0;
	$iTotal = 0;
	
	$orderColumn = array('id','ran_or_order','part_number','serial_reference','to_plt','short_code','txn_qty','from_location_code','to_location_code','customer_reference','last_updated','last_updated_by');
	
	// Total data set length
	$sql = "select TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA = '".$mDbName."' AND table_name='".$table."'";
	$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
	$row = mysqli_fetch_array($result);
	$iTotal = $row[0];

	$timeout = 10;  /* seconds for timeout */
	$realConnection = mysqli_init( );
	$realConnection->options( MYSQLI_OPT_CONNECT_TIMEOUT, $timeout ) ||
		die( 'mysqli_options failed: ' . $link->error );
	$realConnection->real_connect($db,$mDbPassword,$mDbUser,$mDbName) ||
		die( 'mysqli_real_connect failed: ' . $link->error );
	
	$sql = "SELECT order_header.id, order_header.document_reference, customer_reference, order_type, consignee , ship_to, dock_destination, auto_created, auto_confirmed, document_status_code as status, expected_delivery_time, time_slot, order_header.date_created, order_header.created_by, order_header.last_updated_date, order_header.last_updated_by  FROM order_header left join document_status on document_status.id = document_status_id";
	if ($filter != "")
		$sql .= " WHERE ".$filter;
	if ($orderBy != "")
		$sql .= " ORDER BY ".$orderBy;
	elseif(isset($_POST['order']))
	{
		$sql .= " ORDER BY ".$columnName." ".$orderDirection." ";
	}
	else
	{
		$sql .= " ORDER BY date_created DESC";
	}	
		
	if ($records != "")
	{
		$limits = $records+1;
		$sql .= " LIMIT ".$limits;
	}
	else
		$sql .= " LIMIT 25";
	if ($offset != "")
		$sql .= " OFFSET ".$offset;
	else
		$sql .= " OFFSET 0";

    $result = mysqli_query($realConnection, $sql);
	$iFilteredTotal = mysqli_num_rows($result)+$offset;
	if (mysqli_num_rows($result)==0)
	{
//		$dataArray[]=array("id"=>"1","document_reference"=>$sql);
		$dataArray = array();
	}
	else
	{
		$i = 0;
		while($row =mysqli_fetch_array($result))
		{
			$i++;
			if ($i <= $records)
				$dataArray[] = $row;
		}
	}
	$output = array(
		'draw'				=>	intval($_POST['draw']),
		'recordsTotal'		=>	$iTotal,
		'recordsFiltered'	=>	$iFilteredTotal,
		'data'				=>	$dataArray
	);
	
	echo json_encode($output);
    //close the db connection
    mysqli_close($connection);
?>