<?php  
session_start(); 
if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}

	include("connect.php");	header("Access-Control-Allow-Origin: *");
	$umid = $_SESSION['umid'];
	if($_SESSION['utype'] == 'admin')
	{
		$eve = "select * from res_task_master ";
	}else{
	$eve = "select * from res_task_master where taddedby = $umid";
	}
	$re = mysqli_query($conn, $eve);	
	
	$response = array();
	$posts = array();
	
	$result="0";
	
	if(mysqli_num_rows($re) > 0)
	{
		$i=1;
		 while($rt = mysqli_fetch_assoc($re))
		 {
			 
				$taskmid=$rt['taskmid'];
				$taskname=$rt['taskname'];
				$taskpriority=$rt['taskpriority'];
				
				$taskduedate=date("d-m-Y H:i",strtotime($rt['taskduedate']));
				
				
				
				$pidedit='';
				$currentfile1 = basename($_SERVER['PHP_SELF']);
				 $currentfile = ltrim($currentfile1, 'json');
											$eve23 = "select * from res_moduleallocation_detail where smodulemid in (select smodulemid from res_submodule_master where smfilename='$currentfile') and maedit=1 and  rolemid = ".$_SESSION['userrolemid'];
											$re23 = mysqli_query($conn, $eve23);
	
				if(mysqli_num_rows($re23)>0 || $_SESSION['utype'] == 'admin')
					{
				
				$pidedit="<a href='taskedit.php?id=".$rt['taskmid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg'>  </a>";
				}
				
				$piddetail='';
				$eve23 = "select * from res_moduleallocation_detail where smodulemid in (select smodulemid from res_submodule_master where smfilename='$currentfile') and maview=1 and  rolemid = ".$_SESSION['userrolemid'];
											$re23 = mysqli_query($conn, $eve23);
	
				if(mysqli_num_rows($re23)>0 || $_SESSION['utype'] == 'admin')
					{
				$piddetail="<a href='taskdetail.php?id=".$rt['taskmid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-eye fa-lg'>  </a>";
				
				}
				
				$piddelete='';
				$eve23 = "select * from res_moduleallocation_detail where smodulemid in (select smodulemid from res_submodule_master where smfilename='$currentfile') and madelete=1 and  rolemid = ".$_SESSION['userrolemid'];
											$re23 = mysqli_query($conn, $eve23);
	
				if(mysqli_num_rows($re23)>0 || $_SESSION['utype'] == 'admin')
					{
				
				
				$piddelete="<a href='taskdelete.php?id=".$rt['taskmid']."'onclick='return confirm(&#39;Are you sure you want to delete ?&#39;);' data-toggle='tooltip' data-placement='top' title='Delete' class='far fa-trash-alt fa-lg'>  </a>";	
					}	


$eve44  = "select count(*) as aaa from res_taskallocation_detail where taskmid = $taskmid";
$re44 = mysqli_query($conn, $eve44);
							
							
							$total=0;
							while($rt44 = mysqli_fetch_assoc($re44))
							{
								$total=$rt44['aaa'];
							}
							
							$eve44  = "select count(*) as aa1 from res_taskallocation_detail where taskmid = $taskmid and taskstatus='Completed'";
$re44 = mysqli_query($conn, $eve44);
							
							
							$totalcom=0;
							while($rt44 = mysqli_fetch_assoc($re44))
							{
								$totalcom=$rt44['aa1'];
							}
							
							$eve44  = "select count(*) as aa2 from res_taskallocation_detail where taskmid = $taskmid and taskstatus='Pending'";
							$re44 = mysqli_query($conn, $eve44);
							$totalpending=0;
							while($rt44 = mysqli_fetch_assoc($re44))
							{
								$totalpending=$rt44['aa2'];
							}

if($total == $totalcom)
{
	$tstatus = "<div class='badge badge-light-success fw-bold'>Completed</div>";
}else if ($total == $totalpending)
{
	$tstatus = "<div class='badge badge-light-danger fw-bold'>Pending</div>";
}else{
	$tstatus = "<div class='badge badge-light-primary fw-bold'>In Process</div>";
}	
                $op=$piddetail."&nbsp;&nbsp;&nbsp; ".$pidedit."&nbsp;&nbsp;&nbsp; ".$piddelete;
								
								
				
				
								
				$posts[] = array('srno'=> $i++,'op'=> $op,'piddelete'=> $piddelete, 'taskname'=> $taskname, 'taskpriority'=> $taskpriority,  'taskduedate'=> $taskduedate,  'tstatus'=> $tstatus);
		 }
		 
		$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);	
	
?>