<?php
error_reporting(0);
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );

if(!(isset($_POST['urlGET']))){
echo "<br />";
echo "<form method='post' action='' id='frm1' name='frm1'>";
echo "<table align='center'>";
echo "<tr>";
echo "<td>";
$sql = "select * from system_sync_smss left join system_school on system_sync_smss.office_code=system_school.school_code order by system_school.school_type,system_school.school_code";
$dbquery = mysqli_query($connect,$sql);
$ROW=1;
$COLUMN=1;
echo "<fieldset>";
echo "<legend>&nbsp;<B>เลือกสถานศึกษา</B>: &nbsp;</legend>";
echo "<table>";
While ($result = mysqli_fetch_array($dbquery)){
	if($ROW==1){
	echo "<tr>";
	}
	echo "<td><input type='checkbox' name='chk' onclick='Check()'>$result[school_code] $result[school_name]<input type='hidden' name='urlGET[]'  value='$result[url]/export/xml.php?username=$result[requester_username]&password=$result[sync_code]&smss_url=$result[url]&order=plan&school_code=$result[school_code]' disabled></td>";
	if($COLUMN==4){
	echo "</tr>";
	$COLUMN=0;
	$ROW=0;
	}
	$ROW++;
	$COLUMN++;
}
echo "</table>";
echo "</fieldset>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<br />";
echo "<div align='center'><input type='submit'></div>";
echo "</form>";
}

if(isset($_POST['urlGET'])){
$urlGET=$_POST['urlGET'];

foreach ($urlGET as &$value) {
   		$url = $value;
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$announce = $xmlDoc->getElementsByTagName('item');
		$announce2 = $xmlDoc->getElementsByTagName('item2');
		
		$warning=base64_decode($xmlDoc->getElementsByTagName('warning')->item(0)->nodeValue);
		if($warning!="1"){
		echo "<br>";
		echo "<div align='center'>";
		echo $warning;
		echo "</div>";
		}

		//ตรวจสอบการเข้าถุึงเว็บsmss
		$smss_fond="0";
		if($warning!=""){
		$smss_fond="1";
		}
		if($smss_fond=="0"){
		$smss_url = explode("?",$url);
		echo "<br>";
		echo "<div align='center'>";
		echo "ไม่สามารถเข้าถึง".$smss_url[0];
		echo "</div>";
		}
		
if($warning=="1" and $smss_fond=="1"){
echo "<br />";		
echo "<table align='center' width='65%' border='0'>";
echo "<tr><td align='center'><b>แผนปฏิบัติการประจำปีงบประมาณ 2559 " ;
		echo base64_decode($xmlDoc->getElementsByTagName('school_name')->item(0)->nodeValue);
		echo " ";
		echo base64_decode($xmlDoc->getElementsByTagName('office_code')->item(0)->nodeValue);
echo "</b></td></tr>";
echo "</table>";

echo  "<table width='95%' border='1' borderColor='#FF0033' align='center' style='border-collapse: collapse'>";
echo   "<Tr bgcolor='#FBD562'><Td width='5%' valign='center' align='center'><font size='2'>ที่</font></td>";
echo "<Td width='5%' valign='center' align='center'><font size='2'>รหัสโครงการ</font></td>";
echo "<Td valign='center' align='center'><font size='2'>ชื่อโครงการ/ชื่อกิจกรรม</font></td>";
echo "<Td width='15%' valign='center' align='center'><font size='2'>งบประมาณ</font></td>";
echo "<Td width='20%' valign='center' align='center'><font size='2'>ระยะเวลา</font></td>";
echo "<Td width='18%' valign='center' align='center'><font size='2'>หัวหน้าโครงการ</font></td>";
echo "</tr>";

		$i=1;
		foreach ($announce as $ann)
		{
		$code_proj=base64_decode($ann->getElementsByTagName('code_proj')->item(0)->nodeValue);
		$name_proj=base64_decode($ann->getElementsByTagName('name_proj')->item(0)->nodeValue);
		$budget_proj_plan2=base64_decode($ann->getElementsByTagName('budget_proj_plan2')->item(0)->nodeValue);
		$proj_time=base64_decode($ann->getElementsByTagName('proj_time')->item(0)->nodeValue);
		$owner=base64_decode($ann->getElementsByTagName('owner')->item(0)->nodeValue);
		
		echo "<tr bgcolor='#CCFFFF' align='left'>";
			echo "<td align='center'><b>$i</b></td>";
			echo "<td align='center'><b>$code_proj</b></td>";
			echo "<td align='left'><b>$name_proj</b></td>";
			echo "<td align='center'><b>$budget_proj_plan2</b></td>";
			echo "<td align='center'><b>$proj_time</b></td>";
			echo "<td align='left'><b>$owner</b></td>";
		echo "</tr>";
		
				foreach ($announce2 as $ann2)
				{
				$code_proj_acti=base64_decode($ann2->getElementsByTagName('code_proj_acti')->item(0)->nodeValue);
				$code_acti=base64_decode($ann2->getElementsByTagName('code_acti')->item(0)->nodeValue);
				$name_acti=base64_decode($ann2->getElementsByTagName('name_acti')->item(0)->nodeValue);
				$budget_acti=base64_decode($ann2->getElementsByTagName('budget_acti')->item(0)->nodeValue);
				$time_acti=base64_decode($ann2->getElementsByTagName('time_acti')->item(0)->nodeValue);
					
					if($code_proj==$code_proj_acti){
					echo "<tr align='left'>";
					echo "<td colspan='2'></td>";
					echo "<td align='left'>$code_acti $name_acti</td>";
					echo "<td align='right'>$budget_acti</td>";
					echo "<td align='right'>$time_acti</td>";
					echo "<td></td>";
					echo "</tr>";
					}
				}
		$i++;
		}
	$budget_proj_sum=base64_decode($xmlDoc->getElementsByTagName('budget_proj_sum')->item(0)->nodeValue);
	echo "<Tr bgcolor='#FBD562' align='center'>";
	echo "<Td colspan='3' align='center'><b>รวม</b></Td><td align='center'><b>$budget_proj_sum</b></td><Td colspan='2'>&nbsp;</Td></Tr>";
	echo "</table>";
	echo "<br>";
	}
} //if not warning	
unset($value); // break the reference with the last element
}
?>

<script>
function Check() {
		var x = document.getElementsByName("chk");
		var y = document.getElementsByName("urlGET[]");
		var i;
		for (i = 0; i < x.length; i++) {
			if (x[i].type == "checkbox") {
				if(x[i].checked == true){
				y[i].disabled=false;
				}
				if(x[i].checked == false){
				y[i].disabled=true;
				}
			}
		}	
}
</script>
