<?php
	include '../db/db.php';
	session_start();
	$log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
	if (empty($log)) {

	  $redirectUrl = "index.php";
	  echo "<script type=\"text/javascript\">";
	  echo "window.location.href = '$redirectUrl'";
	  echo "</script>";

	}

	if (array_key_exists('cart_details_load', $_POST)) {//data table
		$row_id=htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
	    $sql = "SELECT
		order_details.id,
		order_details.inv_id,
		order_details.user_id,
		order_details.gift_type,
		order_details.title_recip,
		order_details.`name`,
		order_details.phone,
		order_details.address,
		order_details.city,
		order_details.type,
		order_details.land_mark,
		order_details.date_holder,
		order_details.delivery_type,
		order_details.s_instructions,
		order_details.msg,
		order_details.title_sender,
		order_details.sender_name,
		order_details.sender_email,
		order_details.sender_tp,
		order_details.sender_address,
		order_details.country,
		order_details.s_anonymous,
		order_details.order_status,
		order_details.payment_status,
		order_details.date,
		order_details.g_total,
		order_details.dis_card,
		order_details.oredr_pickup_type,
		order_details.payment_type,
		order_details.showroom,
		order_details.sameday,
		discount_table.type AS discount_type,
		discount_table.amount AS dis_amount,
		discount_table.percentage,
		delivery_type.price AS tslot_price,
		delivery_location.price AS d_price
	FROM
		order_details
		LEFT JOIN
		delivery_location
		ON
			order_details.inv_id = delivery_location.inv_id
		LEFT JOIN
		discount_mapping
		ON
			order_details.inv_id = discount_mapping.inv_id
		LEFT JOIN
		discount_table
		ON
			discount_mapping.discount_id = discount_table.id
		INNER JOIN
		delivery_type
		ON
			order_details.delivery_type = delivery_type.id
		WHERE order_details.id= '$row_id'" ;

		//echo $sql;
	    $data = array();
	    $result = mysqli_query($conn,$sql);
	    while ($row = mysqli_fetch_assoc($result)) {
	        $data[] = $row;
	    }
	    echo json_encode($data);
	}
	if (array_key_exists('accept_order', $_POST)) {//data table
		$row_id=htmlspecialchars(trim($_POST['row_id']), ENT_QUOTES, 'UTF-8');
	    $sql = "UPDATE order_details SET order_status ='1' WHERE id= '$row_id'";
		$result = mysqli_query($conn, $sql);
		if ($result) {
			$response['status'] = "ok";
		}else{
			$response['status'] = "err";
		}
		echo json_encode($response);
	}
	if (array_key_exists('reject_order', $_POST)) {//data table
		$row_id=htmlspecialchars(trim($_POST['row_id']), ENT_QUOTES, 'UTF-8');
	    $sql = "UPDATE order_details SET order_status ='2' WHERE id= '$row_id'";
		$result = mysqli_query($conn, $sql);
		if ($result) {
			$response['status'] = "ok";
		}else{
			$response['status'] = "err";
		}
		echo json_encode($response);
	}


	if (array_key_exists('select_pwd', $_POST)) {//data table

		$pass=htmlspecialchars(trim($_POST['pwd']), ENT_QUOTES, 'UTF-8');

	    $user = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');

		$uname = strip_tags($user);
		$pwd = strip_tags($pass);

		$upass = hash('sha256', $pwd);

		$sql = mysqli_query($conn, "SELECT * FROM user_acc_tbl WHERE user='admin'");
		$row = mysqli_fetch_assoc($sql);

		if(mysqli_num_rows($sql) == 1) {

			if($row['pass']==$upass){
				//echo "OK";
				$response['status'] = 'ok'; // log in
				$_SESSION['user'] = $row['user'];

			} else{
				$response['status'] = 'error';
				 // wrong details
				$response['message'] = '<span class="glyphicon glyphicon-info-sign"></span> &nbsp; Username or Password does not match.';
			}
		}

		else{
			$response['status'] = 'error';
			// wrong details
			$response['message'] = '<span class="glyphicon glyphicon-info-sign"></span> &nbsp; Username or Password does not match.';
		}
		echo json_encode($response);
	}
