<?php  
session_start(); 
	include("./connect.php");	
	header("Access-Control-Allow-Origin: *");
	
	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
	$cmpmid=$_SESSION['linkarisecmpmid'];

	$brmid = $_SESSION['linkarisebranchid'];
	$umid = $_SESSION['linkariseumid'];
	date_default_timezone_set("Asia/Kolkata");
	$mdate= date('Y-m-d H:i:s');
	$mdate1= date('Y-m-d');
	
	/* if($_SESSION['utype']!='admin')
	{
		header('Location: index.php');
		return;
	} */
	
	$tt="";
	if(isset($_SESSION['trandatelower']))
	{
		$lower = $_SESSION['trandatelower'];
		$upper = $_SESSION['trandateupper'];
	
		if($_SESSION['trandatelower']=="")
		{}
		else
		{
			$tt=" and date(taskaddedon) between '$lower' and '$upper'";			
		}
	}
	
	$tt1="";
	if(isset($_SESSION['istatus']))
	{
		if($_SESSION['istatus']=="all")
		{}
		else
		{
			$istatus=$_SESSION['istatus'];
			$tt1=" and taskstatus='$istatus'";
		}
	}
	
	$tt2="";
	if(isset($_SESSION['taskstage']))
	{
		if($_SESSION['taskstage']=="all")
		{}
		else
		{
			$taskstage=$_SESSION['taskstage'];
			$tt2=" and taskstage='$taskstage'";
		}
	}
	/* unset($_SESSION['istatus']); */
	
	
	
		
		$eve = "select * from res_task_master where taskallocatedto=$umid $tt $tt1 $tt2";
	$re = mysqli_query($conn, $eve);
	
	
	$response = array();
	$posts = array();
	
	$result="0";
	$iaddedby="0";
	
	if(mysqli_num_rows($re) > 0)
	{
		$i=1;
		while($rt = mysqli_fetch_assoc($re))
		{
			 	$tmid=$rt['tmid'];
			 	$taskname=$rt['taskname'];
				
				
				$idetail="<a href='mytaskstatus.php?id=".$rt['tmid']."' data-toggle='tooltip' data-placement='top' title='Detail' class='fa fa-eye fa-lg'>  </a>";
			

				$op=$idetail;
				
				
				
				$taskduedate=$rt['taskduedatetime'];
				$diff = strtotime($taskduedate) - strtotime($mdate1);
				$datediff1=abs(round($diff / 86400));
						
				$tcmid=$rt['tcmid'];
				$taskdescription=$rt['taskdescription'];
				$taskdescription = preg_replace('/[^\da-z ]/i', '', $taskdescription);
				$taskstatus=$rt['taskstatus'];
				$taskstage=$rt['taskstage'];
				$taskremark=$rt['taskremark'];
			
				$taskaddedby=$rt['taskaddedby'];
				$taskcompletedon=$rt['taskcomplatedon'];
				$taskupdatedon=$rt['taskupdatedon'];
				$taskallocatedto=$rt['taskallocatedto'];
				
						if($taskcompletedon == '0000-00-00 00:00:00')
						{
							$taskcompletedon="";
						}
						if($taskupdatedon == '0000-00-00 00:00:00')
						{
							$taskupdatedon="";
						}
			
				
			
						
				$eve22 = "select uname from res_user_master where umid in(select taskaddedby from res_task_master where tmid=$tmid)";
				$re22= mysqli_query($conn, $eve22);
				$ufullname="";
				if(mysqli_num_rows($re) > 0)
				{
					$t1=1;
					while($rt22 = mysqli_fetch_assoc($re22))
					{
						/* $ufullname=$rt22['ufullname']; */
						
						if($t1==1)
						{
							$ufullname = $ufullname.$rt22['uname'];
							$t1=0;
						}
						else
						{
							$ufullname = $ufullname.", ".$rt22['uname'];
						}
					}
				}
									
				
				$istatus1="";
				if($taskstatus=="Open")
				{
					
					$istatus1="<span class='label label-sm label-success'>Open </span>";
				}
				else if($taskstatus=="Closed")
				{
					
					$istatus1="<span class='label label-sm label-warning'>Closed</span>";
				}
				$stage="";
				if($taskstage=="New")
				{
					
					$stage="<span class='badge badge-light-primary fw-bold'>New </span>";
				}
				else if($taskstage=="Completed")
				{
					
					$stage="<span class='badge badge-light-primary fw-bold'>Completed</span>";
				}
				else if($taskstage=="Hold")
				{
					
					$stage="<span class='badge badge-light-danger fw-bold'>Hold</span>";
				}
				else if($taskstage=="In Progress")
				{
					
					$stage="<span class='badge badge-light-warning fw-bold'>In Progress</span>";
				}
				else if($taskstage=="Verified")
				{
					
					$stage="<span class='badge badge-light-primary fw-bold'>Verified</span>";
				}
				
				$eve_rem2 = "select uname from res_user_master where umid=$taskallocatedto";
				$re_rem2 = mysqli_query($conn, $eve_rem2);
				$iaddedbyname="";
				if(mysqli_num_rows($re_rem2) > 0)
				{
					while($rt_rem2 = mysqli_fetch_assoc($re_rem2))
					{
						$iaddedbyname=$rt_rem2['uname'];
					}
				}
			$istatus2="<div style='display: flex; flex-direction: row;'><form action='inquirystatus.php' method='post' style='flex: 0.1; margin-right: 2px;'><button style='border: none; padding: 0; background: blue;' ;    name='tmid' type='submit' value='$tmid'>$istatus1</button></form>";
				$stage1="<div style='display: flex; flex-direction: row;'><form action='taskstatus.php' method='post' style='flex: 0.1; margin-right: 2px;'><button style='border: none; padding: 0; background: blue;' ;    name='tmid' type='submit' value='$tmid'>$stage</button></form>";	
			
				$posts[] = array('tmid'=> $tmid, 'op'=> $op, 'taskduedate'=> $taskduedate, 'datediff1'=> $datediff1,'taskstatus'=> $istatus2,'taskstage'=> $stage,'taskname'=> $taskname,'taskdescription'=> $taskdescription,'taskremark'=> $taskremark,'taskallocatedto'=> $iaddedbyname,'taskaddedby'=> $ufullname,'taskupdatedon'=>$taskupdatedon,'taskcompletedon'=>$taskcompletedon);
	
			
			
			
		}
		 
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('data' => $posts)));
		mysqli_close($conn);
		return;
	}
	
   else
	{
		$result="0";
		$msg="Invalid Error";
		
		$posts[] = array('p1'=> $result,'p2'=> $msg);
	
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('item' => $posts)));
	}
   
	mysqli_close($conn);	
?>