$('#save,#update').on("click",function (e) {
	var base_url=$("#base_url").val();
    //Initially flag set true
    var flag=true;

    function check_field(id)
    {

      if(!$("#"+id).val() ) //Also check Others????
        {

            $('#'+id+'_msg').fadeIn(200).show().html('Required Field').addClass('required');
            //$('#'+id).css({'background-color' : '#E8E2E9'});
            flag=false;
        }
        else
        {
             $('#'+id+'_msg').fadeOut(200).hide();
             //$('#'+id).css({'background-color' : '#FFFFFF'});    //White color
        }
    }

    var item_group = $("#item_group").val();
    //Validate Input box or selection box should not be blank or empty
	check_field("item_name");
	check_field("category_id");
	check_field("unit_id");//units of measurments
	//check_field("alert_qty");
	check_field("tax_id");
	check_field("tax_type");
	//check_field("profit_margin");
	if(item_group=='Single'){
		check_field("price");
		check_field("purchase_price");
		check_field("sales_price");
	}
	else{
		if(!validate_variants_records()){
			//toastr["warning"]("You have Missed Something to Variants Fields!");
			return;
		}	
	}
	
	
    if(flag==false)
    {
		toastr["warning"]("You have Missed Something to Fillup!");
		return;
    }

    var this_id=this.id;

    var existing_row_count=0;
    if(item_group=='Variants'){
    	var existing_row_count = $("#variant_table  tr").length;
    	if(existing_row_count==1){
    		toastr["warning"]("No Records in Variants List!!");
    		return;
    	}
    }


    if(this_id=="save")  //Save start
    {

					///if(confirm("Do You Wants to Save Record ?")){
						e.preventDefault();
						data = new FormData($('#items-form')[0]);//form name
						/*Check XSS Code*/
						if(!xss_validation(data)){ return false; }
						
						$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
						$("#"+this_id).attr('disabled',true);  //Enable Save or Update button
						$.ajax({
						type: 'POST',
						url: 'newitems?existing_row_count='+existing_row_count,
						data: data,
						cache: false,
						contentType: false,
						processData: false,
						success: function(result){
              //alert(result);return;
							if(result=="success")
							{
								//alert("Record Saved Successfully!");
								window.location=base_url+'items';//"items-view.php";
								return;
							}
							else if(result=="failed")
							{
							   toastr["error"]("Sorry! Failed to save Record.Try again!");
							   //	return;
							}
							else
							{
								toastr["error"](result);
								
							}
							$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
							$(".overlay").remove();

					   }
					   });
				///}
				return;
				//e.preventDefault


    }//Save end
	
	else if(this_id=="update")  //Save start
    {
				
					///if(confirm("Do You Wants to Update Record ?")){
						e.preventDefault();
						data = new FormData($('#items-form')[0]);//form name3
						/*Check XSS Code*/
						if(!xss_validation(data)){ return false; }
						
						$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
						$("#"+this_id).attr('disabled',true);  //Enable Save or Update button
						$.ajax({
						type: 'POST',
						url: base_url+'items/update_items?existing_row_count='+existing_row_count,
						data: data,
						cache: false,
						contentType: false,
						processData: false,
						success: function(result){
              //alert(result);return;
							if(result=="success")
							{
								window.location=base_url+'items';
							}
							else if(result=="failed")
							{
							   toastr["error"]("Sorry! Failed to save Record.Try again!");
							}
							else
							{
								  toastr["error"](result);
							}
							$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
							$(".overlay").remove();
							return;
					   }
					   });
				///}

				//e.preventDefault


    }//Save end
	

});


//On Enter Move the cursor to desigtation Id
function shift_cursor(kevent,target){

    if(kevent.keyCode==13){
		$("#"+target).focus();
    }
	
}

//update status start
function update_status(id,status)
{
	var base_url=$("#base_url").val();
	$.post(base_url+"items/update_status",{id:id,status:status},function(result){
		if(result=="success")
				{
					 toastr["success"]("Status Updated Successfully!");
				  //alert("Status Updated Successfully!");
				  success.currentTime = 0; 
				  success.play();
				  if(status==0)
				  {
					  status="Inactive";
					  var span_class="label label-danger";
					  $("#span_"+id).attr('onclick','update_status('+id+',1)');
				  }
				  else{
					  status="Active";
					   var span_class="label label-success";
					   $("#span_"+id).attr('onclick','update_status('+id+',0)');
					  }

				  $("#span_"+id).attr('class',span_class);
				  $("#span_"+id).html(status);
				  return false;
				}
				else if(result=="failed"){
					toastr["error"]("Failed to Update Status.Try again!");
				  failed.currentTime = 0; 
				  failed.play();

				  return false;
				}
				else{
					toastr["error"](result);
				  failed.currentTime = 0; 
				  failed.play();
				  return false;
				}
	});
}
//update status end
function removerow_also_delete_from_database(item_id,rowid){
	if(item_id==''){
		removerow(rowid);
		return;
	}
	else{
		var base_url=$("#base_url").val();
		$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
	    $.post(base_url+"items/delete_items",{q_id:item_id},function(result){
	   //alert(result);return;
		   if(result=="success")
					{
					  toastr["success"]("Record Deleted Successfully!");
					  removerow(rowid);
					}
					else if(result=="failed"){
					  toastr["error"]("Failed to Delete .Try again!");
					}
					else{
					   toastr["error"](result);
					}
					$(".overlay").remove();
					return false;
	   });

	}
}

//Delete Record start
function delete_items(q_id)
{
	
   if(confirm("Do You Wants to Delete Record ?")){
   	$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
    $.post($("#base_url").val()+"items/delete_items",{q_id:q_id},function(result){
   //alert(result);return;
	   if(result=="success")
				{
				  toastr["success"]("Record Deleted Successfully!");
				  $('#example2').DataTable().ajax.reload();
				}
				else if(result=="failed"){
				  toastr["error"]("Failed to Delete .Try again!");
				}
				else{
				   toastr["error"](result);
				}
				$(".overlay").remove();
				return false;
   });
   }//end confirmation
}
//Delete Record end
function multi_delete(){
	var base_url=$("#base_url").val();
    var this_id=this.id;
    
		if(confirm("Are you sure ?")){
			$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
			$("#"+this_id).attr('disabled',true);  //Enable Save or Update button
			
			data = new FormData($('#table_form')[0]);//form name
			$.ajax({
			type: 'POST',
			url: base_url+'items/multi_delete',
			data: data,
			cache: false,
			contentType: false,
			processData: false,
			success: function(result){
				result=result;
  //alert(result);return;
				if(result=="success")
				{
					toastr["success"]("Record Deleted Successfully!");
					success.currentTime = 0; 
				  	success.play();
					$('#example2').DataTable().ajax.reload();
					$(".delete_btn").hide();
					$(".group_check").prop("checked",false).iCheck('update');
				}
				else if(result=="failed")
				{
				   toastr["error"]("Sorry! Failed to save Record.Try again!");
				   failed.currentTime = 0; 
				   failed.play();
				}
				else
				{
					toastr["error"](result);
					failed.currentTime = 0; 
				  	failed.play();
				}
				$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
				$(".overlay").remove();
		   }
		   });
	}
	//e.preventDefault
}

//CALCULATED PURCHASE PRICE
function calculate_purchase_price(){
	var price = (isNaN(parseFloat($("#price").val()))) ? 0 :parseFloat($("#price").val()); 
	var tax = (isNaN(parseFloat($('option:selected', "#tax_id").attr('data-tax')))) ? 0 :parseFloat($('option:selected', "#tax_id").attr('data-tax')); 
	tax = parseFloat(tax);

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

	console.log('tax='+tax);
	if(tax_type=='Inclusive'){
			purchase_price =price;
	}
	else{
		purchase_price = (price + (price*tax)/parseFloat(100));
	}
	//$("#purchase_price").val( (price + (price*tax)/parseFloat(100)).toFixed(decimals));
	
	$("#purchase_price").val(to_Fixed(purchase_price));
	//calculate_sales_price();

	//$("#purchase_price").val( (price + (price*tax)/parseFloat(100)).toFixed(2));
	//calculate_sales_price();
	calculate_sales_price();
}
$("#price").keyup(function(event) {
	calculate_purchase_price();

});
$("#tax_id").on("change",function(event) {
	calculate_purchase_price();
});

//CALCUALATED SALES PRICE
function calculate_sales_price(){
	var price = (isNaN(parseFloat($("#price").val()))) ? 0 :parseFloat($("#price").val()); 
	var profit_margin = (isNaN(parseFloat($("#profit_margin").val()))) ? 0 :parseFloat($("#profit_margin").val()); 

	var profit_amt = (profit_margin/100) * price;

	var sales_price = price + profit_amt;

	$("#sales_price").val(to_Fixed(sales_price));
	//calculate_profit_margin();
}
$("#tax_type").on("change",function(event) {
	calculate_purchase_price();
});
$("#profit_margin").on("change",function(event) {
	calculate_sales_price();
});
//END
//CALCULATE PROFIT MARGIN PERCENTAGE
function calculate_profit_margin(){
	var price = (isNaN(parseFloat($("#price").val()))) ? 0 :parseFloat($("#price").val()); 
	var sales_price = (isNaN(parseFloat($("#sales_price").val()))) ? 0 :parseFloat($("#sales_price").val()); 	
	var profit_margin = (sales_price-price);
	var profit_margin = (profit_margin/price)*parseFloat(100);
	$("#profit_margin").val(to_Fixed(profit_margin));
}
$("#sales_price").on("change",function(event) {
	calculate_profit_margin();
});
//END

/*function delete_stock_entry(entry_id){
 if(confirm("Do You Wants to Delete Record ?")){
    var base_url=$("#base_url").val();
    $(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
   $.post(base_url+"items/delete_stock_entry",{entry_id:entry_id,item_id:$("#q_id").val()},function(result){
   //alert(result);//return;
   result=result;
     if(result=="success")
        { 
          location.reload(true);
        }
        else if(result=="failed"){
          toastr["error"]("Failed to Delete .Try again!");
          failed.currentTime = 0; 
          failed.play();
        }
        else{
          toastr["error"](result);
          failed.currentTime = 0; 
          failed.play();
        }
        $(".overlay").remove();
   });
   }//end confirmation   
  }*/

  function view_warehouse_wise_stock_item(item_id){
  	  var base_url=$("#base_url").val();
	  $.post(base_url+'warehouse/view_warehouse_wise_stock_item', {item_id: item_id}, function(result) {
	    $(".view_warehouse_wise_stock_item").html('').html(result);
	    $('#view_warehouse_wise_stock_item_model').modal('toggle');
	  });
	}

$("#variant_search").autocomplete({
    source: function(data, cb){
        $.ajax({
        	autoFocus:true,
            url: $("#base_url").val()+'items/get_json_variant_details',
            method: 'GET',
            dataType: 'json',
            /*showHintOnFocus: true,
			autoSelect: true, 
			
			selectInitial :true,*/
			
            data: {
                name: data.term,
            },
            beforeSend: function() {
                if($("#tax_id").val()==''){
                  toastr['warning']("Please Select Tax Type!");
                  $("#tax_id").select2('open');
                  $("#variant_search").removeClass('ui-autocomplete-loading');
                  return;
                }
                $("#variant_search").addClass('ui-autocomplete-loading');
            },
            success: function(res){
              //console.log(res);
                var result;
                result = [
                    {
                        label: 'No Records Found ',
                        value: ''
                    }
                ];

                if (res.length) {
                    result = $.map(res, function(el){
                        return {
                            label: el.variant_name + " - "+el.description,
                            value: '',
                            id: el.id,
                            //item_name: el.value,
                        };
                    });
                }

                cb(result);
            }
        });
    },
     response:function(e,ui){
          /*if(ui.content.length==1){
            $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', ui);
            $(this).autocomplete("close");
          }*/
          //console.log(ui.content[0].id);
        },
        //loader start
        search: function (e, ui) {
        },
        select: function (e, ui) { 
            if(typeof ui.content!='undefined'){
              console.log("Autoselected first");
              if(isNaN(ui.content[0].id)){
                return;
              }
              var item_id=ui.content[0].id;
            }
            else{
              var variant_id=ui.item.id;
            }
            //console.log(variant_id);
            return_variant_data_in_row(variant_id);  
            $("#variant_search").val('');            
        },   
        //loader end
});
function return_variant_data_in_row(variant_id){
  $("#variant_search").addClass('ui-autocomplete-loader-center');
  var base_url=$("#base_url").val();
  var rowcount=$("#hidden_rowcount").val();
  $.post(base_url+"items/return_variant_data_in_row/"+rowcount+"/"+variant_id,{},function(result){
        //alert(result);
        console.log("Result = "+result);

        $('#variant_table tbody').append(result);
        $("#hidden_rowcount").val(parseInt(rowcount)+1);
        success.currentTime = 0;
        success.play();
        //enable_or_disable_item_discount();
        $("#variant_search").removeClass('ui-autocomplete-loader-center');
        $("#variant_search").removeClass('ui-autocomplete-loading');
    }); 
}

function removerow(id){//id=Rowid 
 	$("#row_"+id).remove();
 	//final_total();
 	failed.currentTime = 0;
	failed.play();
}


 function calculate_purchase_price_of_all_row(){
   var rowcount=$("#hidden_rowcount").val();     
 
   for(i=1;i<=rowcount;i++){
 
     if(document.getElementById("td_data_"+i+"_3")){
       // For variants, purchase price is entered manually by user
       // No automatic calculation - user enters their own purchase price
       // Just trigger sales price calculation if purchase price is entered
       var purchase_price = get_float_type_data("#td_data_"+i+"_3");
       if(purchase_price > 0) {
         calculate_sales_price_of_all_row();
       }
           
     }//if end
   }//for end
 }
function calculate_purchase_price_new(price){
	var tax = (isNaN(parseFloat($('option:selected', "#tax_id").attr('data-tax')))) ? 0 :parseFloat($('option:selected', "#tax_id").attr('data-tax')); 
		tax = parseFloat(tax);
	var tax_type = $("#tax_type").val();
	purchase_price = (tax_type=='Inclusive') ? price : parseFloat(price)+parseFloat(calculate_exclusive(price,tax));
	//purchase_price = parseFloat(purchase_price)+parseFloat(price);;
	return to_Fixed(purchase_price);
}

function calculate_sales_price_of_all_row(){
   var rowcount=$("#hidden_rowcount").val();     
 
   for(i=1;i<=rowcount;i++){
     if(document.getElementById("td_data_"+i+"_3")){
       // For variants, use purchase price and profit margin to calculate all prices
       var purchase_price = get_float_type_data("#td_data_"+i+"_3");
       var profit_margin = get_float_type_data("#td_data_"+i+"_4");
       
       // Calculate sales price (base selling price)
       var sales_price = calculate_sales_price_new(purchase_price,profit_margin);
       $("#td_data_"+i+"_5").val(sales_price);
       
       // Calculate wholesale price (typically 5-10% less than sales price)
       var wholesale_price = calculate_wholesale_price_new(purchase_price,profit_margin);
       $("#td_data_"+i+"_6").val(wholesale_price);
       
       // Calculate retail price (typically 10-20% more than sales price)
       var retail_price = calculate_retail_price_new(purchase_price,profit_margin);
       $("#td_data_"+i+"_7").val(retail_price);
           
     }//if end
   }//for end
 }

function calculate_sales_price_new(price,profit_margin){
	// Simple calculation for variants: Purchase Price + Profit Margin
	// No tax calculation needed for variants as they use their own pricing
	var profit_amt = (profit_margin/100) * price;
	var sales_price = price + profit_amt;
	return to_Fixed(sales_price);
}

function calculate_wholesale_price_new(price,profit_margin){
	// Wholesale price should be the same as sales price (no discount)
	// Or slightly less, but never less than purchase price
	var profit_amt = (profit_margin/100) * price;
	var sales_price = price + profit_amt;
	// Wholesale price = Sales price (same as sales price for simplicity)
	var wholesale_price = sales_price;
	return to_Fixed(wholesale_price);
}

function calculate_retail_price_new(price,profit_margin){
	// Retail price is typically 10-20% more than sales price
	// Using 15% markup as default
	var retail_markup = 15;
	// Simple calculation: Purchase Price + Profit Margin + Retail Markup
	var profit_amt = (profit_margin/100) * price;
	var sales_price = price + profit_amt;
	var retail_price = sales_price + (sales_price * retail_markup / 100);
	return to_Fixed(retail_price);
}

// Update variant sales price and profit margin (same as single product system)
function updateVariantSalesPrice(rowcount) {
    var retailPrice = parseFloat($("#td_data_"+rowcount+"_9").val()) || 0;
    var wholesalePrice = parseFloat($("#td_data_"+rowcount+"_8").val()) || 0;
    
    // Set sales price to retail price
    $("#td_data_"+rowcount+"_7").val(retailPrice.toFixed(2));
    
    // Calculate profit margin from retail price
    var purchasePrice = parseFloat($("#td_data_"+rowcount+"_5").val()) || 0;
    var profitMargin = 0;
    
    if (purchasePrice > 0 && retailPrice > 0) {
        profitMargin = ((retailPrice - purchasePrice) / purchasePrice) * 100;
    }
    
    $("#td_data_"+rowcount+"_6").val(profitMargin.toFixed(2));
    
    // Only update wholesale price if it's empty and we have a retail price
    if (wholesalePrice == 0 && retailPrice > 0) {
        $("#td_data_"+rowcount+"_8").val(retailPrice.toFixed(2));
    }
}

// Calculate prices from profit margin (same as single product system)
function calculateVariantFromProfitMargin(rowcount) {
    var purchasePrice = parseFloat($("#td_data_"+rowcount+"_5").val()) || 0;
    var profitMargin = parseFloat($("#td_data_"+rowcount+"_6").val()) || 0;
    
    if (purchasePrice > 0 && profitMargin > 0) {
        var profitAmount = (profitMargin / 100) * purchasePrice;
        var salesPrice = purchasePrice + profitAmount;
        
        $("#td_data_"+rowcount+"_7").val(salesPrice.toFixed(2));
        $("#td_data_"+rowcount+"_9").val(salesPrice.toFixed(2));
        $("#td_data_"+rowcount+"_8").val(salesPrice.toFixed(2));
    }
}

// Calculate prices from single margin percentage
function calculateVariantFromMarginPercentage(rowcount) {
    var purchasePrice = parseFloat($("#td_data_"+rowcount+"_5").val()) || 0;
    var marginPercentage = parseFloat($("#td_data_"+rowcount+"_10").val()) || 0;
    
    if (purchasePrice > 0 && marginPercentage > 0) {
        // Calculate base sales price with margin
        var salesPrice = purchasePrice + (purchasePrice * marginPercentage / 100);
        
        // Set retail price to sales price
        $("#td_data_"+rowcount+"_9").val(salesPrice.toFixed(2));
        $("#td_data_"+rowcount+"_7").val(salesPrice.toFixed(2));
        
        // Only set wholesale price if it's empty
        var currentWholesalePrice = parseFloat($("#td_data_"+rowcount+"_8").val()) || 0;
        if (currentWholesalePrice == 0) {
            $("#td_data_"+rowcount+"_8").val(salesPrice.toFixed(2));
        }
    }
}


function calculate_profit_margin_of_all_row(){
   var rowcount=$("#hidden_rowcount").val();     
 
   for(i=1;i<=rowcount;i++){
     if(document.getElementById("td_data_"+i+"_3")){
       // For variants, calculate profit margin from purchase price and sales price
       var purchase_price = get_float_type_data("#td_data_"+i+"_3");
       var sales_price   = get_float_type_data("#td_data_"+i+"_5");
       var profit_margin = calculate_profit_margin_new(purchase_price,sales_price);

       $("#td_data_"+i+"_4").val(profit_margin);
       
       // Recalculate wholesale and retail prices based on the new sales price
       var wholesale_price = calculate_wholesale_price_new(purchase_price,profit_margin);
       var retail_price = calculate_retail_price_new(purchase_price,profit_margin);
       
       $("#td_data_"+i+"_6").val(wholesale_price);
       $("#td_data_"+i+"_7").val(retail_price);
           
     }//if end
   }//for end
 }
function calculate_profit_margin_new(price,sales_price){
	var profit_margin = (sales_price-price);
	var profit_margin = (profit_margin/price)*parseFloat(100);
	return to_Fixed(profit_margin);
}

$("#item_group").on("change",function(event) {
	var item_group = $("#item_group").val();
	if(item_group=='Variants'){
		$("#price,#purchase_price,#profit_margin,#sales_price,#mrp,#hsn,#sku,#custom_barcode,#adjustment_qty").parent().addClass('hide');	
		$(".variant_div").show();
	}
	else{
		$("#price,#purchase_price,#profit_margin,#sales_price,#mrp,#hsn,#sku,#custom_barcode,#adjustment_qty").parent().removeClass('hide');
		$(".variant_div").hide();
	}
});

$("#tax_id,#tax_type").on("change",function(event) {
	calculate_purchase_price_of_all_row();
	calculate_sales_price_of_all_row();
});

function validate_variants_records(){
   var rowcount=$("#hidden_rowcount").val();     
   var available_rows=0;
   for(i=1;i<=rowcount;i++){
     if(document.getElementById("td_data_"+i+"_5")){
     	available_rows++;

       // Check purchase price, wholesale price, and retail price
       var purchase_price = get_float_type_data("#td_data_"+i+"_5");
       var wholesale_price = get_float_type_data("#td_data_"+i+"_8");
       var retail_price = get_float_type_data("#td_data_"+i+"_9");
       
       if(purchase_price==0 || wholesale_price==0 || retail_price==0){
       	   $("#td_data_"+i+"_5").focus();
       	   toastr["warning"]("Purchase Price, Wholesale Price & Retail Price are Required!");
           return false;
       }
     }//if end
   }//for end
   if(available_rows==0){
   	toastr["warning"]("No Records in Variants List!!");
   	return false;
   }
   return true;
 }
 $(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});