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

<?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/book/time_inc.php";	

$user_db=$user;
$connect_reserve=@mysqli_connect($hostname,$user_db,$password,'amssplus_reserve');
if($connect_reserve){
mysqli_query($connect_reserve,"SET NAMES utf8");
}
echo "<br />";
echo "<table width='70%' border='0' align='center'>";
echo "<tr align='center'><td><font color='#FE2E2E' size='2'>ยังไม่มีฐานข้อมูลสำรอง ระบบไม่สามารถดำเนินการได้</font></td></tr>";
echo "</table>";
exit();

$user=$_SESSION['login_user_id'];

//ส่วนหัว
echo "<br />";
if(!(($index==1) or ($index==2))){

echo "<table width='100%' border='0' align='center'>";
echo "<tr align='center'><td><font color='#006666' size='3'><strong>หนังสือส่งที่มีอายุเกิน 2 ปี</strong></font></td></tr>";
echo "</table>";
}

//ส่วนแสดงผล
if(!(($index==1) or ($index==2))){

// อาเรย์ชื่อหน่วยงาาน
$office_name_ar['saraban']="สารบรรณกลาง";
$office_name_ar['obec']="สพฐ/อื่นๆ";
$sql_work_group = mysqli_query($connect,"SELECT * FROM  system_workgroup") ;
while ($row_work_group= mysqli_fetch_array($sql_work_group)){
$office_name_ar[$row_work_group['workgroup']]=$row_work_group['workgroup_desc'];
}
$sql_sch = mysqli_query($connect,"SELECT * FROM  system_school") ;
while ($row_sch= mysqli_fetch_array($sql_sch)){
$office_name_ar[$row_sch['school_code']]=$row_sch['school_name'];
}


if(!(isset($_REQUEST['search_index']))){
$_REQUEST['search_index']="";
}
if(!(isset($_REQUEST['field']))){
$_REQUEST['field']="";
}
if(!(isset($_REQUEST['search']))){
$_REQUEST['search']="";
}

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

//ส่วนของการแยกหน้า
	if($_SESSION['login_status']<=5){
			if($_REQUEST['search_index']==1){	
					if($_REQUEST['workgroup']!=""){
					$sql="select * from book_main where book_type='1' and $_REQUEST[field] like '%$_REQUEST[search]%' and office='$_REQUEST[workgroup]' ";
					}
					else{
					$sql="select * from book_main where book_type='1' and $_REQUEST[field] like '%$_REQUEST[search]%' ";
					}
			}
			else{
			$sql="select * from book_main where book_type='1'";
			}
	$dbquery = mysqli_query($connect_reserve,$sql);
	$num_rows = mysqli_num_rows($dbquery);
	}
	else if(($_SESSION['login_status']>10) and ($_SESSION['login_status']<=15)){
			if($_REQUEST['search_index']==1){
			$sql="select * from book_main where book_type='2' and office='$_SESSION[user_school]' and  $_REQUEST[field] like '%$_REQUEST[search]%' ";
			}
			else{
			$sql="select * from book_main where book_type='2' and office='$_SESSION[user_school]' ";
			}
			$dbquery = mysqli_query($connect_reserve,$sql);
			$num_rows = mysqli_num_rows($dbquery);
	}

$pagelen=20;  // 1_กำหนดแถวต่อหน้า
$url_link="option=book&task=main/report_2&search_index=$_REQUEST[search_index]&field=$_REQUEST[field]&search=$_REQUEST[search]&workgroup=$_REQUEST[workgroup]";  // 2_กำหนดลิงค์ฺ
$totalpages=ceil($num_rows/$pagelen);

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

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>";  
}					
//จบแยกหน้า

?>
<table border="0" width="98%" id="table1" style="border-collapse: collapse" cellspacing="2" cellpadding="2" align="center">
<tr><td><FONT SIZE="2" COLOR="">ระดับความสำคัญ <IMG SRC="modules/book/images/level1.gif" WIDTH="20" HEIGHT="11" BORDER="0" ALT="ปกติ">ปกติ&nbsp;<IMG SRC="modules/book/images/level2.gif" WIDTH="20" HEIGHT="11" BORDER="0" ALT="ด่วน">ด่วน&nbsp;<IMG SRC="modules/book/images/level3.gif" WIDTH="20" HEIGHT="11" BORDER="0" ALT="ด่วนมาก">ด่วนมาก&nbsp;<IMG SRC="modules/book/images/level4.gif" WIDTH="20" HEIGHT="11" BORDER="0" ALT="ด่วนที่สุด">ด่วนที่สุด</FONT></td>
	<form method="POST" action="?option=book&task=main/report_2">
<td align="right">
				<font size="2">ค้นหาหนังสือ จาก </font>
				<select size="1" name="field">
				<?php
				if($_REQUEST['field']=='subject'){
				echo "<option value='subject' selected>เรื่อง</option>";
				}
				else{
				echo "<option value='subject'>เรื่อง</option>";
				}
				if($_REQUEST['field']=='bookno'){
				echo "<option value='bookno' selected>เลขหนังสือ</option>";
				}
				else{
				echo "<option value='bookno'>เลขหนังสือ</option>";
				}
				echo "</select>";
				
				echo "<font size='2'> ด้วยคำว่า </font>";
				echo "<input type='text' name='search' size='20' value='$_REQUEST[search]'>"; 
				echo "<input type='hidden' name='search_index' value='1'>";
				echo " <input type='submit' value='ค้นหา'>";
				
/////////////////////
if($_SESSION['login_status']<=5){
echo "<td align='right'>";
	echo "<Select  name='workgroup' size='1'>";
	echo  '<option value ="" >ทุกกลุ่ม(งาน)</option>' ;
						$sql = "SELECT *  FROM  system_workgroup";
						$dbquery =mysqli_query($connect,$sql);
						While ($result = mysqli_fetch_array($dbquery))
							{ 
								if($_REQUEST['workgroup']==$result['workgroup']){
								echo "<option value='$result[workgroup]' selected>$result[workgroup_desc]</option>"; 
								}
								else{
								echo "<option value='$result[workgroup]'>$result[workgroup_desc]</option>"; 
								}
							}
	echo "</select>";
	echo " <input type='submit' value='เลือก'>";
echo "</td>";		
}			
/////////////////				
?>				
			</td></form>
		</tr>
</table>


<table border="1" width="98%" id="table2" style="border-collapse: collapse" align="center">
				<tr bgcolor=#003399>
					<td width="70" align="center">
					<font size="2" face="Tahoma" color=#FFFFFF>ที่</font></td>
					<td align="center" width="200">
					<font face="Tahoma" size="2" color=#FFFFFF>เลขหนังสือ</font></td>
					<td align="center"><font face="Tahoma" size="2" color=#FFFFFF>เรื่อง</font></td>
					<td align="center" width="50">
					<font face="Tahoma" size="2" color=#FFFFFF>ราย<br />ละเอียด</font></td>
					<td align="center" width="120">
					<font face="Tahoma" size="2" color=#FFFFFF>ลงวันที่</font></td>
					<td align="center" width="160">
					<font face="Tahoma" size="2" color=#FFFFFF>วันเวลาที่ส่ง</font></td>
					<td align="center" width="150">
					<font face="Tahoma" size="2" color=#FFFFFF>ผู้ส่ง</font></td>
				</tr>
</form>

<?php
	if($_SESSION['login_status']<=5){
			if($_REQUEST['search_index']==1){	
					if($_REQUEST['workgroup']!=""){
					$sql="select * from book_main where book_type='1' and $_REQUEST[field] like '%$_REQUEST[search]%' and office='$_REQUEST[workgroup]' order by ms_id limit $start,$pagelen";
					}
					else{
					$sql="select * from book_main where book_type='1' and $_REQUEST[field] like '%$_REQUEST[search]%' order by ms_id limit $start,$pagelen";
					}
			}
			else{
			$sql="select * from book_main where book_type='1' order by ms_id limit $start,$pagelen";
			}
	$dbquery = mysqli_query($connect_reserve,$sql);
	}
	else if(($_SESSION['login_status']>10) and ($_SESSION['login_status']<=15)){
			if($_REQUEST['search_index']==1){
			$sql="select * from book_main where book_type='2' and office='$_SESSION[user_school]' and $_REQUEST[field] like '%$_REQUEST[search]%' order by ms_id limit $start,$pagelen";
			}
			else{
			$sql="select * from book_main where book_type='2' and office='$_SESSION[user_school]' order by ms_id limit $start,$pagelen";
			}
	$dbquery = mysqli_query($connect_reserve,$sql);
	}

$N=(($page-1)*$pagelen)+1; //*เกี่ยวข้องกับการแยกหน้า
$M=1;

While ($result = mysqli_fetch_array($dbquery)){
		$id = $result['ms_id'];
		$sender = $result['sender'];
		$office = $result['office']; 
		$ref_id = $result['ref_id'];
		$level = $result['level'];
		$bookno = $result['bookno'];
		$signdate = $result['signdate'];
		$subject = $result['subject'];
		$ref_id = $result['ref_id'];
		$rec_date = $result['send_date'];
			if(($M%2) == 0)
			$color="#ffffff";
			else $color="#E5E5FF";
$send_date=thai_date_4($rec_date);
$signdate=thai_date_3($signdate);
// ระดับความสำคัญ
if ($level==1) {
	$img_level = "<IMG SRC=\"modules/book/images/level1.gif\" WIDTH=\"20\" HEIGHT=\"11\" BORDER=\"0\" ALT=\"ปกติ\">" ;
}else if ($level==2) {
	$img_level = "<IMG SRC=\"modules/book/images/level2.gif\" WIDTH=\"20\" HEIGHT=\"11\" BORDER=\"0\" ALT=\"ด่วน\">" ;
}else if ($level==3) {
	$img_level = "<IMG SRC=\"modules/book/images/level3.gif\" WIDTH=\"20\" HEIGHT=\"11\" BORDER=\"0\" ALT=\"ด่วนมาก\">" ;
}else if ($level==4) {
	$img_level = "<IMG SRC=\"modules/book/images/level4.gif\" WIDTH=\"20\" HEIGHT=\"11\" BORDER=\"0\" ALT=\"ด่วนที่สุด\">" ;
}

// ตรวจสอบไฟล์แนบ
if($result['bookregis_link']==0){
$file = mysqli_query($connect,"SELECT id FROM  book_filebook WHERE ref_id='$ref_id' ") ;
}
else if($result['bookregis_link']==1 and $result['book_type']==1){
$file = mysqli_query($connect,"SELECT * FROM  bookregister_send_filebook WHERE ref_id='$ref_id' ") ;
}
else if($result['bookregis_link']==1 and $result['book_type']==2){
$file = mysqli_query($connect,"SELECT * FROM  bookregister_send_filebook_sch WHERE ref_id='$ref_id' ") ;
}
else {
$file = mysqli_query($connect,"SELECT * FROM  bookregister_send_filebook WHERE ref_id='$ref_id' ") ;
}

$file_num = mysqli_num_rows($file) ;
if ($file_num==0) {
	$file_img = "" ;
}else{
	$file_img = "<IMG SRC=\"modules/book/images/file1.gif\" WIDTH=\"13\" HEIGHT=\"10\" BORDER=\"0\" ALT=\"มีไฟล์แนบ\">" ;
}

if($result['secret']==1){
$secret_txt="<font color='#FF0000'>[ลับ]</font>";
}
else{
$secret_txt="";
}

?>
			<tr bgcolor="<?php echo $color;?>">
					<td align="center"><?php echo $id;?></td>
					<td align="left">&nbsp;<?php echo $bookno;?>&nbsp;<?php echo $img_level;?></td>
					<td align="left"><?php echo $subject;?>&nbsp;<?php echo $file_img;?>&nbsp;<?php echo $secret_txt;?></td>
					<td align="center"><A HREF="javascript:void(0)"
onclick="window.open('modules/book/main/booksenddetail_2.php?b_id=<?php echo $id;?>',
'bookdetail','width=500,height=500,scrollbars')" title="คลิกเพื่อดูรายละเอียด"><span style="text-decoration: none">คลิก</span></A></td>
					<td><?php echo $signdate;?></td>
					<td><?php echo $send_date;?></td>
					<td><?php echo $office_name_ar[$office];?></td>
			  </tr>
					<?php
		
	$M++;
	$N++;  //*เกี่ยวข้องกับการแยกหน้า
	}  // end while	
echo "<tr><td colspan='8'>&nbsp;&nbsp;<FONT COLOR='#009933'><IMG SRC='modules/book/images/file1.gif' WIDTH='16' HEIGHT='16' BORDER='0'>มีไฟล์เอกสาร</FONT></td></tr>";
echo "</table>";
}  //end index

if($connect_reserve){
mysqli_close($connect_reserve);
}
?>
