<?php
error_reporting(0);
require("includes/connection.php");
require("includes/function.php");
require("language/language.php");

$response = array();
$_SESSION['class'] = "success";

if (PURCHASE == 'disabled') {
	$response['status'] = 1;
	$_SESSION['class'] = "error";
	$_SESSION['msg'] = "demo_mode";
	echo json_encode($response);
	exit();
} else {
	switch ($_POST['action']) {
		case 'toggle_status':
			$id = $_POST['id'];
			$for_action = $_POST['for_action'];
			$column = $_POST['column'];
			$tbl_id = $_POST['tbl_id'];
			$table_nm = $_POST['table'];

			if ($for_action == 'active') {
				$data = array($column  =>  '1');
				$edit_status = Update($table_nm, $data, "WHERE $tbl_id = '$id'");
				$_SESSION['msg'] = "13";
			} else {
				$data = array($column  =>  '0');
				$edit_status = Update($table_nm, $data, "WHERE $tbl_id = '$id'");
				$_SESSION['msg'] = "14";
			}

			$response['status'] = 1;
			$response['action'] = $for_action;
			echo json_encode($response);
			break;
		case 'admob_add':

			$id = $_POST['id'];
			$for_action = $_POST['for_action'];
			$column = $_POST['column'];
			$tbl_id = $_POST['tbl_id'];
			$table_nm = $_POST['table'];

			if ($for_action == 'deactive') {
				$data = array($column  =>  '1');
				$edit_status = Update($table_nm, $data, "WHERE $tbl_id = '$id'");
			} else {
				$data = array($column  =>  '0');
				$edit_status = Update($table_nm, $data, "WHERE $tbl_id = '$id'");
			}


			$response['status'] = 1;
			$response['action'] = $for_action;
			echo json_encode($response);
			break;

		case 'removeComment':
			$id = $_POST['id'];

			Delete('tbl_comments', 'id=' . $id);

			$response['status'] = 1;
			$response['message'] = 'Something went to wrong !';
			echo json_encode($response);
			break;

		case 'removeReport':
			$id = $_POST['id'];

			Delete('tbl_reports', 'id=' . $id);

			$response['status'] = 1;
			$response['message'] = 'Something went to wrong !';
			echo json_encode($response);
			break;
		case 'removeAllReports':
			$id = $_POST['id'];
			$type = $_POST['type'];

			$deleteSql = "DELETE FROM tbl_reports WHERE `post_id`='$id' AND `type`='$type'";

			if (mysqli_query($mysqli, $deleteSql)) {
				$response['status'] = 1;
				$response['message'] = 'Something went to wrong !';
			} else {
				$response['status'] = -2;
				$response['message'] = 'Something went to wrong !';
			}


			echo json_encode($response);
			break;

		case 'remove_subtitle':

			$id = $_POST['id'];
			$type = $_POST['type'];
			$post_id = $_POST['post_id'];

			$sql = "SELECT * FROM tbl_subtitles WHERE `id` = '$id'";
			$res = mysqli_query($mysqli, $sql);
			$row = mysqli_fetch_assoc($res);

			if ($row['subtitle_type'] == 'local') {
				unlink('uploads/' . $row['subtitle_url']);
			}

			mysqli_free_result($res);

			Delete('tbl_subtitles', 'id=' . $id);

			$sql = "SELECT * FROM tbl_subtitles WHERE `post_id`='$post_id' AND `type`='$type'";
			$res = mysqli_query($mysqli, $sql);

			$response['subtitle_status'] = (mysqli_num_rows($res) > 0) ? 'true' : 'false';
			$response['status'] = 1;
			$response['message'] = $client_lang['remove_subtitle_success'];
			echo json_encode($response);
			break;

		case 'getSeason':
				$id=$_POST['id'];
	
				$sql="SELECT * FROM tbl_season WHERE series_id='$id'";
				$res=mysqli_query($mysqli,$sql);
				$result=array();
				while ($row=mysqli_fetch_assoc($res)) {
					$opt='<option value="'.$row['id'].'">'.$row['season_name'].'</option>';
					array_push($result, $opt);
				}
	
				  $response['status']=1;
				  $response['data']=$result;
				  echo json_encode($response);
				break;

        case 'multi_delete':
            
    		        if (is_array($_POST['id'])) {
    					$ids = implode(",", $_POST['id']);
    				} else {
    					$ids = $_POST['id'];
    				}
    
    				$tbl_nm = $_POST['tbl_nm'];
    
    				if ($tbl_nm == 'tbl_iteams') {
    
    					$sql = "SELECT * FROM $tbl_nm WHERE `itemid` IN ($ids)";
    					$res = mysqli_query($mysqli, $sql);
    					while ($row = mysqli_fetch_assoc($res)) {
    						if ($row['image'] != "") {
    							unlink('images/' . $row['image']);
    							unlink('images/thumbs/' . $row['image']);
    						}
    					}
    					$deleteSql = "DELETE FROM $tbl_nm WHERE `itemid` IN ($ids)";
    					mysqli_query($mysqli, $deleteSql);
    				} 
    				else if ($tbl_nm == 'tbl_subscription') {
    
    					$deleteSql = "DELETE FROM $tbl_nm WHERE `id` IN ($ids)";
    					mysqli_query($mysqli, $deleteSql);
    				} 

		case 'multi_action':

			if (is_array($_POST['id'])) {
				$ids = implode(",", $_POST['id']);
			} else {
				$ids = $_POST['id'];
			}
			$action = $_POST['for_action'];
			$table = $_POST['table'];

			if ($action == 'enable') {

                if($table == "tbl_iteams")
                {
                    $sql = "UPDATE $table SET `status`='1' WHERE `itemid` IN ($ids)";
    				mysqli_query($mysqli, $sql);
    				$response['status'] = 1;
    				$_SESSION['msg'] = "13";
                }
                else
                {	
                    $sql = "UPDATE $table SET `status`='1' WHERE `id` IN ($ids)";
    				mysqli_query($mysqli, $sql);
    				$response['status'] = 1;
    				$_SESSION['msg'] = "13";
                }
			
			} else if ($action == 'disable') {
			    
			    if($table == "tbl_iteams")
                {
                   	$sql = "UPDATE $table SET `status`='0' WHERE `itemid` IN ($ids)";
    				if (mysqli_query($mysqli, $sql)) {
    					$response['status'] = 1;
    					$_SESSION['msg'] = "14";
				    }
                }
                else
                {	
                   	$sql = "UPDATE $table SET `status`='0' WHERE `id` IN ($ids)";
    				if (mysqli_query($mysqli, $sql)) {
    					$response['status'] = 1;
    					$_SESSION['msg'] = "14";
    				}
                }
                
			
			} else if ($action == 'delete') {
				if ($table == 'tbl_iteams') 
				{
				    	$sql = "SELECT * FROM tbl_iteams WHERE `itemid` IN ($ids)";
    					$res = mysqli_query($mysqli, $sql);
    					while ($row = mysqli_fetch_assoc($res)) {
    						if ($row['image'] != "") {
    							unlink('images/' . $row['image']);
    							unlink('images/thumbs/' . $row['image']);
    						}
    					}
    					$deleteSql = "DELETE FROM tbl_iteams WHERE `itemid` IN ($ids)";
    					mysqli_query($mysqli, $deleteSql);
				}
				else if ($table == 'tbl_subscription') 
				{
    					$deleteSql = "DELETE FROM tbl_subscription WHERE `id` IN ($ids)";
    					mysqli_query($mysqli, $deleteSql);
				}
			}

			$response['status'] = 1;
			echo json_encode($response);
			break;

		default:
			# code...
			break;
	}
}
