var month = '';
var year = '';

$(document).ready(function(){
  firstLoad();

  $("#searchForm").on("submit",function(e){
    e.preventDefault();        
    
    month = $("#month").val();
    year = $("#year").val();

    if(month !== '' || year !== ''){
      var page = 1;
  
      $.ajax({
        type:'POST',
        data:{month : month,
              year : year,
              page : page},
        url:CI_ROOT+"tenders/getSearchResults",
        dataType: "json",
        beforeSend:function(){
          $("#preloader").show(); 
          $("#status").show(); 
        },
        success:function(result){
          if(result.status == "success"){
            $("#tenderData").html('');
            $("#paginationLink").html('');
            // $("#total").html(result.total+' Members Found');
            $("#tenderData").html(result.data);
            $("#paginationLink").html(result.pagination);
          }else if(result.status == "fail"){
            $("#tenderData").html('Data not available');
            $("#paginationLink").html('');
            // $("#total").html('0 Members Found');
          }
    
          $("#preloader").hide(); 
          $("#status").fadeOut();
        }
      });
    }
  });

  $("input[type='reset']").closest('form').on('reset', function(event) {
    month = '';
    year = '';
    firstLoad();
  });
});

function firstLoad(){
  var page = '1';
  // $("#total").html('Please wait ....');
  
  $.ajax({
    type:'POST',
    data:{page : page},
    url:CI_ROOT+"tenders/getTendersData",
    dataType: "json",
    beforeSend:function(){
      $("#preloader").show(); 
      $("#status").show(); 
    },
    success:function(result){
      if(result.status == "success"){
        $("#tenderData").html('');
        $("#paginationLink").html('');
        // $("#total").html(result.total+' Members Found');
        $("#tenderData").html(result.data);
        $("#paginationLink").html(result.pagination);
      }else if(result.status == "fail"){
        $("#tenderData").html('Data not available');
        $("#paginationLink").html('');
        // $("#total").html('0 Members Found');
      }

      $("#preloader").hide(); 
      $("#status").fadeOut();
    }
  });
}

function paginate(page,e){
  // $("#total").html('Please wait ....');

  if(page == "prev"){
    page = $(".activetxt").text();
    page = page - 1;
  }else if(page == "next"){
    page = $(".activetxt").text();
    page = page + 1;
  }

  $.ajax({
    type:'POST',
    data:{page : page},
    url:CI_ROOT+"tenders/getTendersData",
    dataType: "json",
    beforeSend:function(){
      $("#preloader").show(); 
      $("#status").show(); 
    },
    success:function(result){
      if(result.status == "success"){
        $("#tenderData").html('');
        $("#paginationLink").html('');
        // $("#total").html(result.total+' Members Found');
        $("#tenderData").html(result.data);
        $("#paginationLink").html(result.pagination);
      }else if(result.status == "fail"){
        $("#tenderData").html('');
        $("#paginationLink").html('');
        // $("#total").html('0 Members Found');
      }

      $("#preloader").hide(); 
      $("#status").fadeOut();
    }
  });
}

function searchPaginate(page,e){
  month = $("#month").val();
  year = $("#year").val();

  // $("#total").html('Please wait ....');

  if(page == "prev"){
    page = $(".activetxt").text();
    page = page - 1;
  }else if(page == "next"){
    page = $(".activetxt").text();
    page = page + 1;
  }

  $.ajax({
    type:'POST',
    data:{month:month,
    year:year,  
    page : page},
    url:CI_ROOT+"tenders/getSearchResults",
    dataType: "json",
    beforeSend:function(){
      $("#preloader").show(); 
      $("#status").show(); 
    },
    success:function(result){
      if(result.status == "success"){
        $("#tenderData").html('');
        $("#paginationLink").html('');
        // $("#total").html(result.total+' Members Found');
        $("#tenderData").html(result.data);
        $("#paginationLink").html(result.pagination);
      }else if(result.status == "fail"){
        $("#tenderData").html('');
        $("#paginationLink").html('');
        // $("#total").html('0 Members Found');
      }

      $("#preloader").hide(); 
      $("#status").fadeOut();
    }
  });
}