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

error_reporting(E_ALL);

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

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
	
	    $clientName = ''; 
	    $domainName = '';
		$country = '';
		$rating = '';
		$catName = '';
		$tags = '';
		$creationDate = '';
		
	    /* Table List */
	    $table1 = 'tbl_videos_ve';
		$table2 = 'tbl_videos_ve_cat';
		$table3 = 'tbl_videos_ve_domain';
		$table4 = 'tbl_videos_ve_tags';	
	
		$data = json_decode(file_get_contents('php://input'), true);

	    /* Gettting Variables */
		$clientName = $data['clientName'];
		$domainName = $data['domainName'];
		$country = $data['country'];
		$rating = $data['rating'];
		$catName = $data['catName'];
		$tags = $data['tags'];
		$creationDate = $data['creationDate'];
		
		/*echo $sql = "SELECT ".$table1.".*, ".$table2.".name as cat_name, ".$table3.".name as domain_name, GROUP_CONCAT(tbl_videos_ve_tags_assigned.tag_id) as tags, GROUP_CONCAT(tbl_videos_ve_tags.title) as ntags FROM `".$table1."` LEFT JOIN ".$table2." on ".$table2.".id = ".$table1.".cat_id LEFT JOIN ".$table3." on ".$table3.".id = ".$table1.".domain   LEFT JOIN tbl_videos_ve_tags_assigned on tbl_videos_ve_tags_assigned.video_id = tbl_videos_ve.id LEFT JOIN tbl_videos_ve_tags on tbl_videos_ve_tags.id = tbl_videos_ve_tags_assigned.tag_id GROUP BY tbl_videos_ve.id ORDER BY `tags` DESC";*/		
		
		if($clientName == '' && $domainName == '' && $country == '' && $rating == '' && $catName == '' && $tags == '' && $creationDate == '') {
		    
			$sql  = "SELECT * FROM `tbl_videos_ve`";
			
		} else {
			
			if(isset($clientName) && $clientName != '') {
				$clientNamesql = " AND tvv.title LIKE '%".$clientName."%'" ;
			} else {
				$clientNamesql = "";
			}
			
			if(isset($domainName) && $domainName != '') {
				$domainNamesql = " AND tvvd.name LIKE '%".$domainName."%'" ;
			} else {
				$domainNamesql = "";
			}
			
			if(isset($country) && $country != '') {
				$countrysql = " AND tvv.country LIKE '%".$country."%'" ;
			} else {
				$countrysql = "";
			}
			
			if(isset($rating) && $rating != '') {
				$ratingsql = " AND tvv.average_rating LIKE '%".$rating."%' " ;
			} else {
				$ratingsql = "";
			}
			
			if(isset($catName) && $catName != '') {
				$catNamesql = " AND tvvc.name LIKE '%".$catName."%' " ;
			} else {
				$catNamesql = "";
			}
			
			if(isset($tags) && $tags != '') {
				$tagssql = " AND tbl_videos_ve_tags.title LIKE '%".$catName."%' " ;
			} else {
				$tagssql = "";
			}
			
			if(isset($creationDate) && $creationDate != '') {
				$creationDatesql = " AND tvv.creation_date = '".$creationDate."'" ;
			} else {
				$creationDatesql = "";
			}
			
			$sql  = "SELECT tvv.* FROM `tbl_videos_ve` as tvv LEFT JOIN tbl_videos_ve_domain as tvvd on tvvd.id = tvv.domain LEFT JOIN tbl_videos_ve_cat as tvvc on tvvc.id = tvv.cat_id LEFT JOIN tbl_videos_ve_tags_assigned on tbl_videos_ve_tags_assigned.video_id = tvv.id LEFT JOIN tbl_videos_ve_tags on tbl_videos_ve_tags.id = tbl_videos_ve_tags_assigned.tag_id  WHERE tvv.id != '' " . $clientNamesql . $domainNamesql . $countrysql . $ratingsql . $catNamesql . $tagssql . $creationDatesql;			
			
			
		}
		
		$find = $conn->query($sql);
		
		
		if($find->num_rows > 0) { 
		
		    /*
		    $items[] = ["<tr><td>Row ID</td><td>Title</td><td>Video URL</td><td>Category</td><td>Quote</td><td>Quote Long</td><td>Client Name</td><td>Company Name</td><td>Designation</td><td>City</td><td>State</td><td>Country</td><td>Domain</td><td>Status</td><td>Website</td><td>Remarks</td><td>Average Rating</td><td>Uploaded By</td><td>Creation Date</td><td>Uploaded At</td></tr>"];
		    */
		    
		    $items[] = ["<tr><td>Row ID</td><td>Title</td><td>Video URL</td><td>Category</td><td>Quote</td><td>Quote Long</td><td>Client Name</td><td>Designation</td><td>Country</td><td>Domain</td><td>Status</td><td>Website</td><td>Remarks</td><td>Average Rating</td><td>Uploaded By</td><td>Creation Date</td><td>Uploaded At</td></tr>"];
		
			
			while( $row = $find->fetch_assoc() ) {
				
				/*
				$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>"];
				*/
				
				
				$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['designation']. "</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>"];
				
				
            }
           
            
			$return = array('success' => true, 'message' => 'Data Exported Successfully!', 'data' => $items);		
						
		} else { 
		
			$return = array('success' => false, 'message' => 'No record found!', 'data' => NULL);
			
		}
		
		//echo '<pre>';
		//print_r($return);

        echo json_encode($return);
		
		//echo json_encode($return);
		//echo $return;
	   	
}

