$(function() {
  function afficheSelection() {
    $.ajax({
      type: "GET",
      url: "includes//panier-rappel.inc.php",
      data: "fromAjax=1",
      cache: false,
      success: function(html){
        $("#div-panier").fadeOut("slow", function () {
          $("#div-panier").html(html);
          $("#div-panier").fadeIn("slow");
        });
      }
    });
    window.location.reload();
  }

  $(".prod_clr").click(function () {
    var my_id = $(this).parents("tr").attr("produit_id");
    var my_ref = $(this).parents("tr").attr("ref");

    $.ajax({
      type: "GET",
      url: "js/ajax/ajax-panier.php",
      data: "action=del&article=" + my_id + "&ref=" + my_ref,
      cache: false,
      success: function(html){
        $("#tr-produit-" + my_id + "-" + my_ref).remove();
        $('#sup2kg').val("false");
        $("tr[class^=bg-pointilles]").each(function() {
          if ($(this).attr("sup2kg") == 1)
            $('#sup2kg').val("true");
        });
				recalculerArticles();
				recalculeTotal();
        afficheSelection();
      }
    });
  });

  $(".prod_add").click(function () {
    var my_id = $(this).parents("tr").attr("produit_id");
    var my_ref = $(this).parents("tr").attr("ref");

    $.ajax({
      type: "GET",
      url: "js/ajax/ajax-panier.php",
      data: "action=add&article=" + my_id + "&ref=" + my_ref + "&qte=1",
      cache: false,
      success: function(html){
    	  $('#qte_' + my_id + "_" + my_ref).val( Number($('#qte_' + my_id + "_" + my_ref).val()) + 1 );
        recalculeLigne(my_id,my_ref,'add');

        $("#tr-produit-" + my_id + "-" + my_ref).find(".span-qte").text( $('#qte_' + my_id + "_" + my_ref).val() );
        afficheSelection();
      }
    });
  });

  $(".prod_del").click(function () {
    var my_id = $(this).parents("tr").attr("produit_id");
    var my_ref = $(this).parents("tr").attr("ref");
		
		if ($('#qte_' + my_id + "_" + my_ref).val() != 1)
		{
			$.ajax({
				type: "GET",
				url: "js/ajax/ajax-panier.php",
				data: "action=add&article=" + my_id + "&ref=" + my_ref + "&qte=-1",
				cache: false,
				success: function(html){
					$('#qte_' + my_id + "_" + my_ref).val( Number($('#qte_' + my_id + "_" + my_ref).val()) -1 );
					recalculeLigne(my_id,my_ref,'del');

					$("#tr-produit-" + my_id + "-" + my_ref).find(".span-qte").text( $('#qte_' + my_id + "_" + my_ref).val() );
					afficheSelection();
				}
			});
		}
  });

  function recalculeTotal() {
    // Le montant total du panier prime sur le poid total du panier
    var somme = 0;

    var totalArticles = Number($('#totalArticles').html());
    
    if ($('#sup2kg').val() == 'true') {
      sup2kg = true;
    } else {
      sup2kg = false;
    }

    if (totalArticles >= 5 && $('#frais_port_4').attr("checked") != true) {
      sup2kg = true;
    }

    $("input[id^=total_]").each(function() { somme+= Math.round(this.value * 100) / 100; });
    $('#totalTTC').html((Math.round(somme * 100 ) / 100).toFixed(2));

    if (sup2kg == true && somme<=450) {
      // On a plus de 2kg d'articles => on payera obligatoirement des frais de ports
      $('#frais_port_1').attr("checked", false); $('#frais_port_2').attr("checked", true);
      $('#frais_port_3').attr("checked", false); $('#frais_port_4').attr("checked", false);
      $('#frais_port_5').attr("checked", false); $('#frais_port_6').attr("checked", true);
      $('#frais_port_7').attr("checked", false); $('#frais_port_8').attr("checked", true);
      $('#frais_port_9').attr("checked", false);

      $('#frais_port_1').attr("disabled", true); $('#frais_port_2').attr("disabled", false);
      $('#frais_port_3').attr("disabled", true); $('#frais_port_4').attr("disabled", true);
      $('#frais_port_5').attr("disabled", true); $('#frais_port_6').attr("disabled", false);
      $('#frais_port_7').attr("disabled", true); $('#frais_port_8').attr("disabled", false);
      $('#frais_port_9').attr("disabled", false);

      $('#totalFraisPort').html(Math.round($('.frais_port:checked').attr("price") * 100) / 100);

    } else if (somme>0 && somme<=450) {
      if ($('#frais_port_4').attr("checked") == true) {
        // On sélectionne les frais soi même ici
        $('#frais_port_1').attr("checked", true); $('#frais_port_2').attr("checked", false);
        $('#frais_port_3').attr("checked", false); $('#frais_port_4').attr("checked", false);
      }
      $('#frais_port_5').attr("checked", true); $('#frais_port_6').attr("checked", false);
      $('#frais_port_7').attr("checked", true); $('#frais_port_8').attr("checked", false);
      $('#frais_port_9').attr("checked", false);

      // On désactive les frais de ports ici
      $('#frais_port_1').attr("disabled", false); $('#frais_port_2').attr("disabled", false);
      $('#frais_port_3').attr("disabled", false); $('#frais_port_4').attr("disabled", true);
      $('#frais_port_5').attr("disabled", false); $('#frais_port_6').attr("disabled", true);
      $('#frais_port_7').attr("disabled", false); $('#frais_port_8').attr("disabled", true);
      $('#frais_port_9').attr("disabled", false);

      $('#totalFraisPort').html(Math.round($('.frais_port:checked').attr("price") * 100) / 100);

    } else {
      // Frais de ports gratuit
      $('#frais_port_1').attr("checked", false); $('#frais_port_2').attr("checked", false);
      $('#frais_port_3').attr("checked", false); $('#frais_port_4').attr("checked", true);
      $('#frais_port_5').attr("checked", true); $('#frais_port_6').attr("checked", false);
      $('#frais_port_7').attr("checked", true); $('#frais_port_8').attr("checked", false);
      $('#frais_port_9').attr("checked", false);

      $('#frais_port_1').attr("disabled", true); $('#frais_port_2').attr("disabled", true);
      $('#frais_port_3').attr("disabled", true); $('#frais_port_4').attr("disabled", false);
      $('#frais_port_5').attr("disabled", false); $('#frais_port_6').attr("disabled", true);
      $('#frais_port_7').attr("disabled", false); $('#frais_port_8').attr("disabled", true);
      $('#frais_port_9').attr("disabled", false);

      $('#totalFraisPort').html(0);
    }

    $('#totalFraisPort').html((Math.round($('.frais_port:checked').attr("price") * 100) / 100).toFixed(2));
    $('#totalTotal').html((Math.round($('#totalTTC').html() * 100 ) / 100 + Math.round($('#totalFraisPort').html() * 100 ) / 100).toFixed(2));
  }

  function recalculeLigne(i,r,action) {
    var qte = $("#qte_" + i + "_" + r).val();
    var prix = $("#prix_" + i + "_" + r).val();

	  $('#total_l_' + i + '_' + r).val( (Math.round( qte * prix * 100 ) / 100).toFixed(2) );
	  $('#total_' + i + '_' + r).html( $('#total_l_' + i + '_' + r).val() + ' &euro;');
		
		recalculerArticles(action);
		
    recalculeTotal();
  }
	
	function recalculerArticles(action)	{
		if (action == 'add') {
			$('#totalArticles').html(Number($('#totalArticles').html()) + 1);
		} else if (action == 'del') {
			$('#totalArticles').html(Number($('#totalArticles').html()) - 1);
		} else {
			$.ajax({
				type: "GET",
				url: "js/ajax/ajax-articles.php",
				data: "fromAjax=1",
				cache: false,
				success: function(html){
					$('#totalArticles').html(html);
					if (html == 0)
					{
						$("#commande-panier").hide();
						$("#commande-panier-vide").fadeIn("slow");
					}
				}
			});
		}
		
		if (Number($('#totalArticles').html()) == 1)
			$('#totalArticlesLibelle').html('article');
		else
			$('#totalArticlesLibelle').html('articles');
	}
  
  $(".frais_port").click(function () {
      recalculeTotal();
  });
  
  // Recalcule pour les frais de port
  recalculeTotal();
});
