<?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_traningmodule_master";
	}else{
		$eve = "select * from res_traningmodule_master where countrymid in (select countrymid from res_traningmoduleroleallocation_detail where rolemid=$rolemid) and traningservicemid in (select traningservicemid from res_traningmoduleroleallocation_detail where rolemid=$rolemid) and traningsubservicemid in (select traningsubservicemid from res_traningmoduleroleallocation_detail where rolemid=$rolemid)  ";
	}
	$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))
		 {
			 
				$tmmid=$rt['tmmid'];
				$countrymid=$rt['countrymid'];
				$traningservicemid=$rt['traningservicemid'];
				$traningsubservicemid=$rt['traningsubservicemid'];
				
				
				$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="";
				}
				
				$eveag1 = "select * from res_traningservice_master where traningservicemid=$traningservicemid";
				$reag1 = mysqli_query($conn, $eveag1);
				if(mysqli_num_rows($reag1) > 0)
				{
					while($rtag1 = mysqli_fetch_assoc($reag1))
					{
						$traningservicename=$rtag1['traningservicename'];
					}
				}
				else
				{
					$traningservicename="";
				}
				$eveag1 = "select * from res_traningsubservice_master where traningsubservicemid=$traningsubservicemid";
				$reag1 = mysqli_query($conn, $eveag1);
				if(mysqli_num_rows($reag1) > 0)
				{
					while($rtag1 = mysqli_fetch_assoc($reag1))
					{
						$traningsubservicename=$rtag1['traningsubservicename'];
					}
				}
				else
				{
					$traningsubservicename="";
				}
				
				/* $eveag1 = "select * from res_traningdocs_master where traningdocsmid=$traningdocsmid";
				$reag1 = mysqli_query($conn, $eveag1);
				if(mysqli_num_rows($reag1) > 0)
				{
					while($rtag1 = mysqli_fetch_assoc($reag1))
					{
						$traningdocsname=$rtag1['traningdocsname'];
					}
				}
				else
				{
					$traningdocsname="";
				} */
				$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='traningmoduleedit.php?id=".$rt['tmmid']."' 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='traningmoduledoctypedetail.php?id=".$rt['tmmid']."' 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='traningmoduledelete.php?id=".$rt['tmmid']."'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;
								
								
				//$tmdocument1 = "<a href='./docs/$tmdocument' target='_blank'>View</a>";
								
				$posts[] = array('srno'=> $i++,'op'=> $op,'piddelete'=> $piddelete, 'traningservicename'=> $traningservicename, 'traningsubservicename'=> $traningsubservicename, 'countryname'=> $countryname, 'pidview'=> $pidview);
		 }
		 
		$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);	
	
?>