<?php session_start();?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<?php
		include_once("../../functions/init.php"); 

		$tmp = $thisUrl."adm1n/functions/access.php";
		include ($tmp);

		$tmp = $thisUrl."adm1n/functions/koneksi.php";
		include_once ($tmp);

		$tmp = $thisUrl."adm1n/functions/head_section.php";
		include ($tmp);
		?>
    </head>	
	<body>
    	<div class="se-pre-con"></div>
		<center>
			<table width="1024">
				<tr valign="top">
					<td width="207" class="bg">
						<?php 
						$tmp = $thisUrl."adm1n/functions/kiri.php";
						include ($tmp);
						?>
					</td>
					<td width="10"></td>
					<td width="807">
						<?php 
						$tmp = $thisUrl."adm1n/functions/logo.php";
						include ($tmp);
						
						$tmp = $thisUrl."adm1n/functions/getUrl.php";
						include ($tmp);
						
						$tmp = $thisUrl."adm1n/functions/php_functions.php";
						include ($tmp);

						echo '<div>'.$txt_legend.'</div>';

						$action = (isset($_REQUEST["action"]) ? $_REQUEST["action"] : null);						
						//echo "action : ".$action ;
						$elementSubID = (isset($_REQUEST["elementSubID"]) ? $_REQUEST["elementSubID"] : null);
						$editProduct  = (isset($_POST["editProduct"])  ? $_POST["editProduct"]  : null);
						//echo "<br>elementSubID : ".$elementSubID;
						//echo "<br>editProduct : ".$editProduct;
					
						if ($action == "Add Product"){
							include ("add_record.php");
						} else if ($action == "Edit Product"){
							include ("edit_record.php");
						} else {
							$action = "location='?action=Add Product'";
							echo '						
							<div style="display:block;">
								<div style="margin-top:15px"></div>
								<div style="float:left">
									<form name="form" id="form" class="form" action="?action=search" onsubmit="return validate(this)" method="post">
										<strong>Product Name</strong>: <br>
										<input type="text" name="s_text" value="'.$s_text.'" size="50">
										<input type="submit" name="s_submit" class="button1" value="Search">
									</form>
								</div>
								<div style="float:right">
									<div id="tab1" class="button" onClick="'.$action.'">Add Record</div>
								</div>
							</div>
							<div style="clear:both; margin-top:15px">&nbsp;</div>
							<table width="100%" cellpadding="10" cellspacing="1" border="0" bgcolor="#222222">
								<tr bgcolor="#DDDDDD">
									<td width="10%" align="center"><strong>NO</strong></td>
									<td width="70%"><strong>PRODUCT NAME</strong></td>
									<td width="20%" colspan="2" align="center"><strong>ACTION</strong></td>
								</tr>';
								
								//echo "elementSubID: ".$elementSubID;
								$sql = 
								" SELECT DISTINCT elementSubID, elementSubDescription ".
								" FROM form_element_sub, form_element_link ".
								" WHERE linkElementSubID = elementSubID ".
								" AND linkDesignID = '41' ";
							
								if ($s_text) {
									$sql .= 
									" AND UPPER(elementSubDescription) ".
									" like '%".strtoupper($_POST["s_text"])."%' ";
								} else if ($elementSubID) {
									$sql .= 
									" AND elementSubID = ".$elementSubID;
								}

								$sql .= 
								" ORDER BY elementSubDescription ";
								//echo $sql; 

								if($ress=mysqli_query($conn, $sql))
									$max = mysqli_num_rows($ress);
								else 
									$max = 0;

								$perhalaman = 15;
								$page = (isset($_SESSION['page']) ? $_SESSION['page'] : (isset($_REQUEST["page"]) ? $_REQUEST["page"] : null));
								//$page = $_SESSION['page'];
								//if (!$page) $page = $_REQUEST["page"];
								if ($page){
									$start=($page - 1) * $perhalaman;
								} else {
									$start=0;
								}
								
								$sql  .= " LIMIT $start, $perhalaman";
								//echo "<br>sql: ".$sql;
								if ($hasil = mysqli_query($conn, $sql)) {
									$counter = $start;
									while ($data = mysqli_fetch_array($hasil)) {
										$counter++;
										$editProduct  = 'onclick="';
										$editProduct .= "location='?action=Edit Product&elementSubID=".$data["elementSubID"]."'";
										$editProduct .= '"';

										$detailProduct	= 'onclick="';
										$detailProduct	= "location='?action=Detail Product&elementSubID=".$data["elementSubID"]."'";
										$detailProduct	= '"';
										echo '
										<tr bgcolor="#FFFFFF">
											<td align="center">'.$counter.'</td>
											<td>'.strtoupper($data["elementSubDescription"]).'</td>
											<td align="center">
												<div style="cursor:pointer;" '.$editProduct.'>Edit</div>
											</td>
											<td align="center">
												<div style="cursor:pointer;" '.$detailProduct.'>Detail</div>
											</td>											
										</tr>';
									}
								}
								echo '
							</table> 
							<div align="center" style="margin:30px 0px">';
								$tmp = $thisUrl."functions/fungsi_paging.php";
								include($tmp);
								halaman($max, $perhalaman, 3, "?");
								echo '
							</div>';
						}
						?>
					</td>
				</tr>
			</table>
		</center>
	</body>
</html>	