<?php
session_start();
header('Content-Type: text/html; charset=utf-8');
//echo chr(239).chr(187).chr(191);
require '../scripts/ClassLibs/autoload.php';
$client_id = $_SESSION['client_id'];
$role = $_SESSION['role_id'];
$user_id = $_SESSION['id'];
$locale = $_SESSION['locale'];

$objMonit = new Monitor();

include "../settings.php";
$db = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
$db->query("SET CHARSET SET utf8");
$db->query("SET NAMES 'utf8'");
if (mysqli_connect_errno()) {
	echo "Error connecting to DB";
	exit;
}
$db1 = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
$db1->query("SET CHARSET SET utf8");
$db1->query("SET NAMES 'utf8'");
if (mysqli_connect_errno()) {
	echo "Error connecting to DB";
	exit;
}

if ($_REQUEST['func'] == 'showMobileSettings') 
{
	//$userID = $_REQUEST['userID'];
	$i = 0;
	$query = "SELECT `id` FROM `wm_user_mobile_settings` WHERE client_id=$client_id LIMIT 1";
	$stmt = $db->prepare($query);
	$stmt->execute();
	$stmt->bind_result($id);
	$stmt->fetch();
	$stmt->close(); 
	if (!$id) 
	{
		$query = "SELECT `id` FROM `wm_mobile_settings`";
		$stmt = $db->prepare($query);
		$stmt->execute();
		$stmt->bind_result($id);
		while ($stmt->fetch()) 
		{
			$data[$i] = $id;
			$i++;
		}
		
		$stmt->close();
		for ($i = 0; $i < count($data); $i++) {
			$query = "INSERT INTO wm_user_mobile_settings (`client_id`, `setting_id`, state ) VALUES ($client_id, $data[$i], 1)";
			$stmt = $db->prepare($query);
			$stmt->execute();
			$stmt->close();
		}
	}
	 
	$i = 0;
	$data = array();
	$query = "SELECT stg.`id`, stg.`setting_name`, stg.`description`, ustg.`state`  FROM `wm_mobile_settings` stg
	INNER JOIN `wm_user_mobile_settings` ustg ON ustg.`setting_id` = stg.`id`
	WHERE ustg.`client_id` = $client_id
	UNION 
  			SELECT id,`setting_name`, description, 0 FROM `wm_mobile_settings` WHERE id NOT IN(SELECT `setting_id` FROM `wm_user_mobile_settings` WHERE `client_id`=$client_id)";
	$stmt = $db->prepare($query);
	$stmt->execute();
	$stmt->bind_result($id, $setting_name, $description, $state);
	while($stmt->fetch()) {
		$data[$i] = array(
			'id' => $id,
			'name' => $setting_name,
			'description' => $objMonit->setLocaleStrFromJSON($description, $locale),
			'check' => $state,
		);
		$i++;
	}
	$stmt->close();
	echo '{rows:'.json_encode($data).'}';	
}

if ($_REQUEST['func'] == 'saveMobileSettings') 
{

	$arrOfSettingsID = $_REQUEST['arrOfSettingsID'];
	$arrOfStates = $_REQUEST['arrOfStates'];
	
	for ($i = 0; $i < count($arrOfSettingsID); $i++) 
	{
		$query = "SELECT id FROM wm_user_mobile_settings WHERE client_id=$client_id AND setting_id=$arrOfSettingsID[$i]";
		//echo $query;
		if($stmt = $db->prepare($query)){
		if($stmt->execute()){
		unset($id);
		$stmt->bind_result($id);
		$stmt->fetch();
		$stmt->close();
		
		if (!$id || $id=="") 
		{
			$query = "INSERT INTO  `wm_user_mobile_settings` (state, client_id,setting_id) VALUES ($arrOfStates[$i], $client_id,$arrOfSettingsID[$i])"; 
			//echo $query;
			if($stmt = $db->prepare($query)){
			if($stmt->execute()){
			$stmt->close();
			}else{ echo $db->errno.' '.$db->error; }
			}else{ echo $db->errno.' '.$db->error; }
		} else {
			$query = "UPDATE `wm_user_mobile_settings` SET `state`=$arrOfStates[$i] WHERE `client_id`=$client_id AND `setting_id`=$arrOfSettingsID[$i]";
			//echo $query;
			//SET `state`=$arrOfStates[$i] WHERE `user_id`=$userID AND `right_id`=$arrOfRigthID[$i]";
			if($stmt = $db->prepare($query)){
			if($stmt->execute()){
			$stmt->close();
			}else{ echo $db->errno.' '.$db->error; }
			}else{ echo $db->errno.' '.$db->error; }
		}
		
		}else{ echo $db->errno.' '.$db->error; }
		}else{ echo $db->errno.' '.$db->error; }
	}
}







