Path : /var/www/html/smart_kpp2_v2/modules/person/
File Upload :
Current File : /var/www/html/smart_kpp2_v2/modules/person/person_sch.php-cc

<?php
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
//if(!(($_SESSION['admin_person']=="person") or ($_SESSION['login_status']<=4 and $result_permission['p1']==1) or $permission_p==1)){
//admin module /เจ้าหน้าที่เขต /เจ้าหน้าที่โรงเรียน
if(!(($_SESSION['admin_person']=="person") or ($permission_p==0) or ($result_permission['p1']==1))){	
exit();
}
?>

<script type="text/javascript">
function showResult(str) {
 if (str.length==0) { 
  document.getElementById("livesearch").innerHTML="";
  document.getElementById("livesearch").style.border="0px";
  return;
 }
 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("livesearch").innerHTML=this.responseText;
   document.getElementById("livesearch").style.border="1px solid #A5ACB2";
  }
 }
 xmlhttp.open("GET","modules/person/livesearch_2.php?q="+str,true);
 xmlhttp.send();
}
</script>

<?php
include("include/time_inc.php");
$officer=$_SESSION['login_user_id'];

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

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

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

//ฟังชั่นupload
function file_upload() {
	$uploaddir = 'modules/person/picture/';      //ที่เก็บไไฟล์
	$uploadfile = $uploaddir.basename($_FILES['userfile']['name']);
	$basename = basename($_FILES['userfile']['name']);

	$pic_code=$_POST['person_id'];
	//ลบไฟล์เดิม
	$exists_file=$uploaddir.$pic_code.substr($basename,-4);
	if(file_exists($exists_file)){
		unlink($exists_file);
	}

	if (move_uploaded_file($_FILES['userfile']['tmp_name'],  $uploadfile))
		{
		$before_name  = $uploaddir.$basename;
		$changed_name = $uploaddir.$pic_code.substr($basename,-4) ;
		rename("$before_name" , "$changed_name");
		
		//ลดขนาดภาพ
		if(substr($basename,-3)=="JPG" or substr($basename,-3)=="jpg" or substr($basename,-3)=="png" or substr($basename,-3)=="gif"){		
		//if(substr($basename,-3)=="JPG" or substr($basename,-3)=="jpg"){		
			$ori_file=$changed_name;
			$ori_size=getimagesize($ori_file);
			$ori_w=$ori_size[0];
			$ori_h=$ori_size[1];
			if($ori_w>500){
				$new_w=500;
				$new_h=round(($new_w/$ori_w)*$ori_h);
				switch (substr($basename,-3)){
					case "png";
					$ori_img=imagecreatefrompng($ori_file);
					break;
					case "gif";
					$ori_img=imagecreatefromgif($ori_file);
					break;
					default;
					$ori_img=imagecreatefromjpeg($ori_file);
					break;
				}
				$new_img=imagecreatetruecolor($new_w, $new_h);
				imagecopyresized($new_img, $ori_img,0,0, 0,0, $new_w, $new_h, $ori_w, $ori_h);
				$new_file=$ori_file;
				switch (substr($basename,-3)){
					case "png";
					imagepng($new_img, $new_file);
					break;
					case "gif";
					imagegif($new_img, $new_file);
					break;
					default;
					imagejpeg($new_img, $new_file);
					break;
				}
				imagedestroy($ori_img);
				imagedestroy($new_img);
			}	
		}						
		return  $changed_name;
	}
}

$sql = "select * from person_sch_position order by position_code";
$dbquery = mysqli_query($connect,$sql);
While ($result = mysqli_fetch_array($dbquery)){
$position_ar[$result['position_code']]=$result['position_name'];
}

$sql = "select * from system_school";
$dbquery = mysqli_query($connect,$sql);
While ($result = mysqli_fetch_array($dbquery)){
$school_ar[$result['school_code']]=$result['school_name'];
}

echo "<br />";
if(!(($index==1) or ($index==1.1) or ($index==2) or ($index==4) or ($index==5) or ($index==5.1) or ($index==5.2))){
//if(!(($index==1) or ($index==2) or ($index==5))){
echo "<table width='50%' 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 "<form Enctype = multipart/form-data id='frm1' name='frm1'>";
echo "<Center>";
echo "<Font color='#006666' Size=3><B>เพิ่ม ข้อมูลรายละเอียดบุคลากร</B></Font>";
echo "</Cener>";
echo "<Br><Br>";
echo "<Table width='60%' Border='0' class='myTableForm'>";
echo "<Tr align='left'><Td width=30></Td><Td align='right'>เลขประจำตัวประชาชน&nbsp;</Td><Td><Input Type='Text' Name='person_id' Size='13' maxlenght='13' onkeydown='integerOnly()'></Td></Tr>";
echo "<Tr align='left'><Td width=30></Td><Td align='right'>คำนำหน้าชื่อ&nbsp;</Td><Td><Input Type='Text' Name='prename' Size='15'></Td></Tr>";
echo "<Tr align='left'><Td ></Td><Td align='right'>ชื่อ&nbsp;</Td><Td><Input Type='Text' Name='name' Size='40'></Td></Tr>";
echo "<Tr align='left'><Td ></Td><Td align='right'>นามสกุล&nbsp;</Td><Td><Input Type='Text' Name='surname' Size='40'></Td></Tr>";

echo "<Tr align='left'><Td ></Td><Td align='right'>ตำแหน่ง&nbsp;</Td><Td>";
echo "<Select name='position_code' size='1'>";
echo "<option value = ''>เลือก</option>" ;

$sql = "select * from person_sch_position order by position_code";
$dbquery = mysqli_query($connect,$sql);
While ($person_result = mysqli_fetch_array($dbquery)){
echo "<option value ='$person_result[position_code]'>$person_result[position_code] $person_result[position_name]</option>" ;
}	
echo "</select>";
echo "</Td></Tr>";

echo "<Tr align='left'><Td ></Td><Td align='right'>สถานศึกษา&nbsp;</Td><Td>";
if($permission_p=='0'){
	$sql = "select * from system_school where school_code='$_SESSION[user_school]' ";
	$dbquery = mysqli_query($connect,$sql);
	$result=mysqli_fetch_array($dbquery);
	echo $result[school_code]." ".$result[school_name];
	echo "<input type=hidden name=school_code value='$result[school_code]'>";
	echo "<input type=hidden name=status value='9'>";
}else{
	echo "<Select name='school_code' size='1'>";
	echo "<option value = ''>เลือก</option>" ;

	$sql = "select * from system_school order by school_type,school_code";
	$dbquery = mysqli_query($connect,$sql);
	While ($school_result = mysqli_fetch_array($dbquery)){
	echo "<option value ='$school_result[school_code]'>$school_result[school_code] $school_result[school_name]</option>" ;
	}	
	echo "</select>";
	echo "<input type=hidden name=status value='0'>";
}	//end if($permission_p)
echo "</Td></Tr>";

echo "<Tr align='left'><Td ></Td><Td align='right'>ลำดับบุคลากรในตำแหน่ง&nbsp;</Td><Td><Input Type='Text' Name='person_order' Size='4'>&nbsp;(หากประสงค์เรียงลำดับ)</Td></Tr>";

echo "<tr align='left'>";
echo "<Td ></Td><td align='right'>ไฟล์รูปภาพ&nbsp;</td>";
echo "<td align='left'><input name = 'userfile' type = 'file'></td>";
echo "</tr>";

if($result_permission['p1']==1){
echo "<tr align='left'>";
echo "<Td ></Td><td align='right' value='top'>ปฏิบัติงานในสถานศึกษาอื่นด้วย&nbsp;<br>(กรณีเจ้าหน้าที่ธุรการที่ปฏิบัติงานหลายโรงเรียน)&nbsp;</td>";
echo "<td align='left'><input name = 'userfile' type ='radio' name='other' value='1'> ปฏิบัติ<br>
	<input name = 'userfile' type ='radio' name='other' value='0' checked> ไม่ได้ปฏิบัติ</td>";
echo "</tr>";
}
echo "<tr><td></td><td></td><td>";
echo "<INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url(1)'>&nbsp;<INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url(0)'>";
echo "</td></tr>";
echo "</Table>";
echo "</form>";

}

//ส่วนกรอกข้อมูลรายละเอียดบุคลากร
if($index==1.1){
echo "<form Enctype = multipart/form-data id='frm1' name='frm1'>";
echo "<Center>";
echo "<Font color='#006666' Size=3><B>เพิ่ม ข้อมูลรายละเอียดบุคลากร</B></Font>";
echo "</Cener>";
$sql = "select * from person_sch_main where person_id='$_GET[person_id]'";
$dbquery = mysqli_query($connect,$sql);
$result = mysqli_fetch_array($dbquery);
echo "<Table Border='0' align=center class='myTableForm' width=50%>";
echo "<Tr align='left'><Td align='right'><font color=#c0c0c0>เลขประจำตัวประชาชน</font> &nbsp;</Td><Td>$result[person_id]</Td></Tr>";
echo "<input type=hidden name=person_id value='$result[person_id]'>";
echo "<Tr><Td align='right'>คำนำหน้า</Td><Td><input type=text name=prename value=$result[prename]><TR>";
echo "<Tr><Td align='right'>ชื่อ</Td><Td><input type=text name=name value=$result[name]><TR>";
echo "<Tr><Td align='right'>สกุล</Td><Td><input type=text name=surname value=$result[surname]><TR>";

echo "<Tr><Td align='right'>ตำแหน่ง</Td><Td>";
echo "<select name=position_code>";
$sql_position = "select * from person_sch_position";
$dbquery_position=mysqli_query($connect,$sql_position);
while ($result_position = mysqli_fetch_array($dbquery_position)){
	if($result_position[position_code]==$result[position_code]){
		echo "<option value=$result_position[position_code] selected>$result_position[position_name]</option>";
	}else{
		echo "<option value=$result_position[position_code]>$result_position[position_name]</option>";
	}
}
echo "</select>";
echo "</Td></Tr>";

echo "<Tr align='left'><Td align='right'>สถานศึกษา <font color=red>กรณี ย้ายไปโรงเรียนอื่นในสังกัด</font></Td><Td>";
	echo "<Select name='school_code' size='1'>";
	echo "<option value = ''>เลือก</option>" ;
	$sql = "select * from system_school order by school_type,school_code";
	$dbquery = mysqli_query($connect,$sql);
	while ($result_school = mysqli_fetch_array($dbquery)){
		if($result_school[school_code]==$school_code){
			echo "<option value ='$result_school[school_code]' selected>$result_school[school_code] $result_school[school_name]</option>" ;
		}else{
			echo "<option value ='$result_school[school_code]'>$result_school[school_code] $result_school[school_name]</option>" ;
		}
	}	
	echo "</select>";
	echo "<input type=hidden name=status value='0'>";
echo "</Td></Tr>";

echo "<tr><td colspan=2 align=center>กรณี ย้ายไปโรงเรียนสังกัดหน่วยงานอื่น คลิก -> <a href=?option=person&task=person_sch&index=5.1&id=$id&page=$page&school_code=$school_code><img src=images/my-icons/no.png width=22 border='0' title='กำหนดสถานะภาพ'> เมนู กำหนดสถานะภาพ</a></td>";

echo "<tr align='left'>";
echo "<td align='right'>ไฟล์รูปภาพ</td>";
echo "<td align='left'><input name='userfile' type='file'></td>";
echo "</tr>";

echo "<tr><td align=right>เพศ</td><td>";
	echo "<input type=radio name=sex value=1> ชาย &nbsp;&nbsp; ";
	echo "<input type=radio name=sex value=2> หญิง";
echo "</Td></Tr>";
echo "<tr><td align=right>ชื่อเล่น</td><td><input type='text' name='nickname' style='width:100px;'></td></tr>";
$day=date("Y-m-d");
$today=getDatePickerThai($day);
echo "<tr><td align=right>วัน/เดือน/ปี (เกิด)</td><td>
	<input type=\"text\" name=\"birth_day\" id='datepicker-th-1' value='$today' style='width:100px;'>
	</td></tr>";
echo "<tr><td align=right>วัน/เดือน/ปี (บรรจุครั้งแรก)</td><td>
	<input type=\"text\" name=\"start_day\" id='datepicker-th-2' value='$today' style='width:100px;'>
	</td></tr>";
echo "<Tr align='left'><Td align='right'>วิทยฐานะ/ระดับ</Td><Td>";
echo "<select name='position_class' style='width:180px'>";
$sql = "select * from person_position_class order by code";
$dbquery = mysqli_query($connect,$sql);
while($result=mysqli_fetch_array($dbquery)){
	echo "<option value='$result[code]'>$result[name]</option>";
}
echo "</Td></Tr>";
echo "<Tr align='left'><Td align='right'>วุฒิการศึกษาสูงสุด</Td><Td>";
echo "<select name='education_class' style='width:180px'>";
$sql = "select * from person_education_class order by code";
$dbquery = mysqli_query($connect,$sql);
while($result=mysqli_fetch_array($dbquery)){
	echo "<option value='$result[code]'>$result[name]</option>";
}
echo "</Td></Tr>";
echo "<tr><td align=right>ชื่อวุฒิการศึกษา/วิชาเอก <font color=#ff8000>เช่น บริหารธุรกิจบัณฑิต(การบัญชี)</font></td><td><input type=text name=major size=30></td></tr>";
echo "<tr><td align=right valign=top>อีเมล์</td><td>";
	echo "<input type=text name=email_1 size=30><br>";
	echo "<input type=text name=email_2 size=30><br>";
	echo "<input type=text name=email_3 size=30>";
echo "</td></tr>";
echo "<tr><td align=right valign=top>หมายเลขโทรศัพท์</td><td>";
	echo "<input type=text name=tel_1 size=15><br>";
	echo "<input type=text name=tel_2 size=15><br>";
	echo "<input type=text name=tel_3 size=15>";
echo "</td></tr>";
echo "<tr><td align=right>เว็บไซต์</td><td><input type=text name=url size=30></td></tr>";

echo "<tr><td></td><td align=left>";
	echo "<Input Type=Hidden Name='person_id' Value='$_GET[person_id]'>";
	echo "<Input Type=Hidden Name='page' Value='$_GET[page]'>";
	echo "<Input Type=Hidden Name='school_code' Value='$_GET[school_code]'>";
	echo "<INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url(2)'>&nbsp;<INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url(0)'>";
echo "</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=person&task=person_sch&index=3&id=$_GET[id]&page=$_REQUEST[page]&school_code=$_REQUEST[school_code]\"'>
		&nbsp;<INPUT TYPE='button' name='back' value='ยกเลิก' onclick='location.href=\"?option=person&task=person_sch&page=$_REQUEST[page]&school_code=$_REQUEST[school_code]\"'";
echo "</td></tr></table>";
}

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

if($index==3.1){
	foreach ($_POST as $person_id =>$person_value){
$sql = "delete from person_sch_main where person_id='$person_id'";
$dbquery = mysqli_query($connect,$sql);
	}
}

//ส่วนเพิ่มข้อมูล
if($index==4){
$rec_date = date("Y-m-d");
$sql = "select * from person_sch_main where person_id='$_POST[person_id]' ";
$dbquery = mysqli_query($connect,$sql);
if(mysqli_num_rows($dbquery)>=1){
echo "<br /><div align='center'>มีเลขประจำตัวประชาชนซ้ำกับรายการที่มีอยู่แล้ว ตรวจสอบอีกครั้ง</div>";
exit(); 
}

$basename = basename($_FILES['userfile']['name']);
if ($basename!="")
{
$changed_name = file_upload();
}
if(!(isset($changed_name))){
$changed_name="";
}
if(!(isset($_POST['other']))){
$_POST['other']=0;
}

if($_POST['person_order']==""){
$_POST['person_order']=0;
}
$sql = "insert into person_sch_main (person_id,prename,name,surname,position_code,school_code,pic,status,person_order,officer,rec_date, other) values ( '$_POST[person_id]','$_POST[prename]','$_POST[name]','$_POST[surname]','$_POST[position_code]','$_POST[school_code]','$changed_name','$_POST[status]','$_POST[person_order]','$officer','$rec_date' ,'$_POST[other]')";
$dbquery = mysqli_query($connect,$sql);
}

//ส่วนเพิ่มรายละเอียดบุคลากร
if($index==4.1){
	$rec_date = date("Y-m-d");
	$basename = basename($_FILES['userfile']['name']);
	if ($basename!=""){
		$changed_name = file_upload();
		$sql = "update person_sch_main set prename='$_POST[prename]', name='$_POST[name]', surname='$_POST[surname]', pic='$changed_name', position_code='$_POST[position_code]',officer='$officer',rec_date='$rec_date' where person_id='$_POST[person_id]'";
	}else{
		$sql = "update person_sch_main set prename='$_POST[prename]', name='$_POST[name]', surname='$_POST[surname]', position_code='$_POST[position_code]', officer='$officer',rec_date='$rec_date' where person_id='$_POST[person_id]'";
	}
	$dbquery = mysqli_query($connect,$sql);

$birth_day=getDatePickerEng($_POST[birth_day]);
$start_day=getDatePickerEng($_POST[start_day]);
list($year_birth, $month_birth, $day)= explode("-",$birth_day); 
if($month_birth<=9){
	$year=$year_birth+60;
	$end_day=$year."-09-30";
}else{
	$year=$year_birth+61;
	$end_day=$year."-09-30";
}
if($_POST[email_1]!=''){ $email=$_POST[email_1]; }
if($_POST[email_2]!=''){ $email=$email."|".$_POST[email_2]; }
if($_POST[email_3]!=''){ $email=$email."|".$_POST[email_3]; }
$search = array(" ","-","(",")");
$replace="";
$email=str_replace($search,$replace,$email);
if($_POST[tel_1]!=''){ $tel=$_POST[tel_1]; }
if($_POST[tel_2]!=''){ $tel=$tel."|".$_POST[tel_2]; }
if($_POST[tel_3]!=''){ $tel=$tel."|".$_POST[tel_3]; }
$tel=str_replace($search,$replace,$tel);

$sql = "insert into person_detail (person_id,position_class,education_class,major,sex,nickname,birth_day,start_day,end_day,email,tel,url) values ('$_POST[person_id]','$_POST[position_class]','$_POST[education_class]','$_POST[major]','$_POST[sex]','$_POST[nickname]','$birth_day','$start_day','$end_day','$email','$tel','$_POST[url]')";
$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>";
//$sql = "select *,person_detail.id from person_detail left join person_sch_main on person_detail.person_id=person_sch_main.person_id where person_detail.person_id='$_GET[person_id]'";
$sql="select * from person_sch_main where person_id='$_GET[person_id]'";
$dbquery = mysqli_query($connect,$sql);
$result = mysqli_fetch_array($dbquery);
echo "<Table Border='0' align=center class='myTableForm' width=50%>";
echo "<Tr align='left'><Td align='right'><font color=#c0c0c0>ID</font> &nbsp;</Td><Td>$result[id]</Td></Tr>";
echo "<Tr align='left'><Td align='right'><font color=#c0c0c0>เลขประจำตัวประชาชน</font> &nbsp;</Td><Td>$result[person_id]</Td></Tr>";
echo "<Tr><Td align='right'>คำนำหน้า</Td><Td>$result[prename]<TR>";
echo "<Tr><Td align='right'>ชื่อ</Td><Td>$result[name]<TR>";
echo "<Tr><Td align='right'>สกุล</Td><Td>$result[surname]<TR>";

echo "<Tr><Td align='right'>ตำแหน่ง</Td><Td>";
echo "<select name=position_code>";
echo "<option value=''>เลือก ตำแหน่ง</option>";
$sql="select * from person_sch_position order by position_code";
$dbquery=mysqli_query($connect,$sql);
while ($result_position = mysqli_fetch_array($dbquery)){
	if($result_position[position_code]==$result[position_code]){
		echo "<option value='$result_position[position_code]' selected>$result_position[position_name]</option>";
	}else{
		echo "<option value='$result_position[position_code]'>$result_position[position_name]</option>";
	}
}
echo "</select>";
echo "</Td></Tr>";

echo "<Tr><Td align='right'>สถานศึกษา</Td><Td>";
echo "<select name='school_code'>";
echo "<option value=''>เลือก สถานศึกษา</option>";
$sql="select * from system_school order by school_code";
$dbquery=mysqli_query($connect,$sql);
while ($result_school=mysqli_fetch_array($dbquery)){
	if($result_school[school_code]==$result[school_code]){
		echo "<option value='$result_school[school_code]' selected>$result_school[school_code] $result_school[school_name]</option>";
	}else{
		echo "<option value=$result_school[school_code]>$result_school[school_code] $result_school[school_name]</option>";
	}
}
echo "</select>";
echo "</Td></Tr>";

if($result_permission['p1']==1){
	if($result['other']==1){ 
		$other_checked1="checked"; $other_checked2="";
	}else{ 
		$other_checked1=""; $other_checked2="checked";
	}
	echo  "<tr align='left'>";
	echo  "<td align='right' valign='top'>ปฏิบัติงานโรงเรียนอื่นอีกด้วย<br><font color=red>(กรณีเจ้าหน้าที่ธุรการที่ปฏิบัติงานหลายโรงเรียน)</font></td>";
	echo  "<td align='left'><input type ='radio' name='other' value='1' $other_checked1> ปฏิบัติ<br>
		<input type ='radio' name='other' value='0' $other_checked2> ไม่ได้ปฏิบัติ</td>";
	echo  "</tr>";
}
echo "<tr><td></td><td align=left>"; 
echo "<input type=hidden name=person_id value='$result[person_id]'>";
echo "<input type=hidden name=prename value='$result[prename]'>";
echo "<input type=hidden name=name value='$result[name]'>";
echo "<input type=hidden name=surname value='$result[surname]'>";
echo "<Input Type=Hidden Name='page' Value='$_GET[page]'>";
echo "<INPUT TYPE='submit' name='smb' value='ตกลง' onclick='goto_url_update(1)'>&nbsp;<INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url_update(0)'>";
echo "</td></tr>";
echo "</Table>";
echo "<table align=center><tr><td>";
	echo "หมายเหตุ<br>
		การแก้ไขนี้เป็นการแก้ไขข้อมูลบุคลากร <font color=red><b>กรณี มีเลขประชาชนอยู่ในระบบแต่เกิดความผิดพลาด</b></font> เช่น รหัสตำแหน่งเปลี่ยน/ ยุบโรงเรียนด้วยการลบ / อื่นๆ";
echo "</td></tr></table>";
echo "</form>";
}

//ส่วนฟอร์มแก้ไขข้อมูลสถานะ
if ($index==5.1){
echo "<form id=='frm1' name='frm1'>";
echo "<Center>";
echo "<Font color='#006666' Size=3><B>แก้ไข สถานะบุคลากร</B></Font>";
echo "</Cener>";
echo "<Br><Br>";
$sql = "select * from person_sch_main where person_id='$_GET[person_id]'";
$dbquery = mysqli_query($connect,$sql);
$result = mysqli_fetch_array($dbquery);
echo "<Table Border='0' align=center>";
echo "<Tr align='left'><Td align='right'>เลขประจำตัวประชาชน&nbsp;</Td><Td>$result[person_id]</Td></Tr>";
echo "<Tr align='left'><Td align='right'>คำนำหน้าชื่อ&nbsp;</Td><Td>$result[prename]</Td></Tr>";
echo "<Tr align='left'><Td align='right'>ชื่อ&nbsp;</Td><Td>$result[name]</Td></Tr>";
echo "<Tr align='left'><Td align='right'>นามสกุล&nbsp;</Td><Td>$result[surname]</Td></Tr>";
echo "<Tr align='left'><Td align='right'>สถานะภาพ&nbsp;</Td><Td>";
echo "<Select name='status' size='1'>";
echo "<option value = ''>เลือก</option>" ;
if($result['status']==0){
echo "<option value ='0' selected>ปฏิบัติงานในสถานศึกษาปัจจุบัน</option>" ;
echo "<option value ='1'>เคยปฏิบัติงานในสถานศึกษา</option>" ;
}
else{
echo "<option value ='0'>ปฏิบัติงานในสถานศึกษาปัจจุบัน</option>" ;
echo "<option value ='1' selected>เคยปฏิบัติงานในสถานศึกษา</option>" ;
}
echo "</select>";
echo "</Td></Tr>";

echo "<Tr align='left'><Td align='right'></Td><Td><br>";
echo "<Input Type=Hidden Name='id' Value='$_GET[id]'>";
echo "<input type='hidden' name='person_id' value='$result[person_id]'>";
echo "<Input Type=Hidden Name='school_code' Value='$result[school_code]'>";
echo "<INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url_update(2)'>&nbsp;<INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url_update(0)'>";
echo "</td></tr>";
echo "</Table>";
echo "<br>";
echo "<table align=center><tr><td><strong>หมายเหตุ</strong></td></tr>
	<tr><td>ใช้กรณี <font color=red>ย้ายออกนอกสังกัด, เกษียณ, ลาออก, เสียชีวิต ฯลฯ</font>&nbsp;</td></tr>
	<tr><td>การดำเนินการเปลี่ยนสถานะ จะไม่ลบข้อมูลออกจากระบบ</td></tr>
	<tr><td>เมื่อเปลี่ยนสถานะเป็นอดีตแล้วผู้นั้นจะไม่สามารถ Login และไม่ปรากฎชื่อเป็นบุคลากรปัจจุบัน</td></tr>
	</table>";
echo "</form>";
}

//ส่วนฟอร์มแก้ไขข้อมูลรายละเอียดบุคลากร
if ($index==5.2){
echo "<form Enctype = multipart/form-data id='frm1' name='frm1'>";
echo "<Center>";
echo "<Font color='#006666' Size=3><B>แก้ไข ข้อมูลรายละเอียดบุคลากร</B></Font>";
echo "</Cener>";
echo "<br>";
$sql = "select * from person_detail left join person_sch_main on person_detail.person_id=person_sch_main.person_id where person_detail.person_id='$_GET[person_id]'";
$dbquery = mysqli_query($connect,$sql);
$result = mysqli_fetch_array($dbquery);
echo "<Table Border='0' align=center class='myTableForm' width=50%>";
echo "<Tr align='left'><Td align='right'><font color=#c0c0c0>ID</font> &nbsp;</Td><Td>$result[id]</Td></Tr>";
echo "<Tr align='left'><Td align='right'><font color=#c0c0c0>เลขประจำตัวประชาชน</font> &nbsp;</Td><Td>$result[person_id]</Td></Tr>";
echo "<input type=hidden name=person_id value='$result[person_id]'>";
echo "<Tr><Td align='right'>คำนำหน้า</Td><Td><input type=text name=prename value='$result[prename]'><TR>";
echo "<Tr><Td align='right'>ชื่อ</Td><Td><input type=text name=name value='$result[name]'><TR>";
echo "<Tr><Td align='right'>สกุล</Td><Td><input type=text name=surname value='$result[surname]'><TR>";

echo "<Tr><Td align='right'>ตำแหน่ง</Td><Td>";
echo "<select name=position_code>";
$sql_position = "select * from person_sch_position";
$dbquery_position=mysqli_query($connect,$sql_position);
while ($result_position = mysqli_fetch_array($dbquery_position)){
	if($result_position[position_code]==$result[position_code]){
		echo "<option value='$result_position[position_code]' selected>$result_position[position_name]</option>";
	}else{
		echo "<option value='$result_position[position_code]'>$result_position[position_name]</option>";
	}
}
echo "</select>";
echo "</Td></Tr>";

echo "<Tr align='left'><Td align='right'>สถานศึกษา <font color=red>กรณี ย้ายไปโรงเรียนอื่นในสังกัด</font></Td><Td>";
	echo "<Select name='school_code_new' size='1'>";
	echo "<option value = ''>เลือก</option>" ;
	$sql = "select * from system_school order by school_type,school_code";
	$dbquery = mysqli_query($connect,$sql);
	while ($result_school = mysqli_fetch_array($dbquery)){
		if($result_school[school_code]==$school_code){
			echo "<option value ='$result_school[school_code]' selected>$result_school[school_code] $result_school[school_name]</option>" ;
		}else{
			echo "<option value ='$result_school[school_code]'>$result_school[school_code] $result_school[school_name]</option>" ;
		}
	}	
	echo "</select>";
	echo "<input type=hidden name=status value='0'>";
echo "</Td></Tr>";

echo "<tr><td colspan=2 align=center>กรณี ย้ายไปโรงเรียนสังกัดหน่วยงานอื่น คลิก -> <a href=?option=person&task=person_sch&index=5.1&id=$id&page=$page&school_code=$school_code><img src=images/my-icons/no.png width=22 border='0' title='กำหนดสถานะภาพ'> เมนู กำหนดสถานะภาพ</a></td>";

echo "<tr align='left'>";
echo "<td align='right'>ไฟล์รูปภาพ&nbsp;</td>";
echo "<td align='left'><input name='userfile' type='file'></td>";
echo "</tr>";

echo "<tr><td align=right>เพศ</td><td>";
if($result[sex]==1){ 
	echo "<input type=radio name=sex value='1' checked> ชาย &nbsp;&nbsp; ";
	echo "<input type=radio name=sex value='2'> หญิง";
}else{
	echo "<input type=radio name=sex value='1'> ชาย &nbsp;&nbsp; ";
	echo "<input type=radio name=sex value='2' checked> หญิง";
}
echo "</Td></Tr>";
echo "<tr><td align=right>ชื่อเล่น</td><td><input type='text' name='nickname' value='$result[nickname]' style='width:100px;'></td></tr>";
$birth_day=getDatePickerThai($result['birth_day']);
echo "<tr><td align=right>วัน/เดือน/ปี (เกิด)</td><td>";
	echo "<input type=\"text\" name=\"birth_day\" id='datepicker-th-1' value='$birth_day' style='width:100px;'>";
echo "</td></tr>";

$start_day=getDatePickerThai($result['start_day']);
echo "<tr><td align=right>วัน/เดือน/ปี (บรรจุครั้งแรก)</td><td>";
	echo "<input type=\"text\" name=\"start_day\" id='datepicker-th-2' value='$start_day' style='width:100px;'>";
echo "</td></tr>";

echo "<Tr align='left'><Td align='right'>วิทยฐานะ/ระดับ</Td><Td>";
echo "<select name='position_class' style='width:180px'>";
$sql_position_class = "select * from person_position_class order by code";
$dbquery_position_class = mysqli_query($connect,$sql_position_class);
while($result_position_class=mysqli_fetch_array($dbquery_position_class)){
	if($result_position_class[code]==$result[position_class]){
		echo "<option value='$result_position_class[code]' selected>$result_position_class[name]</option>";
	}else{
		echo "<option value='$result_position_class[code]'>$result_position_class[name]</option>";
	}
}
echo "</Td></Tr>";
echo "<Tr align='left'><Td align='right'>วุฒิการศึกษาสูงสุด</Td><Td>";
echo "<select name='education_class' style='width:180px'>";
$sql_education_class = "select * from person_education_class order by code";
$dbquery_education_class = mysqli_query($connect,$sql_education_class);
while($result_education_class=mysqli_fetch_array($dbquery_education_class)){
	if($result_education_class[code]==$result[education_class]){
		echo "<option value='$result_education_class[code]' selected>$result_education_class[name]</option>";
	}else{
		echo "<option value='$result_education_class[code]'>$result_education_class[name]</option>";
	}
}
echo "</Td></Tr>";
echo "<tr><td align=right>ชื่อวุฒิการศึกษา/วิชาเอก <font color=#ff8000>เช่น บริหารธุรกิจบัณฑิต(การบัญชี)</font></td><td><input type=text name=major value='$result[major]' size=30></td></tr>";
echo "<tr><td align=right valign=top>อีเมล์</td><td>";
$explode_email=explode("|",$result[email]);
for ( $i=0; $i<3; $i++ ) {
	echo "<input type=\"text\" name=\"email[]\" value=\"$explode_email[$i]\" size='30'><br>";
}
echo "</td></tr>";

echo "<tr><td align=right valign=top>หมายเลขโทรศัพท์</td><td>";
$explode_tel=explode("|",$result[tel]);
for ( $i=0; $i<3; $i++ ) {
	echo "<input type=\"text\" name=\"tel[]\" value=\"$explode_tel[$i]\" size='30'><br>";
}
echo "</td></tr>";
echo "<tr><td align=right>เว็บไซต์</td><td><input type=text name=url value='$result[url]' size=30></td></tr>";

echo "<tr><td></td><td align=left>"; 
echo "<Input Type=Hidden Name='id' Value='$result[id]'>";
echo "<Input Type=Hidden Name='page' Value='$_GET[page]'>";
echo "<Input Type=Hidden Name='school_code' Value='$_GET[school_code]'>";
echo "<INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url_update(3)'>&nbsp;<INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url_update(0)'>";
echo "</td></tr>";
echo "</Table>";
echo "</form>";
}

//ส่วนปรับปรุงข้อมูล
if ($index==6){
/*
	$sql = "select * from person_sch_main where person_id='$_POST[person_id]' and id!='$_POST[id]' ";
	$dbquery = mysqli_query($connect,$sql);
	if(mysqli_num_rows($dbquery)>=1){
	echo "<br /><div align='center'>มีเลขประจำตัวประชาชนซ้ำกับรายการที่มีอยู่แล้ว ตรวจสอบอีกครั้ง</div>";
	exit();  
	}
*/
	$rec_date = date("Y-m-d");
	if($_POST['person_order']==""){
	$_POST['person_order']=0;
	}
	if(!(isset($_POST['other']))){
	$_POST['other']=0;
	}

	$basename = basename($_FILES['userfile']['name']);
	if ($basename!=""){
		$changed_name = file_upload();
		$sql = "update person_sch_main set prename='$_POST[prename]', name='$_POST[name]', surname='$_POST[surname]', pic='$changed_name', position_code='$_POST[position_code]', school_code='$_POST[school]', person_order='$_POST[person_order]',officer='$officer',other='$_POST[other]' where person_id='$_POST[person_id]'";
	}else{
		$sql = "update person_sch_main set prename='$_POST[prename]', name='$_POST[name]', surname='$_POST[surname]', position_code='$_POST[position_code]', school_code='$_POST[school_code]', person_order='$_POST[person_order]',officer='$officer',other='$_POST[other]' where person_id='$_POST[person_id]'";
	}
	$dbquery = mysqli_query($connect,$sql);

}

//ส่วนปรับปรุงข้อมูลสถานะ
if ($index==6.1){
	$sql = "update person_sch_main set status='$_POST[status]' where person_id='$_POST[person_id]'";
	$dbquery = mysqli_query($connect,$sql);
}

//ส่วนปรับปรุงข้อมูลบุคลากร
if ($index==6.2){
	$rec_date = date("Y-m-d");
	$basename = basename($_FILES['userfile']['name']);
	if ($basename!=""){
		$changed_name = file_upload();
		$sql = "update person_sch_main set prename='$_POST[prename]', name='$_POST[name]', surname='$_POST[surname]', pic='$changed_name', position_code='$_POST[position_code]', school_code='$_POST[school_code_new]', officer='$officer',rec_date='$rec_date',other='$_POST[other]' where person_id='$_POST[person_id]'";
	}else{
		$sql = "update person_sch_main set prename='$_POST[prename]', name='$_POST[name]', surname='$_POST[surname]', position_code='$_POST[position_code]', school_code='$_POST[school_code_new]', officer='$officer',rec_date='$rec_date',other='$_POST[other]' where person_id='$_POST[person_id]'";
	}
	$dbquery = mysqli_query($connect,$sql);

	$birth_day=getDatePickerEng($_POST[birth_day]);
	$start_day=getDatePickerEng($_POST[start_day]);
	list($year_birth, $month_birth, $day)= explode("-",$birth_day); 
	if($month_birth<=9){
		$year=$year_birth+60;
		$end_day=$year."-09-30";
	}else{
		$year=$year_birth+61;
		$end_day=$year."-09-30";
	}
	$search = array(" ","-","(",")");
	$replace="";
	for($i=0; $i<=count($_POST[email]); $i++){
		if($email[$i]!=''){
			$email_x=$email_y.$email[$i];
			$email_y=$email_x."|";
		}
	}
	$email_y=str_replace($search,$replace,$email_y);
	for($i=0; $i<=count($_POST[tel]); $i++){
		if($tel[$i]!=''){
			$tel_x=$tel_y.$tel[$i];
			$tel_y=$tel_x."|";
		}
	}
	$tel_y=str_replace($search,$replace,$tel_y);

	$sql = "UPDATE person_detail SET position_class='$_POST[position_class]', education_class='$_POST[education_class]', major='$_POST[major]', sex='$_POST[sex]', nickname='$_POST[nickname]', birth_day='$birth_day', start_day='$start_day', end_day='$end_day', email='$email_y', tel='$tel_y', url='$_POST[url]' WHERE person_id='$_POST[person_id]'";
	$dbquery = mysqli_query($connect,$sql);
}

//ส่วนการแสดงผล
if(!(($index==1) or ($index==1.1) or ($index==2) or ($index==4) or ($index==5) or ($index==5.1) or ($index==5.2))){

	//เกี่ยวการส่งค่ารหัสโรงเรียนตอนเลือกหน้า
	if(($_REQUEST['school_code']=="") and ($_REQUEST['page_var1']!="")){ 
	$_REQUEST['school_code']=$_REQUEST['page_var1'];
	}

//ส่วนของการแยกหน้า
if ($_SESSION['login_status']==12 or $_SESSION['login_status']==13 or $permission_p=='0'){
	$sql_page= "select * from person_sch_main left join system_school on person_sch_main.school_code=system_school.school_code where person_sch_main.school_code='$_SESSION[user_school]' and person_sch_main.status='0' ";
}else{
	if($index==8 and ($_REQUEST['name_search']!="")){
	//$sql_page = "select * from person_sch_main where name = '$_POST[name_search]' and status='0' ";
	$sql_page= "select * from person_sch_main left join system_school on person_sch_main.school_code=system_school.school_code where person_sch_main.name like '%$_REQUEST[name_search]%' or person_sch_main.surname like '%$_REQUEST[name_search]%' or person_sch_main.person_id like '%$_REQUEST[name_search]%' or person_sch_main.person_id like '%$_REQUEST[name_search]%' and person_sch_main.status='0' ";
	$_REQUEST['school_code']="";
	}
	else if($_REQUEST['school_code']==""){
	$sql_page = "select id from person_sch_main ";
	}
	else{
	$sql_page = "select id from person_sch_main where status='0' and school_code='$_REQUEST[school_code]'";
	}
}
$dbquery_page = mysqli_query($connect,$sql_page);
$num_rows=mysqli_num_rows($dbquery_page);

//$pagelen=20; // กำหนดแถวต่อหน้า
$url_link="option=person&task=person_sch&page_var1=$_REQUEST[school_code]&index=$index&name_search=$_REQUEST[name_search]";
$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<7)){
	//echo "<div align=center>";
	//echo "หน้า	";
	echo "<nav><ul class='pagination'>";
	echo "<li><a>หน้า $page จาก $totalpages</a></li>";
		for($i=1; $i<=$totalpages; $i++)	{
			if($i==$page){
				//echo "[<b><font size=+1 color=#990000>$i</font></b>]";
				echo " <li><a href=''><font color=#990000>$i</font></a></li> ";
			}
			else {
				//echo "<a href=$PHP_SELF?$url_link&page=$i>[$i]</a>";
				echo "<li><a href=$PHP_SELF?$url_link&page=$i> $i </a></li>";
			}
		}
	//echo "</div>";
	echo "</nav>";
}		
if($totalpages>6){
	if($page <=3){
		$e_page=6;
		$s_page=1;
	}
	if($page>3){
		if($totalpages-$page>=3){
			$e_page=$page+3;
			$s_page=$page-3;
		}
		else{
			$e_page=$totalpages;
			$s_page=$totalpages-6;
		}
	}
	//echo "<div align=center>";
	echo "<nav><ul class='pagination'>";
	echo "<li><a>หน้า $page จาก $totalpages</a></li>";
	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>";
		echo "<li><a href=$PHP_SELF?$url_link&page=1 aria-label='หน้าแรก'>
		<span aria-hidden='true'>หน้าแรก</span></a></li>";
		echo "<li><a href=$PHP_SELF?$url_link&page=$f_page1 aria-label='หน้าก่อน'>
		<span aria-hidden='true'>หน้าก่อน</span></a></li>";

	}
	else {
	//echo "หน้า	";
	}					
	for($i=$s_page; $i<=$e_page; $i++){
		if($i==$page){
			//echo "[<b><font size=+1 color=#990000>$i</font></b>]";
			echo " <li><a href=''><font color=#990000><strong>$i</strong></font></a></li> ";
		}
		else {
			//echo "<a href=$PHP_SELF?$url_link&page=$i>[$i]</a>";
			echo "<li><a href=$PHP_SELF?$url_link&page=$i>$i</a></li>";
		}
	}
	if($page<$totalpages)	{
		$f_page2=$page+1;
		echo "<li><a href=$PHP_SELF?$url_link&page=$f_page2 aria-label='หน้าถัดไป'>
		<span aria-hidden='true'>หน้าถัดไป</span></a></li>";
		echo "<li><a href=$PHP_SELF?$url_link&page=$totalpages aria-label='หน้าสุดท้าย'>
		<span aria-hidden='true'>หน้าสุดท้าย>$totalpages</span></a></li>";
		//echo "<a href=$PHP_SELF?$url_link&page=$f_page2> หน้าถัดไป</a>>>";
		//echo "<a href=$PHP_SELF?$url_link&page=$totalpages> หน้าสุดท้าย</a>>";
	}
	echo "&nbsp;<select onchange=\"location.href=this.options[this.selectedIndex].value;\" size=\"1\" name=\"select\">";
	echo "<option value=\"\">หน้า</option>";
	for($p=1;$p<=$totalpages;$p++){
		echo "<option value=\"?$url_link&page=$p\">$p</option>";
	}
	echo "</select>";
//echo "</div>"; 
echo "</nav>"; 
}					
//จบแยกหน้า
echo "<form id='frm1' name='frm1'>";
echo "<table width='99%' align='center'>";
if ($_SESSION['login_status']==12 or $_SESSION['login_status']==13 or $permission_p=='0'){	//เมนูผู้บริหารโรงเรียน/รองฯ/เจ้าหน้าที่โรงเรียน
//if(	$_REQUEST['school_code']==$_SESSION['user_school'] ){
//if($permission_p==0){ //อนุญาตให้เจ้าหน้าที่สถานศึกษาเพิ่มบุคลากรใหม่ได้
	echo "<tr><Td align='left' colspan=2><INPUT TYPE='button' name='smb' value='เพิ่มข้อมูล' onclick='location.href=\"?option=person&task=person_sch&index=1\"'></Td><td align='right'>";
	echo "</td></tr>";
}else{
	echo "<tr><td colspan='2' align='right'>";
	?>
	ค้นหาโรงเรียน <input type="text" size="15" onkeyup="showResult(this.value)">
	<div id="livesearch"></div>
	<?php
	echo "</td></tr>";
	echo "<tr><Td align='left'><INPUT TYPE='button' name='smb' value='เพิ่มข้อมูล' onclick='location.href=\"?option=person&task=person_sch&index=1\"'></Td><td align='right'>";

	//ค้นหาบุคลากร
	echo "ค้นหาด้วยชื่อ หรือนามสกุล หรือเลขประจำตัวประชาชน&nbsp;";
		if($index==8){
			echo "<Input Type='Text' Name='name_search' value='$_REQUEST[name_search]'>";
		}
		else{
			echo "<Input Type='Text' Name='name_search'>";
		}
	echo "&nbsp;<INPUT TYPE='submit' name='smb' value='ค้น' onclick='goto_display(1)'>";
	echo "&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;";

	echo "เลือกสถานศึกษา&nbsp";
	echo "<Select name='school_code' size='1' onChange=location.href='index.php?option=person&task=person_sch&school_code='+this.options[this.selectedIndex].value;>";
	echo '<option value ="" >ทั้งหมด</option>' ;
	$sql = "select * from system_school order by school_code";
	$dbquery = mysqli_query($connect,$sql);
	While ($result = mysqli_fetch_array($dbquery)){
		if($_REQUEST['school_code']==""){
			echo "<option value=$result[school_code]>$result[school_code] $result[school_name]</option>"; 
		}
		else{
			if($_REQUEST['school_code']==$result['school_code']){
				echo "<option value=$result[school_code] selected>$result[school_code] $result[school_name]</option>"; 
			}
			else{
				echo "<option value=$result[school_code]>$result[school_code] $result[school_name]</option>"; 
			}
		}		
	}
	echo "</select>";
	echo "</td></tr>";
} //end if

echo "</table>";
if ($_SESSION['login_status']==12 or $_SESSION['login_status']==13 or $permission_p=='0'){
	$sql="select * from person_sch_main left join system_school on person_sch_main.school_code=system_school.school_code where person_sch_main.school_code like '$_SESSION[user_school]' and person_sch_main.status='0' limit $start,$pagelen";
}else{
	if($index==8 and ($_REQUEST['name_search']!="")){
	$sql = "select person_sch_main.id, person_sch_main.person_id, person_sch_main.prename, person_sch_main.name, person_sch_main.surname, person_sch_main.position_code, person_sch_main.school_code, person_sch_main.pic, person_sch_main.person_order,person_sch_main.other from person_sch_main left join system_school on person_sch_main.school_code=system_school.school_code where person_sch_main.name like '%$_REQUEST[name_search]%' or person_sch_main.surname like '%$_REQUEST[name_search]%' or person_sch_main.person_id like '%$_REQUEST[name_search]%' and person_sch_main.status='0' order by person_sch_main.position_code limit $start,$pagelen";
	$_REQUEST['school_code']="";
	}
	else if($_REQUEST['school_code']==""){
	$sql = "select * from person_sch_main limit $start,$pagelen";
	}
	else{
	$sql = "select * from person_sch_main where status='0' and school_code='$_REQUEST[school_code]' order by school_code, position_code,person_order limit $start,$pagelen";
	}
}
$dbquery = mysqli_query($connect,$sql);
echo "<table width='100%' align='center' border=0 class='myTableSky'>";
echo "<th width='50'>ที่</th><th width='120'>เลขประชาชน</th><th>ชื่อ</th><th>ตำแหน่ง</th><th>สถานศึกษา</th><th width='120'>รายละเอียดบุคลากร</th><th width='80'>สถานะภาพ</th><th width='80'>แก้ไข</th><th width='100'>ปรับปรุงข้อมูล</th>";
//echo "<th width='50'>ที่</th><th width='120'>เลขประชาชน</th><th>ชื่อ</th><th>ตำแหน่ง</th><th width='50'>ลำดับ</th><th>สถานศึกษา</th><th width='120'>รายละเอียดบุคลากร</th><th width='80'>สถานะภาพ</th><th width='80'>แก้ไข</th><th width='100'>ปรับปรุงข้อมูล</th>";
$N=(($page-1)*$pagelen)+1; //*เกี่ยวข้องกับการแยกหน้า
$M=1;
While ($result = mysqli_fetch_array($dbquery))
	{
		$id = $result['id'];
		$person_id = $result['person_id'];
		$prename=$result['prename'];
		$name= $result['name'];
		$surname = $result['surname'];
		$position_code= $result['position_code'];
		$school_code= $result['school_code'];
		$person_order= $result['person_order'];
		$status=$result['status'];
			
		echo "<Tr><Td align=center>$N</Td><Td align='center'>$person_id</Td><Td align='left'>$prename&nbsp;$name&nbsp;$surname</Td><Td>";
if(isset($position_ar[$position_code])){
echo $position_ar[$position_code];
}
$sql_delegate = "select id from person_delegate where person_id='$person_id' ";
$dbquery_delegate=mysqli_query($connect,$sql_delegate);
$num_delegate=mysqli_num_rows($dbquery_delegate);
if($num_delegate!=0){
	echo " <font color=red>(รักษาการในตำแหน่ง ผอ.รร.)</font>";
}
echo "</Td>";
//if($person_order!=0){		
//echo "<Td align='center'>$person_order</Td>";
//}
//else{
//echo "<Td align='center'></Td>";
//}

if($result['other']==1){
echo "<Td>$school_ar[$school_code]";
echo " <font color='#FF0000'>(ปฏิบัติงานมากกว่าหนึ่งแห่ง)</font></Td>";
}
else{
echo "<Td>$school_ar[$school_code]</Td>";
}

//ค้นหาว่ามีการสร้างข้อมูลหรือยัง
$sql_detail="select * from person_detail where person_id='$result[person_id]' ";
$dbquery_detail=mysqli_query($connect,$sql_detail);
$num_detail=mysqli_num_rows($dbquery_detail);
$result_detail=mysqli_fetch_array($dbquery_detail);
if($num_detail!=''){
echo "<Td align='center'><a href='modules/person/person_detail_sch.php?&person_id=$person_id' class='dialogify'><img src=images/my-icons/list.png width=28 border='0' title='รายละเอียดบุคลากร'></a></Td>";
echo "<input type=hidden name=office_name value='$_SESSION[office_name]'>";
}
else{
echo "<Td align='center'></Td>";
}
switch ($status){
	case 0:
		echo "<Td align=center><a href=?option=person&task=person_sch&index=5.1&person_id=$person_id&page=$page&school_code=$school_code><img src=images/my-icons/no.png width=22 border='0' title='กำหนดสถานะภาพ'></a></Td>";
		echo "<Td align=center><a href=?option=person&task=person_sch&index=5&person_id=$person_id&page=$page&school_code=$school_code><img src=images/edit.png border='0' title='แก้ไข ผู้ใช้งาน'></a></Td>";
		if($num_detail==0){
			echo "<Td align=center><a href=?option=person&task=person_sch&index=1.1&person_id=$person_id&page=$page&school_code=$school_code><img src=images/my-icons/plus.png width=22 border='0' title='เพิ่ม ข้อมูลบุคลากร'></a></Td>";
		}else{
			echo "<Td align=center><a href=?option=person&task=person_sch&index=5.2&person_id=$person_id&page=$page&school_code=$school_code><img src=images/my-icons/interface-edit.png width=24 border='0' title='แก้ไข ข้อมูลบุคลากร'></a></Td>";
		}
	break;
	case 1:
		echo "<Td align=center><a href=?option=person&task=person_sch&index=5.1&person_id=$person_id&page=$page&school_code=$school_code><img src=images/my-icons/no.png width=22 border='0' title='กำหนดสถานะภาพ'></a></Td>";
		echo "<td colspan=2 align=center>สถานะบุคลากรในอดีต</td>";
	break;
	case 9:
		echo "<td colspan=3 align=center>รอการยืนยันจากเขตพื้นที่</td>";
	break;
	default:
		echo "<td colspan=3 align=center>ไม่มีสถานะ</td>";
	break;

}
/*
if($status==9){
	echo "<td colspan=4 align=center>รอการยืนยันจากเขตพื้นที่</td>";
}else{
	echo "<Td align=center><a href=?option=person&task=person_sch&index=5.1&person_id=$person_id&page=$page&school_code=$school_code><img src=images/my-icons/no.png width=22 border='0' title='กำหนดสถานะภาพ'></a></Td>";
	echo "<Td align=center><a href=?option=person&task=person_sch&index=5&person_id=$person_id&page=$page&school_code=$school_code><img src=images/edit.png border='0' title='แก้ไข ผู้ใช้งาน'></a></Td>";
	if($num_detail==0){
		echo "<Td align=center><a href=?option=person&task=person_sch&index=1.1&person_id=$person_id&page=$page&school_code=$school_code><img src=images/my-icons/plus.png width=22 border='0' title='เพิ่ม ข้อมูลบุคลากร'></a></Td>";
	}else{
		echo "<Td align=center><a href=?option=person&task=person_sch&index=5.2&person_id=$person_id&page=$page&school_code=$school_code><img src=images/my-icons/interface-edit.png width=24 border='0' title='แก้ไข ข้อมูลบุคลากร'></a></Td>";
	}
}
*/
echo "</Tr>";
$M++;
$N++;
	}
echo "</Table>";
echo "</form>";

	if($permission_p=='0'){
		$sql= "select * from person_sch_main where status='1' and school_code='$_SESSION[user_school]' ";
	}else{
		$sql= "select * from person_sch_main where status='1' and school_code='$_REQUEST[school_code]' ";
	}
	$dbquery=mysqli_query($connect,$sql);
	echo "<br>";
	echo "<table width=50% align='center' class=myTableSky>";
	echo "<tr><td colspan=3>ครูในอดีต /สถานะภาพ : ย้ายไปสังกัดหน่วยงานอื่น, เกษียณ, ลาออก, เสียชีวิต ฯลฯ</td></tr>";
	echo "<th>ชื่อ - สกุล</th><th>ตำแหน่ง</th><th>ยกเลิกสถานะภาพ</th>";
	while ($result=mysqli_fetch_array($dbquery)){
		$id=$result['id'];
		$person_id=$result['person_id'];
		$sql_position="select * from person_sch_position where position_code='$result[position_code]' ";
		$dbquery_position=mysqli_query($connect,$sql_position);
		$result_position=mysqli_fetch_array($dbquery_position);
		echo "<tr><td>$result[prename] $result[name] $result[surname]</td>";
		echo "<td align=center>$result_position[position_name]</td>";
		if($permission_p=='0'){
			echo "<td align=center>ติดต่อเขตพื้นที่</td>";
		}else{
			echo "<td align=center><a href=\"?option=person&task=person_sch&index=5.1&person_id=$person_id&page=$page&school_code=$school_code\"><img src=images/my-icons/no.png border='0' title='แก้ไข' width=22></a></td>";
		}
		echo "</tr>";
	}
	echo "</table>";
}

?>
<script type="text/javascript">
function goto_url(val){
	if(val==0){
		callfrm("?option=person&task=person_sch");  // page ย้อนกลับ 
	}else if(val==1){
		if(frm1.person_id.value == ""){
			alert("กรุณากรอกเลขประจำตัวประชาชน");
		}else if(frm1.prename.value==""){
			alert("กรุณากรอกคำนำหน้าชื่อ");
		}else if(frm1.name.value==""){
			alert("กรุณากรอกชื่อ");
		}else if(frm1.surname.value==""){
			alert("กรุณากรอกนามสกุล");
		}else if(frm1.position_code.value==""){
			alert("กรุณาเลือกตำแหน่ง");
		}else{
			callfrm("?option=person&task=person_sch&index=4");  //page ประมวลผล
		}
	}
	else if(val==2){
		callfrm("?option=person&task=person_sch&index=4.1");  //page ประมวลผล
	}
}

function goto_url_update(val){
	if(val==0){
		callfrm("?option=person&task=person_sch");  // page ย้อนกลับ 
	}else if(val==1){
		if(frm1.person_id.value == ""){
			alert("กรุณากรอกเลขประจำตัวประชาชน");
		}else{
			callfrm("?option=person&task=person_sch&index=6");  //page ประมวลผล
		}
	}else if(val==2){
		if(frm1.person_id.value == ""){
			alert("กรุณากรอกเลขประจำตัวประชาชน");
		}else{
			callfrm("?option=person&task=person_sch&index=6.1");  //page ประมวลผล
		}
	}else if(val==3){
		if(frm1.person_id.value == ""){
			alert("กรุณากรอกเลขประจำตัวประชาชน");
		}else if(frm1.prename.value==""){
			alert("กรุณากรอกคำนำหน้าชื่อ");
		}else if(frm1.name.value==""){
			alert("กรุณากรอกชื่อ");
		}else if(frm1.surname.value==""){
			alert("กรุณากรอกนามสกุล");
		}else if(frm1.position_code.value==""){
			alert("กรุณาเลือกตำแหน่ง");
		}else{
			callfrm("?option=person&task=person_sch&index=6.2");  //page ประมวลผล
		}
	}
}

function goto_display(val){
	if(val==1){
		callfrm("?option=person&task=person_sch&index=8"); 
	}
}

function goto_delete_all(){
	callfrm("?option=person&task=person_sch&index=3.1"); 
}

function CheckAll() {
	for (var i = 0; i < document.frm1.elements.length; i++)
	{
	var e = document.frm1.elements[i];
	if (e.name != "allchk")
		if(e.value==1 && e.type=="checkbox"){
		e.checked = document.frm1.allchk.checked;
		}
	}
}
function check(val){
document.getElementById('wrong'+val).style.display="none";
document.getElementById('right'+val).style.display="block";
}
</script>

<link type="text/css" href="include/jquery.DatetimePicker/jquery.datetimepicker.css" rel="stylesheet" />	
<script type="text/javascript" src="include/jquery.DatetimePicker/jquery.min.js"></script>
<script type="text/javascript" src="include/jquery.DatetimePicker/jquery.datetimepicker.full.js"></script>

<script type="text/javascript">
$(function(){
	$.datetimepicker.setLocale('th'); // ต้องกำหนดเสมอถ้าใช้ภาษาไทย และ เป็นปี พ.ศ.
	// กรณีใช้แบบ input #datepicker-th-1
  $("#datepicker-th-1").datetimepicker({
    timepicker:false,
    format:'d-m-Y', // กำหนดรูปแบบวันที่ ที่ใช้ เป็น 00-00-0000			
    lang:'th', // ต้องกำหนดเสมอถ้าใช้ภาษาไทย และ เป็นปี พ.ศ.
		onSelectDate:function(dp,$input){
			var yearT=new Date(dp).getFullYear()-0; 
			var yearTH=yearT+543;
			var fulldate=$input.val();
			var fulldateTH=fulldate.replace(yearT,yearTH);
			$input.val(fulldateTH);
		},
  });
	// กรณีใช้กับ input ต้องกำหนดส่วนนี้ด้วยเสมอ เพื่อปรับปีให้เป็น ค.ศ. ก่อนแสดงปฏิทิน
	$("#datepicker-th-1").on("mouseenter mouseleave",function(e){
		var dateValue=$(this).val();
		if(dateValue!=""){
				var arr_date=dateValue.split("-"); // ถ้าใช้ตัวแบ่งรูปแบบอื่น ให้เปลี่ยนเป็นตามรูปแบบนั้น
				// ในที่นี้อยู่ในรูปแบบ 00-00-0000 เป็น d-m-Y แบ่งด่วย - ดังนั้น ตัวแปรที่เป็นปี จะอยู่ใน array
				// ตัวที่สอง arr_date[2] โดยเริ่มนับจาก 0 
				if(e.type=="mouseenter"){ var yearT=arr_date[2]-543; }		
				if(e.type=="mouseleave"){ var yearT=parseInt(arr_date[2])+543; }	
				dateValue=dateValue.replace(arr_date[2],yearT);
				$(this).val(dateValue);													
		}		
	});

	// กรณีใช้แบบ input #datepicker-th-2
  $("#datepicker-th-2").datetimepicker({
    timepicker:false,
    format:'d-m-Y', // กำหนดรูปแบบวันที่ ที่ใช้ เป็น 00-00-0000			
    lang:'th', // ต้องกำหนดเสมอถ้าใช้ภาษาไทย และ เป็นปี พ.ศ.
		onSelectDate:function(dp,$input){
			var yearT=new Date(dp).getFullYear()-0; 
			var yearTH=yearT+543;
			var fulldate=$input.val();
			var fulldateTH=fulldate.replace(yearT,yearTH);
			$input.val(fulldateTH);
		},
  });    
	// กรณีใช้กับ input ต้องกำหนดส่วนนี้ด้วยเสมอ เพื่อปรับปีให้เป็น ค.ศ. ก่อนแสดงปฏิทิน
	$("#datepicker-th-2").on("mouseenter mouseleave",function(e){
		var dateValue=$(this).val();
		if(dateValue!=""){
				var arr_date=dateValue.split("-"); // ถ้าใช้ตัวแบ่งรูปแบบอื่น ให้เปลี่ยนเป็นตามรูปแบบนั้น
				// ในที่นี้อยู่ในรูปแบบ 00-00-0000 เป็น d-m-Y แบ่งด่วย - ดังนั้น ตัวแปรที่เป็นปี จะอยู่ใน array
				// ตัวที่สอง arr_date[2] โดยเริ่มนับจาก 0 
				if(e.type=="mouseenter"){ var yearT=arr_date[2]-543; }		
				if(e.type=="mouseleave"){ var yearT=parseInt(arr_date[2])+543; }	
				dateValue=dateValue.replace(arr_date[2],yearT);
				$(this).val(dateValue);													
		}		
	});
});
</script>

	<link rel="stylesheet" href="./jquery/themes/ui-lightness/jquery.ui.all.css">
	<script src="./jquery/jquery-1.5.1.js"></script>
	<script src="./jquery/external/jquery.bgiframe-2.1.2.js"></script>
	<script src="./jquery/ui/jquery.ui.core.js"></script>
	<script src="./jquery/ui/jquery.ui.widget.js"></script>
	<script src="./jquery/ui/jquery.ui.mouse.js"></script>
	<script src="./jquery/ui/jquery.ui.draggable.js"></script>
	<script src="./jquery/ui/jquery.ui.position.js"></script>
	<script src="./jquery/ui/jquery.ui.resizable.js"></script>
	<script src="./jquery/ui/jquery.ui.dialog.js"></script>

	<script type="text/javascript">
		/*
		 * jQuery UI Dialog: Load Content via AJAX
		 * http://salman-w.blogspot.com/2013/05/jquery-ui-dialog-examples.html
		 */
		jQuery.noConflict();
		jQuery(function() {
			jQuery("#dialog").dialog({
			height: 580,
			width: 650,
			minHeight: 580,
			minWidth: 650,
			autoOpen: false,
			show: "blind",
			hide: "explode",
				modal: true,
				resizable: false,
			});
			jQuery(".dialogify").click(function(e) {
				e.preventDefault();
				jQuery("#dialog").html("");
				jQuery("#dialog").dialog("option", "title", "Loading...").dialog("open");
				jQuery("#dialog").load(this.href, function() {
					jQuery(this).dialog("option", "title", "ข้อมูลบุคลากร");
					//jQuery(this).dialog("option", "title", jQuery(this).find("h1").text());
					//jQuery(this).find("h1").remove();
				});
			});
		});	
	</script>
<div id="dialog"></div>