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

?>
<link type="text/css" href="./datepicker_thaiyear/css/ui-lightness/jquery-ui-1.8.10.custom.css" rel="stylesheet" />	
<?php

if(!(($_SESSION['admin_person']=="person") or ($_SESSION['login_status']==99) or ($_SESSION['login_status']<=4 and $result_permission['p1']==1))){	
exit();
}

$officer=$_SESSION['login_user_id'];

$sql = "select * from  person_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_workgroup order by workgroup_order";
$dbquery = mysqli_query($connect,$sql);
While ($result = mysqli_fetch_array($dbquery)){
$department_ar[$result['workgroup']]=$result['workgroup_desc'];
}

echo "<br />";
if(!($index==5)){
echo "<table width='50%' border='0' align='center'>";
echo "<tr align='center'><td><font color='#006666' size='3'><strong>ลายเซ็นบุคลากร สพท. (ไฟล์ PNG)</strong></font></td></tr>";
echo "</table>";
}

//ฟังชั่นupload
function file_upload() {
		$uploaddir = 'modules/person/signature/';      //ที่เก็บไไฟล์ลายเซ็น
		$uploadfile = $uploaddir.basename($_FILES['userfile']['name']);
		$basename = basename($_FILES['userfile']['name']);
		
		if(!(substr($basename,-3)=="png" or substr($basename,-3)=="PNG")){	
		echo "<script>alert('ชนิดไฟล์ไม่ถูกต้อง ให้ใช้ไฟล์ PNG'); document.location.href='?option=person&task=signature';</script>\n";
		exit();
		}
		
		$pic_code=$_POST['person_id'];
		//ลบไฟล์เดิม
		$exists_file=$uploaddir.$pic_code.".png";
		
		if(file_exists($exists_file)){
		unlink($exists_file);
		}
			
		if (move_uploaded_file($_FILES['userfile']['tmp_name'],  $uploadfile))
			{
				$before_name  = $uploaddir.$basename;
				$changed_name = $exists_file;
				rename("$before_name" , "$changed_name");
			return  $changed_name;
			}
}

//ส่วนฟอร์มแก้ไขข้อมูล
if ($index==5){
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>";
$sql = "select * from  person_main where id='$_GET[id]'";
$dbquery = mysqli_query($connect,$sql);
$result = mysqli_fetch_array($dbquery);

echo "<Table width='50%' Border='0'>";
echo "<Tr align='left'><Td ></Td><Td align='right'>ชื่อ&nbsp;&nbsp;&nbsp;&nbsp;</Td><Td>$result[prename]$result[name]</Td></Tr>";
echo "<Tr align='left'><Td ></Td><Td align='right'>นามสกุล&nbsp;&nbsp;&nbsp;&nbsp;</Td><Td>$result[surname]</Td></Tr>";
echo  "<tr align='left'>";
echo  "<Td ></Td><td align='right'>ไฟล์รูปภาพ (ไฟล์ PNG)&nbsp;&nbsp;</td>";
echo  "<td align='left'><input name = 'userfile' type = 'file'></td>";
echo  "</tr>";

echo "</Table>";
echo "<Br />";
echo "<Input Type=Hidden Name='person_id' Value='$result[person_id]'>";
echo "<INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url_update(1)'>
		&nbsp;&nbsp;<INPUT TYPE='button' name='back' value='ย้อนกลับ' onclick='goto_url_update(0)'>";
echo "</form>";
}

//ส่วนการอัพโหลด
if ($index==6){
		$basename = basename($_FILES['userfile']['name']);
		if($basename!=""){
		$changed_name = file_upload();
		}
}

//ส่วนการแสดงผล
if(!(($index==1) or ($index==2) or ($index==2.1) or ($index==5))){
echo "<form id='frm1' name='frm1'>";
$sql = "select * from person_main where status='0' order by department, position_code,person_order";
$dbquery = mysqli_query($connect,$sql);
echo  "<table width='85%' border='1' align='center' style='border-collapse: collapse'>";
echo "<Tr bgcolor='#FFCCCC' align='center'><Td width='70'>ที่</Td><Td width='120'>เลขประชาชน</Td><Td width='150'>ชื่อ</Td><Td width='300'>ตำแหน่ง</Td><Td width='200'>กลุ่ม</Td><Td width='50'>ลายเซ็น</Td><Td width='80'>เพิ่ม/แก้ไข</Td></Tr>";
$N=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'];
		$person_order= $result['person_order'];
		$department= $result['department'];
			if(($M%2) == 0)
			$color="#FFFFC";
			else  	$color="#FFFFFF";

		echo "<Tr  bgcolor='$color' align='center'><Td>$N</Td><Td align='left'>$person_id</Td><Td align='left'>$prename&nbsp;$name&nbsp;&nbsp;$surname</Td>";
		echo "</td>";
		echo "<Td align='left'>";
		if(isset($position_ar[$position_code])){
		echo $position_ar[$position_code];
		}
echo "</Td>";

if(isset($department_ar[$department])){
echo "<Td align='left'>$department_ar[$department]</Td>";
}
else{
echo "<Td align='left'></Td>";
}

echo "<Td align='center'>";
				$signature_pic="modules/person/signature/".$person_id.".png";
				if(file_exists($signature_pic)){
				echo "<a href='modules/person/signature_show.php?&person_id=$person_id' target='_blank'><img src='$signature_pic' border='0' width='80%'></a>";
				}
echo "</td>";

echo "<Td><a href=?option=person&task=signature&index=5&id=$id><img src=images/edit.png border='0' alt='แก้ไข'></a></div></Td>
	</Tr>";
$M++;
$N++;
	}
echo "</Table>";
echo "</form>";
}

?>
<script>
function goto_url_update(val){
	callfrm("?option=person&task=signature&index=6");   //page ประมวลผล
}
</script>
