<?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) {
	
		/******************** Postal Charges *********************/
	    
		if (array_key_exists('get_dataset', $_POST)) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id'])), ENT_QUOTES, 'UTF-8');

			$sql = mysqli_query($conn, "SELECT * FROM postal_charges WHERE id=$id"); 
			$row = mysqli_fetch_array($sql);

			echo json_encode($row);
	    }
		
		if(isset($_POST['id_up'])) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id_up'])), ENT_QUOTES, 'UTF-8');
			$del_fee = htmlspecialchars(strip_tags(trim($_POST['del_fee'])), ENT_QUOTES, 'UTF-8');
		
			if($del_fee > 0) {
				$sql = "UPDATE postal_charges SET fee='$del_fee' WHERE id='$id'";
				$stmt_log = mysqli_query($conn, $sql);
				
				if($stmt_log === false) {
					echo 'error';
				
				} else {
					echo 'success';
				}
			
			} else {
				echo 'invalid';
			}
		}
		
		/******************** Courier Charges *********************/

		if(isset($_POST['courier_fee'])) {
			$courier_fee = htmlspecialchars(strip_tags(trim($_POST['courier_fee'])), ENT_QUOTES, 'UTF-8');
			$courier_1kg = htmlspecialchars(strip_tags(trim($_POST['courier_1kg'])), ENT_QUOTES, 'UTF-8');
		
			if($courier_fee > 0 && $courier_1kg > 0) {
				$sql = "UPDATE courier_charges SET courier_fee='$courier_fee', courier_1kg='$courier_1kg'";
				$stmt_log = mysqli_query($conn, $sql);
				
				if($stmt_log === false) {
					echo 'error';
				
				} else {
					echo 'success';
				}
			
			} else {
				echo 'invalid';
			}
		}
		
		if(isset($_POST['cash_on_del'])) {
			$cash_on_del = htmlspecialchars(strip_tags(trim($_POST['cash_on_del'])), ENT_QUOTES, 'UTF-8');
			$cash_on_1kg = htmlspecialchars(strip_tags(trim($_POST['cash_on_1kg'])), ENT_QUOTES, 'UTF-8');
		
			if($cash_on_del > 0 && $cash_on_1kg > 0) {
				$sql = "UPDATE courier_charges SET cash_on_del='$cash_on_del', cash_on_1kg='$cash_on_1kg'";
				$stmt_log = mysqli_query($conn, $sql);
				
				if($stmt_log === false) {
					echo 'error';
				
				} else {
					echo 'success';
				}
			
			} else {
				echo 'invalid';
			}
		}
		
		/*************************** EMS Cahrges ***************************/
		
		if (array_key_exists('get_dataset_ems', $_POST)) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id'])), ENT_QUOTES, 'UTF-8');

			$sql = mysqli_query($conn, "SELECT * FROM ems_charges WHERE id='$id'"); 
			$row = mysqli_fetch_array($sql);

			echo json_encode($row);
	    }
		
		if(isset($_POST['id_up_ems'])) {
			$id_ems = htmlspecialchars(strip_tags(trim($_POST['id_up_ems'])), ENT_QUOTES, 'UTF-8');
			$g250 = htmlspecialchars(strip_tags(trim($_POST['250_g_up'])), ENT_QUOTES, 'UTF-8');
			$g500 = htmlspecialchars(strip_tags(trim($_POST['500_g_up'])), ENT_QUOTES, 'UTF-8');
			$kg1  = htmlspecialchars(strip_tags(trim($_POST['1000_g_up'])), ENT_QUOTES, 'UTF-8');
			$kg10 = htmlspecialchars(strip_tags(trim($_POST['10_kg_up'])), ENT_QUOTES, 'UTF-8');
			$kg20 = htmlspecialchars(strip_tags(trim($_POST['20_kg_up'])), ENT_QUOTES, 'UTF-8');
			$kg30 = htmlspecialchars(strip_tags(trim($_POST['30_kg_up'])), ENT_QUOTES, 'UTF-8');
			$max_kg = htmlspecialchars(strip_tags(trim($_POST['max_kg_up'])), ENT_QUOTES, 'UTF-8');
			
			if($g250 > 0 && $g500 > 0 && $kg1 > 0 && $kg10 > 0 && $max_kg > 0) {
				$sql = "UPDATE ems_charges SET g_250='$g250', g_500='$g500', kg_1='$kg1', kg_10='$kg10', kg_20='$kg20', kg_30='$kg30', max_kg='$max_kg' WHERE id='$id_ems'";
				$stmt_log = mysqli_query($conn, $sql);
				
				if($stmt_log === false) {
					echo 'error';
				
				} else {
					echo 'success';
				}
			
			} else {
				echo 'invalid';
			}
		}
		
	} else {
		$redirectUrl = "../";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}
}
?>