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

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


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

    //Initially flag set true
    var flag=true;

    // Check exchange rate validation before proceeding
    var customer_id = $("#customer_id").val();
    if (customer_id) {
        // Check exchange rate synchronously
        var exchangeRateValid = true;
        $.ajax({
            url: base_url + 'exchange_rates/validate_customer_exchange_rate',
            type: 'POST',
            data: { 
                customer_id: customer_id
            },
            dataType: 'json',
            async: false, // Make it synchronous
            success: function(response) {
                if (!response.valid) {
                    exchangeRateValid = false;
                    // Show persistent modal instead of toast
                    showExchangeRateModal(response.message, response.from_currency, response.to_currency, base_url);
                } else {
                }
            },
            error: function() {
                exchangeRateValid = false;
                toastr.error('Error checking exchange rate. Please try again.');
            }
        });
        
        if (!exchangeRateValid) {
            return false;
        }
    }

    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
        }
    }


   //Validate Input box or selection box should not be blank or empty
	  check_field("customer_id");
    check_field("quotation_date");
    check_field("quotation_status");
    //check_field("warehouse_id");
	/*if(!isNaN($("#amount").val()) && parseInt($("#amount").val())==0){
        toastr["error"]("You have entered Payment Amount! <br>Please Select Payment Type!");
        return;
    }*/
    
    
	if(flag==false)
	{
		toastr["error"]("You have missed Something to Fillup!");
		return;
	}

	//Atleast one record must be added in quotation table 
    var rowcount=document.getElementById("hidden_rowcount").value;
	var flag1=false;
	for(var n=1;n<=rowcount;n++){
		if($("#td_data_"+n+"_3").val()!=null && $("#td_data_"+n+"_3").val()!=''){
			flag1=true;
		}	
	}
	
    if(flag1==false){
    	toastr["warning"]("Please Select Item!!");
        $("#item_search").focus();
		return;
    }
    //end


    var tot_subtotal_amt=$("#subtotal_amt").text();
    var other_charges_amt=$("#other_charges_amt").text();//other_charges include tax calcualated amount
    var tot_discount_to_all_amt=$("#discount_to_all_amt").text();
    var tot_round_off_amt=$("#round_off_amt").text();
    var tot_total_amt=$("#total_amt").text();

    var this_id=this.id;
    
			//if(confirm("Do You Wants to Save Record ?")){
				e.preventDefault();
				data = new FormData($('#quotation-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: base_url+'quotation/quotation_save_and_update?command='+this_id+'&rowcount='+rowcount+'&tot_subtotal_amt='+tot_subtotal_amt+'&tot_discount_to_all_amt='+tot_discount_to_all_amt+'&tot_round_off_amt='+tot_round_off_amt+'&tot_total_amt='+tot_total_amt+"&other_charges_amt="+other_charges_amt,
				data: data,
				cache: false,
				contentType: false,
				processData: false,
				timeout: 30000, // 30 second timeout
				success: function(result){
         // alert(result);return;
				result=result.split("<<<###>>>");
					if(result[0]=="success")
					{
						location.href=base_url+"quotation/invoice/"+result[1];
					}
					else if(result[0]=="failed")
					{
					   toastr['error']("Sorry! Failed to save Record.Try again");
					}
					else
					{
						alert(result);
					}
					$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
					$(".overlay").remove();

			   },
			   error: function(xhr, status, error) {
					$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
					$(".overlay").remove();
					
					if (status === 'timeout') {
						toastr['error']("Request timed out. Please try again.");
					} else {
						toastr['error']("An error occurred while saving. Please try again.");
					}
			   }
			   });
		//}
  
});



$("#item_search").autocomplete({
    source: function(data, cb){
        
        if($("#customer_id").val()==null){

          toastr["error"]("Please Select Customer");
          
          $("#customer_id").select2('open').next().find('input.select2-search__field').focus();

          //$("#item_search").removeClass('ui-autocomplete-loading');

          return false;
        }

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

                if (res.length) {
                    result = $.map(res, function(el){
                        return {
                            label: el.item_code +'--[Qty:'+el.stock+'] --'+ el.label,
                            value: '',
                            id: el.id,
                            item_name: el.value,
                            stock: el.stock,
                            service_bit: el.service_bit,
                           // mobile: el.mobile,
                            //customer_dob: el.customer_dob,
                            //address: el.address,
                        };
                    });
                }

                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(u.item.value==''){
               $("#item_search").removeClass('ui-autocomplete-loader-center');
             }
          
            if(parseInt(u.item.stock)<=0){
              toastr["warning"](u.item.stock+" Items in Stock!!");
              failed.currentTime = 0; 
              failed.play();
              return false;
            }
            var item_id =u.item.id;
            if(restrict_quantity(item_id)){
              return_row_with_data(item_id);  
            }*/
            
           
            if(typeof ui.content!='undefined'){
              if(isNaN(ui.content[0].id)){
                return;
              }
              var stock=ui.content[0].stock;
              var item_id=ui.content[0].id;
              var service_bit=ui.content[0].service_bit;
            }
            else{
              var stock=ui.item.stock;
              var item_id=ui.item.id;
              var service_bit=ui.item.service_bit;
            }
            
            
                return_row_with_data(item_id);  
            
            $("#item_search").val('');
            
            
        },   
        //loader end
});

function check_same_item(item_id){

  if($("#quotation_table tr").length>1){
    var rowcount=$("#hidden_rowcount").val();
    for(i=0;i<=rowcount;i++){
            if($("#tr_item_id_"+i).val()==item_id){
              increment_qty(i);
              failed.currentTime = 0;
              failed.play();
              return false;
            }
      }//end for
  }
  return true;
}

function return_row_with_data(item_id){
  //CHECK SAME ITEM ALREADY EXIST IN ITEMS TABLE 
  var item_check=check_same_item(item_id);
  if(!item_check){return false;}
  //END

  $("#item_search").addClass('ui-autocomplete-loader-center');
  var base_url=$("#base_url").val();
  var rowcount=$("#hidden_rowcount").val();
  var warehouse_id=$("#warehouse_id").val();
  var customer_id=$("#customer_id").val();
  $.post(base_url+"quotation/return_row_with_data/"+rowcount+"/"+item_id,{customer_id:customer_id,warehouse_id:warehouse_id},function(result){
        //alert(result);
        $('#quotation_table tbody').append(result);
        $("#hidden_rowcount").val(parseInt(rowcount)+1);
        success.currentTime = 0;
        success.play();
        enable_or_disable_item_discount();
        $("#item_search").removeClass('ui-autocomplete-loader-center');
        $("#item_search").removeClass('ui-autocomplete-loading');
        
        // Trigger currency check after adding item to ensure symbols are updated
        setTimeout(function() {
            check_customer_currency_for_quotation(true);
        }, 100);
    }); 
}

// Function to refresh existing items with customer-specific pricing for Quotation
function refresh_existing_items_with_customer_pricing_quotation(){
  var customer_id = $("#customer_id").val();
  var warehouse_id = $("#warehouse_id").val();
  var base_url = $("#base_url").val();
  
  // Only proceed if customer is selected and not walk-in
  if (!customer_id || customer_id === '' || customer_id === '1') {
    return;
  }
  
  // Get all existing items in the table
  var existingItems = [];
  $("#quotation_table tbody tr").each(function() {
    var itemId = $(this).find('input[id^="tr_item_id_"]').val();
    var qty = $(this).find('input[id^="td_data_"][id$="_3"]').val();
    
    if (itemId && qty) {
      existingItems.push({
        item_id: itemId,
        qty: qty,
        row: $(this)
      });
    }
  });
  
  if (existingItems.length === 0) {
    return;
  }
  
  // Show loading indicator
  $(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
  
  // Process each item
  var processedCount = 0;
  var totalItems = existingItems.length;
  
  existingItems.forEach(function(item) {
    $.post(base_url + "quotation/find_item_details", {
      id: item.item_id,
      customer_id: customer_id
    }, function(result) {
      try {
        var itemData = jQuery.parseJSON(result);
        
        // Update the price in the existing row
        var priceInput = item.row.find('input[id^="td_data_"][id$="_10"]');
        var priceHiddenInput = item.row.find('input[id^="td_data_"][id$="_4"]');
        
        if (priceInput.length && priceHiddenInput.length) {
          var newPrice = parseFloat(itemData.sales_price);
          var formattedPrice = newPrice.toFixed(2);
          
          // Update the displayed price
          priceInput.val(formattedPrice);
          priceHiddenInput.val(newPrice);
          
          
          // Recalculate totals for this row
          var rowIndex = item.row.attr('data-row');
          if (rowIndex) {
            calculate_tax(rowIndex);
          }
        }
        
        processedCount++;
        
        // If all items processed, remove loading and recalculate totals
        if (processedCount === totalItems) {
          $(".overlay").remove();
          final_total();
        }
      } catch (e) {
        processedCount++;
        
        if (processedCount === totalItems) {
          $(".overlay").remove();
          final_total();
        }
      }
    }).fail(function() {
      processedCount++;
      
      if (processedCount === totalItems) {
        $(".overlay").remove();
        final_total();
      }
    });
  });
}
//INCREMENT ITEM
function increment_qty(rowcount){
  /*var service_bit =$("#service_bit_"+rowcount).val();
  var flag = restrict_quantity($("#tr_item_id_"+rowcount).val(),service_bit);
  if(!flag){ return false;}*/

  var item_qty=$("#td_data_"+rowcount+"_3").val();
  //var available_qty=$("#tr_available_qty_"+rowcount+"_13").val();

  //if(service_bit==1 || parseInt(item_qty)<parseInt(available_qty)){
    item_qty=parseFloat(item_qty)+1;
    $("#td_data_"+rowcount+"_3").val(format_qty(item_qty));
  //}
  calculate_tax(rowcount);
}
//DECREMENT ITEM
function decrement_qty(rowcount){
  var item_qty=parseFloat($("#td_data_"+rowcount+"_3").val());
  /*var service_bit =$("#service_bit_"+rowcount).val();
  */
  if(item_qty<=1){
    $("#td_data_"+rowcount+"_3").val(format_qty(1));
    return;
  }
  $("#td_data_"+rowcount+"_3").val(format_qty(item_qty-1));
  calculate_tax(rowcount);
}

function update_paid_payment_total() {
  var rowcount=$("#paid_amt_tot").attr("data-rowcount");
  var tot=0;
  for(i=1;i<rowcount;i++){
    if(document.getElementById("paid_amt_"+i)){
      tot += parseFloat($("#paid_amt_"+i).html());
    }
  }
  $("#paid_amt_tot").html(to_Fixed(tot));
}


  //Delete Record start
function delete_quotation(q_id)
{
  var base_url=$("#base_url").val();
   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+"quotation/delete_quotation",{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;
    var base_url=$("#base_url").val();
    if(confirm("Are you sure ?")){
      data = new FormData($('#table_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: base_url+'quotation/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
}




  function restrict_quantity(item_id,service_bit=0) {
  	var rowcount=$("#hidden_rowcount").val();
  	var available_qty = 0;
  	var count_item_qty = 0;
  	var selected_item_id = 0;
      for(i=1;i<=rowcount;i++){
        if(document.getElementById("tr_item_id_"+i)){
        	selected_item_id = $("#tr_item_id_"+i).val();
            if(parseInt(item_id)==parseInt(selected_item_id)){
	             available_qty = parseFloat($("#tr_available_qty_"+i+"_13").val());
	             count_item_qty += parseFloat($("#td_data_"+i+"_3").val());
          }

        }
      }//end for
      if( service_bit==0 && available_qty!=0 && count_item_qty>=available_qty){
        toastr["warning"]("Only "+available_qty+" Items in Stock!!");
        failed.currentTime = 0; 
        failed.play();
      	return false;
      }
      return true;
  }

  /*$("#warehouse_id").on("change",function(event) {
    $('#quotation_table tbody').html('');
    final_total();
    if($("#warehouse_id").val()!=''){
      $("#item_search").attr({ disabled: false,});
    }
    else{
     $("#item_search").attr({ disabled: true,}); 
    }
  });*/

$('#item_search').keypress(function (e) {
 var key = e.which;
 // the enter key code
 if(key == 13){
    $("#item_search").autocomplete('search');
  }
});

// Multi-currency functionality for quotations
var last_checked_customer_id = null;
var currency_check_timeout = null;

// Helper function to set all currency symbols
function setAllCurrencySymbols(currency_symbol) {
    
    // Update currency symbols in the table headers and summary
    $('#discount_currency_symbol').text(currency_symbol);
    $('#subtotal_currency_symbol').text(currency_symbol);
    $('#other_charges_currency_symbol').text(currency_symbol);
    $('#discount_to_all_currency_symbol').text(currency_symbol);
    $('#tax_currency_symbol').text(currency_symbol);
    $('#round_off_currency_symbol').text(currency_symbol);
    $('#grand_total_currency_symbol').text(currency_symbol);
    
    // Update unit price column header if it exists
    if ($('#unit_price_currency_symbol').length) {
        $('#unit_price_currency_symbol').text(currency_symbol);
    } else {
    }
    
}

// Check customer currency for quotation
function check_customer_currency_for_quotation(force_check = false) {
    var customer_id = $('#customer_id').val();
    
    
    // Clear any existing timeout
    if (currency_check_timeout) {
        clearTimeout(currency_check_timeout);
    }
    
    // Skip if no customer selected, but allow force check
    if (!customer_id || customer_id === '') {
        return;
    }
    
    // Skip if same customer as last check, unless force check
    if (!force_check && customer_id === last_checked_customer_id) {
        return;
    }
    
    // Set timeout to prevent multiple rapid calls
    currency_check_timeout = setTimeout(function() {
        $.ajax({
            url: base_url + 'customers/get_customer_currency',
            type: 'POST',
            data: { customer_id: customer_id },
            dataType: 'json',
            success: function(currency_response) {
                
                if (currency_response && currency_response.customer_currency_name) {
                    // Update customer currency display
                    $('#customer_currency_display').text(currency_response.customer_currency_name + ' (' + currency_response.customer_currency_code + ')');
                    $('#customer_currency_info').show();
                    
                    // Update all currency symbols
                    setAllCurrencySymbols(currency_response.customer_currency_symbol);
                    
                    // Update last checked customer
                    last_checked_customer_id = customer_id;
                } else {
                    // Hide currency info if no specific currency
                    $('#customer_currency_info').hide();
                    // Reset to base currency symbols
                    setAllCurrencySymbols('$'); // Default fallback - will be updated by server-side
                }
            },
            error: function(xhr, status, error) {
                // Hide currency info on error
                $('#customer_currency_info').hide();
                setAllCurrencySymbols('$'); // Default fallback
            }
        });
    }, 300); // 300ms delay
}

// Event listener for customer selection - using same pattern as sales
$("#customer_id").on("change", function() {
    var is_editing = $("#is_editing").val() === '1';
    
    // If editing, don't change currency based on customer - preserve transaction currency
    if (is_editing) {
        return;
    }
    
    show_customer_currency();
    
    // Refresh existing items with customer-specific pricing
    setTimeout(function() {
        refresh_existing_items_with_customer_pricing_quotation();
    }, 100);
});

// Load base currency on page load, then check customer currency
$(document).ready(function() {
    // First, load base currency symbols
    load_base_currency_symbols();
    
    // Then check if customer is selected and update accordingly
    setTimeout(function() {
        show_customer_currency();
    }, 500);
});

// Function to load base currency symbols
function load_base_currency_symbols() {
    // Get base currency from the PHP variable already defined in the view
    var base_currency = '<?= $currency_symbol ?>';
    setAllCurrencySymbols(base_currency);
}

// Show customer currency information - copied from sales module
function show_customer_currency() {
    var customer_id = $("#customer_id").val();
    var base_url = $("#base_url").val();
    var is_editing = $("#is_editing").val() === '1';
    
    // If editing, don't change currency based on customer - preserve transaction currency
    if (is_editing) {
        return;
    }
    
    if (customer_id) {
        $.ajax({
            url: base_url + 'quotation/check_currency_availability',
            type: 'POST',
            data: { customer_id: customer_id },
            dataType: 'json',
            success: function(response) {
                if (response.success) {
                    // Get customer currency details
                    $.ajax({
                        url: base_url + 'customers/get_customer_currency',
                        type: 'POST',
                        data: { customer_id: customer_id },
                        dataType: 'json',
                        success: function(currency_response) {
                            if (currency_response.currency_name) {
                                $("#customer_currency_display").text(currency_response.currency_name + ' (' + currency_response.currency_code + ')');
                                $("#customer_currency_info").show();
                                
                                // Change currency symbols in table headers and summary
                                setAllCurrencySymbols(currency_response.currency_symbol || currency_response.currency_code);
                            } else {
                                $("#customer_currency_info").hide();
                                // Reset to base currency symbol
                                setAllCurrencySymbols('<?= $currency_symbol ?>');
                            }
                        }
                    });
                } else {
                    // Show error message
                    $("#customer_currency_display").text('Error: ' + response.message);
                    $("#customer_currency_info").show().removeClass('label-info').addClass('label-danger');
                }
            },
            error: function() {
                $("#customer_currency_info").hide();
                load_base_currency_symbols();
            }
        });
    } else {
        $("#customer_currency_info").hide();
        // Reset to base currency symbol
        load_base_currency_symbols();
    }
}  