<?php

    include "config.php";
	include "functions.php";
	
    makeconnection();

    $aColumns = array('customer_lastname', 'customer_firstname', 'customer_company', 'customer_tel', 'customer_email');

    $sLimit = "";
    if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1')
    {
        $sLimit = "LIMIT " . intval($_GET['iDisplayStart']) . ", " .
            intval($_GET['iDisplayLength']);
    }

    $sOrder = "";
    if (isset($_GET['iSortCol_0']))
    {
        $sOrder = "ORDER BY  ";
        for ($i = 0; $i < intval($_GET['iSortingCols']); $i++)
        {
            if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true")
            {
                $sOrder .= "`" . $aColumns[intval($_GET['iSortCol_' . $i])] . "` " .
                    ($_GET['sSortDir_' . $i] === 'asc' ? 'asc' : 'desc') . ", ";
            }
        }

        $sOrder = substr_replace($sOrder, "", -2);
        if ($sOrder == "ORDER BY")
        {
            $sOrder = "";
        }
    }

    $sWhere = "";
    if (isset($_GET['sSearch']) && $_GET['sSearch'] != "")
    {
        $sWhere = "WHERE (";
        for ($i = 0; $i < count($aColumns); $i++)
        {
            if (isset($_GET['bSearchable_' . $i]) && $_GET['bSearchable_' . $i] == "true")
            {
                $sWhere .= "`" . $aColumns[$i] . "` LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR ";
            }
        }
        $sWhere = substr_replace($sWhere, "", -3);
        $sWhere .= ')';
    }

    /* Individual column filtering */
    for ($i = 0; $i < count($aColumns); $i++)
    {
        if (isset($_GET['bSearchable_' . $i]) && $_GET['bSearchable_' . $i] == "true" && $_GET['sSearch_' . $i] != '')
        {
            if ($sWhere == "")
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= "`" . $aColumns[$i] . "` LIKE '%" . mysql_real_escape_string($_GET['sSearch_' . $i]) . "%' ";
        }
    }


    $sQuery = "SELECT SQL_CALC_FOUND_ROWS * FROM `customer`
		$sWhere
		$sOrder
		$sLimit
		";
    $rResult = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());

    /* Data set length after filtering */
    $sQuery = "SELECT FOUND_ROWS()";
    $rResultFilterTotal = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());
    $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
    $iFilteredTotal = $aResultFilterTotal[0];

    /* Total data set length */
    $sQuery = "SELECT COUNT(`customer_id`) FROM `customer`";
    $rResultTotal = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());
    $aResultTotal = mysql_fetch_array($rResultTotal);
    $iTotal = $aResultTotal[0];


    /*
     * Output
     */
    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );

    while ($aRow = mysql_fetch_array($rResult))
    {
        $row = array();
        
        $customer_id = $aRow["customer_id"];
        
        $row[] = '<a href="./?p=customer_add&customer_id='.$customer_id.'">'.$aRow["customer_lastname"].'</a>';
		$row[] = $aRow["customer_firstname"];        
        $row[] = $aRow["customer_company"];
        $row[] = $aRow["customer_tel"];
        $row[] = $aRow["customer_email"];
        $row[] = '<a href="./?p=case&customer_id='.$customer_id.'" class="btn btn-xs default btn-editable"><i class="fa fa-pencil"></i> Cases </a>';
        $row[] = '<input type="checkbox" name="chk_customer_id[]" class="checkboxes" value="'.$customer_id.'"/>';

        $output['aaData'][] = $row;
    }

    echo json_encode($output);
    