<?php  
session_start(); 
if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}
$rolemid = $_SESSION['userrolemid'];
	include("connect.php");	header("Access-Control-Allow-Origin: *");
	if($_SESSION['utype'] == 'admin' )
	{
		$eve = "select * from res_dmdesign_master ";
	}else{
	  $eve = "select * from res_dmdesign_master where dmassignto= ".$_SESSION['umid']." or appeovestatus	=1 or requestedby = ".$_SESSION['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))
		 {
			 
				$dmdmid=$rt['dmdmid'];
				$branchmid=$rt['branchmid'];
				$countrymid=$rt['countrymid'];
				$visatmid=$rt['visatmid'];
				$subservicemid=$rt['subservicemid'];
				$collegemid	=$rt['collegemid'];
				$dmname	=$rt['dmname'];
				$submittedby = $rt['submittedby'];
				$aprovedby	=$rt['aprovedby'];
				$requestedby=$rt['requestedby'];
				 $dmassignto	=$rt['dmassignto'];
				
				$eveag1 = "select * from res_branch_master where branchmid=$branchmid";
				$reag1 = mysqli_query($conn, $eveag1);
				if(mysqli_num_rows($reag1) > 0)
				{
					while($rtag1 = mysqli_fetch_assoc($reag1))
					{
						$branchname=$rtag1['branchname'];
					}
				}
				else
				{
					$branchname="";
				}
				$eveag1 = "select * from res_country_master where countrymid=$countrymid";
				$reag1 = mysqli_query($conn, $eveag1);
				if(mysqli_num_rows($reag1) > 0)
				{
					while($rtag1 = mysqli_fetch_assoc($reag1))
					{
						$countryname=$rtag1['countryname'];
					}
				}
				else
				{
					$countryname="";
				}
				
				$eve1 = "select * from res_visatype_master where visatmid=$visatmid";

																			$re1 = mysqli_query($conn, $eve1);
																			if(mysqli_num_rows($re1) > 0)
																		{
																			while($rt1 = mysqli_fetch_assoc($re1))

																			{
																				$visatype=$rt1['visatype'];
																			}
																		}
																		else
																		{
																			$visatype="";
																		} 
																$subservicename ="";
																			 $eves = "select * from res_subservice_master where subservicemid = $subservicemid ";
																			$res = mysqli_query($conn, $eves);
																			
																			while($rts = mysqli_fetch_assoc($res))

																			{
																				$subservicename = $rts['subservicename'];
																			}
																			
				
				$eveag1 = "select * from res_college_master where collegemid=$collegemid";
																		$reag1 = mysqli_query($conn, $eveag1);
																		if(mysqli_num_rows($reag1) > 0)
																		{
																			while($rtag1 = mysqli_fetch_assoc($reag1))
																			{
																				$cmname=$rtag1['cmname'];
																			}
																		}
																		else
																		{
																			$cmname="";
																		} 
																		
																		$eveag = "select ufullname,utype from res_user_master where umid=$requestedby";
				$reag = mysqli_query($conn, $eveag);
				if(mysqli_num_rows($reag) > 0)
				{
					while($rtag = mysqli_fetch_assoc($reag))
					{
						$requestedby1=$rtag['ufullname'];
						$utype=$rtag['utype'];
					}
				}
				else
				{
					$requestedby1="";
					$utype="";
				}
					$appeovestatus	=$rt['appeovestatus'];													
																		
				$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='dmdesignedit.php?id=".$rt['dmdmid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg'>  </a>";
				}
				
				$pidview='';
				$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')
					{
				$pidview="<a href='dmdesigndetail.php?id=".$rt['dmdmid']."' data-toggle='tooltip' data-placement='top' title='Edit' > View </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='dmdesigndelete.php?id=".$rt['dmdmid']."'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>";
					}				
                $op=$pidedit."&nbsp;&nbsp;&nbsp; ".$piddelete;
					if($submittedby == 0)
				{
					if($dmassignto == $_SESSION['umid'] || $_SESSION['utype'] == 'admin' )
					{
				 $aa = "<a href='#' data-bs-toggle='modal' data-id='".$dmdmid."' data-bs-target='#kt_modal_close_meeting' class='btn btn-sm btn-primary align-self-center' id='closemeeting'>Upload</a>";
					}else{
						$aa ='';
					}
				}else{

					if($appeovestatus==2 && $submittedby == 0)
					{ $aa = "<a href='#' data-bs-toggle='modal' data-id='".$dmdmid."' data-bs-target='#kt_modal_close_meeting' class='btn btn-sm btn-primary align-self-center' id='closemeeting'>Upload</a>";
						
					}else{
						$dmfilename = $rt['dmfilename'];
					$aa = "<a href='./docs/$dmfilename' target='_blank'>View</a>";
					}
					
				}	
$aa1 ='';				
				if($aprovedby == 0)
				{
					if($submittedby != 0)
					{
						
				if($requestedby == $_SESSION['umid'] || $_SESSION['utype'] == 'admin' )
					{
						
				  $aa1 = "<a href='#' data-bs-toggle='modal' data-id='".$dmdmid."' data-bs-target='#kt_modal_close_meeting1' class='btn btn-sm btn-primary align-self-center' id='closemeeting1'>Approve</a>";
					}
					}else{
						if($appeovestatus == 2)
					{
						$dmrejectremark	=$rt['dmrejectremark'];
						$aa1 = "Rejected <i class='fas fa-exclamation-circle ms-1 fs-7' data-bs-toggle='tooltip' title='$dmrejectremark'></i>";
					}else{
						$aa1 = '';
					}
					}
				}else{
					
					
					
					
					$aa1 = "Approved";
					
				}			
								
				
								
				$posts[] = array('srno'=> $i++,'op'=> $op,'piddelete'=> $piddelete, 'subservicename'=> $subservicename, 'visatype'=> $visatype, 'countryname'=> $countryname,'branchname'=> $branchname,'cmname'=> $cmname,'dmname'=> $dmname, 'upload'=> $aa, 'aprove'=> $aa1, 'requestedby'=> $requestedby1);
		 }
		 
		$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);	
	
?>