var trans_type_filter='All', balance_type_filter='All', date_filter='All';
tenants.prototype.transactions = function () {
  var user_id = this.user_id; /* This defines the User for AJAX Save */
  var the_tenant = this.id;
  var the_filter = this.filter;
  var the_tab = this.tab;
  var lease_id = this.lease_id;
  
    $('#allTransactions').tablesort();
/*
 * 12-11-2018 Old code to Dim or hide on data table
    $("#right-secondary-menu").off("hover");  
    $('#right-secondary-menu').hover(function() {
      //$('#allTransactions').hide();
//      $('#allTransactions').css('opacity', '0.3');
//      $('#allTransactions').dimmer({opacity: 0.3}).dimmer('show');
    });
    
    $("#right-secondary-menu").off("mouseleave");  
    $('#right-secondary-menu').mouseleave(function() {
      $('#allTransactions').show();
//      $('#allTransactions').css('opacity', '1');
//      $('#allTransactions').dimmer('hide');
    });
*/
    $(document).off("click", ".createTransaction");
    $(document).on("click", ".createTransaction", function () {
      var transType = $(this).attr('data-transType');
      var transBal = $(this).attr('data-transBal');
      router.navigate('Tenants/' + the_filter + '/' + the_tenant + '/Transactions/'+lease_id+'/NewTransaction/'+transType+'/'+transBal+'/New');
	});

    $(document).off("click", ".trasactionItem");
    $(document).on("click", ".trasactionItem", function () {
      var transID = $(this).data("id");
      var transType = $(this).data('transType');
      var transBal = $(this).data('transBal');
      router.navigate('Tenants/' + the_filter + '/' + the_tenant + '/Transactions/'+lease_id+'/ViewTransaction/'+transID+'/View');
    });

    $(".filter-TransType").off("click");
    $(".filter-TransType").on("click", function () {
      $(".iTransType").each(function() {
        $(this).removeClass('check');
        $(this).addClass('lighter');
      });
     
      trans_type_filter = $(this).attr('data-filterTransType');
      
      $("#i"+trans_type_filter).addClass('check');
      $("#i"+trans_type_filter).removeClass('lighter');
      
      view_trans(lease_id);
    });
    
    $(".filter-TransBal").off("click");
    $(".filter-TransBal").on("click", function () {
      $(".iTransBal").each(function() {
        $(this).removeClass('check');
        $(this).addClass('lighter');
      });
     
      balance_type_filter = $(this).attr('data-filterBalType');
      
      $("#ib"+balance_type_filter).addClass('check');
      $("#ib"+balance_type_filter).removeClass('lighter');
      
      view_trans(lease_id);
    });

    $(".filter-transDates").off("click");
    $(".filter-transDates").on("click", function () {
      $(".itransDates").each(function() {
        $(this).removeClass('check');
        $(this).addClass('lighter');
      });
      
      date_filter = $(this).attr('data-transDate');
      
      $("#ic"+date_filter).addClass('check');
      $("#ic"+date_filter).removeClass('lighter');
      
      view_trans(lease_id);
    });
};

function view_trans(lease_id) {
  var view_params = { lease_id: lease_id, trans_type_filter: trans_type_filter, balance_type_filter: balance_type_filter, date_filter: date_filter };
  $.get("app/tenants/view-transactions.php", view_params, function (data) {
    $("#transactionHistory").html(data);
  });
}


/* Note this function is called printLedger as it is needed by an on CLick Event */
function print_ledger() {
  var lease_id = $('#lease_id').val(); /* from hidden input on tab-tran..... */
  var frame_id = 'printTransReport';
  var src = 'app/tenants/print-transactions.php?lease_id=' + lease_id + '&trans_type_filter=' + trans_type_filter + '&balance_type_filter=' + balance_type_filter + '&date_filter=' + date_filter;
  debug_print(src);
  print_frame(frame_id, src);
}