<?php
session_start();
?>

<!-- Bootstrap Include -->
<link rel="stylesheet" type="text/css" href="../../bootstrap-3.3.5-dist/css/bootstrap.min.css"> 
<link rel="stylesheet" type="text/css" href="../../bootstrap-3.3.5-dist/css/datepicker.css"> 
<script src="../../bootstrap-3.3.5-dist/js/jquery-1.11.3.min.js"></script> 
<script src="../../bootstrap-3.3.5-dist/js/bootstrap.min.js"></script> 
<script src="../../bootstrap-3.3.5-dist/js/bootstrap-confirmation.min.js"></script> 
<script src="../../bootstrap-3.3.5-dist/js/bootstrap-datepicker.js"></script> 
<script src="../../bootstrap-3.3.5-dist/js/jquery.dataTables.min.js"></script> 
<script src="../../bootstrap-3.3.5-dist/js/dataTables.bootstrap.min.js
"></script> 
<script src="../../bootstrap-3.3.5-dist/js/bootstrap-select.min.js"></script> 
<link rel="stylesheet" type="text/css" href="../../bootstrap-3.3.5-dist/css/bootstrap-select.min.css"> 
<!--script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> 
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js
"></script--> 
<style type="text/css">
@font-face {
    font-family: 'Sarabun';
    src:url('fonts/Sarabun.ttf.woff') format('woff'),
        url('fonts/Sarabun.ttf.svg#Sarabun') format('svg'),
        url('fonts/Sarabun.ttf.eot'),
        url('fonts/Sarabun.ttf.eot?#iefix') format('embedded-opentype'); 
    font-weight: normal;
    font-style: normal;
}
@page 
{
    size: A4;
    margin: 0 2cm; 
}

@media print {
   .page {
    margin: 0 2cm; 
    border: initial;
    border-radius: initial;
    width: initial;
    min-height: initial;
    box-shadow: initial;
    background: initial;
    page-break-after: always;
    position: relative;
  }
}
body , *,#content td{
  font-family: "Sarabun";
  font-size: 16pt;
}
</style>

<?php	
/** ensure this file is being included by a parent file */
date_default_timezone_set('Asia/Bangkok');
require_once "../../amssplus_connect.php";	
require_once("../../mainfile.php");

require_once "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']] = $data_idocument_status['bookstatusname'];
}
?>
<br>
<?php
$id = $_REQUEST['id'];
$sql = "Select * From idocument_main where id = $id";
$dbquery = mysqli_query($connect,$sql);
$dataDisplay = mysqli_fetch_assoc($dbquery);
?>

<font face="Sarabun" size="16pt" >
<table width="95%" border="0" align=center>
  <tr>
    <td width="60">
      <img = src="main/krut.png" border="0" width="60">
    </td>
    <td align = "left" valign="top" colspan="3">
      <strong> &nbsp;&nbsp;&nbsp;บันทึกข้อความ</strong>
    </td>
  </tr>
  <tr>
    <td colspan="4">
      <p>
        <strong>ส่วนราชการ </strong> <?php echo $dataDisplay['workgroup_txt'];?>
      </p>
    </td>
  </tr>
  <tr>
    <td valign="middle" width = "40%" colspan="2" align="left" > 
      <p>    
        <strong>ที่  </strong><?php echo $dataDisplay['book_no'];?>
      </p>
    </td>
    <td valign="middle" align="left" colspan="2">
      <p>
        <strong>วันที่ </strong><?php echo thai_dateBook($dataDisplay['book_date']);?>
      </p>
    </td>
  </tr>  
  <tr>
    <td colspan="4" style="border-bottom: 1px solid">
      <p>
        <strong>เรื่อง</strong> <?php echo $dataDisplay['subject'];?>
      </p>
    </td>
  </tr>
  <tr>
    <td colspan="4" style="">
      <p><br>
        <strong>เรียน </strong><?php echo $dataDisplay['book_to'];?>
      </p>
    </td>
  </tr>
<?php
//Check Ref doc
$sql = "Select * From idocument_files Where document_id = '$id' and docType = 'ref'";
$result = mysqli_query($connect,$sql);
if($result->num_rows > 0){
$i = 1;
echo'  <tr>
    <td valign="top" width ="120"><strong>อ้างถึง</strong>
    </td>
    <td colspan="3" style="">
      <p>';
      while ($row = $result->fetch_assoc()) {
        if(!empty($row['file_name'])){
          if(empty($row['file_des'])){
            echo $i.". <a target='_blank' href='".$row['file_name'] ."'>เอกสารหนังสืออ้างถึง</a><br>";
          }else{
            echo $i.". <a target='_blank' href='".$row['file_name'] ."'>".$row['file_des']."</a><br>";
          }
        }else{
          echo $i.". ". $row['file_des']."<br>";
        }
        $i++;
      }
echo'        </p>
    </td>
  </tr>';
}
?>  
<?php
//Check Ref doc
$sql = "Select * From idocument_files Where document_id = '$id' and docType = 'attach'";
$result = mysqli_query($connect,$sql);
if($result->num_rows > 0){
$i = 1;
echo'  <tr>
    <td valign="top" width ="120"><strong>สิ่งที่ส่งมาด้วย</strong>
    </td>
    <td colspan="3" style="">
      <p>';
      while ($row = $result->fetch_assoc()) {
        if(!empty($row['file_name'])){
          if(empty($row['file_des'])){
            echo $i.". <a target='_blank' href='".$row['file_name'] ."'>เอกสารสิ่งที่ส่งมาด้วย</a><br>";
          }else{
            echo $i.". <a target='_blank' href='".$row['file_name'] ."'>".$row['file_des']."</a><br>";
          }
        }else{
          echo $i.". ". $row['file_des']."<br>";
        }
        $i++;
      }
echo'        </p>
    </td>
  </tr>';
}
?>  
  <tr>
    <td colspan="4" style="">
      <p align="justify">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo nl2br(str_replace(" ", "&nbsp;", $dataDisplay['content1']));?>
      </p>
    </td>
  </tr>
  <tr>
    <td colspan="4" style="">
      <p align="justify">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo nl2br(str_replace(" ", "&nbsp;", $dataDisplay['content2']));?>
      </p>
    </td>
  </tr>
  <tr>
    <td colspan="4" style="">
    <p align="justify">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo nl2br(str_replace(" ", "&nbsp;", $dataDisplay['content3']));?>
      </p>
    </td>
  </tr>
  <tr>
    <td colspan="3" style="text-align:center;">
ตรวจสอบเสนอ
<br>
<?php
// Add signature
$sigFile = "../person/signature/". $dataDisplay['officer'] .".png";
//$sigFile = "./signature/3700300113267.png";
if(file_exists($sigFile)){
	echo "<img src='". $sigFile ."' border='0' width='150'>";
}
?>
<br>
(<?php echo $dataDisplay['officer_name'];?>)
<br>
<?php echo $dataDisplay['officer_position'];?>
<br>
<?php echo thai_dateBook($dataDisplay['book_date']);?>
</td>
    <td style=""></td>
  </tr>
</table>
</font>
<?php
$sqlComment = "Select * From idocument_comment where document_id = $id Order by id";
//echo $sqlComment;
$dbqueryComment = mysqli_query($connect,$sqlComment); 
if(mysqli_num_rows($dbqueryComment)==0){

}else{
  echo "<br><br>";
  while($commentData = mysqli_fetch_array($dbqueryComment)){
$fullname  = '( '. $commentData['person_comments_name'].' )<br>
'. $commentData['person_comments_position'].'<br>
'. thai_dateBook($commentData['comments_date']);
//'. thai_dateBook(date("Y-m-d",$commentData['comments_date']));
$data_comment = explode(", ", $commentData['comments_select']);
$comment ="";
foreach($data_comment as $value){
  @$comment .=($value=="")?"":("-". (($arrComment[$value])?$arrComment[$value]:$value)."<br />");
}

echo "<div style='position: relative; width:33%; display: inline-block; font-size: 12pt; font-family: \"Sarabun\";'>";
echo $comment;
echo "<p align=center style='font-size: 12pt; font-family: \"Sarabun\";'>";

// Add signature
$sigFileComment = "../person/signature/". $commentData['person_comments_id'] .".png";
//$sigFile = "./signature/3700300113267.png";
if(file_exists($sigFileComment)){
	echo "<img src='". $sigFileComment ."' border='0' width='150'>";
}

echo "<br>".$fullname;
echo "</p>";
echo "</div>";
  }
}
?>
<?php
//print_r($_SESSION);
?>
<script>
    window.print();
</script>