<script type="text/javascript">

	function getItemList(event,obj,type)
	{
        event.preventDefault();

        type = $('.'+type).val();

				if($(obj).val() == "0")
				{

				}


			else{
	    var route = "{{url('/getitemlist')}}/"+$(obj).val()+'/'+type;

        showLoader();

		$.get(route,function(data)
		{

    		$('.item_list').html(data);

         removeLoader();

  		});
		}
	}

	function getItemDetails(id)
	{
        event.preventDefault();
				var myid = id.options[id.selectedIndex].value;
	    var route = "{{url('/getitemDetails')}}/"+myid;
        showLoader();
		$.get(route,function(data)
		{
    		alert(data);
         removeLoader();
  		});
	}

	function getItemCategoryByType(event,obj)
	{
        event.preventDefault();

        $('.item_list option').first().prop('selected',true);

	    var route = "{{url('/getitemcategorylist')}}/"+$(obj).val();

        showLoader();

		$.get(route,function(data)
		{


    		$('.category_list').html(data.view);
        $('.item_list').html(data.view1);


         removeLoader();

  		});
	}
  function addOtherFloor(event,obj)
  {

     event.preventDefault();

     var floor_value = $('.floor_list').val();

     if(floor_value == 'Other')
     {

        $('.floor_div').show();
     }
     else
     {
         $('.floor_div').hide();
     }

  }

    function addOtherBrand(event,obj,iteration)
  {

     event.preventDefault();


     var brand_value = $('.brand_list'+iteration).val();

     if(brand_value == 'Other')
     {

        $('#brand_div'+iteration).show();
     }
     else
     {
         $('#brand_div'+iteration).hide();
     }

  }

	function addItemData(event,id,list)
	{


		 event.preventDefault();

		var item = $('.item').val();
		var pid = $("#b_id").val();
		var uid = $("#au_id").val();

		if(uid == "" || uid == null)
		{
			alert("Please Select User");
			removeLoader();
			return;
		}
		if(pid == "" || pid == null || pid == "0")
		{
			alert("Please Select Project");
			removeLoader();
			return;
		}

		$("#setpid").val(pid);

		$("#setuid").val(uid);

    var current_unit = $('.item_unit').val();



		var route = "{{url('/getitemdata')}}/"+item+'/'+current_unit;

		 showLoader();

		$.get(route,function(data)
		{
			var quantity = $('.quantity').val();
			var date     = $('.date').val();
			var description = $('.description').val();

			if(description == null || description == "")
			{
				description = "None";
			}
			if(data.item_name == null || data.item_code == null || quantity == null || date == null)
			{
				alert("Please fill in the fields");
				removeLoader();
				return;
			}
       //COUNTER FOR ROWS
	  var auto_generate = 'item_'+(countItemList('#boq_item_table tbody tr') + 1);


       //INTRODUCED THIS QUOTE VARIABLE BECAUSE WE HAD TO CONCATENATE IT WITH THE CLASS BECAUSE OF CONFLICT OF SINGLE AND DOUBLE QUOTES
      var special_char = "'";

      var item_name ='<input class="cls_fullname cls_item_name form-control form-control-sm" readonly ="true" type="text" name="item_name[]" value = "'+data.item_name+'"/>';

      var item_code ='<input class="cls_fullname cls_item_code form-control form-control-sm" readonly ="true" type="text" name="item_code[]" value = "'+data.item_code+'"/>';




      var item_list = '<input type="hidden" name="item[]" value="'+data.item_id+'"/>';

      var item_quantity ='<input class="cls_required cls_item_quantity form-control form-control-sm"  type="text" name="item_quantity[]" value = "'+quantity+'"/>';

      var item_unit ='<select name="item_unit[]" class="form-control cls_selection form-control-sm select2" >'+data.unit_list+'</select>';

      var item_date ='<input class="cls_date cls_item_date form-control form-control-sm"  type="date" name="item_date[]" value = "'+date+'"/>';

       var item_description ='<input class=" form-control " readonly type="text" name="item_description[]" value = "'+description+'"/>';

       var action = '<button class="btn-danger btn-sm" onclick="removeItemRow(event,this)"><i class="fa fa-times"></i></button>';

      var view = '<tr><th scope = "row">'+(countItemList('#boq_item_table tbody tr') + 1)+'</th><td><div>'+item_code+'<small style = "float:none;" class="form-errors  req" value="*">{{ $errors->first("product_code[]",":message") }}</small></div></td><td><div>'+item_name+'<small style = "float:none;" class="form-errors  req" value="*">{{ $errors->first("product_name[]",":message") }}</small></div></td><td><div>'+item_quantity+'<small style = "float:none;" class="form-errors  req" value="*">{{ $errors->first("item_quantity[]",":message") }}</small></div></td><td><div>'+item_unit+'<small style = "float:none;" class="form-errors  req" value="*">{{ $errors->first("item_unit[]",":message") }}</small></div></td><td><div>'+item_description+'<small style = "float:none;" class="form-errors  req" value="*">{{ $errors->first("item_description[]",":message") }}</small></div></td><td><div>'+item_date+'<small style = "float:none;" class="form-errors  req" value="*">{{ $errors->first("item_date[]",":message") }}</small></div></td><td>'+action+'</td>'+item_list+'</tr>';

      $(id).append(view);

      resetAddItemSection();
			document.getElementById("b_id").disabled = true;
			document.getElementById("au_id").disabled = true;
			document.getElementById("dl_id").disabled = true;
         removeLoader();

  		});


	}

	// TABLE ID = "tab"  $('#tab tr')
  function countItemList(table_object)
  {
  	 var count = 0;

  	 table_object = $(table_object);

  	 table_object.each(function(){

  	 	count++;

  	 });

  	 return count;
  }
  function rearrangeTableOrder(table_object)
  {
  	 var count = 0;

  	 table_object = $(table_object);

  	 table_object.each(function(){

  	 	$(this).find('th').html(++count);


  	 });

  	 return count;
  }

   function removeItemRow(event,obj)
  {

  	event.preventDefault();



    // VIA OBJECT FIND CLOSEST TR THEN REMOVE NOW
  	$(obj).closest("tr").remove();

    // REARANGE THE SERIAL NO 1.2.3.....
  	rearrangeTableOrder('#boq_item_table tbody tr');




  }






  function resetAddItemSection()
  {
  	$('.item_type option').first().prop('selected',true);
    $('.quantity').val('');
    $('.date').val('');
    $('.description').val('');



  }


	function getUserlist()
	{
				event.preventDefault();

				var desi = $('#dl_id').val();
$('#mydegid').attr('disabled','disabled');
			var route = "{{url('/userbydesi')}}/"+$('#dl_id').val();

				showLoader();

		$.get(route,function(data)
		{

			$('#au_id').html(data);
				 removeLoader();

			});
	}



</script>
