<?php require_once("../default/common.php"); ?>


<script>
jQuery(document).ready(function($) {
	$("input[name*='br_Colisage_0[0]']").attr("readonly","readonly");
	
	for (v = 0; v < 50; v++) { 
		$("select[name*='br_product_id_be_0["+v+"]']").parent().append('<i class="fa fa-plus-circle text-success add_ligne" alt="#table_br_product" ></i>');
	
		$("input[name*='br_product_coulissage_0["+v+"]']").css("width","70px");
		$("select[name*='br_product_id_be_0["+v+"]']").css("width","75px");
	
	
	}
	
	
	//===================================================================
	var id_frnn = $("#product").val();



		$(document).on( "change","#table_br_product select",function() {
			var val_select = $(this).val();
			var name_select = $(this).attr("name");
			var res = name_select.split("[");
			// var index = res[1].toString().substr(0, 1);
			var index = res[1].replace("]","");
			
			
			if( res[0] == "br_product_name_0"){
					   
						for (v = 0; v < 50; v++) { 
							$("select[name*='br_product_id_be_0["+v+"]'] option").hide();
						}
			
					$.ajax({
					   url: "/admin/ajax/get_product_mp_list2.php",
						type: "GET",
						data: {
							id: val_select,
						},
						dataType: "JSON",
						success: function (jsonStr) { 
							var text = JSON.stringify(jsonStr) ;
							console.log(text);
							var obj = JSON.parse(text);
							var count_mf = Object.keys(obj).length;
								var i = 1;
							
								for (j = 0; j < count_mf; j++) { 	
									// console.log('==>'+obj[j].id);
									if(obj[j].id == val_select){
										var ids_be = obj[j].ids_be.split(",");
										var i;
										for (i = 0; i < ids_be.length; ++i) {
											console.log("be==>"+index+"--"+ids_be[i]);
											$("select[name*='br_product_id_be_0["+index+"]'] [value='"+ids_be[i]+"']").show();								
										}
									
									}
									
								}
						}
					});	

			}

				
			if( res[0] == "br_product_id_be_0"){
				
					var id_prd = $(this).attr("name");
					id_prd = id_prd.replace('br_product_id_be_0[', "");
					id_prd = id_prd.replace(']', "");
					
					var id_product = $("select[name*='br_product_name_0["+id_prd+"]']").val();
					
					var id_be = $(this).val();
					// alert(id_product);
					$.ajax({
					   url: "/admin/ajax/get_qte_be2.php",
						type: "GET",
						data: {
							id: id_product,
							id_be: id_be,
						},
						dataType: "JSON",
						success: function (jsonStr) { 
							var text = JSON.stringify(jsonStr) ;
							console.log(text);
							var obj = JSON.parse(text);
							var count = Object.keys(obj).length;
							for (j = 0; j < count; j++) { 
								$(".qteGet_"+id_prd).text("");
								$("input[name*='br_product_qte_0["+id_prd+"]'] ").parent().append("<span style='color:green;font-size: 13px;' class='qteGet_"+id_prd+"'> Max:"+obj[j].qte+"</span>");
								// alert(obj.qte);
							}	

						}
					});		
			}		

		});
		
		
	//===================================================================
	
	
	
	
	
	
		$(document).on( "click",".add_ligne",function() {

			
			var data_line = $(this).parent().parent();
			
			var id_index = $(this).prev().attr("name");
			
			var qte_val = $(this).prev().val();
			
			
			id_index = id_index.replace('br_product_id_be_0[', "");
			id_index = id_index.replace(']', "");
			
			var prod_val = $("select[name*='br_product_id_produits_0["+id_index+"]']").val();
			var unite_val = $("select[name*='br_product_unite_0["+id_index+"]']").val();
			
			// alert(id_index);
			
			// $(".new_entry").click();
			var table = $($(this).attr('alt'));
            var row = $('tr', table);
            var index = row.length-1;
            // row = row.last().clone();
            row = $(this).parent().parent().clone();
            
            $('td', row).css('background-color','#dff0d8');
            // $('input, textarea', row).val('');
			// alert($('select > option', row).val());
			
            // $('select > option', row).prop('selected', false);
            // $('checkbox, radio', row).prop('checked', false);
            $('input, textarea, select, checkbox, radio', row).each(function(){
                old_name = $(this).attr('name');
                $(this).attr('name', old_name.replace(/([a-zA-Z_0-9\[\]]+)\[([0-9]+)\](\[\])?/, '$1['+index+']$3'));
            });
            // row.appendTo(table);
            row.insertAfter(data_line);
			
			$("select[name*='br_product_id_be_0["+index+"]']").val('');
			$("select[name*='br_product_id_produits_0["+index+"]']").val(prod_val);
			$("select[name*='br_product_unite_0["+index+"]']").val(unite_val);
			
			$("input[name*='br_product_qte_0["+index+"]']").val(0);
			$("input[name*='br_product_qte_0["+index+"]']").next().html("");
            return false;
		});
	
	
	
});
</script>