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

$Docin = getDocin($whereclause, '');
$output_filename = "ETL_Import_document_".$_SESSION['orgunitname'].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>ລາຍການເອກະສານຂາເຂົ້າຂອງ ".$_SESSION['orgunitname']." </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>ປະເພດຂາເຂົ້າ</th>
			<th>ເລກທີ ETL</th>
			<th style='min-width: 20em;'>ເລື່ອງ</th>
			<th>ມາຈາກ</th>
			<th>ຜູ້ຮັບເອກະສານ</th>
			<th>ຄໍາເຫັນ</th>
			<th>ໝາຍເຫດ</th>	
		</tr>
		<?php $i=1;
			while ($item = mysql_fetch_array($Docin)) { 
				$bg = $i%2 == 1 ? "" : "#e1f9fa";
			?>
			<tr bgcolor="<?=$bg?>">
				<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 class="centered"><?=date("d-m-Y", strtotime($item['m_date']))?></td>
				<td><?=$item['doctype_name'] ?></td>
				<td><?=$item['io_name'] ?></td>
				<td width=""><?=$item['etl_code'] ?></td>
				<td><?=$item['doc_title'] ?></td>
				<td><?=$item['doc_from'] ?></td>
				<td>
				<table id="inuser">
				<?php
					$j = 1;
					$rs_org = orgunit($item['orgunitid'], $item['doc_id']);
					while ($rs = mysql_fetch_array($rs_org, MYSQL_ASSOC)) {
						$r_status = $rs['receive_status'] == 2 ? "ຮັບແລ້ວ" : "ບໍ່ໄດ້ຢືນຢັນຮັບ";
						$bgcolor = $rs['receive_status'] == 2 ? "" : "#fcfb82";
						?>
						
						<tr bgcolor="<?=$bgcolor?>"><td><?php echo $j.".".$rs['orgunitname'];?><?php echo "(".$r_status.")"?></td>
						</tr>
						<?php
						$j++;
					}
					?>
				</table>
				</td>
				<td><?= $item['doc_comment'] ?></td>
				<td><?=$item['remark'] ?></td>
				
			</tr>
		<?php $i++; } ?>
	</table>
<?php mysql_close($conn); ?>
