<?php 
	include("../config.php");
	include("../Datatable.php");


	$obj = new Datatable($conn);

	$obj->aColumns 		= array('tracking.tracking_id', 'tracking.t_date', 'tracking.lr_no', 'tracking.location', 'tracking.tracking_status', 'tracking.truck_no', 'tracking.follow_up_time', 'tracking.created_id', 'tracking.updated_id');
    $obj->eColumns 		= array('tracking.id');
    $obj->sIndexColumn 	= "tracking.id";
    $obj->sTable 		= "tracking";
    $obj->myWhere 		= "WHERE tracking.branch_id = '".$_SESSION['branch_id']."' AND tracking.company_id = '".$_SESSION['company_id']."' AND DATE(tracking.created_datetime)>='".$_SESSION['from_year']."' AND DATE(tracking.created_datetime)<='".$_SESSION['to_year']."'";
    $obj->datatable_process();

    $cou = 0;
    if(isset($_GET['iDisplayStart'])) {
    	$cou = $_GET['iDisplayStart'];
    }
    $master_user = combine(getAllRecords('users'), 'id', 'name');
    while ($aRow = mysqli_fetch_array($obj->rResult)) {
        $temp_arr = array();
        if($aRow['tracking_status'] == 'P'){
        	$status = '<span class="label label-md label-danger trcking-label">PENDING</span>';
        }else{
        	$status = '<span class="label label-md label-primary trcking-label">CLOSE</span>';
        }

        $temp_arr[] = ++$cou;
        $temp_arr[] = date("d-m-Y", strtotime($aRow['t_date']));
		$temp_arr[] = $aRow['truck_no'];
		$temp_arr[] = $aRow['lr_no'];
		$temp_arr[] = $aRow['location'];
		$temp_arr[] = $status;
        $temp_arr[] = $aRow['follow_up_time'];
		$temp_arr[] = @$master_user[$aRow['created_id']];
        $temp_arr[] = @$master_user[$aRow['updated_id']];
		$str = '';
		if( hasPermission($_SESSION['user_detail']['role_id'], 'tr_edit') ) {
		$str .= '<a href="index.php?page=tracking/edit.php&eid='.$aRow['id'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT TRACKING...!!"><em class="fa fa-pencil"></em></a>&nbsp;';
		}
		
		if( hasPermission($_SESSION['user_detail']['role_id'], 'tr_delete') ) {
		$str .= '<button type="button" class="btn btn-danger delete_data" id="'.$aRow["id"].'" data-toggle="tooltip" data-placement="top" title="DELETE TRACKING..!!" onclick="confirmDelete(this);"><em class="fa fa-trash"></em></button>';
		}
		$temp_arr[] = $str;

        $obj->output['aaData'][] = $temp_arr;
    }

    echo json_encode($obj->output);
    exit();
?>