<?php
	$HOST = '184.168.194.75';
	$UID = 'Mars';
	$PWD = 'ExineAdmin1#27';
	$DB = 'StormyTest';
	
	$connectionInfo = array( "Database"=>$DB, "UID"=>$UID, "PWD"=>$PWD);
	$conn = sqlsrv_connect( $HOST, $connectionInfo);
	
	if ($conn)
	{
		$selectPoints = "SELECT * FROM tCash WHERE userNo='".$_SESSION['userID']."' AND cashtype='0'";
		$doSelectQuery = sqlsrv_query($conn, $selectPoints, array(), array( "Scrollable" => 'static' ));
		
		$count = sqlsrv_num_rows($doSelectQuery);
		
		if ($count == 0)
		{
			$insertPoints = "INSERT INTO tCash VALUES('".$_SESSION['userID']."', '0', '0', '0')";
			$doInsertQuery = sqlsrv_query($conn, $insertPoints);
			$userCoins = 0;
		}
		else if ($count == 1)
		{
			$coinValue = sqlsrv_fetch_array($doSelectQuery);
			$userCoins = $coinValue['cash'];
		}
		else {
			echo 'There has been an error loading user point information.';	
		}
	}
	else {
		
	}
?>
<h1>Cash Shop<small style="float: right;">You have <?php echo $userCoins; ?> Coins</small></h1>
<p>Here you can buy some special items for Anonymous Coins. If you run out of coins you can buy more using form below.</p>
<p>
  <input type="hidden" name="cmd" value="_s-xclick">
  <form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="none">
<input type="hidden" name="custom" value="<?php echo $_SESSION['userID']; ?>">
      <input type="hidden" name="hosted_button_id" value="S6VMZ6MTGWDCC">
      <input type="hidden" name="currency_code" value="USD">
      <input type="hidden" name="on0" value="Amount">
      <table>
          <tr>
              <td><select name="os0"><option value="5000 Gold">5000 Gold $5.00</option><option value="10,000 Gold">10,000 Gold $10.00</option><option value="25,000 Gold">25,000 Gold $25.00</option><option value="50,000 Gold">50,000 Gold $50.00</option><option value="100,000 Gold">100,000 Gold $100.00</option><option value="200,000 Gold">200,000 Gold $200.00</option></select></td>
              <td><input type="image" style="none" src="https://www.paypal.com/en_US/i/btn/btn_buynow_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"></td>
          </tr>
      </table>
</form>
</p>
<?php
	$catID = $_GET['cat'];
	$itemViewID = intval($_GET['item']);
	if ($catID == '') $catID = NULL;
	
	// Here we fetch categories
	$catQuery = "SELECT * FROM tItemCategory WHERE parent=2";
	$doCatQuery = sqlsrv_query($conn, $catQuery, array(), array( "Scrollable" => 'static' ));
	
	// If neither category nor item are set
	if ( $catID == NULL && $itemViewID == 0 )
	{
		
		while ($category = sqlsrv_fetch_array($doCatQuery))
		{
			// It's not even getting to this line
			echo '<h2>'.$category['name'].'<small style="float: right;"><a style="font-size: 10px; color: #4c9ad3;" href="index.php?cat='.$category['category'].'">See all</a></small></h2>';
			
			// Select top 4 items from this category
			$selectItems = sqlsrv_query($conn, "SELECT TOP 3 * FROM tItem WHERE category = ".$category['category']." AND isSell = 'True' ORDER BY registerDate DESC", array(), array( "Scrollable" => 'static' ));
			echo '<table width="100%" class="cashShop"><tr>';
			while ( $item = sqlsrv_fetch_array( $selectItems ) )
			{
				if ( $item['imageName'] == NULL ) $item['imageName'] = "NONE";
				echo '<td width="100px">
						<h1>['.$item['unit'].'x] '.$item['name'].'</h1>
						<a href="index.php?item='.$item['goodsNo'].'"><img src="/StormyCP/img/mall/'.$item['imageName'].'.jpg" /></a><br />';
						if($item['price'] == 0) 
						echo 'Free';
						else
						echo $item['price'].' Gold'; //<img src="/StormyCP/img/mall/detailbtn.gif">
						echo '<br /><a href="index.php?item='.$item['goodsNo'].'"><img src="/StormyCP/img/mall/detailbtn.gif"></a></td>';
			}
			echo '<td><a href="index.php?cat='.$category['category'].'"><img src="/StormyCP/img/mall/next.png" /></a></td>';
			echo '</tr></table><br />';
		}
	}
	else if ($itemViewID != 0)
	{
		$selectItemInfo = sqlsrv_query($conn, "SELECT * FROM tItem WHERE goodsNo = $itemViewID AND isSell = 'True'", array(), array( "Scrollable" => 'static' ));
		
		if (sqlsrv_num_rows($selectItemInfo) == 1 )
		{
			$item = sqlsrv_fetch_array($selectItemInfo);
			$selectCategoryName = sqlsrv_query($conn, "SELECT name FROM tItemCategory WHERE category = (SELECT category FROM.tItem WHERE goodsNo = $itemViewID)");
			$categoryRow = sqlsrv_fetch_array($selectCategoryName);
			if ( $item['imageName'] == NULL ) $item['imageName'] = "NONE";
			echo '<h2>['.$item['unit'].'x] '.$item['name'].'<small style="float: right;"><a style="font-size: 10px; color: #4c9ad3;" href="index.php?cat='.$item['category'].'">'.$categoryRow['name'].'</a></small></h2>';
			echo '<table width="100%"><tr><td width="90px"><img src="/StormyCP/img/mall/'.$item['imageName'].'.jpg" /><br /><center>';
			if($item['price'] == 0) 
			echo 'Free';
			else
			echo $item['price'].' Gold';
			echo '</center></td><td valign="top">'.$item['description'].'</td></tr><tr><td colspan="2"><div style="float: right;">';
			echo '<form method="post">';
			echo '<input class="roundedButton" type="submit" name="Buy" value="Buy Now!" /></form>';
			echo '</div></td></tr></table>';
			
			if ( $_POST['Buy'] )
			{
				// First check if user has enought points
				$selectPoints = sqlsrv_query($conn, "SELECT * FROM tCash WHERE userNo = '".$_SESSION['userID']."' AND cashtype = '0';", array(), array( "Scrollable" => 'static' ));			
				
				$userPoints = sqlsrv_fetch_array($selectPoints);
				
				if ($userPoints['cash'] >= $item['price'] )
				{
					$newCash = ($userPoints['cash'] - $item['price'] );
					$changePoints = sqlsrv_query($conn, "UPDATE tCash SET cash = ".$newCash." WHERE userNo = ".$_SESSION['userID']." AND cashtype = 0;" );
					
					if ( $changePoints )
					{
						$insertItem = sqlsrv_query($conn, "INSERT INTO tChargeItem(userNo, goodsNo) VALUES(".$_SESSION['userID'].",".$item['goodsNoCheck'].");" );
						
						if ( $insertItem )
						{
							echo '<script type="text/javascript">window.location.href = window.location.href;</script>';
						}
						else
						{
							//showMessage( 'error', 'Internal item purchase error' );
						}
					}
					else
					{
						//showMessage( 'error', 'Internal item error' );
					}
				}
				else
				{
					//showMessage( 'error', 'You do not have enought coins.' );
				}
			}
		}
		else
		{
			//showMessage( 'error', 'Item not found!' );
		}
	}
	else
	{
		$selectCategory = sqlsrv_query($conn, "SELECT name FROM tItemCategory WHERE category = '$catID' AND status = 'True'", array(), array( "Scrollable" => 'static' ));
		if ( sqlsrv_num_rows( $selectCategory ) == 1 )
		{
			// Select sub-categories
			$selectSubCategories = sqlsrv_query($conn, "SELECT * FROM tItemCategory WHERE parent = '$catID' AND status = 'True';", array(), array( "Scrollable" => 'static' ));
			if ( sqlsrv_num_rows( $selectSubCategories ) > 0 )
			{
				while ( $category = sqlsrv_fetch_array( $selectSubCategories ) ) // This is just like it I guess that's why this isnt working either
				{
					echo '<h2>'.$category['name'].'<small style="float: right;"><a style="font-size: 10px; color: #4c9ad3;" href="index.php?cat='.$category['id'].'">See all</a></small></h2>';
					
					// Select top 4 items from this category
					$selectItems = sqlsrv_query($conn, "SELECT TOP 3 * FROM tItem WHERE category = ".$category['category']." AND isSell = 'True' ORDER BY registerDate DESC;", array(), array( "Scrollable" => 'static' ));
					echo '<table width="100%" class="cashShop"><tr>';
					while ( $item = sqlsrv_fetch_array( $selectItems ) )
					{
						if ( $item['imageName'] == NULL ) $item['imageName'] = "NONE";
						echo '<td width="100px"><h1>['.$item['unit'].'x] '.$item['name'].'</h1>
								<img src="/StormyCP/img/mall/'.$item['imageName'].'.jpg" /><br />
								';
								if($item['price'] == 0) 
								echo 'Free';
								else
								echo $item['price'].' Gold';
								echo '<br /><a href="index.php?item='.$item['goodsNo'].'"><img src="/StormyCP/img/mall/detailbtn.gif"></a>
							</td>';
					}
					echo '<td><a href="index.php?cat='.$category['category'].'"><img src="/StormyCP/img/mall/next.png" /></a></td>';
					echo '</tr></table><br />';
				}
			}
			
			// Select Items in Category/Subcategory
			$selectCategoryItems = sqlsrv_query($conn, "SELECT * FROM tItem WHERE category = '$catID' AND isSell = 'True' ORDER BY registerDate DESC;", array(), array( "Scrollable" => 'static' ));
			$catName = sqlsrv_fetch_array($selectCategory);
			echo '<h2>'.$catName['name'].'<small style="float: right;"><a style="font-size: 12px; color: #000000;" href="index.php">Cash Shop Home Page</a></small></h2>';
			
			if ( sqlsrv_num_rows( $selectCategoryItems ) > 0 )
			{
				$column = 1;
				echo '<table width="100%" class="cashShop">';
				while ( $item = sqlsrv_fetch_array( $selectCategoryItems ) )
				{
					if ( $item['imageName'] == NULL ) $item['imageName'] = "NONE";
					if ( $column == 1 ) { echo '<tr>'; }
					$column++;
					echo '<td width="100px"><h1>['.$item['unit'].'x] '.$item['name'].'</h1><a href="index.php?item='.$item['goodsNo'].'"><img src="/StormyCP/img/mall/'.$item['imageName'].'.jpg" /></a><br />';
					if($item['price'] == 0)  //<a href="index.php?module=mall&item='.$item['goodsNo'].'"><img src="/StormyCP/img/mall/'.$item['imageName'].'.jpg" /></a>
					echo 'Free';
					else
					echo $item['price'].' Gold';
					echo '<br /><a href="index.php?item='.$item['goodsNo'].'"><img src="/StormyCP/img/mall/detailbtn.gif"></a></td>';
					if ( $column == 4 ) { echo '</tr>'; $column = 1; }
				}
				echo '</table>';
			}
			else
			{
				//showMessage( 'info', 'There are no items in this category.' );
			}
		}
		else
		{
			//showMessage( 'error', 'Category not found' );
		}
	}
?>
