<?php require_once("../../includes/initialize.php");
	  sec_session_start(); 
      require_once("../logincheck.php");
    $x = 1;
?>
<?php
        $where = "";
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 * Easy set variables
	 */

	/* Array of database columns which should be read and sent back to DataTables. Use a space where
	 * you want to insert a non-database field (for example a counter or static image)
	 */
	$arraycols = array('sr_no', 'name', 'area', 'state', 'addedon', 'actions');

	/* Indexed column (used for fast and accurate table cardinality) */
	$indexcols = "id";

	/* DB table to use */
	$tablename = "customer";

	/*
	 * Paging
	 */
	$limit = "";
	if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength']!='-1')
	{
		$limit = "LIMIT ".mysqli_real_escape_string($con, $_GET['iDisplayStart']).", ".mysqli_real_escape_string($con, $_GET['iDisplayLength']);
	}


	/*
	 * Ordering
	 */
	$orderby = "";
	if(isset($_GET['iSortCol_0']))
	{
            $orderby = "ORDER BY  ";
            for($i=0;$i<intval($_GET['iSortingCols']);$i++)
            {
                if($_GET['bSortable_'.intval($_GET['iSortCol_'.$i])]=="true")
                {
                    if($arraycols[ intval($_GET['iSortCol_'.$i])] == "name")
                    {
                        $orderby .= "name ".mysqli_real_escape_string($con, $_GET['sSortDir_'.$i]).", ";
                    } 
                    else
                    {
                        $orderby .= $arraycols[ intval($_GET['iSortCol_'.$i])]." ".mysqli_real_escape_string($con, $_GET['sSortDir_'.$i]).", ";
                    }
                }
            }

            $orderby = substr_replace( $orderby, "", -2 );
            if($orderby == "ORDER BY")
            {
                $orderby = "";
            }
	}


	/*
	 * Filtering
	 * NOTE this does not match the built-in DataTables filtering which does it
	 * word by word on any field. It's possible to do here, but concerned about efficiency
	 * on very large tables, and MySQL's regex functionality is very limited
	 */
	
	if (isset($_GET['sSearch']) && $_GET['sSearch'] != "")
	{
            $where = "WHERE (";
            for($i=0;$i<count($arraycols);$i++)
            {
                    if(isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true")
                    {
                        if($arraycols[$i] == "name")
                        {
                            $where .= "name LIKE '%".mysqli_real_escape_string($con, $_GET['sSearch'])."%' OR ";
                        }                        
                        else
                        {
                            $where .= $arraycols[$i]." LIKE '%".mysqli_real_escape_string($con, $_GET['sSearch'])."%' OR ";
                        }
                    }
            }
            $where = substr_replace( $where, "", -3 );
            $where .= ')';
	}
        
//        if($where == "")
//        {
//            $where .= "WHERE s.batch_id = b.id AND s.scheduled_by_manager_id = m.id";
//        }
//        else
//        {
//            $where .= " AND s.batch_id = b.id AND s.scheduled_by_manager_id = m.id";
//        }

	/* Individual column filtering */
	for($i=0;$i<count($arraycols);$i++)
	{
            if(isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '')
            {
                if($where == "")
                {
                    $where = "WHERE ";
                }
                else
                {
                    $where .= " AND ";
                }
                
                if($arraycols[$i] == "name")
                {
                    $where .= "name LIKE '%".mysqli_real_escape_string($con, $_GET['sSearch_'.$i])."%' ";
                }                
                else
                {
                    $where .= $arraycols[$i]." LIKE '%".mysqli_real_escape_string($con, $_GET['sSearch_'.$i])."%' ";
                }
            }
	}

	/*
	 * SQL queries
	 * Get data to display
	 */
	$sql = "SELECT SQL_CALC_FOUND_ROWS id, name, area, state, addedon
		FROM  $tablename
                $where
		$orderby
		$limit";
    echo $sql; exit;
	$result = mysqli_query($con, $sql);

	/* Data set length after filtering */
	$sqlcnt = "SELECT FOUND_ROWS()";
	$resultcnt = mysqli_query($con, $sqlcnt);
	$myrowcnt = mysqli_fetch_array($resultcnt);
	$filteredtotal = $myrowcnt[0];

	/* Total data set length */
	$sqlcnt = "SELECT COUNT(id) FROM $tablename";
	$resultcnt = mysqli_query($con, $sqlcnt);
	$myrowcnt = mysqli_fetch_array($resultcnt);
	$totalcnt = $myrowcnt[0];


	/*
	 * Output
	 */
	$output = array(
		"sEcho" => intval($_GET['sEcho']),
		"iTotalRecords" => $totalcnt,
		"iTotalDisplayRecords" => $filteredtotal,
		"aaData" => array()
	);
        $cnt = $_GET['iDisplayStart'];
	while($myrow = mysqli_fetch_array($result))
	{
            $cnt++;
            $record = array();


            for($i=0;$i<count($arraycols);$i++)
            {
                if($arraycols[$i] == "sr_no")
                {
                    $sr_no = $x;
                    $record[] = $sr_no;
                    $x++;
                }
                elseif($arraycols[$i] == "name")
                {                    
                    $name = ucwords($myrow["name"]);
                    $record[] = $name;
                }
                elseif($arraycols[$i] == "area")
                {
                    $area = $myrow["area"];
                    $record[] = $area;
                }
                elseif($arraycols[$i] == "state")
                {
                    $state = $myrow["state"];
                    $record[] = $state;
                }
                elseif($arraycols[$i] == "addedon")
                {
                    $addedon = $myrow["addedon"];                   
                    $record[] = $addedon;
                } 
                elseif($arraycols[$i] == "actions")
                {
                    $actionoption = "test";
                    
                    $record[] = $actionoption;
                }
                else
                {
                    /* General output */
                    $record[] = $myrow[$arraycols[$i]];
                }
            }
            $output['aaData'][] = $record;
	}
	echo json_encode( $output );
?>