text: $( document ).ready(function() {
         addNewField(1);
         fetch_booking();
         addTenancyContract(1);
         fetch_tenancy_contract();
         undertaking();
      fetchrenwal_booking();
  
  
  
  });
  
  function addNewField(val) {
      // alert(val)
   if(val){
  
              var id = $("#year_rent_installment tbody  tr:last input[name=row_count]").val();
  
              if (id == undefined) {
                  var r_count = 1;
              } else {
                  var r_count = parseInt(id) + 1;
  
              }
              var html = '';
              html='<tr class="var_row">'+'<td>'+r_count+'</td><td><input type="hidden" value="'+r_count+'" name="year_rent['+r_count+'][installment]"><div class="form-group"><input  type="date" name="year_rent['+r_count+'][date]" class="form-control"   required></div></td>';
              html+=' <td><div class="form-group"> <select class="form-control select2bs4" style="width: 100%;" name="year_rent['+r_count+'][type]" ><option value="cheque">Cheque</option><option value="cash">Cash</option></select></div></td>';
              html+='<td><div class="form-group"><input  type="text" name="year_rent['+r_count+'][cheque_no]" class="form-control"  placeholder="Cheque no" required></div></td>' ;
              html+='<td><div class="form-group"><input  type="text" name="year_rent['+r_count+'][bank]" class="form-control"  placeholder="Bank" required></div></td>' ;
              html+='<td><div class="form-group"><input type="text"  name="year_rent['+r_count+'][amount]"class="form-control" cols="3" placeholder="Amount" required></div></td><td> <a type="button" class="btn bg-gradient-danger btn-sm" onclick="removeField(this)"><i class="las la-trash"></i></a><input type="hidden" name="row_count"  value="' + r_count + '"></td></tr>';
              var count_table_tbody_tr = $("#year_rent_installment tbody tr").length;
                      if (count_table_tbody_tr >= 1) {
                          $("#year_rent_installment tbody tr:last").after(html);
                      } else {
  
                          $("#year_rent_installment tbody").html(html);
                      }
            }
   }
  
  
   function removeField(val) {
    $(val).closest('.var_row').remove();
  
  }
  
  function fetch_booking(){
  
      var manageTable = $('#booking-table').DataTable();
      manageTable.destroy();
      manageTable= $("#booking-table").DataTable({
        "ajax": {
                  "url": 'fetchLeaseBooking',
                  'type': "GET",
                 },
        lengthMenu: [
                  [10, 25, 50, 100, -1],
                  ['10', '25', '50', '100', 'All']],
                  "responsive": false, "lengthChange": true, "autoWidth": true,
       "order": [[0, 'asc']],
         dom: "<'row'<'col-sm-4'l><'col-sm-4 text-center'B><'col-sm-4'f>>" +
                      "<'row'<'col-sm-12'tr>>" +
                      "<'row'<'col-sm-6'i><'col-sm-6'p>>",
              buttons: [{
                      extend: 'copy',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'csv',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'print',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))',
                      }
  
                  },
                  {
                      extend: 'excel',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'pdf',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  }],
  
  
        });
  }
  
  function get_file(id){
       $("#upload_lease_booking_signed_fileform").on('submit', function(event){
       event.preventDefault();
          // form.find("button[type='submit']").prop('disabled',true);
  
  $.ajaxSetup({
      headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
  });
  
    $.ajax({
     url:'upload_lease_booking_signed_file/'+id,
     method:"POST",
     data:new FormData(this),
     dataType:'JSON',
     contentType: false,
     cache: false,
     processData: false,
     success:function(data)
     {
          if (data.success == true) {
  
          $("#upload_lease_booking_signed_fileModal").modal('hide');
          $("#upload_lease_booking_signed_fileform")[0].reset();
          fetch_booking();
          }
  
     }
    });
  
           location.reload();
      });
  }
   $(document).on('click', '#deleteLeaseBooking', function (e) {
  
      e.preventDefault();
      var id = $(this).data('id');
  
          swal({
            title: "Are you sure?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
          })
          .then((willDelete) => {
            if (willDelete) {
              $.ajaxSetup({
              headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              }
              });
              $.ajax({
                  type: "POST",
                  url: 'deleteLeaseBooking/' + id,
                  success: function (data) {
                  fetch_booking();
                  swal("Successfully deleted!", {
                  icon: "success",
                  });
                  }
              });
  
          }
          location.reload();
          });
  
  });
  
   function addTenancyContract(val) {
      // alert(val)
   if(val){
  
              var id = $("#tenancy_contract_description tbody  tr:last input[name=row_count]").val();
  
              if (id == undefined) {
                  var r_count = 1;
              } else {
                  var r_count = parseInt(id) + 1;
  
              }
              var html = '';
              html+='<tr class="var_row">'+'<td>'+r_count+'</td><td><div class="form-group"><textarea  name="contract['+r_count+'][description]"class="form-control" cols="3" ></textarea></div></td><td> <a type="button" class="btn bg-gradient-danger btn-sm" onclick="removeField(this)"><i class="las la-trash"></i></a><input type="hidden" name="row_count"  value="' + r_count + '"></td></tr>';
              var count_table_tbody_tr = $("#tenancy_contract_description tbody tr").length;
                      if (count_table_tbody_tr >= 1) {
                          $("#tenancy_contract_description tbody tr:last").after(html);
                      } else {
  
                          $("#tenancy_contract_description tbody").html(html);
                      }
            }
   }
  
  
   function fetch_tenancy_contract(){
  
      var manageTable = $('#fetch_tenancy_contract').DataTable();
      manageTable.destroy();
      manageTable= $("#fetch_tenancy_contract").DataTable({
        "ajax": {
                  "url": 'fetchLeaseTenancyContract',
                  'type': "GET",
                 },
        lengthMenu: [
                  [10, 25, 50, 100, -1],
                  ['10', '25', '50', '100', 'All']],
                  "responsive": false, "lengthChange": true, "autoWidth": true,
       "order": [[0, 'asc']],
         dom: "<'row'<'col-sm-4'l><'col-sm-4 text-center'B><'col-sm-4'f>>" +
                      "<'row'<'col-sm-12'tr>>" +
                      "<'row'<'col-sm-6'i><'col-sm-6'p>>",
              buttons: [{
                      extend: 'copy',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'csv',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'print',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))',
                      }
  
                  },
                  {
                      extend: 'excel',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'pdf',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  }],
  
  
        });
  }
  
   $(document).on('click', '#deleteLeaseTenancyContract', function (e) {
  
      e.preventDefault();
      var id = $(this).data('id');
  
          swal({
            title: "Are you sure?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
          })
          .then((willDelete) => {
            if (willDelete) {
              $.ajaxSetup({
              headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              }
              });
              $.ajax({
                  type: "POST",
                  url: 'deleteLeaseTenancyContract/' + id,
                  success: function (data) {
                  fetch_tenancy_contract();
                  swal("Successfully deleted!", {
                  icon: "success",
                  });
                  }
              });
  
          }
          location.reload();
          });
  
  });
  
   function undertaking(){
       var manageTable = $('#undertaking-visa').DataTable();
      manageTable.destroy();
      manageTable= $("#undertaking-visa").DataTable({
        "ajax": {
                  "url": 'fetchUndertakingVisaAndEid',
                  'type': "GET",
                 },
        lengthMenu: [
                  [10, 25, 50, 100, -1],
                  ['10', '25', '50', '100', 'All']],
                  "responsive": false, "lengthChange": true, "autoWidth": true,
       "order": [[0, 'asc']],
         dom: "<'row'<'col-sm-4'l><'col-sm-4 text-center'B><'col-sm-4'f>>" +
                      "<'row'<'col-sm-12'tr>>" +
                      "<'row'<'col-sm-6'i><'col-sm-6'p>>",
              buttons: [{
                      extend: 'copy',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'csv',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'print',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))',
                      }
  
                  },
                  {
                      extend: 'excel',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'pdf',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  }],
  
  
        });
  
   }
  
    $(document).on('click', '#deleteUndertakingVisaAndEid', function (e) {
  
      e.preventDefault();
      var id = $(this).data('id');
  
          swal({
            title: "Are you sure?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
          })
          .then((willDelete) => {
            if (willDelete) {
              $.ajaxSetup({
              headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              }
              });
              $.ajax({
                  type: "POST",
                  url: 'deleteUndertakingVisaAndEid/' + id,
                  success: function (data) {
                  undertaking();
                  swal("Successfully deleted!", {
                  icon: "success",
                  });
                  }
              });
  
          }
          location.reload();
          });
  
  });
  
  
    function fetchrenwal_booking(){
  
      var manageTable = $('#renewalwal-table').DataTable();
      manageTable.destroy();
      manageTable= $("#renewalwal-table").DataTable({
        "ajax": {
                  "url": 'fetchLeaseRenewalBooking',
                  'type': "GET",
                 },
        lengthMenu: [
                  [10, 25, 50, 100, -1],
                  ['10', '25', '50', '100', 'All']],
                  "responsive": false, "lengthChange": true, "autoWidth": true,
       "order": [[0, 'asc']],
         dom: "<'row'<'col-sm-4'l><'col-sm-4 text-center'B><'col-sm-4'f>>" +
                      "<'row'<'col-sm-12'tr>>" +
                      "<'row'<'col-sm-6'i><'col-sm-6'p>>",
              buttons: [{
                      extend: 'copy',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'csv',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'print',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))',
                      }
  
                  },
                  {
                      extend: 'excel',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  },
                  {
                      extend: 'pdf',
                      exportOptions: {
                          columns: ':visible:not(:eq(11))'
                      }
                  }],
  
  
        });
  }
  
  $(document).on('click', '#deleteLeaseRenewalBooking', function (e) {
  
      e.preventDefault();
      var id = $(this).data('id');
  
          swal({
            title: "Are you sure?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
          })
          .then((willDelete) => {
            if (willDelete) {
              $.ajaxSetup({
              headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              }
              });
              $.ajax({
                  type: "POST",
                  url: 'deleteLeaseRenewalBooking/' + id,
                  success: function (data) {
                  fetchrenwal_booking();
                  swal("Successfully deleted!", {
                  icon: "success",
                  });
                  }
              });
  
          }
          location.reload();
          });
  
  });
  
  function get_renewal_file(id){
       $("#upload_lease_renewal_booking_signed_form").on('submit', function(event){
       event.preventDefault();
          // form.find("button[type='submit']").prop('disabled',true);
  
  $.ajaxSetup({
      headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
  });
  
    $.ajax({
     url:'upload_lease_renewal_booking_signed_file/'+id,
     method:"POST",
     data:new FormData(this),
     dataType:'JSON',
     contentType: false,
     cache: false,
     processData: false,
     success:function(data)
     {
          if (data.success == true) {
  
          $("#upload_lease_booking_signed_fileModal").modal('hide');
          $("#upload_lease_renewal_booking_signed_form")[0].reset();
          fetch_booking();
          }
  
     }
    });
  
           location.reload();
      });
  }
  
  function optTenant(id) {
  // alert(id)
      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
  
          $.ajax({
                method: "GET",
                      url: 'gettenantDetails/' + id,
                      dataType:'JSON',
              success: function (data) {
                  if (data.merital_status) {
                      $("#merital_status").empty();
                  }
                          $('#mobile_number').val(data.mob);
                          $('#email_id').val(data.email);
                          $('#nationality').val(data.nationality);
                          $('#number_of_occupants').val(data.no_occupant);
  if (data.merital_status ) {
    $("#merital_status").append(
                              " <option  value='" + data.merital_status + "'>" + data.merital_status  + "</option>"
                              );
  }
  
                          $('#tenant_id').val(data.id);
                      }
                  });
      }
  
  