<?php 
session_start();
header('Content-Type: text/html; charset=utf-8');


$client_id = $_SESSION['client_id'];


include "writedblog.php";
include "logger.php";

$dbset = json_decode($_SESSION['settingsApp'],true);
$db = new mysqli ($dbset['DB']['DB_HOST'],$dbset['DB']['DB_USER'],$dbset['DB']['DB_PASS'],$dbset['DB']['DB_NAME']);



$db->query("SET CHARSET SET utf8");
$db->query("SET NAMES 'utf8'");
if (mysqli_connect_errno()) {
	echo "Error connecting to DB";
	exit;
}


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

//$query = "SELECT `id`,`NameClient`,`Address`,`Phone`,`Email`,`ContactPerson`,`Code`,DATE_FORMAT(`StartTime`, '%H:%i'),DATE_FORMAT(`EndTime`, '%H:%i'),`GeoX`,`GeoY`, PriceList, ContactBoss, EmailBoss, PhoneBoss FROM `mc_client` WHERE instance_id = ".$client_id.";";
$query = "SELECT c.`id`,c.`NameClient`,c.`Address`,c.`Phone`,c.`Email`,c.`ContactPerson`,c.`Code`,DATE_FORMAT(c.`StartTime`, '%H:%i'),DATE_FORMAT(c.`EndTime`, '%H:%i'),c.`GeoX`,c.`GeoY`,c.PriceList,c.ContactBoss,c.EmailBoss,c.PhoneBoss,u.`login`,u.`password` FROM `mc_client` c
LEFT JOIN `mc_user` u ON u.`client_id` = c.`id` WHERE c.instance_id = ".$client_id.";";
$data = array();
if( $stmt = $db->prepare($query) ){
if( $stmt->execute() ){
$stmt->bind_result($id,$NameClient,$Address,$Phone,$Email,$ContactPerson,$Code,$StartTime,$EndTime,$GeoX,$GeoY, $PriceList, $ContactBoss, $EmailBoss, $PhoneBoss, $login, $password);
while( $stmt->fetch() ){
    if($PriceList == "Cost1" ){
        $PriceListName = "Стандарт";
    }elseif ($PriceList == "Cost2"){
        $PriceListName = "Мастер";
    }elseif ($PriceList == "Cost3"){
        $PriceListName = "Премиум";
    }
	$data[] = array(
			'id' => $id,
			'NameClient' => $NameClient,
			'Address' => $Address,
			'Phone' => $Phone,
			'Email' => $Email,
			'ContactPerson' => $ContactPerson,
			'Code' => $Code,
			'StartTime' => $StartTime,
			'EndTime' => $EndTime,
			'GeoX' => $GeoX,
			'GeoY' => $GeoY,
            'PriceListName'=> $PriceListName,
            'PriceList'=> $PriceList,
			'ContactBoss'=> $ContactBoss,
			'PhoneBoss'=> $PhoneBoss,
			'EmailBoss'=> $EmailBoss,
			'login'=> $login,
			'password'=> $password
		);
}
}
}
$stmt->close();

echo'{rows:'.json_encode($data).'}';

$db->close();
exit;
}


if ($_REQUEST['func'] == 'addedit') {
	
$id = $_REQUEST['id'];
$Name = $_REQUEST['Name'];
$Code = $_REQUEST['Code'];
$OriginalAddress = $_REQUEST['OriginalAddress'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$ContactPerson = $_REQUEST['ContactPerson'];
$StartTime = $_REQUEST['StartTime'];
$EndTime = $_REQUEST['EndTime'];
$GeoX = $_REQUEST['GeoX'];
$GeoY = $_REQUEST['GeoY'];
$PriceList = $_REQUEST['PriceList'];

$ContactBoss = $_REQUEST['ContactBoss'];
$PhoneBoss = $_REQUEST['PhoneBoss'];
$EmailBoss = $_REQUEST['EmailBoss'];

$login = $_REQUEST['login'];
$password = $_REQUEST['password'];

    if($id || $id!=''){
		$query = "UPDATE `mc_client` SET `NameClient`='".$Name."',`Address`='".$OriginalAddress."',`Phone`='".$phone."',`Email`='".$email."',`ContactPerson`='".$ContactPerson."',`Code`='".$Code."',`GeoX`=".$GeoX.",`GeoY`=".$GeoY.",`StartTime`='".$StartTime."',`EndTime`='".$EndTime."',PriceList = '". $PriceList."', ContactBoss='".$ContactBoss."', PhoneBoss='".$PhoneBoss."', EmailBoss='".$EmailBoss."' WHERE id = ".$id."";
	}else{
		$query = "INSERT INTO `mc_client` (`NameClient`,`Address`,`Phone`,`Email`,`ContactPerson`,`Code`,`GeoX`,`GeoY`,`StartTime`,`EndTime`, PriceList, `instance_id`, ContactBoss, PhoneBoss, EmailBoss) VALUES ('".$Name."','".$OriginalAddress."','".$phone."','".$email."','".$ContactPerson."','".$Code."',".$GeoX.",".$GeoY.",'".$StartTime."','".$EndTime."','". $PriceList."', ".$client_id.", '".$ContactBoss."', '".$PhoneBoss."', '".$EmailBoss."')";
	}
	
	if( $stmt = $db->prepare($query) ){
	if( $stmt->execute() ){
	$last_id = $db->insert_id;
	$stmt->close();
		echo json_encode(array('success'=>true, 'message'=>''));
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	
	if($id || $id!=''){
		$query = "UPDATE `mc_user` SET `login`='".$login."',`password`='".$password."' WHERE id = ".$id."";
	}else{
		$query = "INSERT INTO `mc_user` (`login`,`password`,`locale`,`instance_id`,`client_id`,`role`) VALUES ('".$login."','".$password."','ru',146,".$last_id.",1)";
	}
	if( $stmt = $db->prepare($query) ){
	if( $stmt->execute() ){
	$stmt->close();
	}
	}
	
	$db->close();
	exit;
}

if(isset($_REQUEST['delete'])){
	$arrayId = $_REQUEST['arrayId'];
	$stringId = implode(",",$arrayId);
	
	$query = "DELETE FROM mc_client WHERE instance_id = ".$client_id." AND id IN (".$stringId.")";
	if( $stmt = $db->prepare($query) ){
	if( $stmt->execute() ){
	$stmt->close();
		echo json_encode(array('success'=>true, 'message'=>''));
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	
	$db->close();
	exit;
}

?>