<?php
include("../db.php");
$type = $_POST['type'];
$requestData= $_REQUEST;
$columns = array( 
	0 =>  'id_ads',
	1 =>  'name_ads',
    3 =>  'none_ads'
);
if($type == "s"){
    $sql = "
    SELECT * FROM `ads`
    where `active_ads` = '0'
    ";
}else{
    $sql = "
    SELECT * FROM `ads`
    where `active_ads` = '0' and `type_ads` = '$type'
    ";
}

$query=mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
if($type == "s"){
    $sql = "
    SELECT * FROM `ads`
    where `active_ads` = '0'
    ";
}else{
    $sql = "
    SELECT * FROM `ads`
    where `active_ads` = '0' and `type_ads` = '$type'
    ";
}
if( !empty($requestData['search']['value']) ) {  
	$sql.=" AND ( id_ads LIKE '".$requestData['search']['value']."%'  ";
	$sql.=" OR name_ads LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR none_ads LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR paltform_ads 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();
$i = 0;
while( $row=mysqli_fetch_array($query)) {
    $i = $i +1;
    if($row['paltform_ads'] == 0){
        $name_platform = "FACEBOOK";
    }
    if($row['paltform_ads'] == 1){
        $name_platform = "instagram";
    }
    if($row['paltform_ads'] == 2){
        $name_platform = "tiktok";
    }
    if($row['paltform_ads'] == 3){
        $name_platform = "GOOGLE";
    }


    if($row['type_ads'] == 0){
        $type_ads = "غير نشط";
        $btn_type = "<button type='button' class='btn btn-dark '  onclick='start_ads(".$row['id_ads'].")'>تشغيل </button>";

    }
    if($row['type_ads'] == 1){
        $type_ads = " نشط";
        $btn_type = "<button type='button' class='btn btn-danger'  onclick='end_ads(".$row['id_ads'].")'>انتهاء</button>";
    }

   
	$nestedData=array(); 
	$nestedData[] = $i;
	$nestedData[] = $row["name_ads"];
	$nestedData[] = $row["none_ads"];
	$nestedData[] = $name_platform;
	$nestedData[] = $type_ads;
;
	$nestedData[] = "<button type='button' class='btn btn-danger' onclick=del_table_1('".$row['id_ads']."',1)>حذف</button>
	<button type='button' class='btn btn-success'  onclick='get_ads(".$row['id_ads'].")'>تعديل</button>
	".$btn_type;
	
	$data[] = $nestedData;
}

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

echo json_encode($json_data);

?>



