<?php
ini_set('display_errors', 1);

require("./../../dbconnect_mysqli.php");
require("./../../functions.php");
$data = array();

if($_POST)
{
	if($_POST['action'] == 'getResultByDate')
	{
		$month = $_POST['month'];
		$year = $_POST['year'];

		$stmt = 'SELECT id, group_concat(user SEPARATOR ", ") as users, event_date, description, group_concat(id SEPARATOR ",") as id_string, unique_activity_id FROM admin_day_planner WHERE event_date like "'.$year.'-'.$month.'%" group by event_date,unique_activity_id; ';
		$rslt=mysql_to_mysqli($stmt, $link);

		$activity_ct = mysqli_num_rows($rslt);

		$o = 0;

		while($activity_ct > $o)
		{
			$row=mysqli_fetch_row($rslt);
			$data[$o]['id'] = '_'.$row[5];
			$data[$o]['name'] = 'Activity For Today :-';
			$data[$o]['date'] = $row[2];
			$data[$o]['description'] = '<hr style="background-color:white;margin-bottom: 0px;"><input type="hidden" value="'.$row[4].'" class="id_string_for_addOrDelete"><div style="text-align:center">USERS</div><hr style="background-color:white;margin-top: 0px;"><b><u>'.$row[1].'</u></b><br><hr style="background-color:white;">'.$row[3];
			$data[$o]['type'] = 'event';
			$o++;
		}
		
		echo json_encode($data); 
	}
	elseif($_POST['action'] == 'getResultByUser')
	{
		$userString = $_POST['id_string'];

		if($userString)
		{
			$stmt = "SELECT GROUP_CONCAT(id) as id, GROUP_CONCAT(user) as user, event_date, description, unique_activity_id FROM admin_day_planner WHERE id IN($userString) GROUP BY event_date,unique_activity_id;";
			$rslt=mysql_to_mysqli($stmt, $link);

			$activity_ct = mysqli_num_rows($rslt);
			$o = 0;

			while($activity_ct > $o)
			{
				$row=mysqli_fetch_assoc($rslt);
				$data[$o] = $row;
				$o++;
			}
			echo json_encode($data);
		}
	}
	elseif($_POST['action'] == 'deleteDataById')
	{
		$user_id_forDelete = $_POST['user_id_forDelete'];
		$unique_id_forDelete = $_POST['unique_id'];
		$event_date = $_POST['event_date'];
		$type = $_POST['type'];

		if($type == 'one')
		{
			$whereQuery = "user IN($user_id_forDelete) AND unique_activity_id = '".$unique_id_forDelete."' AND event_date = '".$event_date."' ";
		}
		elseif($type == 'all')
		{
			$whereQuery = "user IN ($user_id_forDelete) AND unique_activity_id = '".$unique_id_forDelete."' ";
		}
		$stmt = "DELETE FROM admin_day_planner WHERE $whereQuery;";

		$rslt=mysql_to_mysqli($stmt, $link);

		if($rslt)
		{
			$data['result'] = TRUE;
		}
		else
		{
			$data['result'] = FALSE;
		}
		echo json_encode($data);
	}
}


?>