<?php

ini_set("auto_detect_line_endings", "1");

function handlePost() {
	$response = array("status" => false, "message" => false);
	if (isset($_POST['pin'])) {
		$pin = trim($_POST['pin']);
		if ($_POST['action'] == "set") {
			$_SESSION[$pin . '_pin_active']	 = true;
			$response['status']				 = true;
		} elseif ($_POST['action'] == "remove") {
			if (isset($_SESSION[$pin . '_pin_active'])) {
				unset($_SESSION[$pin . '_pin_active']);
			}
			$response['status'] = true;
		}
	} else if (isset($_POST['stats'])) {
		$stats = trim($_POST['stats']);
		if ($_POST['action'] == "set") {
			$_SESSION[$stats . '_stats_active']	 = true;
			$response['status']					 = true;
		} elseif ($_POST['action'] == "remove") {
			if (isset($_SESSION[$stats . '_stats_active'])) {
				unset($_SESSION[$stats . '_stats_active']);
			}
			$response['status'] = true;
		}
	} else if (isset($_POST['upload-contacts'])) {
		$fileName	 = 'contacts-' . date("Y-m-d-H-i-s", time()) . '.csv';
		$path		 = $_SERVER['DOCUMENT_ROOT'] . "/v2/uploads/";
		if (move_uploaded_file($_FILES['csv']['tmp_name'], $path . $fileName)) {
			$csv = csv_to_array($path . $fileName);
			if (count($csv) > 0 && false !== $csv) {

				$response['html']		 = "";
				$response['filename']	 = $fileName;

				foreach ($csv as $row) {
					if (isset($row['first name']) && isset($row['surname']) && isset($row['vehicle registration'])) {
						$fiestname				 = trim($row['first name']);
						$surname				 = trim($row['surname']);
						$vehicle_registration	 = trim($row['vehicle registration']);
						$error					 = false;
						if ("" == $fiestname || "" == $surname || "" == $vehicle_registration) {
							$error = true;
						}
						$uid = rand(1, 1000);
						$response['html'] .= '<a data-uid="' . $uid . '" data-name="' . $fiestname . '" data-surname="' . $surname . '" data-vehiclenumber="' . $vehicle_registration . '"  href="#" class="' . (($error) ? "c-namesError" : "") . '">' . $fiestname . ' ' . $surname . ' <span href="#" class="c-contact-delete icon-cross c-circle"></span></a>';
						$response['assing_html'] .= '<div class="c-AssignContact-Box-Contacts-Item ui-draggable ui-draggable-handle"><a data-uid="' . $uid . '" data-name="' . $fiestname . '" data-surname="' . $surname . '" data-vehiclenumber="' . $vehicle_registration . '" data-group="" href="#" data-group="" data-uid="1">' . $fiestname . ' ' . $surname . ' </a><span class="c-AssignContact-Clear icon-cross" href="#"></span></div>';
					}
				}
				if ("" !== $response['html']) {
					$response['status'] = "success";
				} else {
					$response['message'] = "Invalid csv format found. Please try header format like first name, surname and vehicle registration";
				}
			} else {
				$response['message'] = "Invalid csv format found. Please try header format like first name, surname and vehicle registration";
			}
		} else {
			$response['message'] = "Error while uploading file! Please tr again.";
		}
	} else if (isset($_POST['group-pin'])) {
		$action				 = isset($_POST['action']) ? $_POST['action'] : false;
		$pin				 = isset($_POST['password']) ? $_POST['password'] : false;
		$editPin			 = "1111";
		$masterPin			 = "0000";
		$response['review']	 = false;
		$response['master']	 = false;
		switch ($action) {
			case "master":
				if ($masterPin == $pin) {
					$_SESSION['review-group-pin']	 = true;
					$response['review']				 = true;
					$_SESSION['master-group-pin']	 = true;
					$response['master']				 = true;
					$response['status']				 = true;
				}
				break;
			case "review":
				if (in_array($pin, array($masterPin, $editPin))) {
					$_SESSION['review-group-pin']	 = true;
					$response['review']				 = true;
					if ($masterPin == $pin) {
						$_SESSION['master-group-pin']	 = true;
						$response['master']				 = true;
					}
					$response['status'] = true;
				}
				break;
			case "logout":
				if (isset($_SESSION['review-group-pin'])) {
					unset($_SESSION['review-group-pin']);
				}
				if (isset($_SESSION['master-group-pin'])) {
					unset($_SESSION['master-group-pin']);
				}
				$response['status'] = true;
				break;
		}
	}
	die(json_encode($response));
}

function csv_to_array($filename = '', $delimiter = ',') {
	if (!file_exists($filename) || !is_readable($filename))
		return FALSE;

	$header	 = false;
	$data	 = array();
	if (($handle	 = fopen($filename, 'r')) !== FALSE) {
		while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
			if (!$header) {
				$header = $row;
			} else {
				$data[] = @array_combine($header, $row);
			}
		}
		fclose($handle);
	}
	return $data;
}

if ($_POST) {
	handlePost();
}
?>