<?php
session_start();
if (!isset($_SESSION['FirstName'])) {
	exit;
}

include ('includes/db1config.php');
include ('../includes/mysql_connect.php');

if (!isset($_POST['action']) || empty($_POST['action'])) {
	die('0');
}

switch ($_POST['action']) {
	case 'get_info':
		if (isset($_POST['recordkey'])) {
			$recordkey = $_POST['recordkey'];
		} else {
			die('0');
		}
		
		if ($menuItem = getMenuItem($recordkey)) {
			die(json_encode($menuItem));
		} else {
			die('0');
		}
		break;
	case 'save':
		if (!isset($_POST['recordkey']) || empty($_POST['recordkey'])) die('0');
		$recordkey = $_POST['recordkey'];

		$icon_type = isset($_POST['icon_type']) ? $_POST['icon_type'] : 'custom';
		$icon = isset($_POST['icon']) ? $_POST['icon'] : '';

		$icon_text = isset($_POST['icon_text']) ? $_POST['icon_text'] : '';
		$themekey = isset($_POST['color_theme']) ? $_POST['color_theme'] : '';

		if ($icon_type == 'custom') {
			$sql = "SELECT *
					FROM color_themes
					WHERE themekey = '".mysql_real_escape_string($themekey)."'";
			$result = mysql_query($sql) or die(mysql_error());

			if (mysql_num_rows($result) > 0) {
				$colorTheme = mysql_fetch_assoc($result);
			} else {
				$colorTheme = getDefaultColorTheme();
			}

			$sql = "UPDATE nav_slider_mobile
					SET 
						icon_text = '".mysql_real_escape_string($icon_text)."', 
						background_color = '".mysql_real_escape_string($colorTheme['background_color'])."', 
						text_color = '".mysql_real_escape_string($colorTheme['text_color'])."',
						icon_image = ''
					WHERE recordkey = '".mysql_real_escape_string($recordkey)."'";
			mysql_query($sql) or die(mysql_error());
		} else {
			$sql = "UPDATE nav_slider_mobile
					SET 
						icon_image = '".mysql_real_escape_string($icon)."'
					WHERE recordkey = '".mysql_real_escape_string($recordkey)."'";
			mysql_query($sql) or die(mysql_error());
		}

		if ($menuItem = getMenuItem($recordkey)) {
			die(json_encode($menuItem));
		} else {
			die('0');
		}

		break;
	default:
		die('0');
}

die('0');

function getDefaultColorTheme() {
	$sql = "SELECT *
			FROM color_themes
			ORDER BY default_option DESC 
			LIMIT 1";
	$result = mysql_query($sql) or die(mysql_error());

	$colorTheme = mysql_fetch_assoc($result);

	return $colorTheme;
}

function getMenuItem($recordkey) {
	$sql = "SELECT *
			FROM nav_slider_mobile
			WHERE recordkey = '".mysql_real_escape_string($recordkey)."'";
	$result = mysql_query($sql) or die(mysql_error());

	if (mysql_num_rows($result) > 0) {
		$row = mysql_fetch_assoc($result);

		// try to get color theme
		$sql = "SELECT *
				FROM color_themes
				WHERE 
					background_color = '".mysql_real_escape_string($row['background_color'])."' AND 
					text_color = '".mysql_real_escape_string($row['text_color'])."'";
		$result = mysql_query($sql) or die(mysql_error());

		if (mysql_num_rows($result) > 0) {
			$colorTheme = mysql_fetch_assoc($result);
			$row['themekey'] = $colorTheme['themekey'];
		} else {
			$row['themekey'] = 0;
		}
		die(json_encode($row));
	} else {
		return false;
	}

	return $row;
}

?>