<?php
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );

?>
<script type="text/javascript" src="./css/js/calendarDateInput.js"></script> 

<script>
function loadDoc() {
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState == 4 && this.status == 200) {
    // document.getElementById("demo").innerHTML =
	document.getElementById("subject").value=
      this.responseText;
    }
  };
  xmlhttp.open("GET", "modules/affair/main/livesearch1.php", true);
  xmlhttp.send();
}

function loadDoc2() {
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState == 4 && this.status == 200) {
    // document.getElementById("demo").innerHTML =
	document.getElementById("location").value=
      this.responseText;
    }
  };
  xmlhttp.open("GET", "modules/affair/main/livesearch2.php", true);
  xmlhttp.send();
}

</script>

<?php

if(!($_SESSION['admin_affair']=="affair" or $result_permission['p1']==1)){	
exit();
}

function thai_date_3($date){
		if(!(isset($date))){
		return;
		}
$thai_month_arr=array(
	"01"=>"ม.ค.",
	"02"=>"ก.พ.",
	"03"=>"มี.ค.",
	"04"=>"เม.ย.",
	"05"=>"พ.ค.",
	"06"=>"มิ.ย.",	
	"07"=>"ก.ค.",
	"08"=>"ส.ค.",
	"09"=>"ก.ย.",
	"10"=>"ต.ค.",
	"11"=>"พ.ย.",
	"12"=>"ธ.ค."					
);
	$f_date_2=explode(" ", $date);
	$f_date=explode("-", $f_date_2[0]);
	$f_date[2]=intval($f_date[2]);
	$thai_date_return="";
	$thai_date_return.=	$f_date[2];
	$thai_date_return.= " ".$thai_month_arr[$f_date[1]]." ";
	$thai_date_return.=	$f_date[0]+543;
	if($date!=""){
	return $thai_date_return;
	}
	else{
	$thai_date_return="";
	return $thai_date_return;
	}
}


//ส่วนหัว
echo "<br />";
if(!(($index==1) or ($index==2) or ($index==5))){
echo "<table width='100%' border='0' align='center'>";
echo "<tr align='center'><td><font color='#006666' size='3'><strong>ภารกิจศึกษาธิการจังหวัด</strong></font></td></tr>";
echo "</table>";
echo "<br />";
}

//ส่วนฟอร์มรับข้อมูล
if($index==1){
echo "<br>";
echo "<form id='frm1' name='frm1'>";
echo "<Center>";
echo "<Font color='#006666' Size=3><B>เพิ่มภารกิจศึกษาธิการจังหวัด</Font>";
echo "</Cener>";
echo "<Br><Br>";
echo "<Table width='70%' Border='0'>";
echo "<Tr align='left'><Td align='right'>วันที่&nbsp;&nbsp;</Td>";
echo "<Td align='left'>";
?>
<script>
								var Y_date=<?php echo date("Y")?>  
								var m_date=<?php echo date("m")?>  
								var d_date=<?php echo date("d")?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('affair_date', true, 'YYYY-MM-DD', Y_date)</script> 
<?php
echo "</Td></Tr>";
echo "<Tr><Td align='right'>เวลา&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='affair_time' Size='20'></td></tr>";
echo "<Tr><Td align='right'>เรื่องภารกิจ&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='subject' id='subject'  Size='70'><button type='button' onclick='loadDoc()'>เรียกข้อมูลล่าสุด</button></td></tr>";
echo "<Tr><Td align='right'>สถานที่&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='location' id='location' Size='70'><button type='button' onclick='loadDoc2()'>เรียกข้อมูลล่าสุด</button></td></tr>";
echo "<Tr><Td align='right'>ผู้ปฏิบัติ&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><div align='left'><Select name='operation_person' size='1'>";
$sql = "select * from  person_main where status='0' order by position_code,person_order";
$dbquery = mysqli_query($connect,$sql);
While ($result_person = mysqli_fetch_array($dbquery)){
echo  "<option  value ='$result_person[person_id]'>$result_person[prename]$result_person[name] $result_person[surname]</option>" ;
}	
echo "</select>"; 
echo "</div></td></tr>";

echo "<Tr><Td align='right'>หมายเหตุ&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='remark' Size='70'></td></tr>";
echo "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
echo "<tr><td align='right'><INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url(1)'>&nbsp;&nbsp;</td>";
echo "<td align='left'><INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url(0)'></td></tr>";
echo "</Table>";
echo "</form>";
}

//ส่วนยืนยันการลบข้อมูล
if($index==2) {
echo "<table width='500' border='0' align='center'>";
echo "<tr><td align='center'><font color='#990000' size='4'>โปรดยืนยันความต้องการลบข้อมูลอีกครั้ง</font><br></td></tr>";
echo "<tr><td align=center>";
echo "<INPUT TYPE='button' name='smb' value='ยืนยัน' onclick='location.href=\"?option=affair&task=main/affair&index=3&id=$_GET[id]&page=$_REQUEST[page]\"'>
		&nbsp;&nbsp;<INPUT TYPE='button' name='back' value='ยกเลิก' onclick='location.href=\"?option=affair&task=main/affair&page=$_REQUEST[page]\"'";
echo "</td></tr></table>";
}

//ส่วนลบข้อมูล
if($index==3){
$sql = "delete from affair_main where id=$_GET[id]";
$dbquery = mysqli_query($connect,$sql);
}

//ส่วนบันทึกข้อมูล
if($index==4){
$rec_date = date("Y-m-d");
$_POST['subject']=trim($_POST['subject']);
$_POST['location']=trim($_POST['location']);
$sql = "insert into affair_main (affair_date,affair_time,subject,location,operation_person,remark,rec_date,officer) values ('$_POST[affair_date]', '$_POST[affair_time]','$_POST[subject]','$_POST[location]','$_POST[operation_person]','$_POST[remark]','$rec_date','$_SESSION[login_user_id]')";
$dbquery = mysqli_query($connect,$sql);
}

//ส่วนฟอร์มแก้ไขข้อมูล
if ($index==5){
echo "<form id='frm1' name='frm1'>";
echo "<Center>";
echo "<Font color='#006666' Size=3><B>แก้ไข</B></Font>";
echo "</Cener>";
echo "<Br><Br>";
echo "<Table width='50%' Border= '0'>";
$sql = "select * from affair_main left join person_main on affair_main.operation_person=person_main.person_id where affair_main.id='$_GET[id]'";
$dbquery = mysqli_query($connect,$sql);
$ref_result = mysqli_fetch_array($dbquery);
echo "<Tr align='left'><Td align='right'>วันที่&nbsp;&nbsp;</Td>";
echo "<Td align='left'>";
$affair_date=explode("-", $ref_result['affair_date']);
?>
<script>
										var Y_date=<?php echo $affair_date[0]?>  
										var m_date=<?php echo $affair_date[1]?>  
										var d_date=<?php echo $affair_date[2]?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('affair_date', true, 'YYYY-MM-DD', Y_date)</script> 
<?php
echo "</Td></Tr>";
echo "<Tr><Td align='right'>เวลา&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='affair_time' Size='20' value='$ref_result[affair_time]'></td></tr>";
echo "<Tr><Td align='right'>เรื่องภารกิจ&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='subject' Size='70' value='$ref_result[subject]'></td></tr>";
echo "<Tr><Td align='right'>สถานที่&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='location' Size='70' value='$ref_result[location]'></td></tr>";
echo "<Tr><Td align='right'>ผู้ปฏิบัติ&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><div align='left'><Select name='operation_person' size='1'>";
$sql = "select * from  person_main where status='0' order by position_code,person_order";
$dbquery = mysqli_query($connect,$sql);
While ($result_person = mysqli_fetch_array($dbquery)){
			if($result_person['person_id']==$ref_result ['operation_person']){
			echo  "<option  value ='$result_person[person_id]' selected>$result_person[prename]$result_person[name] $result_person[surname]</option>" ;
			}
			else{
			echo  "<option  value ='$result_person[person_id]'>$result_person[prename]$result_person[name] $result_person[surname]</option>" ;
			}
}	
echo "</select>"; 
echo "</div></td></tr>";

echo "<Tr><Td align='right'>หมายเหตุ&nbsp;&nbsp;&nbsp;&nbsp;</Td>";
echo "<td><INPUT TYPE='text' name='remark' Size='70' value='$ref_result[remark]'></td></tr>";
echo "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
echo "<tr><td align='right'><INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url_update(1)'>&nbsp;&nbsp;</td>";
echo "<td align='left'><INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url_update(0)'></td></tr>";
echo "</Table>";
echo "<Br>";
echo "<Input Type=Hidden Name='id' Value='$_GET[id]'>";
echo "<Input Type=Hidden Name='page' Value='$_GET[page]'>";
echo "</form>";
}

//ส่วนปรับปรุงข้อมูล
if ($index==6){
$rec_date = date("Y-m-d");
$sql = "update affair_main set  affair_date='$_POST[affair_date]',affair_time='$_POST[affair_time]',subject='$_POST[subject]',location='$_POST[location]',operation_person='$_POST[operation_person]',remark='$_POST[remark]',officer='$_SESSION[login_user_id]',rec_date='$rec_date' where id='$_POST[id]'";
$dbquery = mysqli_query($connect,$sql);
}

//ส่วนแสดงผล
if(!(($index==1) or ($index==2) or ($index==5))){
//ส่วนของการแยกหน้า
$pagelen=20;  // 1_กำหนดแถวต่อหน้า
$url_link="option=affair&task=main/affair";
$sql = "select id from affair_main";
$dbquery = mysqli_query($connect,$sql);
$num_rows = mysqli_num_rows($dbquery );  
$totalpages=ceil($num_rows/$pagelen);

if(!(isset($_REQUEST['page']))){
$_REQUEST['page']="";
}

if($_REQUEST['page']==""){
$page=$totalpages;
		if($page<2){
		$page=1;
		}
}
else{
		if($totalpages<$_REQUEST['page']){
		$page=$totalpages;
					if($page<1){
					$page=1;
					}
		}
		else{
		$page=$_REQUEST['page'];
		}
}

$start=($page-1)*$pagelen;

if(($totalpages>1) and ($totalpages<16)){
echo "<div align=center>";
echo "หน้า	";
			for($i=1; $i<=$totalpages; $i++)	{
					if($i==$page){
					echo "[<b><font size=+1 color=#990000>$i</font></b>]";
					}
					else {
					echo "<a href=$PHP_SELF?$url_link&page=$i>[$i]</a>";
					}
			}
echo "</div>";
}			
if($totalpages>15){
			if($page <=8){
			$e_page=15;
			$s_page=1;
			}
			if($page>8){
					if($totalpages-$page>=7){
					$e_page=$page+7;
					$s_page=$page-7;
					}
					else{
					$e_page=$totalpages;
					$s_page=$totalpages-15;
					}
			}
			echo "<div align=center>";
			if($page!=1){
			$f_page1=$page-1;
			echo "<<a href=$PHP_SELF?$url_link&page=1>หน้าแรก </a>";
			echo "<<<a href=$PHP_SELF?$url_link&page=$f_page1>หน้าก่อน </a>";
			}
			else {
			echo "หน้า	";
			}					
			for($i=$s_page; $i<=$e_page; $i++){
					if($i==$page){
					echo "[<b><font size=+1 color=#990000>$i</font></b>]";
					}
					else {
					echo "<a href=$PHP_SELF?$url_link&page=$i>[$i]</a>";
					}
			}
			if($page<$totalpages)	{
			$f_page2=$page+1;
			echo "<a href=$PHP_SELF?$url_link&page=$f_page2> หน้าถัดไป</a>>>";
			echo "<a href=$PHP_SELF?$url_link&page=$totalpages> หน้าสุดท้าย</a>>";
			}
echo "</div>";  
}					
//จบแยกหน้า

$sql = "select *,affair_main.id from affair_main left join person_main on affair_main.operation_person=person_main.person_id order by affair_main.affair_date,affair_main.affair_time limit $start,$pagelen";
$dbquery = mysqli_query($connect,$sql);

echo  "<table width='95%' border='0' align='center'>";
echo "<Tr><Td colspan='5' align='left'><INPUT TYPE='button' name='smb' value='เพิ่มภารกิจ' onclick='location.href=\"?option=affair&task=main/affair&index=1\"'></Td></Tr>";
echo "</table>";
echo  "<table width='95%' border='1' align='center' style='border-collapse: collapse'>";
echo "<Tr bgcolor='#FFCCCC'><Td align='center' width='50'>ที่</Td><td align='center' width='100'>วันที่</td><Td align='center' width='120'>เวลา</Td><Td align='center'>เรื่อง</Td><Td align='center'>สถานที่</Td><Td align='center'>ผู้ปฏิบัติ</Td><Td align='center'>หมายเหตุ</Td><Td align='center' width='50'>ลบ</Td><Td align='center' width='50'>แก้ไข</Td></Tr>";
$N=(($page-1)*$pagelen)+1;  //*เกี่ยวข้องกับการแยกหน้า
$M=1;
While ($result = mysqli_fetch_array($dbquery))
	{
		$id = $result['id'];
		$prename = $result['prename'];
		$name = $result['name'];
		$surname = $result['surname'];
			if(($M%2) == 0)
			$color="#FFFFC";
			else  	$color="#FFFFFF";
		echo "<Tr bgcolor=$color><Td align='center'>$N</Td>";
		echo "<Td align='center'>";
		echo thai_date_3($result['affair_date']);
		echo "</Td>";
		echo "<Td align='center'>$result[affair_time]</Td>";
		echo "<Td align='left'>$result[subject]</Td>";
		echo "<Td align='left'>$result[location]</Td>";
		
		$sql_position = "select position_name from person_position where position_code='$result[position_code]'";
		$dbquery_position = mysqli_query($connect,$sql_position);
		$result_position = mysqli_fetch_array($dbquery_position);
		echo "<Td align='left'>$prename$name $surname&nbsp;&nbsp;&nbsp;$result_position[position_name]</Td>";
		echo "<td>$result[remark]</td>";
		echo "<Td align='center'><a href=?option=affair&task=main/affair&index=2&id=$id&page=$page><img src=images/drop.png border='0' alt='ลบ'></a></Td>
		<Td align='center'><a href=?option=affair&task=main/affair&index=5&id=$id&page=$page><img src=images/edit.png border='0' alt='แก้ไข'></a></Td>
	</Tr>";
$M++;
$N++;  //*เกี่ยวข้องกับการแยกหน้า
	}
echo "</Table>";
}

?>
<script>
function goto_url(val){
	if(val==0){
		callfrm("?option=affair&task=main/affair");   // page ย้อนกลับ 
	}else if(val==1){
		if(frm1.subject.value == ""){
			alert("กรุณากรอกเรื่องภารกิจ");
		}else{
			callfrm("?option=affair&task=main/affair&index=4");   //page ประมวลผล
		}
	}
}

function goto_url_update(val){
	if(val==0){
		callfrm("?option=affair&task=main/affair");   // page ย้อนกลับ 
	}else if(val==1){
		if(frm1.subject.value == ""){
			alert("กรุณากรอกเรื่องภารกิจ");
		}else{
			callfrm("?option=affair&task=main/affair&index=6");   //page ประมวลผล
		}
	}
}
</script>
