<?php
session_start();
include('../../globals/error-reporting.php');
global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH  .'globals/globals.php');
$admin = 'todo';
$updateString = ''; 
$udID = ''; 

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

if (!empty($_POST['op'])) {
	$op = $_POST['op'];
	
	if ($op == 'complete-multi') {
		if (!empty($_POST['completeToDos'])) {
			$todoArray = array();
			foreach($_POST['completeToDos'] as $td) {
				$todoArray[] = $td;
			}
			$updateID = $_POST['completeToDos'];
			foreach ($todoArray as $todo) {
				$query = "UPDATE todo_list SET todoDone=1, display=0 WHERE todoID=". $todo; 
				$result = $con->query($query);
			}
		}
	} elseif ($op == 'delete') {
		$editID = $_POST['editID'];
		$query = 'UPDATE todo_list SET display=0 WHERE todoID='. $editID;
		$result = $con->query($query);
	} else {
		$fieldString = '';
		$valueString = '';
		foreach($_POST as $key => $value) {
			//if (($key == 'submit')||($key == 'todoDone')||($key == 'display')||($key == 'adminType')) {
			if (in_array($key,$variables['excludeArray'])) {
				// Do Not Include in query
			}
			elseif ($key == 'op') {
				if ($op == "complete") {
					$fieldString .= 'todoDone,';
					$valueString .= '"1",';
					$fieldString .= 'display,';
					$valueString .= '"0",';
					$op = 'delete'; 
				} else  {
					if ($op == "react") {
						$fieldString .= 'todoDone,';
						$valueString .= '"0",';
						$fieldString .= 'display,';
						$valueString .= '"1",';
					} elseif ($op == "delete") {
						$fieldString .= 'todoDone,';
						$valueString .= '"'. $_POST['todoDone'] .'",';
						$fieldString .= 'display,';
						$valueString .= '"0",';
					} else {
						$fieldString .= 'todoDone,';
						$valueString .= '"'. $_POST['todoDone'] .'",';
					}
				}
			} else {
				if ($key == 'editID') {
					$key = 'todoID';
					$udID  = $value;
				} elseif ($key == 'compID')
					$key = 'clientID';
				$fieldString .= $key .',';
				$valueString .= '"'. $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 todo_list ('. $fieldString .') VALUES ('. $valueString .')';
		$result = $con->query($query);
		$editID = (!empty($udID) ? $udID : $con->insert_id);
		
		$updateInfo = ''; 
		if (($_POST['op'] == 'add') || ($_POST['op'] == 'edit')) {
			if (($_POST['op'] == 'add') || ($_POST['op'] == 'edit')) {
				$ajaxUpdate = true; 
				/*if ($page == 'todo') {
					include('../list/list-item/todo.php');
					$updateString = $listItem;
				} else*/if ($page == 'dash') {
					include('../../pages/dashboard/inc/list-item/todo.php');
					$updateString = $listItem;
				}
			}
		}
		$updateID = (($page == 'dash') ? 'todo-'. $editID : 'record-'. $editID);
	}
	$output = (($result) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString,'section'=>$_POST['section']) : 'UPDATE ERROR');
} else {
	$output = 'ERROR 2'; 	
}

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

?>