<?php
include("../db.php");
$requestData= $_REQUEST;
$columns = array( 
	0 =>  'name_cus',
	1 =>  'phone_cus',
    3 =>  'id_scanner'
);
$date_page = date('Y-m-d');
$date_today = $date_page;
$sql = "
select * from `cus` left join `member` on `member`.`id_member` = `cus`.`id_member` where `cus`.`cus_active` = 0 and `cus`.`id_scanner` != ''   and `member`.`type_member` != '0'
";
$query=mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "
select * from `cus` left join `member` on `member`.`id_member` = `cus`.`id_member` where `cus`.`cus_active` = 0 and `cus`.`id_scanner` != '' and `member`.`type_member` != '0'
";
if( !empty($requestData['search']['value']) ) {  
	$sql.=" AND ( name_cus LIKE '%".$requestData['search']['value']."%'  ";
	$sql.=" OR phone_cus LIKE '%".$requestData['search']['value']."%' ";    
	$sql.=" OR id_scanner LIKE '%".$requestData['search']['value']."%' )";    
}
$query=mysqli_query($db, $sql);




$totalFiltered = mysqli_num_rows($query); 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$query=mysqli_query($db, $sql);
$data = array();
while( $row=mysqli_fetch_array($query)) {

    if($row['date_game'] == 0)
{
    $day = 1;
    $data1['day'] = 1;
}
if($row['date_game'] == 1)
{
    $data1['day'] = 7;
}
if($row['date_game'] == 2)
{
    $data1['day'] = 30;
}
if($row['date_game'] == 3)
{
    $data1['day'] = 365;
}
if($row['date_game'] == 4)
{
    $data1['day'] = 90;
}
if($row['date_game'] == 5)
{
    $data1['day'] = 180;
}
if($row['date_game'] == 6)
{
    $data1['day'] = 270;
}
        if($row['date_game'] == 7)
{
    $data1['day'] = 15;
}
if($row['date_game'] == 8)
{
    $data1['day'] = 30;
}

if($row['date_game'] == 9)
{
    $data1['day'] = 21;
}
if($row['date_game'] == 10)
{
    $data1['day'] = 45;
}
if($row['date_game'] == 11)
{
    $data1['day'] = 120;
}
if($row['date_game'] == 12)
{
    $data1['day'] = 150;
}
if($row['date_game'] == 13)
{
    $data1['day'] = 210;
}
if($row['date_game'] == 14)
{
    $data1['day'] = 240;
}
if($row['date_game'] == 15)
{
    $data1['day'] = 300;
}
if($row['date_game'] == 16)
{
    $data1['day'] = 330;
}

$data1['type'] = $row['type_member'];
$data1['id_cus'] = $row['id_cus'];
$id_cus = $row['id_cus'];

if($data1['type'] == 0)
{
    $data1['end_c'] = $row['count_s'];
    $data1['end_c_1'] = $row['count_s'] + $row['count_game'];
}
if($data1['type'] == 1)
{
    $start_date = $row['member_date'];
    $day = $data1['day'];
    $data1['end_d']  = date('Y-m-d', strtotime($start_date. ' + '.$day.' days'));
    $data1['end_d_1']  = date('Y-m-d', strtotime($date_page. ' + '.$day.' days'));
    if($data1['end_d'] > date('Y-m-d'))
    {
        $data1['end_date'] = 1;
    }else{
        $data1['end_date'] = 0;
    }
}
if($data1['type'] == 2)
{
    $start_date = $row['member_date'];
    $day = $data1['day'];
    $data1['end_d']  = date('Y-m-d', strtotime($start_date. ' + '.$day.' days'));
    $data1['end_d_1']  = date('Y-m-d', strtotime($date_page. ' + '.$day.' days'));
    $data1['end_c'] = $row['count_s'];
    $data1['end_c_1'] = $row['count_s'] + $row['count_game'];
    if($data1['end_d'] > date('Y-m-d'))
    {
        $data1['end_date'] = 1;
    }else{
        $data1['end_date'] = 0;
    }
}

	$nestedData=array(); 

    $le_img = strlen($row['img']);
    if($le_img == 0)
    {
        $img = "images/a.png";
    }else{
        $img = $row['img'];
    }
    try {
        if($data1['end_date'] == 1)
        {
            continue;

            $nestedData[] = "end";
            $nestedData[] = "end";
            $nestedData[] = "end";
            $nestedData[] = "end";
            $nestedData[] = "end";
            $nestedData[] = "end";

        }
        if($data1['end_date'] == 0)
        {
            $nestedData[] = $row["name_cus"];
            $nestedData[] = $row["phone_cus"];
            $nestedData[] = $row["id_scanner"];
            $nestedData[] = $row["name_member"];
            $nestedData[] = $data1['end_d'];
            $nestedData[] = "<img src='".$img."' style='width:15%'/>";
            $nestedData[] = "";

        }
           // break;
            
        

    }
    catch (exception $e) {
        //code to handle the exception
    }



	$data[] = $nestedData;
}

$json_data = array(
			"draw"            => intval( $requestData['draw']),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);

echo json_encode($json_data);



?>





