<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.gridData.php");
include_once(PATH_TO_ROOT."config.php");

$interviewer = '_Interviewer';
$intStructure = '_InterviewerStructure';

class appManManipulator extends GridDataManipulator
{
		
	public function selectAction()
	{
		if($_POST['countGrid'])
		{
			
			$sql = "SELECT DATE_FORMAT(FROM_UNIXTIME(app_time),'%Y-%m-%d') as date, count(*) as count FROM $this->table WHERE status = 91 AND tdisable=0 GROUP BY date";
			if(!$rs = $this->db->SQLexecute($sql)) return $this->db->JsonError();
			$total = 0;
			while ($row = $this->db->fetchAssoc($rs))
			{
				$this->arr[] = $row;
				$total+= $row['count'];
			}
			$this->arr[] = array('date'=>"<b>Total</b>",'count'=>"<b>$total</b>");
				
		}
		else
		{
			
			//lets change the field value of the date
			if(is_array($_POST['filter']))
				foreach ($_POST['filter'] as &$filter) {
					if($filter['field'] == 'app_time')
						$filter['field'] = "DATE_FORMAT(FROM_UNIXTIME(app_time),'%Y-%m-%d')";
					else if($filter['field'] == 'last_call')
						$filter['field'] = "DATE_FORMAT(FROM_UNIXTIME(last_call),'%Y-%m-%d')";
						
				}

		

			// if we have multiple tables and sort enabled for addon fileds we must add b.
			if($_POST['sort']) {
				$extraFields = explode(",", $_POST['str_extraFields']);
				if (in_array($_POST['sort'], $extraFields)) 
					$_POST['sort'] = "b.".$_POST['sort'];
			}	
			
			$this->fetchSelect();
			foreach ($this->arr as &$row)
			{
				if($row['status'] == 91 OR $row['status'] == 22 OR $row['status'] == 25)
				{
					$row['statIcon'] = 'RMicon_cancel_app';
					$row['statTooltip'] = 'Cancel';
				}
				else 
				{
					$row['statIcon'] = 'RMicon_restore_app';
					$row['statTooltip'] = 'Restore';
				}
			}
			
			// create new UPDATE for setSelection on multiple tables
			// get second table name and add over addOnTablesecond table name
			if($_POST['addOn'] == 'setSelection') {
				$getTel = explode(" INNER JOIN ",$_POST['SQL'],2);
				$getTel = explode(" ON ", $getTel[1]);
				$TelName = trim($getTel[0]); 
				$this->addOnTable = "a, ".$TelName;
				$this->addOnTable_where = "AND a.id_tel=b.id";
			}
			
			
		}
		
		return parent::selectAction();
	}
	
	
	public function cancelAppAction()
	{
		$status = $this->getStatus($_POST['id']);
		$sql = "UPDATE $this->table SET status=".$status['cancel']." WHERE status=".$status['original']." AND id = ".$_POST['id'];
		if(!$this->db->SQLexecute($sql)) return $this->db->JsonError();
		return "{success:true}";
	}
	public function restoreAppAction()
	{
		$status = $this->getStatus($_POST['id']);
		$sql = "UPDATE $this->table SET status=".$status['restore']." WHERE status=".$status['original']." AND id = ".$_POST['id'];
		if(!$this->db->SQLexecute($sql)) return $this->db->JsonError();
		return "{success:true}";
	}
	public function deleteAppAction()
	{
		$status = $this->getStatus($_POST['id']);
		$sql = "UPDATE $this->table SET status=".$status['delete']." WHERE status=".$status['original']." AND id = ".$_POST['id'];
		if(!$this->db->SQLexecute($sql)) return $this->db->JsonError();
		return "{success:true}";
	}
	public function reactivateAppAction()
	{
		$status = $this->getStatus($_POST['id']);
		$sql = "UPDATE $this->table SET status=".$status['activate']." WHERE status=".$status['original']." AND id = ".$_POST['id'];
		if(!$this->db->SQLexecute($sql)) return $this->db->JsonError();
		return "{success:true}";
	}
	
	private function getStatus($id) {
		$status = $this->db->get($this->table,$_POST['id'],'status',DB_WARPIT_WEBCATI_BASE);
		if ($status ==21 OR $status==22) {
			$update_status['cancel'] = 21;
			$update_status['restore'] = 22;
		} elseif ($status ==24 OR $status==25) {
			$update_status['cancel'] = 24;
			$update_status['restore'] = 25;
		} elseif ($status ==89 OR $status==91) {
			$update_status['cancel'] = 89;
			$update_status['restore'] = 91;
		}
		$update_status['original'] = $status;
		$update_status['delete'] = 3;
		$update_status['activate'] = 0;
		
		return $update_status;
	}
}

	

$db->ChangeDatabase($_POST['database']);

$tableName = $_POST['RMtable'];
$tableAddon = $tableName.'Addon';

$manipulator = new appManManipulator($db,$_POST['RMtable']);
//$db->LogWrite($_REQUEST['action'], false, "Checking ACTION in ajax.appintmentMan.php");
$manipulator->extraFields = $_POST['extraFields'];
$manipulator->sql_extraFields = $_POST['sql_extraFields'];

$manipulator->tableAddon = $tableAddon;
$comm = $_REQUEST['action'].'Action';
if(method_exists($manipulator,$comm))
	echo $manipulator->$comm();
else 
{
	echo '{success:false,error:"Unknown action!"}';	
}


?>
