<?php
include '../db/db.php';
session_start();

if(isset($_SESSION['userID'])) {
	$log = htmlspecialchars(strip_tags(trim($_SESSION['userID'])), ENT_QUOTES, 'UTF-8');
} else {
	$log = '';
}

if (empty($log)) {     
	$redirectUrl = "../";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";
	 
} else {
	$sqlu = "SELECT id FROM user_acc_tbl WHERE userID='$log' and access=1 and val=1";
	$stmtu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($stmtu);
	
	if($countu == 1) {

		if (array_key_exists('delType_enable', $_POST)) {
			$del_id  = htmlspecialchars(strip_tags(trim($_POST['del_id'])), ENT_QUOTES, 'UTF-8');
		
			if(!empty($del_id)) {
				$sql = "UPDATE delivery_methods SET enable_method=1 where id='$del_id'";
				$stmt_log = mysqli_query( $conn, $sql);
				
				if( $stmt_log === false ) {
					echo json_encode(array("msgType" => 2));
				
				} else {
					echo json_encode(array("msgType" => 1));
				}
			
			} else {
				echo json_encode(array("msgType" => 3));
			}
		}
		
		if (array_key_exists('delType_disable', $_POST)) {
			$del_id  = htmlspecialchars(strip_tags(trim($_POST['del_id'])), ENT_QUOTES, 'UTF-8');
		
			if(!empty($del_id)) {
				$sql = "UPDATE delivery_methods SET enable_method=0 where id='$del_id'";
				$stmt_log = mysqli_query( $conn, $sql);
				
				if( $stmt_log === false ) {
					echo json_encode(array("msgType" => 2));
				
				} else {
					echo json_encode(array("msgType" => 1));
				}
			
			} else {
				echo json_encode(array("msgType" => 3));
			}
		}
		
		if (array_key_exists('update_del_days', $_POST)) {
			$del_id  = htmlspecialchars(strip_tags(trim($_POST['del_id'])), ENT_QUOTES, 'UTF-8');
			$no_of_days  = htmlspecialchars(strip_tags(trim($_POST['no_of_days'])), ENT_QUOTES, 'UTF-8');
		
			if(!empty($del_id)) {
				$sql = "UPDATE delivery_methods SET delivery_days='$no_of_days' where id='$del_id'";
				$stmt_log = mysqli_query( $conn, $sql);
				
				if( $stmt_log === false ) {
					echo json_encode(array("msgType" => 2));
				
				} else {
					echo json_encode(array("msgType" => 1));
				}
			
			} else {
				echo json_encode(array("msgType" => 3));
			}
		}
		
	} else {
		$redirectUrl = "../";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}
}
?>