<!doctype html>
<html>

<head>
	<meta charset="UTF-8">
	<title>Quotation List</title>
	<!--<link rel="stylesheet" href="../css/bootstrap.min.css"> -->
	<link rel="stylesheet" href="../css/bootstrap-theme.min.css">
	<style type="text/css">
		td,
		th {
			border-right: 1px solid #DBDBDB;
			vertical-align: top;
		}

		#headq {
			background: green;
			color: aliceblue;
		}

		/*div#menu { margin:5px auto; }*/

		.third-level-menu {
			position: absolute;
			top: 0;
			right: -150px;
			width: 150px;
			list-style: none;
			padding: 0;
			margin: 0;
			display: none;
		}

		.third-level-menu>li {
			height: 30px;
			background: #999999;
		}

		.third-level-menu>li:hover {
			background: #CCCCCC;
		}

		.second-level-menu {
			position: absolute;
			top: 30px;
			left: 0;
			width: 150px;
			list-style: none;
			padding: 0;
			margin: 0;
			display: none;
		}

		.second-level-menu>li {
			position: relative;
			height: 30px;
			background: #999999;
		}

		.second-level-menu>li:hover {
			background: #CCCCCC;
		}

		.top-level-menu {
			list-style: none;
			padding: 0;
			margin: 0;
		}

		.top-level-menu>li {
			position: relative;
			float: left;
			height: 30px;
			width: 150px;
			background: #999999;
		}

		.top-level-menu>li:hover {
			background: #CCCCCC;
		}

		.top-level-menu li:hover>ul {
			display: inline;
		}

		/* On hover, display the next level's menu */

		/* Menu Link Styles */
		/* Apply to all links inside the multi-level menu */
		.top-level-menu a {
			font: bold 14px Arial, Helvetica, sans-serif;
			color: #FFFFFF;
			text-decoration: none;
			padding: 0 0 0 10px;
			/* Make the link cover the entire list item-container */
			display: block;
			line-height: 30px;
			text-align: center;
		}

		.top-level-menu a:hover {
			color: #000000;
		}

		#search {
			position: absolute;
			left: 572px;
		}

		.wd {
			table-layout: fixed;
			width: 100%;
			margin-top: 50px;
		}
	</style>
</head>

<body>
	<ul class="top-level-menu">
		<li><a href="actionController.php?
					 action=seller/addQuotationAction&
					 actionIs=insert">Add Quotation</a>
		</li>
		<li><a href="#">Quotation Report By</a>
			<ul class="second-level-menu">
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQA">All Quot.</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQD">Date</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQNo">Quotation No.</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQC">Contact</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQN">Name</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQSl">Saller</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQPj">Project Name</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQV">Value(Desc.)</a></li>
				<li><a href="actionController.php?
					             action=seller/retrieveQuotationActionQ">All Quot.(Ordered)</a></li>
			</ul>
		</li>
		<li><a href="#"></a></li>
	</ul>

	<?php
	include "functions/dblink.php";
	$sql = "SELECT * FROM quot002 WHERE QLOCK='0' ORDER BY QDATE,QC_NO DESC";
	$query = mysqli_query($con, $sql);
	?>

	<div id="search">
		<!-- echo $_SERVER['SCRIPT_NAME']; -->
		<form name="frmSearch" method="post" action="
          <?php echo 'actionController.php?action=seller/retrieveQuotationAction
															&$strKeyword1&$strKeyword2'; ?>">
			<table>
				<tr>
					<td>
						พนักงานขาย
						<input type="text" name="txtSeller" id="txtSeller" size="49" placeholder="ค้นหา - ทั้งหมด">
					</td>
				</tr>
				<tr>
					<td>
						วันที่
						<input name="txtKeyword1" type="date" id="txtKeyword1" value="<?php echo $strKeyword1; ?>"> -
						<input name="txtKeyword2" type="date" id="txtKeyword2" value="<?php echo $strKeyword2; ?>">
						<input type="submit" value="ค้นหา">
					</td>
				</tr>
			</table>
		</form>
	</div>

	<?php

	//error_reporting(0);
	include "pagination.php";
	include "functions/dblink.php";

	ini_set('display_errors', 1);
	error_reporting(~0);

	if (isset($_POST["txtKeyword1"]) and isset($_POST["txtKeyword2"])) {
		if ($_POST["txtKeyword1"] == "") {
			$strKeyword1 = date('Y-m-d', strtotime('1900-01-01'));
		} else {
			$strKeyword1 = $_POST["txtKeyword1"];
		}
		if ($_POST["txtKeyword2"] == "") {
			$strKeyword2 = date('Y-m-d');
		} else {
			$strKeyword2 = $_POST["txtKeyword2"];
		}
	} else {
		$strKeyword1 = date('Y-m-d', strtotime('1900-01-01'));
		$strKeyword2 = date('Y-m-d');
		$sql = "SELECT * FROM quot002 WHERE (QLOCK=0 OR QLOCK='' OR QLOCK IS NULL) AND QDATE>='$strKeyword1' AND QDATE<='$strKeyword2'
		            ORDER BY QDATE DESC,QNO DESC ";
	}

	if (isset($_POST["txtSeller"])) {
		$seller = $_POST["txtSeller"];
		if ($seller == "") {
			$sql = "SELECT * FROM quot002 WHERE (QLOCK=0 OR QLOCK='' OR QLOCK IS NULL) AND QDATE>='$strKeyword1' AND QDATE<='$strKeyword2'
		            ORDER BY QDATE DESC,QNO DESC ";
		} else {
			$sql = "SELECT * FROM quot002 WHERE (QLOCK=0 OR QLOCK='' OR QLOCK IS NULL) AND QDATE>='$strKeyword1' AND QDATE<='$strKeyword2' AND
					QSMFNAME LIKE '%$seller%'
					ORDER BY QDATE DESC,QNO DESC ";
		}
	}
	$perpage = 25;
	//$seller=$_POST["txtSeller"];

	$find = "ทั้งหมด";
	$condition = 1;
	//$perpage=25;

	//กรณี chkbox แล้วโพสกลับขึ้นมา
	if (isset($_POST['NO'])) {
		$id = implode(", ", $_POST['NO']);
		$sql = "DELETE FROM quot002 WHERE NO IN($id)";
		mysqli_query($con, $sql);
	}
	$query = mysqli_query($con, $sql);
	$num = mysqli_num_rows($query);

	if (isset($_POST["txtKeyword1"]) and isset($_POST["txtKeyword2"])) {
		$perpage = $num;
	}

	$rs = page_query($con, $sql, $perpage);

	$first = page_start_row();
	$last = page_stop_row();
	$total = page_total_rows();
	if ($total == 0) {
		$first = 0;
	}

	//<a href='form.php?action=insert'>เพิ่มข้อมูล</a>
	echo '<form method="post">';
	echo "<table class='wd'><caption>";     //อ่านข้อมูล result set มาแสดงในรูปแบบตาราง
	echo "<h1>รายการใบเสนอราคา (Quotation List)</h1>";
	echo "<h1>(ยังไม่ได้ออก Order)</h1>";
	echo "<span id=\"result\">รายการที่ : $first - $last จากทั้งหมด $total</span>
					 </caption>";
	echo "<tr id='headq' >";      //ส่วนหัวตาราง(ชื่อฟิลด์)

	echo "<th width='13%'>act</th>";
	echo "<th width='8%'>วันที่</th>";
	echo "<th width='10%'>Quot.No.</th>";
	echo "<th width='19%'>Customer Name</th>";
	echo "<th width='8%'>ลด</th>";
	echo "<th width='26%'>รายการสินค้า</th>";
	echo "<th width='3%'>Qty</th>";
	echo "<th width='13%'>บาท</th>";
	echo "</tr>";

	while ($data = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {
		error_reporting(~E_NOTICE);
		echo "<tr>";
		echo "<td><a href=\"actionController.php?
			                action=seller/editQuotationAction&NO={$data['NO']}&
							actionIs=update\">[แก้ไข]</a>
				  <a href=\"actionController.php?
							action=seller/deleteQuotationAction&NO={$data['NO']}&
							actionIs=delete\">[ลบ]</a>  									
				  <a href=\"report-content/quotationIndReport.php?
							no={$data['QNO']}\">[พิมพ์]</a></td>"; //เวลานำไปใช้ $no=$_GET['no'];

		$tot1 = $data{"QQTY1"} * $data{"QSLEPRC1"};
		$tot2 = $data{"QQTY2"} * $data{"QSLEPRC2"};
		$tot3 = $data{"QQTY3"} * $data{"QSLEPRC3"};
		$tot4 = $data{"QQTY4"} * $data{"QSLEPRC4"};
		$tot5 = $data{"QQTY5"} * $data{"QSLEPRC5"};
		$tot6 = $data{"QQTY6"} * $data{"QSLEPRC6"};
		$tot7 = $data{"QQTY7"} * $data{"QSLEPRC7"};
		$tot8 = $data{"QQTY8"} * $data{"QSLEPRC8"};
		$tot9 = $data{"QQTY9"} * $data{"QSLEPRC9"};
		$tot10 = $data{"QQTY10"} * $data{"QSLEPRC10"};
		$tot = $tot1 + $tot2 + $tot3 + $tot4 + $tot5 + $tot6 + $tot7 + $tot8 + $tot9 + $tot10;
		if ($data['QC_DISC'] <= 100) {
			$disc = $tot * $data['QC_DISC'] / 100;
		} else {
			$disc = $data['QC_DISC'];
		}
		$gtot = $tot - $disc;

		echo "<td>" . date_format(date_create("$data[QDATE]"), "d-m-Y") . "</td>";
		echo "<td>$data[QNO]</td>";
		echo "<td>$data[QC_NAME]</td>";
		echo "<td class=rt>" . number_format($data['QC_DISC'], 2) . "</td>";
		//echo "<td>$data[QC_ADRS]</td>";
		echo "<td>$data[QPDNAME1]</td>";
		echo "<td class=rt>" . number_format($data['QQTY1']) . "</td>";
		echo "<td class=rt>" . number_format($data["QQTY1"] * $data["QSLEPRC1"],2) . "</td>";
		echo "<tr>";

		//$data{"QPDNAME{$i}"}; ${"tsle{$i}"}
		for ($i = 2; $i <= 10; $i++) {
			$blank=NULL;
			if ($data{"QPDNAME{$i}"} != "" OR $data{"QQTY{$i}"} != 0 OR $data{"QQTY{$i}"} != is_null($blank)) {
				//echo "<td style='width:5%;'></td>"; ${"oqpdno{$i}"}
				echo "<td></td>";
				echo "<td></td>";
				echo "<td></td>";
				echo "<td></td>";
				//echo "<td></td>";
				echo "<td></td>";
				echo "<td>" . $data{"QPDNAME{$i}"} . "</td>";
				echo "<td class=rt>" . number_format($data{"QQTY{$i}"}) . "</td>";
				echo "<td class=rt>" . number_format($data{"QQTY{$i}"} * $data{"QSLEPRC{$i}"},2) . "</td>";
				//echo "<td class=rt>".number_format($tot.$i)."</td>";
				echo "<tr>";
			}
		}

		echo "<tr>
				<td></td><td></td><td></td><td></td><td></td>
				<td class=rtq>ทั้งสิ้น(หักลด)</td><td></td>
				<td class=rtq>" . number_format($gtot, 2) . "</td>
		      </tr>";

						echo "<tr>";
						echo "<th width='13%'><hr color='#DBDBDB' size='.1'></th>";
						echo "<th width=' 8%'><hr color='#DBDBDB' size='.1'></th>";
						echo "<th width='10%'><hr color='#DBDBDB' size='.1'></th>";
						echo "<th width='19%'><hr color='#DBDBDB' size='.1'></th>";
						echo "<th width=' 8%'><hr color='#DBDBDB' size='.1'></th>";
						echo "<th width='26%'><hr color='#DBDBDB' size='.1'></th>";
						echo "<th width=' 3%'><hr color='#DBDBDB' size='.1'></th>";
						echo "<th width='13%'><hr color='#DBDBDB' size='.1'></th>";
						echo "</tr>";
	}
	echo "</table>";
	echo "</form>";

	if (page_total() > 1) {
		echo '<p id="pagenum">';
		page_echo_pagenums();
		echo '</p>';
		echo "Total Page : " . ceil($total / $perpage);
	}

	mysqli_close($con);
	?>
	<script src="../js/bootstrap.min.js"></script>
</body>

</html>