<?php
session_start();
if(!isset($_SESSION['sys_id'])) header("Location: ?d=index");
require_once("../../config.php");
require_once("m_docout.php");

$Docout = getDocout($whereclause, '');
$output_filename = "ETL_Export_document".date("d-m-Y").".xls";
// Redirect the output to the stream
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename={$output_filename}");

echo "<h4>ລາຍງານເອກະສານຂາອອກ ລວມທັງຂອງ ພະແນກ/ສາຂາ</h4> ";

if ($startdate != null && $enddate != null) echo "<p>ແຕ່ວັນທີ ".date("d-m-Y", strtotime($startdate))." ຫາ ".date("d-m-Y", strtotime($enddate))."</p> \n"; ?>		
	<table border='1'> 
		<tr bgcolor='#8EE5EE'>
			<th width="50">No.</th>
			<th>ເລກທີເອກະສານ</th>
			<th>ວັນທີເອົາເຂົ້າ</th>
			<th>ປະເພດເອກະສານ</th>
			<th style='min-width: 20em;'>ເລື່ອງ</th>
			<th>ຜູ້ເຊັນເອກະສານ</th>
			<th>ບ່ອນສົ່ງ(ຮຽນ)</th>
			<th>ຂາເຂົ້າເລກທີ</th>
			<th>ຜູ້ຮັບເອກະສານ</th>	
			<th>ຄໍາເຫັນ</th>	
			<th>ໝາຍເຫດ</th>	
		</tr>
		<?php $i=1;
			while ($item = mysql_fetch_array($Docout)) { ?>
			<tr>
				<td class="centered">
					<span><?= ($i+$start) ?></span>
					<input type="hidden" name="type[]" class="type" value="unchanged" />
					<input type="hidden" id = "id[]" name="id[]" value="<?=$item["doc_id"]?>" class="id"/>
				</td>
				<td><?=$item['doc_code'].", ".date("d-m-Y", strtotime($item['doc_date']));?></td>
				<td><?=date("d-m-Y", strtotime($item['m_date']))?></td>
				<td><?=$item['doctype_name'] ?></td>
				<td><?=$item['doc_title'] ?></td>
				<td><?=$item['tm_signer'] ?></td>
				<td><?=$item['place_sent'] ?></td>
				<td><?=$item['refer_code'] ?></td>
				<td>
				<?php if($item['doc_io_id']==1 || $item['doc_io_id']==3){ ?>
				<table id="inuser">
					<?php if($item['doc_to']!="")?><tr><td><?=$item['doc_to'] ?></td></tr>
				<?php
					$j = 1;
					$rs_org = orgunit($item['orgunitid'], $item['doc_id']);
					$doc_status ="";
					while ($rs = mysql_fetch_array($rs_org, MYSQL_ASSOC)) { 
						$r_status = "";
						if($rs['receive_status'] == 1){
							$r_status =  "ບໍ່ໄດ້ຢືນຢັນຮັບ";
						}elseif($rs['receive_status'] == 2){
							if($rs['type_doc_status']=='doc_out'){
								$r_status =  "ຂາອອກຂອງພະແນກ";
							}else{
								$r_status =  "ຮັບແລ້ວ";
							}
						}
						
						$bgcolor = $rs['receive_status'] == 2 ? "" : "#fcfb82";
						?>
						
						<tr bgcolor="<?=$bgcolor?>"><td><?echo $j.".".$rs['orgunitname'];?><?php echo "(".$r_status.")"?></td>
						</tr>
						<?php
						$j++;
					}
					?>
				</table>
				<?php }else{ ?>
				<?=$item['doc_to'] ?>
				<?php } ?>
				</td>
				<td><?=$item['doc_comment'] ?></td>
				<td><?=$item['remark'] ?></td>
			</tr>
		<?php $i++; } ?>
	</table>
<?php mysql_close($conn); ?>
