<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>

      <div class="main my-0">
        <div class="row mt-3 mx-0">
          <div class="col-md-6">
            <h3><?php echo $pgheading;?></h3>
          </div>
          <div class="col-md-5 text-right">
            &nbsp;
          </div>
        </div>
        
        <div class="container_fluid mt-2 px-3">
		<form name="frmgeneral" method="post" action="warehouse/<?php echo $mgmtname;?>/managedata" enctype="multipart/form-data" onsubmit="return validateFrm();">
		<input type="hidden" name="rsrcid" value="<?php echo encodeValue($rsrcid);?>">                
        <table class="webforms mt-0 table-responsive-sm">
		<tr>
          <td width="20%"><label>Product:</label></td>
          <td width="80%">
		  <?php
		  if($rsrcid>0)
		  {						
				echo $productinfo->fld_name;						
		  }
		  else
		  {
			  ?>
		  <select name="productid" id="productid" required>
		  <option value="">Select Product</option>
						<?php
						if(is_array($productlist) && count(array_filter($productlist))>0)
						{

							foreach($productlist as $productrow)
							{
								$sel_prod ="";
								if($info4edit->fld_productid==$productrow->fld_id)
								{
									$sel_prod = 'selected="selected"';
								}				
									?>
											<option value="<?php echo $productrow->fld_id;?>" <?php echo $sel_prod;?>><?php echo $productrow->fld_name;?></option>
									<?php
								
							}
						}
						?> 
						
		  </select>
		  <?php
			}
			?>
		  </td>
         </tr>   


         <tr>
          <td width="20%"><label>MRP:</label></td>
          <td width="80%"><input type="text" step="any" name="mrp" id="mrp"  required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="calcPrice();" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46" value="<?php if($info4edit) { echo $info4edit->fld_mrp; }?>"></td>
         </tr>
         <tr>
          <td width="20%"><label>Discount%:</label></td>
          <td width="80%"><input type="text" name="discount" id="discount" value="<?php if($info4edit) { echo $info4edit->fld_discount; }?>" required onchange="calcPrice();" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"></td>
         </tr>

         <tr>
          <td width="20%"><label>Price:</label></td>
          <td width="80%"><input type="text" readonly name="price" id="price" value="<?php if($info4edit) { echo $info4edit->fld_price; }?>" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"> <br><span id="pricemsg"></span> </td>
         </tr>


         <tr>
          <td width="20%"><label>3 Bottles Price:</label></td>
          <td width="80%"><input type="text" name="price3bottles" id="price3bottles" value="<?php if($info4edit) { echo $info4edit->fld_price3bottles; }?>" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"> <br><span id="price3bottlesmsg"></span> </td>
         </tr>


         <tr>
          <td width="20%"><label>6 Bottles Price:</label></td>
          <td width="80%"><input type="text" name="price6bottles" id="price6bottles" value="<?php if($info4edit) { echo $info4edit->fld_price6bottles; }?>" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"> <br><span id="price6bottlesmsg"></span></td>
         </tr>
         <tr>
          <td width="20%"><label>9 Bottles Price:</label></td>
          <td width="80%"><input type="text" name="price9bottles" id="price9bottles" value="<?php if($info4edit) { echo $info4edit->fld_price9bottles; }?>" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"> <br><span id="price9bottlesmsg"></span></td>
         </tr>
         <tr>
          <td width="20%"><label>12 Bottles Price:</label></td>
          <td width="80%"><input type="text" name="price12bottles" id="price12bottles" value="<?php if($info4edit) { echo $info4edit->fld_price12bottles; }?>" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"> <br><span id="price12bottlesmsg"></span></td>
         </tr>
         <tr>
          <td width="20%"><label>24 Bottles Price:</label></td>
          <td width="80%"><input type="text" name="price24bottles" id="price24bottles" value="<?php if($info4edit) { echo $info4edit->fld_price24bottles; }?>" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"> <br><span id="price12bottlesmsg"></span></td>
         </tr>
		<tr>
          <td width="20%"><label>Zones:</label></td>
          <td width="80%">
					 
		
		 
		  <?php
						$dbzoneids = array();
						$this->db->select("fld_zoneid");
						$this->db->where("fld_productid", $info4edit->fld_productid);
						$this->db->where("fld_price", $info4edit->fld_price);
						$this->db->where("fld_price3bottles", $info4edit->fld_price3bottles);
						$this->db->where("fld_price6bottles", $info4edit->fld_price6bottles);
						$this->db->where("fld_price9bottles", $info4edit->fld_price9bottles);
						$this->db->where("fld_price12bottles", $info4edit->fld_price12bottles);
						$this->db->where("fld_price24bottles", $info4edit->fld_price24bottles);
						$this->db->where("fld_del", 0);
						$this->db->group_by('fld_zoneid');
						$results = $this->db->get('tbl_price');
						//echo $this->db->last_query();
						if($results->num_rows()>0)
						{
							$azonelist = $results->result();
							
							foreach($azonelist as $azonerow)
							{
								$dbzoneids[]= $azonerow->fld_zoneid;
							}							
						}
			
						if(is_array($zonelist) && count(array_filter($zonelist))>0)
						{
							?>
							<div class="row">
							<?php
			
							foreach($zonelist as $zonerow)
							{		
									$tpindex++;
									$sel_zone = "";									
									if(in_array($zonerow->fld_id,$dbzoneids))
									{
										$sel_zone = 'checked="checked"';
									}
								
									?>
												<div class="col-md-3"><label class="container-checkbox horzbox">
												<input type="checkbox" name="zoneid[]" id="zoneid<?php echo $tpindex;?>" value="<?php echo $zonerow->fld_id ;?>"  <?php echo $sel_zone;?>/><?php echo $zonerow->fld_name ;?>
												<span class="checkmark leftchkbx"></span>
												</label>
												</div>
									<?php
									
							}
							?>
							</div>
							<?php
							
						}
		  
			?>
						

			
		  
		  </td>
         </tr>
		 <tr>
             <td></td>
             <td><button class="btn btn-danger" id="submit">Submit</button></td>
         </tr>
         </table>
         </form> 
	         <div class="clearfix"></div>          
        </div>
       </div>
<script type="text/javascript">


        function calcPrice()
        {			
		  	price  =0 ;
			
		
			var mrp = $("#mrp").val();
			mrp = parseFloat(mrp);
			//alert(mrp);
			
			var discount = $("#discount").val();
			discount = parseFloat(discount);		
			 ///alert(discount);
			  if(discount>0 && mrp>0)
			  {
				var discountamt = (discount * mrp/100);
				var price = (mrp-discountamt);
			  }
			  else
				{
					var price = mrp;
				}
				price = parseFloat(price);
				$('#price').val(price);
		}

function validateFrm()
{
	var mrp = $("#mrp").val();
	mrp = parseFloat(mrp);

	var price = $("#price").val();
	price = parseFloat(price);

	var price3bottles = $("#price3bottles").val();
	price3bottles = parseFloat(price3bottles);

	var price6bottles = $("#price6bottles").val();
	price6bottles = parseFloat(price6bottles);

	var price9bottles = $("#price9bottles").val();
	price9bottles = parseFloat(price9bottles);
	
	var price12bottles = $("#price12bottles").val();
	price12bottles = parseFloat(price12bottles);

	var price24bottles = $("#price24bottles").val();
	price24bottles = parseFloat(price24bottles);

	if(mrp<1)
	{
		$("#mrpmsg").html('Enter mrp greater than 0');
		$("#mrpmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#mrpmsg").removeClass('alert alert-danger');			
			$("#mrpmsg").html("");
		},4000);
		$("#mrp").val('');
		return false;
	}
	if(price>mrp)
	{
		$("#pricemsg").html('Price cannot be greater than mrp');
		$("#pricemsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#pricemsg").removeClass('alert alert-danger');			
			$("#pricemsg").html("");
		},4000);
		$("#price").val(mrp);
		return false;
	}
	if(price24bottles>price12bottles || price24bottles>price9bottles || price24bottles>price6bottles  || price24bottles>price3bottles || price24bottles>price)
	{
		
		$("#price24bottlesmsg").html('24 bottles price cannot be greater than 1,2,3,6 and 12 bottles price');
		$("#price24bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price24bottlesmsg").removeClass('alert alert-danger');			
			$("#price24bottlesmsg").html("");
		},4000);
		$("#price24bottles").val('');
		return false;
	}
	else if(price12bottles>price9bottles || price12bottles>price6bottles  || price12bottles>price3bottles || price12bottles>price)
	{		
		$("#price12bottlesmsg").html('12 bottles price cannot be greater than 1,3,6 and 9 bottles price');
		$("#price12bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price12bottlesmsg").removeClass('alert alert-danger');			
			$("#price12bottlesmsg").html("");
		},4000);
		$("#price12bottles").val('');
		return false;
	}
	else if(price12bottles<price24bottles)
	{		
		$("#price12bottlesmsg").html('12 bottles price cannot be less than 24 bottles price');
		$("#price12bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price12bottlesmsg").removeClass('alert alert-danger');			
			$("#price12bottlesmsg").html("");
		},4000);
		$("#price12bottles").val('');
		return false;
	}
	else if(price9bottles>price6bottles || price9bottles>price3bottles || price9bottles>price)
	{
		$("#price9bottlesmsg").html('9 bottles price cannot be greater than 1,3 and 6 bottles price');
		$("#price9bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price9bottlesmsg").removeClass('alert alert-danger');			
			$("#price9bottlesmsg").html("");
		},4000);
		$("#price9bottles").val('');
		return false;
	}
	else if(price9bottles<price12bottles || price9bottles<price24bottles)
	{
		$("#price9bottlesmsg").html('9 bottles price cannot be less than 12 and 24 bottles price');
		$("#price9bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price9bottlesmsg").removeClass('alert alert-danger');			
			$("#price9bottlesmsg").html("");
		},4000);
		$("#price9bottles").val('');
		return false;
	}
	else if(price6bottles>price3bottles || price6bottles>price)
	{
		$("#price6bottlesmsg").html('6 bottles price cannot be greater than 1,3 bottle price');
		$("#price6bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price6bottlesmsg").removeClass('alert alert-danger');			
			$("#price6bottlesmsg").html("");
		},4000);
		$("#price6bottles").val('');
		return false;
	}
	else if(price6bottles<price9bottles || price6bottles<price12bottles || price6bottles<price24bottles)
	{
		$("#price6bottlesmsg").html('6 bottles price cannot be less than 9,12 and 24 bottles price');
		$("#price6bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price6bottlesmsg").removeClass('alert alert-danger');			
			$("#price6bottlesmsg").html("");
		},4000);
		$("#price6bottles").val('');
		return false;
	}
	else if(price3bottles>price)
	{		
		$("#price3bottlesmsg").html('3 bottles price cannot be greater than 1 bottle price');
		$("#price3bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price3bottlesmsg").removeClass('alert alert-danger');			
			$("#price3bottlesmsg").html("");
		},4000);
		$("#price3bottles").val('');
		return false;
	} 
	else if(price3bottles<price6bottles || price3bottles<price9bottles || price3bottles<price12bottles || price3bottles<price24bottles)
	{
		$("#price3bottlesmsg").html('3 bottles price cannot be less than 6,9,12 and 24 bottles price');
		$("#price3bottlesmsg").addClass('alert alert-danger');
		setTimeout(function(){
			$("#price3bottlesmsg").removeClass('alert alert-danger');			
			$("#price3bottlesmsg").html("");
		},4000);
		$("#price3bottles").val('');
		return false;
	} 

	

	
		  
}


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

	  productid = $("#productid").val();
	  price = $("#price").val();
	 
	 if(productid>0 && price>0)
	  {
		checked = $("input[type=checkbox]:checked").length;		
		  if(!checked) {
			alert("You must check at least one zone.");
			return false;
		  }
	  }
    });
    

</script>