<?php
 include "../../pidbco/pidbjoin.php";
 // storing  request (ie, get/post) global array to a variable
 $requestData = $_REQUEST;

$columns = array(
// datatable column index  => database column name
    0 => 'entered_date',
    1 => 'comp_name',
    2 => 'phone',
    3 => 'entered_date',
    4 => 'follow_up'
);

// getting total number records without any search
$sql = "SELECT t.`id`, t.`entered_date`, t.`entered_time`, t.`comp_name`, t.`person_name`, t.`phone`, t.`phone2`, t.`location`, t.`bdo`, t.`executive_bdo`, t.`email`, t.`status`, t.`executive_status`, t.`follow_up`,t.`executive_follow_up`, t.`cust_or_not`, t.`remark`, e.`emp_name` FROM `tele_calls_det` as t
LEFT JOIN  `employee_registration` as e
ON t.`bdo` = e.`id` ORDER BY t.`id` DESC";
$query=mysqli_query($con, $sql) or die("telecalls-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.

$sql = "SELECT t.`id`, t.`entered_date`, t.`entered_time`, t.`comp_name`, t.`person_name`, t.`phone`, t.`phone2`, t.`location`, t.`bdo`, t.`executive_bdo`, t.`email`, t.`status`, t.`executive_status`, t.`follow_up`,t.`executive_follow_up`, t.`cust_or_not`, t.`remark`,e.`emp_name` FROM `tele_calls_det` as t
LEFT JOIN  `employee_registration` as e
ON t.`bdo` = e.`id` ";

if( !empty($requestData['search']['value']) )
{

 // if there is a search parameter, $requestData['search']['value'] contains search parameter
    $sql.=" WHERE ( entered_date LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR emp_name LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR comp_name LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR person_name LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR phone LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR entered_date LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR follow_up LIKE '".$requestData['search']['value']."%' ) ";
}
$query=mysqli_query($con, $sql) or die("telecalls-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
//$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$sql.=" ORDER BY id DESC  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */
$query=mysqli_query($con, $sql) or die("telecalls-grid-data.php: get employees");

$data = array();
while( $row=mysqli_fetch_object($query) )
{  // preparing an array

$calid = $row->id;
$sql2="SELECT report FROM `tele_reports` WHERE `telecall_id`='$calid' ";
$query2=mysqli_query($con, $sql2);
$row2=mysqli_fetch_object($query2);



    $fw = $row->follow_up;
    list($year,$month,$day) = explode('-', $fw);

    $en = $row->entered_date;
    list($eyear,$emonth,$eday) = explode('-', $en);

    $s="";
    if($row->status == 1)
    {
        $s = 'Interested';
    }
    elseif($row->status == 2)
    {

        $s = 'Not-Interested';
    }
    elseif($row->status == 3)
    {
        $s =  'Future';
    }
    elseif($row->status == 4)
    {
        $s = 'Visit Scheduled';
    }


    if($row->status == 2 || empty($row->status))
    {

       $check_box = '<input type="checkbox" class="checkbox"  id="'.$row->id.'" style="cursor: pointer;" />';

    }
    elseif($row->cust_or_not == 1)
    {

        $check_box = '<input disabled type="checkbox" class="checkbox"  id="" style="cursor: pointer;" />';

    }
    else
    {

        $check_box = '<input disabled type="checkbox" class="checkbox"  id="" style="cursor: pointer;" />';

    }


    $nestedData=array();

    $nestedData[] = $check_box;
    $nestedData[] = '<span style=" cursor:pointer; " onclick="edt_tele_call('.$row->id.');mylinkfunction();"><a title="'.$row2->report.'" style=" text-decoration:none;" >'.$en.'</a></span>';
    $contnt = '<a data-toggle="modal" data-target=".telecalls'.$row->id.'" style="cursor: pointer;text-decoration: none;">'.$row->emp_name.'</a>
     <div class="modal fade telecalls'.$row->id.'" tabindex="-1" role="dialog" aria-hidden="true">
												<div class="modal-dialog">
													<div class="modal-content">

														<div class="modal-header">
															<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span>
															</button>
															
															<h4 class="modal-title" id="myModalLabel">#'.$row->id.'</h4>
														</div>
														<div class="modal-body">
															<table id="datatable-fixed-header" class="table table-striped table-bordered">
																<tr>
																	<td>Company</td>
																	<td>'.$row->comp_name.'</td>
																</tr>
																<tr>
																	<td>Contact Person</td>
																	<td>'.$row->person_name.'</td>
																</tr> 
																
																<tr>
																	<td>Phone</td>
																	<td>'.$row->phone.'</td>
																</tr>
																<tr>
																	<td>Phone2</td>
																	<td>'.$row->phone2.'</td>
																</tr>
																<tr>
																	<td>Email</td>
																	<td>'.$row->email.'</td>
																</tr>
																<tr>
																	<td>Location</td>
																	<td>'.$row->location.'</td>
																</tr>
																<tr>
																	<td>Date</td>
																	<td>'.$en.'&nbsp;'.$row->entered_time.'</td>
																</tr> 
																<tr>
																	<td>Telecaller</td>
																	<td>'.$row->emp_name.'</td>
																</tr> 
																
																<tr>
																	<td>Last Follow</td>
																	<td>'.$fw.'</td>
																</tr>
																<tr>
																	<td>Status</td>
																	<td>'.$s.'</td>
																</tr></table>';
															
$sql22="SELECT tele_bdo_id,entered_date,report FROM `tele_reports`  WHERE `telecall_id`='$calid' ";
$query22=mysqli_query($con, $sql22);
while($row22=mysqli_fetch_array($query22)){
	$tele_bdo_id  = $row22['tele_bdo_id'];
	$entered_date2 = $row22['entered_date'];
	$report = $row22['report'];
	
	$sql5 = "SELECT `emp_name` FROM `employee_registration` WHERE `id`='$tele_bdo_id' ";
	$qry1 = mysqli_query($con, $sql5);
	$res1 = mysqli_fetch_array($qry1);
	$telecaller = $res1['emp_name'];
												
			 $contnt.= '<div style="margin-top: 20px;">
              <span style="font-style: italic; "><small class="label label-danger"><i class="fa fa-clock-o"></i>'.$entered_date2.'</small>&nbsp; '.$telecaller.'&nbsp;'.Says.'&nbsp;'.$report.'</span>
              <br>
            </div>';
}
														 $contnt.= '</div>
														<div class="modal-footer">
															<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
														</div>

													</div>
												</div>
											</div>';
  	$nestedData[] = $contnt;										
    $nestedData[] = '<span style=" cursor:pointer; " onclick="edt_tele_call('.$row->id.');mylinkfunction();">'.$row->comp_name.'</span>';
    $nestedData[] = '<span style=" cursor:pointer; " onclick="edt_tele_call('.$row->id.');mylinkfunction();">'.$row->person_name.'</span>';
    $nestedData[] = '<span style=" cursor:pointer; " onclick="edt_tele_call('.$row->id.');mylinkfunction();">'.$row->phone.'</span>';
    $nestedData[] = '<span style=" cursor:pointer; " onclick="edt_tele_call('.$row->id.');mylinkfunction();">'.$fw.'</span>';
    if($s == 'Interested'){
      $nestedData[] = '<span style=" cursor:pointer; color:red;font-size:15px; " onclick="edt_tele_call('.$row->id.');mylinkfunction();"><a title="'.$row2->report.'"  style=" text-decoration:none;" >'.$s.'</a></span>';
     } else if($s == 'Future'){ 
      $nestedData[] = '<span style=" cursor:pointer;color:#0000FF;font-size:15px; " onclick="edt_tele_call('.$row->id.');mylinkfunction();"><a title="'.$row2->report.'"  style=" text-decoration:none;" >'.$s.'</a></span>';
     } else if($s == 'Visit Scheduled'){
      $nestedData[] = '<span style=" cursor:pointer;color:#006400;font-size:15px; " onclick="edt_tele_call('.$row->id.');mylinkfunction();"><a title="'.$row2->report.'"  style=" text-decoration:none;" >'.$s.'</a></span>';
     } else if($s == 'Not-Interested'){
      $nestedData[] = '<span style=" cursor:pointer;color:#8B008B;font-size:15px; " onclick="edt_tele_call('.$row->id.');mylinkfunction();"><a title="'.$row2->report.'"  style=" text-decoration:none;" >'.$s.'</a></span>';
     }
 
    $data[] = $nestedData; ?>


                          
<?php

}



$json_data = array(
    "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
    "recordsTotal"    => intval( $totalData ),  // total number of records
    "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
    "data"            => $data   // total data array
);

echo json_encode($json_data);  // send data as json format

?>