<?php
include 'pagename.php';
include $session;
Session::checkSession_app();
include '../config.php';

error_reporting(0);
ini_set('display_errors', 0);
$list_name = $_POST['list_name'];
$selected_tag_name = $_POST['selected_tag_name'];
$target_csv_name = $_POST['target_csv_name'];
$user_id = $_POST['user_id'];
$clear_tag = $_POST['clear_tag'];
	
if(!empty($list_name)) {
	
	if (isset($_POST['list_name']) && isset($_POST['target_csv_name']) && isset($_POST['user_id'])) {
	
	$check_sql = $db->select("SELECT target_csv_name, list_name, user_id FROM evp_tag_list WHERE target_csv_name='$target_csv_name' and  user_id='$user_id'");
	$check_sql = mysqli_num_rows($check_sql);
		if($check_sql == 0) {
			$insert_sql = $db->select("INSERT INTO evp_tag_list (target_csv_name, list_name, user_id) VALUES ('$target_csv_name', '$list_name', '$user_id')");
			
		} else {
			
			$insert_sql = $db->select("UPDATE evp_tag_list SET list_name='$list_name' WHERE target_csv_name='$target_csv_name' and user_id='$user_id'");
		}
		
		return print_r(json_encode(array('title' => 'Success','text' => 'Scanned list added into newly created collection','type' =>'success')));
		
	}
	
	
} elseif(!empty($selected_tag_name)) {
	
	if (isset($_POST['selected_tag_name']) && isset($_POST['target_csv_name']) && isset($_POST['user_id'])) {
		
	$check_sql = $db->select("SELECT target_csv_name, list_name, user_id FROM evp_tag_list WHERE target_csv_name='$target_csv_name' and user_id='$user_id'");
	$check_sql = mysqli_num_rows($check_sql);
	if($check_sql == 0) {
			$insert_sql = $db->select("INSERT INTO evp_tag_list (target_csv_name, list_name, user_id) VALUES ('$target_csv_name', '$selected_tag_name', '$user_id')");
		} else {
			$insert_sql = $db->select("UPDATE evp_tag_list SET list_name='$selected_tag_name' WHERE target_csv_name='$target_csv_name' and user_id='$user_id'");
		}
	}

	return print_r(json_encode(array('title' => 'Success','text' => 'Scanned list assigned in selected tag','type' =>'success')));
	
} elseif(isset($_POST['clear_tag'])) {
				$del_sql = $db->select("DELETE FROM evp_tag_list WHERE target_csv_name='$target_csv_name' and user_id='$user_id'");
						return print_r(json_encode(array('title' => 'Success','text' => 'Scanned list Unassigned from Previous Tag','type' =>'success')));

}else {
	header('HTTP/1.1 500 Internal Server...');
    header('Content-Type: application/json; charset=UTF-8');
    die(json_encode(array('type' => 'error', 'code' => 1337)));
}
