<?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 = 'loginfo';
$updateString = ''; 
$udID = ''; 

$page = (isset($_POST['page']) ? $_POST['page'] : '');
$section = (isset($_POST['section']) ? $_POST['section'] : '');
$variables = getVariables('loginfo',$page);

if (!empty($_POST['op'])) {
	$op = $_POST['op'];
	
	if ($op == 'delete') {
		$editID = $_POST['editID'];
		$query = 'UPDATE loginfo SET display=0 WHERE infoID='. $editID;
		$result = $con->query($query);
	} else {
		$editID = $_POST['editID'];
		$fieldString = '';
		$valueString = '';
		
		foreach($_POST as $key => $value) {
			if (in_array($key,$variables['fieldArray'])) {
				if (in_array($key,$variables['excludeArray'])) {
					// Do Not Include in query
				} elseif ($key == 'compID') {
					$key = 'clientID';
				} elseif ($key == 'op') {
					$key = 'display';
					if ($op == "delete") 
						$value = 0;
					elseif ($op == "react") 
						$value = 1;
					else
						$value = $_POST['display'];
				} 
				$fieldArr[] = $key;
				$valueArr[] = "'". nice_addslashes($value) ."'";
			}
		}
	
		// Remove Last comma from string to avoid mySQL errors
		$fieldString = implode(',',$fieldArr);
		$valueString = implode(',',$valueArr);
	
		$query = "REPLACE INTO loginfo (". $fieldString .") VALUES (". $valueString .")";
		$result = $con->query($query);
		$editID = (!empty($editID) ? $editID : $con->insert_id);
		
		$updateInfo = ''; 
		if (($_POST['op'] == 'add') || ($_POST['op'] == 'edit')) {
			if ($page == 'loginfo') {
				include('../list/list-item/loginfo.php');
				$updateString = $listitem;
			}
		}
	}
	$updateID = (($page == 'dash') ? 'loginfo-'. $editID : 'record-'. $editID);
	$output = (($result) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString,'page'=>$page,'section'=>$section) : 'UPDATE ERROR');
} else {
	$output = 'ERROR'; 	
}
if ($con) $con->close();
header('Content-Type: application/json; charset=utf-8', true); 
echo json_encode(array("output"=>$output));

?>