<?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/$_REQUEST[option]/time_inc.php";
foreach($_REQUEST as $key=>$value)
{
  $$key=$value;
}
$sql_idocument_status =  "SELECT * FROM `idocument_status`";
$query_idocument_status = mysqli_query($connect, $sql_idocument_status);
$arrComment = array();
while($data_idocument_status = mysqli_fetch_assoc($query_idocument_status)){
  $arrComment[$data_idocument_status['bookstatusid']]['txt'] = $data_idocument_status['bookstatusname'];
  $arrComment[$data_idocument_status['bookstatusid']]['badge'] = $data_idocument_status['badge_color'];
  $arrComment[$data_idocument_status['bookstatusid']]['color'] = $data_idocument_status['textColor'];
}

?>
<br>
<center>
<font color='#006666' size='3'><strong>รายการบันทึกเสนอ</strong></font>
</center>
<?php
//ส่วนของการแยกหน้า
$sql="select *  from `idocument_main`"; 
//echo $sql;
$dbquery = mysqli_query($connect,$sql);
$num_rows = mysqli_num_rows($dbquery );

$pagelen=15;  // 1_กำหนดแถวต่อหน้า
$url_link="option=idocument&task=viewlist";  // 2_กำหนดลิงค์ฺ
$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<10)){
  echo "<div align=center><nav aria-label=\"page navigation\">";
  echo "<ul class=\"pagination pagination-sm justify-content-center\">";
			for($i=1; $i<=$totalpages; $i++)	{
        if($i==$page){
					echo "<li class=\"page-item active\"><a href='#'>$i</a></li>";
					}
					else {
					echo "<li class=\"page-item\"><a href=$PHP_SELF?$url_link&page=$i>$i</a></li>";
					}
			}
  echo "</ul></nav></div>";  
    }			
if($totalpages>=10){
			if($page <=5){
			$e_page=10;
			$s_page=1;
			}
			if($page>5){
					if($totalpages-$page>=6){
					$e_page=$page+4;
					$s_page=$page-4;
					}
					else{
					$e_page=$totalpages;
					$s_page=$totalpages-9;
					}
			}
			echo "<div align=center><nav aria-label=\"page navigation\">";
			echo "<ul class=\"pagination pagination-sm justify-content-center\">";
			if($page!=1){
			$f_page1=$page-1;
			echo "<li class=\"page-item\"><a href=\"$PHP_SELF?$url_link&page=1\">First</a></li>";
			echo "<li class=\"page-item\"><a href=\"$PHP_SELF?$url_link&page=$f_page1\">Previous</a></li>";
			}
			else {
        echo "<li class=\"page-item disabled\"><a class = 'disabled'>First</a></li>";        
        echo "<li class=\"page-item disabled\"><a class = 'disabled'>Previous</a></li>";
        }					
			for($i=$s_page; $i<=$e_page; $i++){
					if($i==$page){
					echo "<li class=\"page-item active\"><a >$i</a></li>";
					}
					else {
					echo "<li class=\"page-item\"><a href=$PHP_SELF?$url_link&page=$i>$i</a></li>";
					}
			}
			if($page<$totalpages)	{
			$f_page2=$page+1;
			echo "<li class=\"page-item\"><a href=$PHP_SELF?$url_link&page=$f_page2>Next</a></li>";
			echo "<li class=\"page-item\"><a href=$PHP_SELF?$url_link&page=$totalpages>Last</a></li>";
      }
      else {
        echo "<li class=\"page-item disabled\"><a class = 'disabled'>Next</a></li>";        
        echo "<li class=\"page-item disabled\"><a class = 'disabled'>Last</a></li>";
        }	
echo "</ul></nav></div>";  
}					
//จบแยกหน้า
?>
<table border="1" width="98%" id="table2" align="center">
  <thead>
  <tr bgcolor=#99ccff>
    <td width="55" align="center">
    <font size="2" face="Tahoma" color=#FFFFFF>ที่</font></td>
    <td align="center" width="80">
    <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="400">
    <font face="Tahoma" size="2" color=#FFFFFF>ถึง</font></td>
    <td align="center" width="70">
    <font face="Tahoma" size="2" color=#FFFFFF>ราย<br />ละเอียด</font></td>         
    <td align="center" width="70">
    <font face="Tahoma" size="2" color=#FFFFFF>พิมพ์</font></td>         
    <td align="center" width="100">
    <font face="Tahoma" size="2" color=#FFFFFF>หมายเหตุ</font></td>
  </tr>
  </thead>
  <tbody>
<?php

$N=(($page-1)*$pagelen)+0; //*เกี่ยวข้องกับการแยกหน้า
$sql = "SELECT * FROM `idocument_main` order by id limit $start,$pagelen ";
$dbquery = mysqli_query($connect,$sql);
$row = 1;
$i = 0;
while($datarow = mysqli_fetch_assoc($dbquery)){
$bgcolor = (($i%2)==1)?" class='even'":" class='odd'";
$i++;
$N++;
?>
  <tr <?=$bgcolor;?>>
    <td align="center" height=25><?php echo $N;?></td>
    <td align="center"><?php echo $datarow['book_no'];?></td>
    <td><?php echo $datarow['subject'];?> </td>
    <td>
      <?php echo $datarow['book_to'];?>
  <!--a href="?option=<?php echo $option;?>&task=detail&id=<?php echo $datarow['id'];?>" class="btn btn-sm btn-success" ><i class="glyphicon glyphicon-search"></i>
  </a>
  <a href="#" class="btn btn-warning" ><i class="glyphicon glyphicon-menu-right"></i></a>
  <a href="?option=<?php echo $option;?>&task=edit&id=<?php echo $datarow['id'];?>" class="btn btn-sm btn-info " title = "แก้ไข"><i class="glyphicon glyphicon-edit"></i></a-->
  </td>
  <td align="center">
    <a href="?option=<?php echo $option;?>&task=detail&id=<?php echo $datarow['id'];?>" title = "รายละเอียด"><img src="images/browse.png" border="0"></a>
    </td>
    <td align="center">
    <a href="#" title = "รายละเอียด"><a href="modules/idocument/document-detail.php?id=<?php echo $datarow['id'];?>" target="_blank"><img src="images/b_print.png" border="0"></a>
    </td>
    <td>
<?php
if($datarow['book_status'] == "" || $datarow['book_status'] < 2){
  $commenttext = "กำลังดำเนินการ";
  $badge = "DodgerBlue";
}
if($datarow['book_status'] == 0){
  $commenttext = "กำลังดำเนินการ";
  $badge = "DodgerBlue";
}
if($datarow['book_status'] == 1){
  $commenttext = "กำลังดำเนินการ";
  $badge = "DodgerBlue";
}
if($datarow['book_status'] == 2){
  $commenttext = "รอง ผอ.ลงนาม";
  $badge = "green";
}
if($datarow['book_status'] == 3){
  $commenttext = "ผอ.ลงนามแล้ว";
  $badge = "green";
}
if($datarow['book_status'] == 99){
  $commenttext = "คืนเรื่อง";
  $badge = "orange";
}
$sql_chk_comment = "SELECT * FROM `idocument_comment` 
                    WHERE document_id = '".$datarow['id']."' 
                    ORDER BY ID DESC";
$dbquery_chk_comment = mysqli_query($connect,$sql_chk_comment);
  @$datacomment = mysqli_fetch_assoc($dbquery_chk_comment);

if($datacomment){
  $data_comment = explode(", ", $datacomment['comments_select']);
  @$commenttext .= ' : '.$arrComment[$data_comment[0]]['txt'];
  //$commenttext .= ' : '.$data_comment[0];
  @$badge = $arrComment[$data_comment[0]]['badge'];
}
$commenttext = '<span class="badge" style="background-color:'.$badge.'">'.$commenttext.'</small></span>';
?>      
    <!--span class="pull-right badge" style="background-color:green;">อนุมัติ</span>
    <span class="pull-right badge" style="background-color:orange;">ยกเลิก</span>
    <span class="pull-right badge" style="background-color:Red;">คืนเรื่อง</span-->
    <?=$commenttext;?>    
   </td>
  </tr>
<?php
$row++;
}
?>  
  </tbody>

</table>