<?php
if(isset($_POST['name'])){
	$name=$_POST['name'];
	$sql = "SELECT * FROM prod001 WHERE PDNAME LIKE '%$name%' " ;
	$prev="href='prodExportFormName.php'";
}
if(isset($_POST['kind'])){
	$kind=$_POST['kind'];
	$sql = "SELECT * FROM prod001 WHERE PDKIND LIKE '%$kind%' " ;
	$prev="href='prodExportFormKind.php'";
}
if(isset($_POST['no'])){
	$no=$_POST['no'];
	$sql = "SELECT * FROM prod001 WHERE PDNO LIKE '%$no%' " ;
	$prev="href='prodExportFormNo.php'";
}
if(isset($_POST['begin']) and isset($_POST['finish'])){
	$begin=$_POST['begin'];
	$finish=$_POST['finish'];
	$sql = "SELECT * FROM prod001 WHERE PDDATE BETWEEN '$begin' AND '$finish'" ;
	$prev="href='prodExportForm.php'";
}

if(isset($_GET['act'])){
	if($_GET['act']== 'excel'){
		header("Content-Type: application/xls");
		header("Content-Disposition: attachment; filename=Product.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
	}
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Product DB Export To Excel</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
	<div class="row">
		<div class="col-md-5">
			<br /><br /><br />
			<h4>DB EXPORT TO EXCEL</h4>
			<div style="width: 500px;">
			<p>
				<a href="?act=excel" class="btn btn-primary"> Product Export->Excel </a>
				<a <?php echo $prev; ?> class="btn btn-primary"> Previous Menu </a>
				
				<?php session_start();
					if($_SESSION['Status']=='ADMIN'){ ?>
						<a href="../home.php" class="btn btn-primary"> Home </a>
					<?php }else{ ?>
						<a href="../homeOther.php" class="btn btn-primary"> Home </a>
					<?php } ?>	
				<a href="../logout.php" class="btn btn-primary"> Logout </a>
			</p>
			</div>
			
			<table border="1" class="table table-hover">
				<thead>
					<tr>
						<th style="text-align: center;">Date</th>
						<th style="text-align: center;">รหัสสินค้า</th>
						<th style="text-align: center;">ประเภท</th>
						<th style="text-align: center;">ผลิตภัณฑ์</th>
						<th style="text-align: center;">ราคา</th>
						<th style="text-align: center;">ขาย</th>
						<th style="text-align: center;">From</th>
						<th style="text-align: center;">ส่วนลด</th>
						<th style="text-align: center;">หมายเหตุ</th>									
					</tr>
				</thead>
				<?php
					include "../functions/dblink.php" ;
					/*$sql = "SELECT * FROM prod001 WHERE (PDNO LIKE '%".$prod."%') AND (PDDATE BETWEEN '$begin' AND '$finish') " ;*/
					$result = mysqli_query($con,$sql) ;
					while($row = mysqli_fetch_assoc($result)){ ?>
						<tr>
							<td style="text-align: left;"><?php echo $row['PDDATE']; ?></td>
							<td style="text-align: left;"><?php echo $row['PDNO']; ?></td>
							<td style="text-align: left;"><?php echo $row['PDKIND']; ?></td>
							<td style="text-align: left;"><?php echo $row['PDNAME']; ?></td>
							<td style="text-align: right;"><?php echo number_format($row['PDCSTPRC'],2); ?></td>
							<td style="text-align: right;"><?php echo number_format($row['PDSLEPRC'],2); ?></td>
							<td style="text-align: left;"><?php echo $row['PDFROM']; ?></td>
							<td style="text-align: right;"><?php echo number_format($row['PDDISC'],2); ?></td>
							<td style="text-align: left;"><?php echo $row['PDNOTE']; ?></td>
						</tr>
				<?php } ?>
			</table>
		</div>
	</div>
</div>
</body>
</html>