<?php

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

?>
<?php
	$UserID=$_SESSION['user_id'];
	$getEmpQuery=$conn->query("SELECT branch_id FROM tbl_user_branch WHERE user_id='$UserID' ");
    if($emp=$getEmpQuery->fetch_array()){
        $branch_id = $emp['0'];
	}

?>
<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 quotation_items LEFT JOIN mobileproducts ON mobileproducts.prid=quotation_items.qi_pro_id
		WHERE quotation_items.qi_no=0 AND quotation_items.qi_user='$user_id'  ORDER BY quotation_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="deleteQuoteItem(<?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>

<input type="hidden" id="branch_id" value="<?php echo $branch_id; ?>">

<h3>Customer Details</h3>
<div class="row">
<div class="col-md-6">
<select class="form-select" id="cusname">
<option selected>Choose Cutomers</option>
<?php

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

										$stmt->execute();

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

										{

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

										{

										extract($row);

?>
<option value="<?php echo $row['coid']; ?>"><?php echo $row['cname']; ?> | <?php echo $row['cphone']; ?></option>
<?php 
} 
}
?>
</select>
</div>
<div class="col-md-2">
<a href="addcustomer.php" class="btn btn-primary btn-block" style="width:100%;color:#ffffff;" target="_blank">Add Customers</a>
</div>
<div class="col-md-4">
<button class="btn btn-success btn-block" onclick="saveQuotation();" style="width:100%;color:#ffffff;">Create Quotation</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 saveQuotation() {
	var total_amount = document.getElementById("total_amount_view").innerHTML;
	var cusname = document.getElementById("cusname").value;
	var branch_id = document.getElementById("branch_id").value;
	if(cusname){

		$.ajax({
		url : "load-quote-save.php",
		type : "POST",
		data : "total_amount="+total_amount+"& cusname="+cusname+"& branch_id="+branch_id+"&",
		success: function(data){
			if(data>0){
				alertify.alert("Quotation Created..<br>Quotation No : "+data);
				window.open('quotations-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>



