<script type="text/javascript">

	$(document).ready(function(event){

		getEmployeeInfo(event,'select[name=employee]');
	});

	function showDetailModal(event,data)
	{

		//DEFAULT EVENT REQUEST PREVENT
		event.preventDefault();

		var contract_id = data;

		var route = 'contractdetail/'+contract_id;

	$.get(route,function(data)
					{
						

						 $('#contract-allowance-table-body').html(data);

	
			    	       $('#md_contract_detail').modal('show');
			  		});
	

	}

	function removeContractDetail(event,obj)
	{
		var id = $(obj).attr('id');

		var route = 'removecontractdetail/'+id;
		
		$.get(route,function(data)
		{
			$(obj).closest('tr').remove();		       
		});


	}

	

	function getEmployeeInfo(event,obj)
	{
		var employee_id = $(obj).val();


       var route = 'getEmployeeDetail/'+employee_id;
		
		$.get(route,function(data)
		{
	    		var selected_value = data.user_designation;


		$('.employee_type').val(data.employee_type);

			$('select[name=designation]').find('option').each(function(){

				if(selected_value == $(this).text().trim())
				{
					$(this).prop('selected','true');
				}
				else
				{
					// $(this).prop('selected','false');

				}
			});

		});
		


	}

	function assignToContract(event,id)
	{
		var assign_allowance_id = id;

		var route = 'getcontracts/'+assign_allowance_id;
		
		$.get(route,function(data)
		{

		
			             $('#contracts-table-body').html(data);

			    	     $('#md_allot_contract').modal('show');	       
		});

	}

  function selectAllContract(event,obj)
  {
    event.preventDefault();
    
    obj = $(obj);

     $('.checked_contract').each(function(event)
     {

          if(obj.prop('checked'))
          {
             $(this).prop('checked',true);

          }
          else
          {
             $(this).prop('checked',false);

          }
         
     });

    


  }

	function updateContractDetail(event,obj,element1,element2)
	{
		var id = $(obj).attr('id');
		var amount = $('.'+element1).val();
		var percentage = $('.'+element2).val();

	    var route = 'updatecontractdetail';

	showLoader();

		$.post(route,{id:id,percentage:percentage,amount:amount,_token:token},function(data,status)
		{
    		

    		removeLoader();
  		},'json').fail(function(data) {
    		removeLoader();

    		$.each(data.responseJSON.errors, function(index, value ) {

    			if(index === "amount")
    			{
					ajax_base_field_unsuccess($('.'+element1),value);
    			}
    			else if(index === "percentage")
    			{
    				ajax_base_field_unsuccess($('.'+element2),value);
    			}
    		});


    	// if(amount == 0)
    	// {
    	// 	ajax_base_field_unsuccess($('.'+element1),'Amount must be an integer or greater than zero.');
    	// }
    	// else
    	// {
    	// 	ajax_base_field_unsuccess($('.'+element2),'Percentaage must be an integer or greater than zero.');
    	// }

    	

          
       
         });


	}

</script>