var url = $('meta[name="base-url"]').attr('content');
 $.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});


$(document).ajaxStart(function() {
  $("#loading").fadeIn('200');
}).ajaxStop(function() {
  $("#loading").fadeOut('200');
});

function BureauChange(newBureau, selectedTab)
     {
        $('#Search_Bureau').val(newBureau);  //update hidden search object for ajax cals
        $(".bureauTabs.nav.nav-bar li a").removeClass('active');   //set the selected div
        selectedTab.className += " active";

        //change text of dipuste button
        if (newBureau == '1') {
            document.getElementById('btnDispute').innerHTML = "Create Transunion Dispute";
        }
        else if (newBureau == '2') {
            document.getElementById('btnDispute').innerHTML = "Create Experian Dispute";
        } 
        else if (newBureau == '3') {
            document.getElementById('btnDispute').innerHTML = "Create Equifax Dispute";
        }

        $.ajax({
        	type:'get',
        	url:url+'/setDisputeSession/'+ newBureau,
        	success:function()
        	{

        	}
        });
    }

function refreshAccounts(openAccount) 
{
	var check = [];
	var dispute = [];
  	$.each($(".AccountStatus:checked"), function(){
        check.push($(this).val());
    });

    $.each($(".DisputeStatus:checked"), function(){
    	dispute.push($(this).val());
    });

  
	$.ajax({
		type:'post',
		url:url+'/getAccountSummary' ,
		data:{'id':$('#Search_Bureau').val(),'status':check,'dispute':dispute},
		dataType:'json',
		success:function(res)
		{
			// console.log(res);
			var acc = '';
			$('.tranCount').text(res.length);
			if(res.length>0)
			{
				for(var i=0;i<res.length;i++)
				{
				 	var acc_status = '';
				 	var color = '';
				 	var open_date = '';
				 	var table = '';

				 	if(res[i].dateOpened!=null)
				 	{
				 		open_date = new Date(res[i].dateOpened);
				 		open_date = open_date.getDate()  + '/'+ (open_date.getMonth()+1) + '/' + open_date.getFullYear(); 
				 	}
				 	var account = '';
				 	if(res[i].accountNumber!=null)
				 	{
					 	account = res[i].accountNumber.toString();
					 	account = account.replace(/\d{4}$/, '****');				 		
				 	} 

				 	var tr = '';
				 	if(res[i].disputLetter!=null && res[i].disputLetter!='')
		 			{
		 				var data = res[i].disputLetter;
		 				for(var j=0;j<data.length;j++)
		 				{
			 				var dl_date = new Date(data[j].created);
			 				dl_date = (dl_date.getMonth()+1)+ '/' + dl_date.getDate() + '/' + dl_date.getFullYear();
			 				if(data[j].status==1){var dl = 'Pending';}else if(data[j].status==2){var dl = 'Sent';}else if(data[j].status==3){var dl = 'Completed';}
			 				tr += '<tr>'+
					                	'<td data-label="Dispute" style="width:10%;"> Round '+data[j].round+'</td>'+
					                	'<td data-label="'+dl+'" style="width:20%;">'+
						                    '<div class="d-flex align-items-center">'+
						                        '<span>'+dl_date+'</span>'+
						                        '<a class="btn-border btn btn-icon btn-xs ml-2" data-class="dynamic-class" data-content="Dispute letters sent to credit bureaus and results have been received. " data-placement="top" data-toggle="popover" data-trigger="hover" href="javascript:;" title="" rel="popover" data-original-title="Completed"><i class="fa fa-info"></i></a>'+
						                    '</div>'+
						                '</td>'+
					                	'<td data-label="Reason" style="width:20%;"><span>'+data[j].subject+'</span></td>'+
					                	'<td class="font-weight-normal"> '+
					                		'<span>'+data[j].reason+'</span>'+
					                	'</td>'+
					            	'</tr>';
		 				}
		 			}

				 	if(res[i].AccountCondition_abbreviation=='Open' && res[i].AccountCondition_symbol=='O')
				 	{
				 		acc_status = 'Positive';
				 		color = 'success';
				 		table = '<div class="row table-data">'+
							    	'<div class="col-md-6">'+
							        	'<table class="table-sm table-striped table-data__content">'+
							            	'<tbody>'+
								            	'<tr>'+
								                	'<th>Account #</th>'+
								                	'<td>'+account+'</td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Status</th>'+
								                	'<td>'+res[i].AccountCondition_abbreviation+'</td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Credit Limit</th>'+
								                	'<td> $'+res[i].GrantedTrade_CreditLimit+'  </td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>High Balance</th>'+
								                	'<td>$'+res[i].highBalance+'  </td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Payment Amount</th>'+
								                	'<td> $'+res[i].GrantedTrade_monthlyPayment+'</td>'+ 
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Last Payment</th>'+
								                	'<td>6/26/2020  </td>'+
								            	'</tr>'+
								              	'<tr>'+
								                	'<th>Term</th>'+
								                	'<td>'+res[i].GrantedTrade_termMonths+'</td>'+
								            	'</tr>'+
								            	'<tr>'+
						                			'<th>Description</th>'+
				                					'<td>'+res[i].AccountDesignator_description+'</td>'+
				            					'</tr>'+  
				        					'</tbody>'+
				        				'</table>'+
				    				'</div>'+
								    '<div class="col-md-6 table-data__table">'+
								        '<table class="table-sm table-striped table-data__content">'+
								           '<tbody>'+
									           	'<tr>'+
									                '<th> Open Date</th>'+
									                '<td>'+open_date+'</td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th> Closed Date</th>'+
									                '<td>   </td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Last Reported</th>'+
									                '<td>7/2/2020   </td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Days Late</th>'+
									                '<td> 30: <span class=""> '+res[i].GrantedTrade_late90Count+' </span>'+
									                	'| 60: <span class=" "> '+res[i].GrantedTrade_late60Count+' </span>'+
									                	'| 90: <span class=" "> '+res[i].GrantedTrade_late30Count+' </span>'+ 
									                '</td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Payment Status</th>'+
									                '<td class=" ">'+res[i].PayStatus_abbreviation+'</td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Past Due</th>'+
									                '<td class=" "> $'+res[i].GrantedTrade_amountPastDue+'</td>'+
									            '</tr>'+
									          	'<tr>'+
									                '<th>Worst Rating</th>'+
									                '<td class=" "> </td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Remarks</th>'+
									                '<td class=" ">'+res[i].remark_description+'</td>'+
									           	'</tr>'+           
			        						'</tbody>'+
			        					'</table>'+
			    					'</div>'+
								'</div>';
				 	}
				 	else if(res[i].AccountCondition_abbreviation=='Derog' && res[i].AccountCondition_symbol=='F')
				 	{
				 		acc_status = 'Negative';
				 		color = 'danger';
				 		table = '<div class="row table-data">'+
								    '<div class="col-md-6">'+
								        '<table class="table-sm table-striped table-data__content">'+
								            '<tbody>'+
								            	'<tr> '+
								            		'<th>Account #</th>'+
								            		'<td>'+account+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Status</th>'+
								            		'<td>'+res[i].AccountCondition_abbreviation+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Original Creditor</th>'+
								            		'<td>CONWAY HOSPITAL</td> '+
								            	'</tr>'+
								            	'<tr>'+
								            		'<th>Creditor Type</th>'+
								            		'<td>Collection Services</td> '+
								            	'</tr>'+
								        	'</tbody>'+
								    	'</table>'+
								    '</div>'+
								    '<div class="col-md-6 table-data__table">'+
								        '<table class="table-sm table-striped table-data__content">'+
								            '<tbody>'+
								            	'<tr> '+
								            		'<th>High Balance</th>'+
								            		'<td>$'+res[i].highBalance+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Open Date</th>'+
								            		'<td>'+open_date+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Last Reported</th>'+
								            		'<td>6/24/2018  </td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Remarks</th>'+
								            		'<td>'+res[i].remark_description+'</td> '+
								            	'</tr>'+
									        '</tbody>'+
									    '</table>'+
								    '</div>'+
								'</div>'+
								'<table class="table-accordion__footer">'+
						            '<tbody>'+tr+'</tbody>'+
						        '</table>';
				 	}


					var dis_status = '';
					var round = 'N/A';
				 	if(res[i].dispute_letter!=null && res[i].dispute_letter!='')
				 	{
				 	 	round = 'Round '+res[i].dispute_letter.round;
				 		if(res[i].dispute_letter.status==1){dis_status='Pending';}else if(res[i].dispute_letter.status==2){dis_status='Sent';}else if(res[i].dispute_letter.status==3){dis_status='Completed';}
				 	}

				  acc+='<tr class="table-accordion__header tr--'+color+'">'+
							'<td data-label="Credit Card " class="text-uppercase label--">'+res[i].creditorName+'</td>'+
							'<td data-label="BALANCE">$'+res[i].currentBalance+'</td>'+
							'<td data-label="ACCOUNT STATUS" class="text-uppercase">'+acc_status+'</td>'+
		                	'<td class="d-none  btnDisputeReason">'+
		                        '<button type="button" class="btn btn-link p-0 btn-font--wider" onclick="openDisputeReasonPopUp(\''+res[i].trans_id+'\',\''+res[i].bureau+'\',"", 0,edit); ">'+
		                            'Add Dispute Reason'+
		                        '</button>'+
		                	'</td>'+
		                    '<td class="d-none  btnRemoveFromDispute">'+
		                        '<button class="btn btn-link btn-gray btn-icon" onclick="RemoveAccountFromDispute(\''+res[i].bureau+'\', \''+res[i].trans_id+'\', "")" type="button"><i class="fa fa-trash" aria-hidden="true"></i></button>'+
		                    '</td>'+
		                    '<td data-label="DISPUTE STATUS" class="  ">'+round+' '+ dis_status+'</td>'+
		                    '<td>'+
		                        '<div class="table-accordion__icon collapsed" data-toggle="collapse" href="#collapse-Accounts-'+i+'" aria-controls="#collapse-Accounts-'+i+'">'+
		                            '<i class="fa fa-plus"></i>'+
		                            '<i class="fa fa-minus"></i>'+
		                        '</div>'+
		                	'</td>'+
		                '</tr>'+
		                '<tr class="table-accordion__content">'+
		                    '<td colspan="7">'+
		                        '<label class="checkbox-on" for="check-Accounts-'+i+'"> </label>'+
		                        '<div name="DIV_'+res[i].trans_id+'" class="collapse table-accordion__card table-accordion__card--'+color+'" id="collapse-Accounts-'+i+'">'+
									table +
		                        '</div>'+
		                    '</td>'+
		                '</tr>';
				}
			}
			$('#actb').html(acc);
		}
	});
}

function mobileAllSelected(group) 
{
    if (document.getElementById("AllPill-" + group).checked) {
        document.getElementById("AllPill-label-" + group).innerHTML = ""
        document.getElementById("checkbox-label-" + group + "-All").checked = true;
        FilterUpdateSelected(group, "All")
    } else {
        document.getElementById("AllPill-label-" + group).innerHTML = "Clear Filters";
        document.getElementById("checkbox-label-" + group + "-All").checked = false;
        FilterUpdateSelected(group, "All")
    }

}

function FilterUpdateSelected(group, labelValue) {
    var itemsInGroup = $("." + group);
    var allItem;
    itemsInGroup.each(function () {
        var item = this;
        $.each(this.attributes, function () {
            if (this.name == 'labelvalue') {
                if (this.value == "All") {
                    //allItem.checked = false;
                    allItem = item;
                }
            }
        });
    });

    //when all is selected unselect everything in that group
    if (labelValue == "All") {
        itemsInGroup.prop("checked", false)
        allItem.checked = false; //recheck the all
        //update the text of label for mobile to say All Selected
         $(".check-all #AllPill-label-" + group ).text('') //for mobile update 
       
    } else {
        //when anything but all is selected unselect all
        allItem.checked = false;
         $(".check-all #AllPill-label-" + group ).text('Clear Filters') //for mobile update 
        
    }

    //if nothing is checked in the group recheck all
    if ($('.' + group + ':checked').length == 0) {
        allItem.checked = true;
        $(".check-all #AllPill-label-" + group ).text('') //for mobile update 
    }
}

$(document).ready(function(){
	localStorage.setItem('dispute_letter','');
});