
<script type="text/javascript">

	var sarsai_flag = false;
	var size_flag   = false;
	var features_flag = false;
	var dropdown_key = "<?php echo e(Crypt::encrypt('0')); ?>";

	// resetPlotSection();

function addImage(pk) {
    alert("addImage: " + pk);
}

$('#myModal .save').click(function (e) {
    e.preventDefault();
    addImage(5);
    $('#myModal').modal('hide');
    //$(this).tab('show')
    return false;
});

	function checkReserveBooking(event,obj)
	{
		
		obj = $(obj);




		if(obj.prop('checked'))
		{
			$('.reserve_booking').show();
		}
		else
		{
			$('.reserve_booking').hide();
			$('.reserve_sdate').val('');
			$('.reserve_edate').val('');
		}
	}

	function getPlotList(event,category,type,project)
	{
		event.preventDefault();

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

		var route = '/plotlist/'+category+'/'+type+'/'+project;

		showLoader();
		$.get(route,function(data)
		{

    		$('.plot_list').html(data);
    		removeLoader();
  		});
	}
//FOR RED SUN
		function getPlotTypeList(event,obj)
		{
		
			resetPlotSection(false);

			event.preventDefault();
			var route = '/plottypelist/'+$(obj).val();
			showLoader();
					$.get(route,function(data)
					{

			    		$('.plot_list').html(data);
			    		 setFstDropdown();
    		
                         
                        document.querySelector('.fstdropdown-select').fstdropdown.rebind();
                        
			    		removeLoader();
			  		});


		}
		function markTags(event,obj,booking,value,column)
		{
          event.preventDefault();
          	var route = '/marktag/'+booking+'/'+value+'/'+column;
			showLoader();
					$.get(route,function(data)
					{
					
					
						if(data.column == 'is_block' && data.value == 1)
						{
							$(obj).closest('tr').css('background-color','pink');
						}
						else if(data.column == 'is_block' && data.value == 0)
						{
							$(obj).closest('tr').css('background-color','white');
						}


						$(obj).find('img').attr('src',data.img);



			    		removeLoader();
			  		});
    
		}
		function getCategoryList(event,type,project)
		{
			event.preventDefault();


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

        type = $(type).val();

			var route = '/plotcategorylist/'+type+'/'+project;
			showLoader();
					$.get(route,function(data)
					{

						
			    		$('.category_list').html(data);
			    		removeLoader();
			  		});


		}

	function showDocument(event,booking)
	{

		$('#document_modal').modal('hide');

    		$('#document_model').html('');
		//DEFAULT EVENT REQUEST PREVENT
		event.preventDefault();

		var route = "<?php echo e(url('showdocuments')); ?>/"+booking;
		
		showLoader();
		$.get(route,function(data)
		{

    		$('#document_model').html(data);

    		$('#document_modal').modal('show');

    		removeLoader();
  		});

		// var booking_id = data;

		// var booking_form     = "<?php echo e(url('bookingform')); ?>/"+data;
		// var allotment_form   = "<?php echo e(url('allotmentform')); ?>/"+data;
		// var payment_form     = "<?php echo e(url('paymentform')); ?>/"+data;
		// var customer_summary = "<?php echo e(url('customersummary')); ?>/"+data;
		// var transfer_history = "<?php echo e(url('transferhistory')); ?>/"+data;
		// var nok_detail       = "<?php echo e(url('nokdetail')); ?>/"+data;
		// var nominee_detail   = "<?php echo e(url('nomineedetail')); ?>/"+data;
		// var receipt          = "<?php echo e(url('allreceipt')); ?>/"+data;



		// $('#booking_form').attr('href',booking_form);
		// $('#allotment_form').attr('href',allotment_form);
		// $('#payment_form').attr('href',payment_form);
		// $('#customer_summary').attr('href',customer_summary);
		// $('#transfer_history').attr('href',transfer_history);
  //       $('#nok_detail').attr('href',nok_detail);
  //       $('#nominee_detail').attr('href',nominee_detail);
  //       $('#all_receipt').attr('href',receipt);

		$('#document_modal').modal('show');

	}

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

		resetPlotInfoSection();

		var route = '/plotsize/'+$(obj).val();
		showLoader();
		$.get(route,function(data)
		{
    		if(data.id > 0)
    		{
    			$('.size').val(data.size);
    			$('.type').val(data.type);
    			$('.category').val(data.category);

    			$('.rate').val(data.rate);
    			$('.feature_charges').val(data.featurecharges);
    		    $('.feature_description').val(data.feature_description);
    		    $('.reference').val(data.plot_detail);
    		    $('input[name=file]').val(data.file_detail);



    		    calculateAgreed();
    		}

    		removeLoader();
  		});
	}

	function resetPlotInfoSection()
	{
		$('.size').val('');
		$('.type').val('');
		$('.category').val('');

		$('.rate').val('');
		$('.feature_charges').val('');
	    $('.feature_description').val('');

	    $('.total_amount').val('');
	    $('.discount').val('');

		$('.rate_into_words').val('');
		$('.discount_inwords').val('');
	}


	function ratePerMarla()
	{
		var rate = $('.rate').val();
		var size = $('.size').val();
		var total_amount = rate;
	


        //FOR CALCULATING TOTAL AMOUNT BY MULTIPLICATION OF SIZE AND RATE
		if(size_flag === true)
		{
			size = size.split(" ")[0];
			total_amount = isDefaulter(rate * size);
		}
		 //FOR CALCULATING TOTAL AMOUNT BY SARSAI FLAG
		if(sarsai_flag === true)
		{
			
		}
		return total_amount;
	}
	
     //CALCULATING AGREED AMOUNT
	function calculateAgreed()
	{
		if(ratePerMarla() > 0)
		{
			 // CALXULATE RATE PER MARLA AND SHOW TOTAL AMOUNT FIELD
			 var total_amount = isDefaulter(ratePerMarla());
			 $('.total_amount').val(total_amount);
			 $('.rate_into_words').val(numberInWords(total_amount));
			 // END

             //GETTING FEATURE CHARGES 
			 var feature_charges = isDefaulter(featureCharges());
			 // END

			 // GET DISCOUNT FROM DISCOUNT  AND SHOW INTO WORD
			 var discount = isDefaulter(getDiscount());
			 var discount_inwords = numberInWords(discount);
			 $('.discount_inwords').val(discount_inwords.trim());
			 //END

	         // CALCULATE AGREED AMOUNT AND DISPLAY IT IN WORDS
	         var agreed_amount = ((total_amount + feature_charges) - discount);
			 $('.agreed_amount').val(agreed_amount);
			 var agreed_inwords = numberInWords(agreed_amount);
			 $('.agreed_inwords').val(agreed_inwords);


			 //END
	    }
	}
	
     //CALCULATE FEATURE CHARGES
	function featureCharges()
	{
		var charges = 0;
		
		var feature_charges = $('.feature_charges').val();

		charges = isDefaulter(feature_charges);

		if(features_flag === true)
		{

		}

		return charges;
	}
	//END

	function getDiscount()
	{
		 var discount = isDefaulter($('.discount').val());

		 var total_amount = isDefaulter( $('.rate').val());

		 var feature_charges = isDefaulter($('.feature_charges').val());

		 var amount = isDefaulter((total_amount + feature_charges));

         //TO COMPARE DISCOUNT WITH SUM OF TOTAL AMOUNT AND FEATURE CHARGES
		 if(discount >= amount)
		    {
			     $('.discount').val("");

				 var agreed_amount = amount;

			     $('.agreed_amount').val(agreed_amount);

			     var agreed_inwords = numberInWords(agreed_amount);

			     $('.agreed_inwords').val(agreed_inwords);
		    }

		 else
		    {				
			     return discount;
            }	
	}

	//Validating customeric cnic format
     function validateCnic(event,obj)
        {
			 event.preventDefault();
 
			 var valid =  validate_cnic($(obj));
   
			        if(valid == true)
			        {
			        	showLoader();

			        	 var route = "<?php echo e(url('usercnic')); ?>/"+$(obj).val();

			        	 $.get(route, function(data)
						 {

							if(data.new_id)
							{

						     	$("input[name=customer_name]").val(data.name);
								$("input[name=user_id]").val(data.new_id);
			        			messageToaster('success','User Found','Success');

							}
							else
							{
								 $('#md_make_booking').modal('hide');
								 $('#form_add_customer')[0].reset();
								 $('input[name=cnic]').val($(obj).val());
								 $('#form_add_customer input[name=cnic]').attr('readonly',true); 
								 $('#md_add_customer').modal('show');		 

							}

			        	removeLoader();


			        });
			        }
			        else
			        {
			        	messageToaster('error','Cnic' ,'Format not correct','Invalid Cnic');

			        }
    }

    //RESET PLOT SECTION
	function resetPlotSection(flag = true)
	{
		if(flag == true && ratePerMarla() == 0)
		{
			$('.feature_charges').val("");
			$('.total_amount').val("");
			$('.discount').val("");
			$('.discount_inwords').val("");
			$('.agreed_amount').val("");
			$('.agreed_inwords').val("");
		}
		else
		{
			var type_view = '<option value='+dropdown_key+'>Select Type</option>';

			var category_view = '<option value='+dropdown_key+'>Select Category</option>';

			var plot_view = '<option value='+dropdown_key+'>Select Article</option>';

			$('.feature_charges').val("");
			$('.feature_description').val("");
			$('.total_amount').val("");
			$('.discount').val("");
			$('.discount_inwords').val("");
			$('.agreed_amount').val("");
			$('.agreed_inwords').val("");
			$('.size').val("");
			$('.rate').val("");
			$('.category').val('');
			$('.plot_list').html(plot_view);
			$('.type').val('');

		}
		
	}
	function feeInWords(event,obj)
	{
		event.preventDefault();
		var transfer_fee = $(obj).val();
		var fee_in_words = numberInWords(transfer_fee);
		$('.transfer_fee').val(fee_in_words);
	}
	function calculateRefund(event,obj,received)
	{
		event.preventDefault();
		
		var value = $(obj).val();
		var deduction_in_words = numberInWords(value);
		var received_amount = received;
	    var refund = received_amount - value;
	    var refund_in_words = numberInWords(refund);
	     $('.refund').val(refund);
		 $('.deduction').val(deduction_in_words);
		 $('.refund_in_words').val(refund_in_words);
	}

	function showBookingLedger(event,booking)
	{
		event.preventDefault();

		var route = '/showbookingledger/'+booking;

		showLoader();

		$.get(route,function(data)
		{
    		$('#td_booking_ledger').html(data);
    		$('#md_booking_ledger').modal('show');


    		removeLoader();
  		},'html');
	}


function showRemovePlan()
  {
    var counter = 0;

    $('.checked_bookingledger').each(function(event)
     {
          if($(this).prop('checked'))
          {
             $(this).prop('checked',true);
             counter++;

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

          }
         
     });


    if(counter > 0)
    {
      $('.cancel_reverse_ledger_button').show();
    }
    else
    {
        $('.cancel_reverse_ledger_button').hide();
    }
  }

  $(document.body).on('change','.checked_bookingledger',function(){

     showRemovePlan();

  });

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

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

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

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

          }
         
     });

     showRemovePlan();


  }

	function showCancelBookingLedger(event,booking)
	{
		event.preventDefault();

		var route = '/showcancelbookingledger/'+booking;

		showLoader();

		$.get(route,function(data)
		{
    		$('#td_booking_ledger').html(data);
    		$('#md_booking_ledger').modal('show');


    		removeLoader();
  		},'html');
	}


	//CALLING RESET PLOT SECTION FUNCTION	
	$('.rate').keyup(function(event){

		resetPlotSection();

	});


     //AJAX REQUEST TO STORE USER(CUSTOMER) WHILE BOOKING
	function storeCustomer(event,obj)
	{
		event.preventDefault();

		var obj = $('#form_add_customer');
//To send image
		var form = document.querySelector('#form_add_customer') // Find the <form> element
		var formData = new FormData(form); // Wrap form contents

		var route = obj.attr('action');	


		// alert(ajax_validate(obj,'add customer'));

		showLoader();

		$.ajax({
		url: route,
		type: obj.attr('method'),
		data:  formData,
		dataType: "json",
		contentType: false,
		cache: false,
		processData:false,
		success: function(result){
			if(result.id)
		    {
		    	$("input[name=user_id]").val(result.id);
		    	$("input[name=customer_name]").val(result.name);
		    	$("input[name=customer_cnic]").val(result.cnic);
				$('#md_add_customer').modal('hide');
		    	$('#md_make_booking').modal('show');
			    messageToaster('success','Customer has been registered.','Success');
					
		    }
	    	removeLoader();
		},
		error: function(data)
			   {
			       ajax_show_error(data);
			   } 	        
		});


	}



$(function() {
  return $(".modal").on("show.bs.modal", function() {
    var curModal;
    curModal = this;
    $(".modal").each(function() {
      if (this !== curModal) {
        $(this).modal("hide");
      }
    });
  });
});

</script><?php /**PATH /home/ubuntu/rebmgisplus/skylite/resources/views/scripts/booking.blade.php ENDPATH**/ ?>