<?php

	include 'Header.php';
    include 'Navbar.php';
	include 'Sidebar.php';
	include 'includes/admin_API_Config.php';

	$obj = new API_Config;
	$data = array();
	$data['admin_username'] = $_SESSION['admin_username'];
	$response = json_decode($obj->call_API("API_Product_List.php", $data));
	$storeList  = json_decode($obj->call_API("API_Store_List.php", $data));
	//var_dump($response);
	error_reporting(0);

?>




<!-- BEGIN CONTENT -->
	<div class="page-content-wrapper">
		<div class="page-content">
			<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
			<div class="modal fade" id="portlet-config" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
							<h4 class="modal-title">Modal title</h4>
						</div>
						<div class="modal-body">
							 Widget settings form goes here
						</div>
						<div class="modal-footer">
							<button type="button" class="btn blue">Save changes</button>
							<button type="button" class="btn default" data-dismiss="modal">Close</button>
						</div>
					</div>
					<!-- /.modal-content -->
				</div>
				<!-- /.modal-dialog -->
			</div>
			<!-- /.modal -->
			<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
			<!-- BEGIN PAGE HEADER-->
			<!-- BEGIN PAGE BREADCRUMB -->
			<ul class="page-breadcrumb breadcrumb">
				<li>
					<a href="#">Dashboard</a>
					<i class="fa fa-circle"></i>
				</li>
				<li>
					<a href="#">Purchase</a>
					<i class="fa fa-circle"></i>
				</li>
				<li>
					<a href="#">Add Purchase</a>
				</li>
			</ul>
			<!-- END PAGE BREADCRUMB -->
			<!-- END PAGE HEADER-->
			<!-- BEGIN PAGE CONTENT-->


			<!-- BEGIN SAMPLE FORM PORTLET-->
					<div class="portlet box blue">
						<div class="portlet-title">
							<div class="caption">
								<i class="icon-settings"></i>
								<span class="caption-subject bold uppercase">Purchase</span>
							</div>
						</div>
						
						<div class="portlet-body form">
							<form action="#" class="horizontal-form" name="frmPurchase" id="frmPurchase">
												<div class="form-body">
													<div class="row">
														<div class="col-md-3">
															<div class="form-group form-md-line-input form-md-label">
                                                                <input type="text" class="form-control" name="memberID" id="memberID" maxlength="9">
                                                                <label for="MemberID">Member ID:</label>
                                                                <span class="help-block"></span>
										                        </div>
										
														</div>
														<!--/span-->
														<div class="col-md-3">
                                                                <div class="form-group form-md-line-input form-md-label">
                                                                <input type="text" class="form-control" name="memberName" id="memberName">
                                                                <label for="memberName">Member Name:</label>
                                                                <span class="help-block"></span>
										                        </div>
														</div>
														<div class="col-md-3">
															<div class="form-group form-md-line-input form-md-label">
                                                                <input type="text" class="form-control" name="customerID" id="customerID">
                                                                <label for="customerID">Customer ID:</label>
                                                                <span class="help-block"></span>
										                    </div>
														</div>
														<!--/span-->
													
													</div>
													<!--/row-->
													<div class="row">
													   <div class="col-md-4">
															<div class="form-group form-md-line-input form-md-floating-label">
<!--																<label>Inline Radios</label>-->
																<div class="input-group">
																	<div class="icheck-inline">
																			<input type="radio" name="salesType" id="salesType1" value="1" class="icheck"   data-radio="iradio_square-grey"> Shipping Method
																			<input type="radio" name="salesType" id="salesType2" value="2" class="icheck" data-radio="iradio_square-grey"> Store Pickup
																	</div>
																</div>
															</div>
														</div>
													</div>
													<!--/row-->
													<p>&nbsp;</p>
													<div class="row" id="storeRow" style="display:none;">
														<div class="col-md-4">
															<div class="form-group form-md-line-input form-md has-info">
																<select class="form-control input-large" id="store">
																	<?php
																		foreach($storeList->eCommerceStores as $row)
																		{
																			echo "<option value='" . $row->id . "' price='" . $row->price . "'  pv='" . $row->pricevalue . "'>" . ucwords($row->name) . "</option>";
																		}
																	?>
																</select>
																<label for="store">Select Store:</label>
															</div>
														</div>
														<div class="col-md-4">
															<div class="form-group form-md-line-input form-md-label">
																<textarea class="form-control valid" rows="3" name="address" id="address" aria-invalid="false"></textarea>
																<label for="address">Customer Address:</label>
                                                                <span class="help-block"></span>
															</div>															
														</div>
														<div class="col-md-3">
															<div class="form-group form-md-line-input form-md-label">
                                                                <input type="text" class="form-control" name="pincode" id="pincode">
                                                                <label for="pincode">Pincode:</label>
                                                                <span class="help-block"></span>
										                    </div>
														</div>


													</div>

												
												</div>
							<div class="table-responsive">
                                        <table class="table table-bordered">
                                            <tbody><tr style="background:#ECF0F5;">

                                                

                                                <th style="width:50%; text-align:center;border:1px solid #E1E1E1;">PRODUCT</th>				
                                                <th style="width:10%; text-align:center;border:1px solid #E1E1E1;">QTY</th>
                                                <th style="width:15%; text-align:center;border:1px solid #E1E1E1;">RATE</th>
                                                <th style="width:15%; text-align:center;border:1px solid #E1E1E1;">TOTAL</th>
                                                <th style="width:15%;border:1px solid #E1E1E1;"></th>
                                            </tr>
                                            <tr>
												<td style="text-align:center; width:20%; " class="responsive_col">
                                                    <select class="form-control select2 select2-hidden-accessible" name="productList" id="productList" data-rel="chosen" tabindex="-1" aria-hidden="true">
														<option value="0.0" pv="0.0"> Select Product</option>
                                                        <?php
															foreach($response->productList as $row)
															{
																echo "<option value='" . $row->id . "' price='" . $row->price . "'  pv='" . $row->pricevalue . "'>" . ucwords($row->name) . "</option>";
															}
														?>                                                            
                                                      </select>
                                                </td>                             
												<td class="responsive_col"> 
												<center><input type="text" class="form-control" name="qty" id="qty" style="width:100%;" value="0" maxlength="2"/></center>
												</td>
												<td class="responsive_col">
												<center><input type="text" class="form-control" name="rate" id="rate" style="width:100%;" value="0.0" />
												</center></td>
												<td class="responsive_col">
												<center><input type="text" class="form-control" name="total" id="total" style="width:100%;" value="0.0" readonly="readonly"></center>
												</td>
												<td class="responsive_col">
												<center><button type="button" class="btn btn-block btn-default btn-flat" name="addProduct" id="addProduct" style="background:#0080C0; color:#fff;">ADD</button></center>
												</td>
                                            </tr>
                                        </tbody></table></div>
                                        <div class="table-responsive">
                                    <table class="table table-bordered style=" border:1px="" solid="" #e1e1e1;> 
									   <tbody name='products' id='products'>
											<tr style="background:#ECF0F5;">
												 <th style="width:10%;text-align:center;border:1px solid #E1E1E1;">SR.NO</th>
												 <th style="width:45%; text-align:center;border:1px solid #E1E1E1;">PRODUCT</th>
												 <th style="width:15%; text-align:center;border:1px solid #E1E1E1;">QTY</th>
												 <th style="width:15%; text-align:center;border:1px solid #E1E1E1;">RATE</th>
												 <th style="width:15%; text-align:center;border:1px solid #E1E1E1;">TOTAL</th>
											</tr>
										</tbody>
									</table>
									<button type="submit" class="btn btn-default btn-flat col-md-offset-10 col-md-2" name="btnPurchase" id="btnPurchase" style="background:#0080C0; color:#fff;">SUBMIT</button>
                                  </div>
								  </form>
                                      
						</div>
					</div>
					<!-- END SAMPLE FORM PORTLET-->
					
				</div>
			</div>


<?php

	include 'footer.php';
?>



<script type="text/javascript">

	var productList = 0;
	var arrayElement = new Array();
	var request = 0;

	$("#productList").change(function(){

		var quantity = 1;
		var rate     = $(this).find('option:selected').attr('pv');
		var total    = quantity * rate;
		
		$("#qty").val(quantity);
		$("#rate").val(rate);
		$("#total").val(total);
	
	});

   $("#qty").on("keypress keyup blur",function (event){

		$(this).val($(this).val().replace(/[^\d].+/, ""));
        if ((event.which < 48 || event.which > 57)) {
                event.preventDefault();
        }
		  
		else
		{
			var quantity = $("#qty").val();
			var rate     = $("#productList").find('option:selected').attr('pv');
			var total    = quantity * rate;
			$("#rate").val(rate);
			$("#total").val(total);
		}

	});

	

	
	$("#addProduct").click(function(){

		var selectedIndex = $("#productList option:selected").index();

		
		if (selectedIndex == 0)
		{
			return false;
		}
		
		
		else
		{
			var totalItems = 0;
			var totalRate  = 0;
			var grandTotal = 0;
			var quantity = 	$("#qty").val();
			var rate     = 	$("#rate").val();
			var total    =  $("#total").val();
			var productName = $("#productList").find('option:selected').text();
			var tableRow = "<tr style='background:#ECF0F5;'><th style='width:10%;text-align:center;border:1px solid #E1E1E1;'>SR.NO</th><th style='width:45%; text-align:center;border:1px solid #E1E1E1;'>Product</th><th style='width:15%; text-align:center;border:1px solid #E1E1E1;'>QTY</th>			 <th style='width:15%; text-align:center;border:1px solid #E1E1E1;'>AMOUNT</th><th style='width:15%; text-align:center;border:1px solid #E1E1E1;'>TOTAL</th></tr>";
    		
			arrayElement[productList] = [productList, productName, quantity, rate, total];
			productList++;

			for(i=0; i<arrayElement.length; i++)
			{
				tableRow += "<tr><td style='text-align:center;>'>" + (arrayElement[i][0]+1)  + "</td>";
				tableRow += "<td style='text-align:center;>'>" + arrayElement[i][1]  + "</td>";
				tableRow += "<td style='text-align:center;>'>" + arrayElement[i][2] + "</td>";
				tableRow += "<td style='text-align:center;>'>" + arrayElement[i][3]  + "</td>";
				tableRow += "<td style='text-align:center;>'>" + arrayElement[i][4]  + "</td></tr>";
				totalItems += parseFloat(arrayElement[i][2]);
			    grandTotal += parseFloat(arrayElement[i][4]);
			}

			tableRow += "<tr><td style='text-align:center;>'></td>";
			tableRow += "<td style='text-align:center;>'><strong>TOTAL ITEMS</strong></td>";
			tableRow += "<td style='text-align:center;>'><strong>" + totalItems + "</strong></td>";
			tableRow += "<td style='text-align:center;>'><strong>GRAND TOTAL</strong></td>";
			tableRow += "<td style='text-align:center;>'><strong>" + grandTotal + "</strong></td></tr>";


			$("#products").html(tableRow);
			$("#productList").prop('selectedIndex', 0);
			$("#qty").val(0);
			$("#rate").val(0.0);
			$("#total").val(0.0);

		}
	});



 	$("#rate").on("keypress keyup blur paste change",function (event) {

	   $(this).val($(this).val().replace(/[^\d].+/, ""));
            if ((event.which < 48 || event.which > 57)) {
                event.preventDefault();
        }

		var rate  = $("#rate").val();
		var qty   = $("#qty").val();
		var total = rate * qty;
        $("#total").val(total);
		
	});


	$("#memberID").on("keypress keyup blur paste change",function (event) {

	   
           $(this).val($(this).val().replace(/[^\d].+/, ""));
            if ((event.which < 48 || event.which > 57)) {
                event.preventDefault();
            }
			else
			{
				if($("#memberID").val().length==9)
				{
					 request++;
					 if(request>1)
						 return false;

					  $.post("../API/API_check_refer_idV3.php",
					  {
						refer_id: $("#memberID").val(),
						//ajax : 1,

					  },

					  function(data, status){
						if(status)
						{
						  request = 0;
						  if(data.status==1)
						  {
							 $("#memberName").val(data.user_info.name + "  " + data.user_info.last_name);
							 $("#customerID").val(data.user_info.customer_id);
							 var address = data.user_info.address1 + ", " + data.user_info.address2 + ", " + data.user_info.city + " - " + data.user_info.zip_code + " "  + data.user_info.state + "."; 
							 $("#address").text(address);

						  }
						  else
						  {
							 $("#memberName").val("Invalid Member ID!");
 							 $("#memberName").val("Invalid Member ID!");
						  }

						}

						else
						  {
							alert("Cannot get a response from the server. Please try later!");
							$('#finish').attr('disabled', true);

						  }
						
						});
				   }

				
			  }
    });



	
	$("input[type='radio']").on('ifChecked', function () { 

		var salesType = $(this).val();

		if(salesType==1)
			$("#storeRow").css("display", "none");

		else
			$("#storeRow").css("display", "block");

		
		
	})

		

</script>
</body>
</html>