<?php
header("Content-type: text/html; charset=utf-8");
session_start();

if (!function_exists('ConvertCurrency')) {
	function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
	{
		$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
		$result = selectData($connection, $query);
		//echo $query;
		Return	$result[0];

	}
}

if (!empty($_GET["action"])) {
	$dftorderby   = $_GET["dftorderby"];
	$action 	  = $_GET["action"];
	$orderby 	  = $_GET["orderby"];
	$morelines	  = $_GET["morelines"];
	$startrange   = $_GET["startrange"];
	$perpage 	  = $_GET["perpage"];
	$totalrows    = $_GET["totalrows"];
	$direction    = $_GET["direction"];
	$filepath     = $_GET["filepath"];
	$transid      = $_GET["transid"];
	$transdtid    = $_GET["transdtid"];
	$form      	  = $_GET["form"];
	
	
	
	include("config.inc.php");
	include("Functions_Sales.php");
	
	$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
	mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");
	
	
	if ($form == 'IAD') {
		ShowItemAuctionDtGrid($connection,$form,$transid,$_SESSION['gprd_parameters'],$_SESSION['gprd_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	
}

function ShowItemAuctionDtGrid($connection,$form,$auction_id,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
		
	/// grid Motion
		$Motion   = 1;
		$NextCell = 'ledger_number[0]';
		$TabIndex = 0;		
	/////////////
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid_Items.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("ledger_number", "ledger_name", "checked_auction");


		
	$columnsarrayall = array("ledger_number", "ledger_name", "checked_auction", "auctiondt_id");
	$orderbyarray = array("item_auctiondt.supplier_code");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 660;
		
	//Initializations for Div Ids
	$ContainerDiv = 'container1';
	$LoaderDiv    = 'Loader1';
	$HeaderDiv    = 'grid_headerDiv1';
	$ContentDiv   = 'grid_contentDiv1';
	$TableFocus   = 'table-focus-g1';
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='$ContainerDiv' style='position: relative;z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:700px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25'>";
		echo"  <th width='30px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='30px;' align='center'><span>&nbsp;</span></th>";		
		echo"  <th width='160px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$auction_id."', 'datagrid-1', 1, '".$dftorderby."', 'COR','suppliers.ledger_number','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Supplier Code".$orderimg2."</span></th>";
		echo"  <th width='310px;' ><span>Supplier Name</span></th>";
		
		echo"  <th width='100px;' align='center'><span>Checked</span></th> ";	
		echo"  <th width='30px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv');\" style='width:700px;height:430px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec_G1' id='SelectedRec_G1' value='$SelectedRec_G1'>
			 <input type='hidden' name='SelectedRecColor_G1' id='SelectedRecColor_G1' value='$SelectedRecColor_G1'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($auction_id != '' ) {
				  
			$query = " select auctiondt_id, supplier_code,ledger_name, auction_checked ".
					 " from temp_item_auctiondt as item_auctiondt ".
					 " left join suppliers on suppliers.ledger_number = item_auctiondt.supplier_code 
					where auction_id=$auction_id ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
						
					
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_1['.$i.']\')"';
				
				echo"<tr id='CurRec_G1$i' class='$bgcolor $TableFocus'>";		
				echo"	<td width='30px;' $RecStyle>$recno</td>";
				echo"  <td width='30px;' align='center'><img src='img/delete.png' width='20' height='20' onmouseover=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$i."','".$filepath."', '".$form."', '".$auction_id."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."','ledger_number[$j]',$Motion,$TabIndex); return false;\" title='Delete Record'></td>";			
					
				echo "	<td width='160px;' align='left'>
							<input type='hidden' name='txtAuctionIddt[$i]' id='auction_iddt[$i]' value='".$rowsdt[0]."' >
							<input type='text' name='txtLedger_Number[$i]' id='ledger_number[$i]' value='".$rowsdt[1]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:150px; height:20px;' readonly>
						</td>";
										
				echo"  	<td width='310px;' align='center'><input type='text' name='txtLedger_Name[$i]' id='ledger_name[$i]' value='".$rowsdt[2]."' class='".$ClassE."' style='font-size:12px; width:330px; height:20px;' readonly />
						</td>";
				if($rowsdt[3] == 1)	$chk = "checked";
				else $chk = " ";
				echo"  	<td width='100px;' align='center'>
							<input type='checkbox' name='txtChecked_Auction[$i]' id='checked_auction[$i]' value='".$rowsdt[3]."'   onclick=\"setCheck('$i'); \" $chk  />
						</td>";
				
			
				$j=$i+1;
				
				echo "	<td width='30px;' align='center' valign='center'><input type='button' id='imgSubmit_1[$i]' 
				onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$auction_id."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."','ledger_number[$j]',$Motion,$TabIndex); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
			}
			
			
		}
		
		
							
	
	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$auction_id,'datagrid-1',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	$query = " Select Count(*) FROM temp_item_auctiondt ".
			 " WHERE auction_id = ".$auction_id;
			 
	$result = selectData($connection, $query);
	$txtPrdCountFG = $result[0];
	if($txtPrdCountFG == '') $txtPrdCountFG = 0;
	
	echo"<input type='hidden' name='txtPrdCountFG' value='$txtPrdCountFG' >";
	
}


function showDataGridFooter($filepath,$form,$transid,$gridid,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction) {
	
	echo"<div class='grid_toolbarDiv'>
			<table width='100%' border=0 cellpadding='0' cellspacing='0'>
			<tr>";
	
	$rangefrom = ($startrange + 1);
	if (($startrange + $perpage) >= $totalrows) $rangeto = $totalrows; else $rangeto = ($startrange + $perpage);
				
	if ($totalrows > 0)			
		echo"	<td align='left' style='width:33%; padding-left:10px;border-top:1px solid #000000;'><span class='toolbar_style'>View ".$rangefrom." - ".$rangeto." of ".$totalrows."</span></td>";
	else
		echo"	<td align='left' style='width:33%; padding-left:10px;border-top:1px solid #000000;'><span class='toolbar_style'>No records to view</span></td>";
	
	echo"		<td align='center' style='width:15%;border-top:1px solid #000000;'><span class='toolbar_style'>Per Page </span><span class='toolbar_style'><select name='perpage' id='perpage' value='$perpage' onchange=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'CP','".$orderby."','".$morelines."',0,this.value,'".$totalrows."','".$direction."');\">";
				  
				if ($perpage==10)		 $s10 = 'selected';
				else if ($perpage==20)	 $s20 = 'selected';
				else if ($perpage==30)	 $s30 = 'selected';
				else if ($perpage==40)	 $s40 = 'selected';
				else if ($perpage==50)	 $s50 = 'selected';
				else if ($perpage==100)	 $s100 = 'selected';
				else if ($perpage==1000) $s1000 = 'selected';

								
				echo'<option '.$s10.' value="10">10</option>
					 <option '.$s20.' value="20">20</option>
					 <option '.$s30.' value="30">30</option>
					 <option '.$s40.' value="40">40</option>
					 <option '.$s50.' value="50">50</option>
					 <option '.$s100.' value="100">100</option>
					 <option '.$s1000.' value="1000">1000</option>
					 </select></span>
				</td>';
				
	echo"		<td  style='width:10%;border-right:1px solid #000000;text-align:right;border-top:1px solid #000000;'>";
				
				if($startrange > 0)
					echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='pointer'\"    onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'F','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";

				$diff= $startrange - $perpage;
					
				if (($diff) >= 0 ){
					$prvstartrange = $startrange - $perpage;
					echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='pointer'\"  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'P','".$orderby."','".$morelines."','".$prvstartrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";
				}
				else
					echo "&nbsp;";
			
	echo"		</td><td  align='left' style='width:10%;border-top:1px solid #000000;'>&nbsp;";
			
			
				if ($startrange <= $totalrows){
					$startrange1 = $startrange + $perpage;
					if  ($startrange1 < $totalrows){ 
						echo"<img src='img/arrowright.png' title='Next' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'N','".$orderby."','".$morelines."','".$startrange1."','".$perpage."','".$totalrows."','".$direction."');return false;\" onmouseover=\"style.cursor='pointer'\" >";
						echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='pointer'\" onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'L','".$orderby."','".$morelines."','".$prvstartrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";	
					}
				} else
					echo "&nbsp;";
			
				
				$currentpage = ($startrange / $perpage) + 1;
				$totalpages  = ceil($totalrows / $perpage);
				
	echo"		</td><td style='text-align:center; width:14%;border-top:1px solid #000000;'>&nbsp;<span class='toolbar_style'>Page $currentpage / $totalpages</span></td>";
	

	
	echo"		<td style='text-align:center; width:6%;border-top:1px solid #000000;'><img src='img/Refresh-icon.png'  onMouseOver=\"style.cursor='pointer'\"  width='20' height='20' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'R','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\" ></td>";
	echo"		<td align='right' style='width:12%; padding-right:10px;border-top:1px solid #000000;'>&nbsp;</td>";
	
	echo"	</tr>";
	echo"	</table></div>";

}

function showSupplierBidGrid($auction_id, $connection){
	$querysetup="select DecimalBase from setup";
	$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultsetup)){
	  extract($rows);
	}
	
	echo '<table border="0" cellspacing="2" cellspadding="0" width="680px" style="table-layout:fixed">
		  <col width=50>
		  <col width=140>
		  <col width=140>	
		  <col width=90><col width=120><col width=50><col width=90>';
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='center' ><span class='tstyle1'>REF</span></td><td align='center' ><span class='tstyle1'>Start date / Time</span></td>
			<td align='center' ><span class='tstyle1'>End date / Time</span></td><td align='center' ><span class='tstyle1'>Status</span></td>
			<td align='center' ><span class='tstyle1'>Item</span></td><td align='center' ><span class='tstyle1'>Rank</span></td><td align='left' ><span class='tstyle1'>Price</span></td>
		 </tr>";

	$query = "select  auctiondt_id, item_auction.auction_id, auction_reference, date_format(auction_startDate,'%d-%m-%Y %h:%i %p'),date_format(auction_endDate,'%d-%m-%Y %h:%i %p'), 
				if( (TIME_TO_SEC(TIMEDIFF(auction_endDate, now()))/60) > 0 and (TIME_TO_SEC(TIMEDIFF( now(), auction_startDate))/60) > 0, 'Active', 'Inactive'),
				date_format(auction_endDate,'%b %d, %Y %H:%i:%s'), item_description
				from item_auctiondt
				inner join item_auction on item_auction.auction_id = item_auctiondt.auction_id
				inner join items on items.item_code = item_auction.item_code
				where supplier_code=  '".$_SESSION['useraccount']."' and ifnull(auction_checked,0) = 1
				order by date_format(auction_startDate,'%d-%m-%Y %h:%i %p'); ";//echo $query;
	$res = mysql_query($query,$connection);
	while($row = mysql_fetch_row($res))	 {
		$resultvars = mysql_query("set @prev:=null ", $connection);		  
		$resultvars = mysql_query("set @x :=0 ", $connection);	
		//if(@prev = bidprice,@x,@x:=@x+1)	
		$queryR = "select bidprice, rank, supplier_code from (
				select @prev, bidprice, @x:=@x+1 as rank, supplier_code,  @prev:= bidprice, bid_dateTime from (
				select min(bid_price) as bidprice, supplier_code, bid_dateTime from (
					select bid_price, bid_dateTime,supplier_code
					from supplierBid
					where auction_id=$row[1]
					order by bid_price, bid_dateTime asc
					) as QQQ
				group by supplier_code ) as QQ
				order by bidprice, bid_dateTime asc
				) as Q where supplier_code = '".$_SESSION['useraccount']."' ;";//echo $query;	 
		$resR = mysql_query($queryR,$connection);
		$rw = mysql_fetch_row($resR);
		$bidPrice = $rw[0];	  
		$rank = $rw[1];	
		
		if($auction_id == $row[1]) $bgcolor = '#7fffd4';
		else  $bgcolor = 'rgba(211, 211, 211, 0.36)';
		echo "<tr height='18px' style='background-color: $bgcolor;' ";
		if($row[5] == 'Active')
		echo " onclick=\"getAuctionInfo('$row[1]'); setBidTimer('$row[6]','$row[1]'); getCurRank('$row[1]');\" ";
		echo " onmouseover=\"style.cursor='pointer'\">
			<td align='center' ><span class='tstyle11'>$row[2] </span></td><td align='center' ><span class='tstyle11'>$row[3]</span></td>
			<td align='center' ><span class='tstyle11'>$row[4]</span></td><td align='center' ><span class='tstyle11'>$row[5]</span></td>
			<td align='center' ><span class='tstyle11'>$row[7]</span></td><td align='center' ><span class='tstyle11'>".($row[5] != 'Active'?$rank:'')."</span></td>
			<td align='left' ><span class='tstyle11'>".($row[5] != 'Active'?number_format($bidPrice,$DecimalBase):'')."</span></td>
		 </tr>";
	}
	echo "</table>";
}

function showSupplierHistory($auction_id, $connection){
	echo '<table border="0" cellspacing="1" cellspadding="0" width="360px" style="table-layout:fixed">
		  <col width=140><col width=50><col width=90><col width=80>';
	echo "<tr height='20px' style='background-color: lightgrey;'><td align='center'><span class='style1'>Bid Date / Time</span></td><td align='center'><span class='style1'>Rank</span></td>
			<td align='center'><span class='style1'>Price</span></td><td align='center'><span class='style1'>Qty</span></td></tr>";	  
	$query = "select date_format(systemdate, '%d-%m-%Y'), bid_rank, bid_price, bid_qty, date_format(systemdate, '%H:%i')
			from bidhistory where auction_id=$auction_id and supplier_code='".$_SESSION['useraccount']."'
			order by systemdate, bid_rank;";	
	$res = mysql_query($query,$connection);
	while($rw = mysql_fetch_array($res)){
		if($auction_id == $row[1]) $bgcolor = '#7fffd4';
		else  $bgcolor = 'rgba(211, 211, 211, 0.36)';
		echo "<tr height='18px' style='background-color: $bgcolor;'>
			<td align='center' ><span class='tstyle11'>$rw[0] / $rw[4] </span></td><td align='center' ><span class='tstyle11'>$rw[1]</span></td>
			<td align='center' ><span class='tstyle11'>$rw[2]</span></td><td align='center' ><span class='tstyle11'>$rw[3]</span></td>
		 </tr>";
	}
	echo "</table>";	  
}
function showAuctionInfoGrid($auction_id, $connection){
	
	$query1 = "select  auction_reference, item_auction.item_code as itemCode,date_format(auction_startDate,'%d-%m-%Y %h:%i %p'), date_format(auction_endDate,'%d-%m-%Y %h:%i %p'),
			round(TIME_TO_SEC(TIMEDIFF(auction_endDate, now()))/60) as leftMin, auction_qty, item_description as itemName,  items.Item_UnitSalesDesc
			from item_auction
			left join items on items.item_code = item_auction.item_code
			where auction_id= $auction_id ;";
	$res1 = mysql_query($query1,$connection);
	$rw1 = mysql_fetch_row($res1);
	$auction_reference = $rw1[0];
	$itemCode = $rw1[1];
	$auction_startDate = $rw1[2];
	$auction_endDate = $rw1[3];
	$leftMin = $rw1[4];
	$auction_qty = $rw1[5];
	$itemName = $rw1[6];
	$itemunit = $rw1[7];
	
	$querymax = "select max(bid_price) from supplierBid where auction_id=$auction_id;";
	$resmax = mysql_query($querymax,$connection);
	$rwmax = mysql_fetch_row($resmax);
	$maxbid = $rwmax[0];
	
	$querymin = "select min(bid_price) from supplierBid where auction_id=$auction_id;";
	$resmin = mysql_query($querymin,$connection);
	$rwmin = mysql_fetch_row($resmin);
	$minbid = $rwmin[0];
	
	$queryf = "select bid_price from supplierBid where auction_id=$auction_id order by bid_id limit 1;";
	$res = mysql_query($queryf,$connection);
	$rw = mysql_fetch_row($res);
	$firstbid = $rw[0];
	
	echo '<table border="0" cellspacing="1" cellspadding="0" style="table-layout:fixed">
		  <col width=200>
		  <col width=250>';
	echo "<tr height='30px' style='background-color: rgba(1, 106, 132, 0.5);' >
          <td valign='center' align='left' colspan='2' style='padding-left: 10px;' ><span class='style1'>Auction Info</td></tr>";	 

	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Auction REF</span></td><td align='center' ><span class='tstyle11'>$auction_reference</span></td>
		 </tr>";		
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Item Code</span></td><td align='center' ><span class='tstyle11'>$itemCode</span></td>
		 </tr>";
	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Item Description</span></td><td align='center' ><span class='tstyle11'>$itemName</span></td>
		 </tr>";
	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Unit of Measurement</span></td><td align='center' ><span class='tstyle11'>$itemunit</span></td>
		 </tr>";
		 
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Quantity to be Supplied</span></td><td align='center' ><span class='tstyle11'>$auction_qty
			<input type='hidden' name='auction_qty' id='auction_qty' value='$auction_qty'></span></td>
		 </tr>";	
	
	/*
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Minimum Bid Price</span></td><td align='center' ><span class='tstyle11'>$minbid</span></td>
		 </tr>";		
	*/
	/*	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Maximum Bid Price</span></td><td align='center' ><span class='tstyle11'>$maxbid</span></td>
		 </tr>";				
	*/
	/*
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Opening Bid Price</span></td><td align='center' ><span class='tstyle11'>$firstbid</span></td>
		 </tr>";	
	*/	 
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Time Left For Auction To Conclude</span></td><td align='center' ><span class='tstyle11'>$leftMin Mins</span></td>
		 </tr>";	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Start Date</span></td><td align='center' ><span class='tstyle11'>$auction_startDate</span></td>
		 </tr>";			 
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>End Date</span></td><td align='center' ><span class='tstyle11'>$auction_endDate</span></td>
		 </tr>";			 	 
	echo "</table>";	  
	
}

function showSupplierAuctionBidGrid($auction_id, $base1, $connection){
	echo '<table border="0" cellspacing="1" cellspadding="0" style="table-layout:fixed">
		  <col width=200><col width=200><col width=150>';
		
	echo "<tr height='30px' style='background-color: rgba(1, 106, 132, 0.5);' >
          <td valign='center' align='left' colspan='2' style='padding-left: 10px;'  ><span class='style1'>Place Your Bid</span></td>
		  <td valign='center' align='center' ><span class='style1'>Remaining Time</span><br><span style='color:red;font-weight: bold;font-size: 12px;' id='bidTimer'></span></td>
		  </tr>";
	$query = "select Ledger_name from suppliers where ledger_number= '".$_SESSION['useraccount']."';";	 
	$res = mysql_query($query,$connection);
	$rw = mysql_fetch_row($res);
	$aliasname = $rw[0];
	
	$queryitem = "select auction_qty, Item_UnitSalesDesc from item_auction
				inner join items on items.item_code = item_auction.item_code
				where auction_id=$auction_id;";
	$resitem = mysql_query($queryitem, $connection);
	$rwitem = mysql_fetch_row($resitem);	
	$qtySupp = $rwitem[0];
	$itemUnit = $rwitem[1];
	
	$querymin = "select ifnull(min(bid_price),0) from supplierBid where supplier_code='".$_SESSION['useraccount']."' and auction_id=$auction_id;";
	$resmin = mysql_query($querymin, $connection);
	$rwmin = mysql_fetch_row($resmin);
	$minPrice = $rwmin[0];
	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Alias Name</span></td>
			<td align='center' ><span class='tstyle11'><input type='text' name='aliasname' id='aliasname' value='$aliasname' readonly style='width:180px;border: none'></span></td>
			<td align='center'><button type='button' id='btnRefresh' name='btnRefresh' onclick=\"refreshBid('$auction_id');return false;\" title='Refresh Bid'><span><span>Refresh</span></span></button></td>
		 </tr>";		  
	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Quantity</span></td>
			<td align='center' ><span class='tstyle11'><input type='text' name='qtySupp' id='qtySupp' value='$qtySupp' style='width:180px;border: none'></span></td>
			<td align='center'> <span class='tstyle11'><b>$itemUnit</b></span></td>
		 </tr>";
		 
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Price Per Unit (EX VAT)</span></td>
			<td align='center' ><span class='tstyle11'><input type='text' name='pidprice' id='pidprice' value='$pidprice' style='width:150px;border: none'> $base1</span>
			<input type='hidden' name='minPrice' id='minPrice' value='$minPrice'></td>
			<td align='center'><button type='button' id='btnBid' name='btnBid' onclick=\"placeBid('$auction_id');return false;\" title='Place Bid'><span><span>Place Bid</span></span></button></td>
		 </tr>";		
	
	/* echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Bid Decrement</span></td>
			<td align='center' ><span class='tstyle11'></span></td>
			<td></td>
		 </tr>"; */	
	
	/* echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Auto Bid up To</span></td>
			<td align='center' ><span class='tstyle11'><input type='text' name='autopidprice' id='autopidprice' value='$autopidprice' style='width:150px;border: none'> $base1</span></td>
			<td align='center'><button type='button' id='btnAutoBid' name='btnAutoBid' onclick=\"autoBid('$auction_id');return false;\" title='Auto Bid'><span><span>Auto Bid</span></span></button></td>
		 </tr>";	 */	
	echo "<tr height='30px'><td  align='left' colspan='3' ></td></tr>";
	
	echo "<tr height='30px' style='background-color: rgba(1, 106, 132, 0.5);' >
          <td valign='center' align='center' colspan='3' style='padding-left: 10px;'><span class='style1a'>Your Current Rank</td></tr>";
	$resultvars = mysql_query("set @prev:=null ", $connection);		  
	$resultvars = mysql_query("set @x :=0 ", $connection);	
//if(@prev = bidprice,@x,@x:=@x+1)	
	$query = "select bidprice, rank, supplier_code from (
			select @prev, bidprice, @x:=@x+1 as rank, supplier_code,  @prev:= bidprice, bid_dateTime from (
			select min(bid_price) as bidprice, supplier_code, bid_dateTime from (
				select bid_price, bid_dateTime,supplier_code
				from supplierBid
				where auction_id=$auction_id
				order by bid_price, bid_dateTime asc
				) as QQQ
			group by supplier_code ) as QQ
			order by bidprice, bid_dateTime asc
			) as Q where supplier_code = '".$_SESSION['useraccount']."' #having min(bidprice);";//echo $query;	 
	$res = mysql_query($query,$connection);
	$rw = mysql_fetch_row($res);
	$bidPrice = $rw[0];	  
	$rank = $rw[1];	
	$query = "select qty_supplied from supplierBid where auction_id=$auction_id and supplier_code = '".$_SESSION['useraccount']."' having min(bid_price);";
	$res = mysql_query($query,$connection);
	$rw = mysql_fetch_row($res);
	$qty = $rw[0];	  
	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Current Rank:</span><span id='curRank' class='style1a'> $rank</span></td>
			<td align='left' colspan='2' style='padding-left: 10px;'><span class='tstyle1'>Bid Price:</span><span class='tstyle11'> $bidPrice</span></td>
			<td></td>
		 </tr>";
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Total Value</span></td>
			<td  colspan='3' style='padding-left: 10px;'><span class='tstyle11'> ".($qty*$bidPrice)." </span></td>
		 </tr>";	 	 
	echo "</table>";
	
		  
}


function showAllAuctionsGrid($auction_id, $connection){
	if ($startrange=='')
	$startrange = 0;
	if ($perpage=='')
	$perpage = 20;

	echo '<table border="0" cellspacing="2" cellspadding="0" style="table-layout:fixed">
		  <col width=120>
		  <col width=350>
		  <col width=215>
		  <col width=215>	
		  <col width=180>';
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='center' ><span class='tstyle1'>REF</span></td><td align='center' ><span class='tstyle1'>Item</span></td>
			<td align='center' ><span class='tstyle1'>Start date / Time</span></td><td align='center' ><span class='tstyle1'>End date / Time</span></td>
			<td align='center' ><span class='tstyle1'>Operation</span></td>
		 </tr>";

	$query = "select  item_auction.auction_id, auction_reference, concat(item_auction.item_code, ' - ', item_description) as itemcode, date_format(auction_startDate,'%d-%m-%Y %h:%i %p'),
			date_format(auction_endDate,'%d-%m-%Y %h:%i %p'), if( (TIME_TO_SEC(TIMEDIFF(auction_endDate, now()))/60) > 0, '1', '0')
			from item_auction
			left join items on items.item_code = item_auction.item_code
			order by auction_reference  "; 
	$res = mysql_query($query,$connection);
	$totalrows = mysql_num_rows($resultsearch);
	while($row = mysql_fetch_row($res))	 {
		if($auction_id == $row[0]) $bgcolor = '#7fffd4';
		else  $bgcolor = 'rgba(211, 211, 211, 0.36)';
		echo "<tr height='18px' style='background-color: $bgcolor;' ";
		echo " onclick=\"getAllAuction('$row[0]')\" ";
		echo " onmouseover=\"style.cursor='pointer'\">
			<td align='center' ><span class='tstyle11'>$row[1] </span></td><td align='center' ><span class='tstyle11'>$row[2]</span></td>
			<td align='center' ><span class='tstyle11'>$row[3]</span></td><td align='center' ><span class='tstyle11'>$row[4]</span></td>
			<td align='center' ><span class='tstyle11'>";
		if($row[5] == 1) echo "<img src='./img/view.png' height='20px' width='20px' />";
		else echo "<img src='./img/auction-icon.png' height='20px' width='20px' />";
		echo "</span></td>
		 </tr>";
	}
	echo "</table>";
}


function showHistoryBidGrid($auction_id, $orderBid, $connection){
	$querysetup="select DecimalBase from setup";
	$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultsetup)){
	  extract($rows);
	}

	if($orderBid == '') $orderBid = '1';
	$query1 = "select item_auctiondt.supplier_code, suppliers.ledger_name, bid_price, qty_supplied, date_format(bid_dateTime,'%d-%m-%Y'), date_format(bid_dateTime,'%H:%i:%s')
			from item_auctiondt
			inner join item_auction on item_auction.auction_id = item_auctiondt.auction_id
			left join suppliers on suppliers.ledger_number = item_auctiondt.supplier_code
			inner join supplierBid on supplierBid.auction_id = item_auction.auction_id
			and supplierBid.supplier_code = item_auctiondt.supplier_code
			where item_auction.auction_id=$auction_id ";
	if($orderBid =='1')	 $query1 .= " order by bid_price, bid_dateTime asc ";	
	else if($orderBid =='2')	 $query1 .= " order by item_auctiondt.supplier_code asc ";	
	else if($orderBid =='3')	 $query1 .= " order by bid_dateTime desc ";//	echo $query1;
	$res1 = mysql_query($query1,$connection);
	
		
	echo '<table border="0" cellspacing="1" cellspadding="0" style="table-layout:fixed" width=630px>
		  <col width=20><col width=140><col width=135><col width=115><col width=60><col width=80><col width=80>'; //<col width=75>
	
	echo "<tr height='30px'><td colspan='5'><span class='style1'>Order by</span> <select name='orderBid' id='orderBid' onchange=\"getAllAuction('$auction_id');\" style='border-radius: 4px;font-size: 13px;font-family: monospace;'>";
	echo "<option value='1' ".($orderBid == '1'?'selected':'').">Price</option>";
	echo "<option value='2' ".($orderBid == '2'?'selected':'').">Customer</option>";
	echo "<option value='3' ".($orderBid == '3'?'selected':'').">Date</option>";
	echo "</select></td><td colspan='2'> ";
	echo" <button type='button' id='btnPrint' name='btnPrint' onclick=\"PrintAuction('$auction_id');return false;\" title='Export To Excel'><span><span>Export To Excel</span></span></button>";				
	echo "</td></tr>";
	
	echo "<tr height='30px' style='background-color: rgba(1, 106, 132, 0.5);' >
          <td valign='center' align='left' colspan='7'  style='padding-left: 10px;'><span class='style1'>BID HISTORY</td></tr>";	 

	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' ><span class='tstyle1'>No</span></td><td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Alias</span></td>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Company Name</span></td><td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Bid Price</span></td>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Qty.</span></td><td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Date</span></td>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Time</span></td>
		 </tr>";
	//<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Status</span></td>	 
	echo "</table>";	 
	$i=1;
	
	echo "<div style='height:250px; width:638px; overflow-x:hidden; overflow-y:scroll;'>";
	echo '<table border="0" cellspacing="1" cellspadding="0" style="table-layout:fixed" width=630px>
		  <col width=20><col width=140><col width=135><col width=115><col width=60><col width=80><col width=90>';	
	while($rw1 = mysql_fetch_row($res1)){		
	echo "<tr height='25px' style='background-color: rgba(211, 211, 211, 0.36);'>
			<td align='center' ><span class='tstyle11'>$i</span></td><td align='left' ><span class='tstyle11'>$rw1[0]</span></td>
			<td align='left' ><span class='tstyle11'>$rw1[1]</span></td><td align='center' ><span class='tstyle11'>".number_format($rw1[2],$DecimalBase)."</span></td>
			<td align='center' ><span class='tstyle11'>$rw1[3]</span></td><td align='center' ><span class='tstyle11'>$rw1[4]</span></td>
			<td align='left' ><span class='tstyle11'>$rw1[5]</span></td>
		 </tr>";
		 //<td align='center' ><span class='tstyle11'></span></td>
	$i++;	 
	}
				 	 
	echo "</table></div>";	  
	
}


function showAuctionDetailGrid($auction_id, $connection){
	
	$query1 = "select  auction_reference, concat(item_auction.item_code,' - ', item_description) as itemName
			from item_auction
			left join items on items.item_code = item_auction.item_code
			where auction_id= $auction_id ;"; 
	$res1 = mysql_query($query1,$connection);
	$rw1 = mysql_fetch_row($res1);
	$auction_reference = $rw1[0];
	$itemName = $rw1[1];
	
			
	$querymin = "select bid_price, supplier_code, ledger_name from (
				select bid_price, supplier_code from supplierBid where auction_id=$auction_id
				group by bid_id
				order by bid_price, bid_dateTime asc ) as Q
				left join suppliers on suppliers.ledger_number = Q.supplier_code
				having min(bid_price);"; 
	$resmin = mysql_query($querymin,$connection);
	$rwmin = mysql_fetch_row($resmin);
	$minbid = $rwmin[0];
	$minSupplier = $rwmin[1]." - ".$rwmin[2];
	
	$queryCount = "select distinct supplier_code from supplierBid where auction_id=$auction_id;";
	$resCount = mysql_query($queryCount,$connection);
	$numUsers = mysql_num_rows($resCount);
			
	echo '<table border="0" cellspacing="1" cellspadding="0" style="table-layout:fixed">
		  <col width=200>
		  <col width=200>';
	echo "<tr height='30px' style='background-color: rgba(1, 106, 132, 0.5);' >
          <td valign='center' align='left' colspan='2'  style='padding-left: 10px;' ><span class='style1'>AUCTION DETAILS</td></tr>";	 

	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>Auction REF</span></td><td align='center' ><span class='tstyle11'>$auction_reference</span></td>
		 </tr>";		
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Item Name</span></td><td align='center' ><span class='tstyle11'>$itemName</span></td>
		 </tr>";
	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Minimum Bid Price</span></td><td align='center' ><span class='tstyle11'>$minbid</span></td>
		 </tr>";		
			
	/*echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>No. Of Suppliers Logged In</span></td><td align='center' ><span class='tstyle11'>$maxbid</span></td>
		 </tr>";		*/		
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>No. Of Suppliers Who placed their Bid</span></td><td align='center' ><span class='tstyle11'>$numUsers</span></td>
		 </tr>";	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;' ><span class='tstyle1'>R1 Supplier</span></td><td align='center' ><span class='tstyle11'>$minSupplier</span></td>
		 </tr>";		 
	/*echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>No. Of Extensions Happened</span></td><td align='center' ><span class='tstyle11'></span></td>
		 </tr>";	
	echo "<tr height='25px' style='background-color: lightgrey;'>
			<td align='left' style='padding-left: 10px;'><span class='tstyle1'>Reserve Price</span></td><td align='center' ><span class='tstyle11'>$auction_startDate</span></td>
		 </tr>";			 
			 	 */
	echo "</table>";	  
	
}

?>