<?php
// Include DB file for credentials and database connection 
include("db.php");

error_reporting(E_ALL);

/* Validate Request */
include("auth_api.php");

if ($_SERVER['REQUEST_METHOD'] === 'PUT') {

$data = json_decode(file_get_contents('php://input'), true);			



/* $stmt=$conn->prepare("SELECT * FROM `tbl_videos_ve` v inner join `tbl_videos_ve_domain` d on d.id=v.domain inner join `tbl_videos_ve_cat` c on c.id=v.cat_id where 1 = 1 AND  d.name = ? AND  v.country = ? AND v.average_rating = ? ") */;


 
$sql = "SELECT v.id as id, v.title as title,  v.client_name as client_name, v.country as country, v.youtube_video_id as youtube_video_id,c.name as cat_id,
 v.status as status ,v.company_name as company_name, v.designation as designation, v.city as city, v.state as state, d.name as domain, v.average_rating as average_rating,  v.creation_date as creation_date  FROM `tbl_videos_ve` v Left join `tbl_videos_ve_domain` d on d.id=v.domain Left join `tbl_videos_ve_cat` c on c.id=v.cat_id Left join tbl_videos_ve_tags_assigned va on va.video_id=v.id Left join tbl_videos_ve_tags t on t.id=va.tag_id  where 1 = 1 ";
  
/*   $sql = "SELECT * FROM `tbl_videos_ve` v inner join `tbl_videos_ve_domain` d on d.id=v.domain inner join `tbl_videos_ve_cat` c on c.id=v.cat_id 
  inner join `tbl_videos_ve_tags` T on T.created_by=d.created_by where 1 = 1 "; */

 
        if($data['clientName'] != '') {
			  $sql .= "   AND  ( d.name= '".$data['clientName']."' OR  v.country= '".$data['clientName']."' OR v.title = '".$data['clientName']."' OR v.client_name = '".$data['clientName']."' OR v.creation_date = '".$data['clientName']."' ) ";
		}

		if($data['domainName'] != '' && $data['domainName'] != '') {
		  $sql .= "   AND  d.name= '".$data['domainName']."' ";
		}

		if($data['country'] != '' && $data['country'] != '') {
		  $sql .= "   AND  v.country= '".$data['country']."' ";
		}

		if($data['rating'] != '' && $data['rating'] != '') {
			if($data['rating']==4){
			$sql .= "   AND  v.average_rating>='".$data['rating']."'";
			}
			else{
				$sql .= "   AND  IFNULL(v.average_rating,-1) = '".$data['rating']."'";
			}
		  
		}




// Year Front END Wrong Backend Correct
// SELECT * FROM tbl_videos_ve where creation_date like '%2021%'


//giving wrong record for 4 and above rating and no rating 

//select `average_rating` from tbl_videos_ve where IFNULL(`average_rating`,-1) = -1


//select `average_rating` from tbl_videos_ve where `average_rating`>= 4






	
		if($data['creationDate'] != '') {
		  $sql .= "   AND  v.creation_date like '%".$data['creationDate']."%'";
		}
		
		 if($data['catName'] != '') {
		  $sql .= "   AND  c.name = '".$data['catName']."'";
		} 
		
		 if($data['tags'] != '') {
		  $sql .= "   AND  t.title= '".$data['tags']."'";
		}
	/*
		if($data['rating'] != '' && $data['rating'] != '') {
		  $sql .= "   AND  v.tags= '".$data['tags']."'";
		}
		if($data['rating'] != '' && $data['rating'] != '') {
		  $sql .= "   AND  v.tags= '".$data['creationDate']."'";
		} */
		
		//echo $sql;
		//die;
		$res = $conn->query($sql);
   
		   $i = 1;
				foreach ($res as $row) {
					
				   $items[] = ["<tr><td>" .$i. "</td><td>" . $row['title']. "</td><td>" . "https://www.youtube.com/watch?v=".$row['youtube_video_id']. "</td><td>" . $row['cat_id']. "</td><td>" . $row['client_name']. "</td><td>" . $row['company_name']. "</td><td>" . $row['designation']. "</td><td>" . $row['city']. "</td><td>" . $row['state']. "</td><td> ".$row['country']." </td><td>" . $row['domain']. "</td><td>" . $row['status']. "</td><td>" . $row['average_rating']. "</td><td>" . $row['creation_date']. "</td></tr>"];
				   $i++;


/* 				   $items[] = ["<tr><td>" .$row['id']. "</td><td>" . $row['title']. "</td><td>" . "https://www.youtube.com/watch?v=".$row['youtube_video_id']. "</td><td>" . $row['cat_id']. "</td><td>" . $row['quote']. "</td><td>" . $row['quote_long']. "</td><td>" . $row['client_name']. "</td><td>" . $row['company_name']. "</td><td>" . $row['designation']. "</td><td>" . $row['city']. "</td><td>" . $row['state']. "</td><td> ".$row['country']." </td><td>" . $row['domain']. "</td><td>" . $row['status']. "</td><td>" . $row['website']. "</td><td>" . $row['remarks']. "</td><td>" . $row['average_rating']. "</td><td>" . $row['uploaded_by']. "</td><td>" . $row['creation_date']. "</td><td>" . $row['uploaded_at'] . "</td></tr>"]; */

				}
    
	    if($i > 1) {
  		
			$return = array('success' => true, 'message' => 'Data Exported Successfully!', 'data' => $items);		

		 } else { 

			$return = array('success' => false, 'message' => 'No record found!', 'data' => NULL);

		} 

		echo json_encode($return);

}

