<?php
session_start();
global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH  .'globals/globals.php');
$adminAct = $_REQUEST['adminType']; 
$admin = 'service';
$updateString = ''; 
$udID = ''; 

$fieldArray = array('svcID','domainName','compID','svcType','svcProvider','svcBeginDate','svcExpDate','svcOwner','svcNotes','dateAdded','display');
$sanitizeExclude = array(); 

if (!empty($_POST['op'])) {
	$op = $_POST['op'];

	if ($op == 'delete') {
		$editID = $_POST['editID'];
		$query = 'UPDATE serviceInfo SET display=0 WHERE svcID='. $editID;
		$result = $con->query($query);
	} else {	
		$fieldString = '';
		$valueString = '';
		
		if ($_POST['op'] == 'delete') {
			$fieldString .= 'svcID,display'; 
			$valueString .= $_POST['svcID'].',0';
		}  else {
			foreach($_POST as $key => $value) {
				if (in_array($key,$fieldArray)) {
					if ($key == 'compID') 
						$key = 'clientID';
					elseif (($key == 'svcProvider') && ($value == "NEW")) {
						$newProdierName = '';
						if ((!empty($_POST['newProviderName'])) && ($_POST['newProviderName'] != 'Service Provider Name'))
							$newProdierName = $_POST['newProviderName'];
						$newProdierWebisite = '';
						if ((!empty($_POST['newProdierWebisite'])) && ($_POST['newProdierWebisite'] != 'Service Provider Website'))
							$newProdierWebisite = $_POST['newProdierWebisite'];
						$query = "INSERT INTO serviceProviders (spName,spWebsite,dateAdded,display) VALUES ('". $newProdierName ."','". $newProdierWebisite ."','". date("Y-m-d H:i:s") ."',1)";
						$result = $con->query($query);
						$value = $con->insert_id;
					}
					$fieldString .= $key .',';
					$valueString .= '"'. nice_addslashes($value) .'",';
				}
			}
			// Remove Last comma from string to avoid mySQL errors
			$fieldString = substr($fieldString,0,strlen($fieldString)-1);
			$valueString = substr($valueString,0,strlen($valueString)-1);
		}
		$query = 'REPLACE INTO serviceInfo ('. $fieldString .') VALUES ('. $valueString .')';
		$result = $con->query($query);
		$editID = (!empty($udID) ? $udID : $con->insert_id);
	}
	$updateID = (($page == 'dash') ? 'service-'. $editID : 'record-'. $editID);
	$output = (($result) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString) : 'UPDATE ERROR');
} else {
	$output = 'ERROR'; 	
}

if ($con) $con->close();
header('Content-Type: application/json; charset=utf-8', true); 
echo json_encode(array("output"=>$output));

?>