<?php

session_start();
require_once 'dbconfig.php'; 
require_once 'db_connection.php'; 

?>
<table class="table">
	<thead>
		<tr>
			<th scope="col">#</th>
			<th scope="col">Image</th>
			<th scope="col">Name</th>
			<th scope="col" style="text-align: right;">Quantity</th><th scope="col" style="text-align: right;">Price</th>
			<th scope="col" style="text-align: right;">Dis%</th>
			<th scope="col" style="text-align: right;">Amount</th>
			<th scope="col" style="text-align: right;">X</th>
		</tr>
	</thead>
	<tbody style="padding: 0px;">
		<?php
		
		$user_id = $_SESSION['user_id'];
		$total_amount = 0; 

		$sql = "SELECT * FROM mobilepurchase_items LEFT JOIN mobileproducts ON mobileproducts.prid=mobilepurchase_items.qi_pro_id
		WHERE mobilepurchase_items.qi_no=0 AND mobilepurchase_items.qi_user='$user_id'  ORDER BY mobilepurchase_items.qi_id  ASC";

		$result = $conn->query($sql);
		$rowCount = 0;
		if ($result->num_rows > 0) {
			while($row = $result->fetch_assoc()) {
				$rowCount = $rowCount+1;
				$qi_id = $row["qi_id"];
				$qi_pro_id = $row["qi_pro_id"];
				$proname = $row["proname"];
				$cover = $row["cover"];
				$qi_pro_discount = $row["qi_pro_discount"];
				$qi_pro_price = $row["qi_pro_price"];
				$qi_pro_qty = $row["qi_pro_qty"];

				$discounted_price = ($qi_pro_price/100) * $qi_pro_discount;
				$product_price = ($qi_pro_price - $discounted_price)*$qi_pro_qty;

				$total_amount = $total_amount + $product_price;
				
				?>

				<tr>
					<th scope="row"><?php echo $rowCount; ?></th>
					<td><img width="30px" src="images/products/cover/<?php echo $cover; ?>" alt="img"></td>
					<td><?php echo $proname; ?></td>
					<td style="text-align: right;"><?php echo $qi_pro_qty; ?></td>
					<td style="text-align: right;"><?php echo number_format((float)$qi_pro_price,2); ?></td>
					<td style="text-align: right;"><?php echo $qi_pro_discount; ?>%</td>
					<td style="text-align: right;"><?php echo number_format((float)$product_price,2); ?></td>
					<td style="text-align: right;">
					<a class="confirm-text" href="javascript:void(0);"><img src="assets/img/icons/delete-2.svg" alt="img" onclick="deletePurchaseItem(<?php echo $qi_id; ?>);"></a>
					</td>
				</tr>
		<?php
		}
		}else{
		echo "Not found items..";
		}
		?>
	</tbody>
	<tfoot style="background: #ebebeb;">
		<tr>
			<td colspan="6"> Total </td>
			<td style="text-align: right;"><?php echo number_format((float)$total_amount,2); ?></td>
			<td colspan=""> </td>
		</tr>
	</tfoot>
</table>
<div id="total_amount_view" style="display: none;"><?php echo $total_amount; ?></div>
<hr><br>
<h3>Supplier/Vender Details</h3>
<div class="row">
<div class="col-md-6">
<select class="form-select" id="cusname">
<option selected>Choose Supplier/Vender</option>
<?php

										$stmt = $DB_con->prepare('SELECT * FROM mobilesuppliers where status="1" ORDER BY spid');

										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);

?>
<option value="<?php echo $row['spid']; ?>"><?php echo $row['spname']; ?> | <?php echo $row['vtype']; ?></option>
<?php 
} 
}
?>
</select>
</div>
<div class="col-md-2">
<a href="addsupplier.php" class="btn btn-primary btn-block" style="width:100%;color:#ffffff;" target="_blank">Add Supplier/Vender</a>
</div>
<div class="col-md-4">
<button class="btn btn-info btn-block" onclick="savePurchase();" style="width:100%;color:#ffffff;">Create Purchase</button>
</div>

	</div>


<style type="text/css">
	.paymentmethod2{
		width: 32%;
		border: 1px solid #edf;
		border-radius: 5px;
		padding: 20px;
		margin: 2px;
	}

	.paymentmethod2:hover{
   background-color:#e3e3e3;
}
</style>

<script type="text/javascript">
	function savePurchase() {
	var total_amount = document.getElementById("total_amount_view").innerHTML;
	var cusname = document.getElementById("cusname").value;
	if(cusname && contact && address){

		$.ajax({
		url : "load-purchase-save.php",
		type : "POST",
		data : "total_amount="+total_amount+"& cusname="+cusname+"&",
		success: function(data){
			if(data>0){
				alertify.alert("Purchase Created..<br>Purchase No : "+data);
				window.open('purchase-view.php?id='+data, '_blank');
				loadQuotationItems();
			}else{
				alertify.error(data);
			}
		},
		error: function(jXHR, textStatus, errorThrown){
			alert(jXHR.responseText);
		}
	})


	}else{
		alertify.error("Enter Customer Details...");
	}
	}
</script>



