import {Component, ViewChild} from "@angular/core";
import {WpPaginator} from "../sharedmodules/wp-elements/Controls/WpTable/WpPaginator";
import {DeleteDialog} from "../sharedmodules/controls/NonAngularDialogs/DeleteDialog";
import {WpJsonApi} from "../sharedmodules/wp-elements/Services/WpJsonApi";
@Component({
selector:'app-root',
template:`
`
})
export class InvoiceComponent{
IsLoading:boolean=false;
Filter:string='';
FilterStatus:string='-1';
FilterToUse:string='';
Data:Invoice[];
CurrentPage=0;
@ViewChild('bottomPaginator') private bottomPaginator:WpPaginator;
@ViewChild('topPaginator') private topPaginator:WpPaginator;
constructor(private ajax:WpJsonApi){
}
ngOnInit(): void {
this.ExecuteSearch(0);
}
private async ExecuteSearch(pageNumber:number) {
this.IsLoading=true;
let result=await this.ajax.Post<{count:number,rows:any[]}>('invoices/getlist',{
Page:pageNumber,
Filter:this.FilterToUse,
Size:20,
FilterStatus:this.FilterStatus
});
this.IsLoading=false;
if(result!=null) {
this.Data = result.rows;
this.CurrentPage=pageNumber;
this.SetPaginatorData(result.count,(result.count>0?Math.floor(result.count/20)+1:0),this.CurrentPage);
}else{
this.Data=[];
this.SetPaginatorData(0,0,0);
}
}
private SetPaginatorData(count:number,numberOfPages:number,currentPage:number)
{
this.bottomPaginator.Count=count;
this.bottomPaginator.CurrentPage=currentPage;
this.bottomPaginator.NumberOfPages=numberOfPages;
this.topPaginator.Count=count;
this.topPaginator.CurrentPage=currentPage;
this.topPaginator.NumberOfPages=numberOfPages;
}
private PageChanged(pageNumber:number)
{
this.ExecuteSearch(pageNumber);
}
FilterBox() {
this.FilterToUse=this.Filter;
this.ExecuteSearch(0);
}
CreateInvoice() {
window.location.href='?page=rednao_invoice_on_the_go&invoice_id=0';
}
EditInvoice(row:Invoice) {
window.location.href='?page=rednao_invoice_on_the_go&invoice_id='+row.InvoiceId;
}
async DeleteInvoice(row:Invoice) {
if(await DeleteDialog.Show(row.FormattedInvoiceNumber))
{
this.IsLoading=true;
let result=await this.ajax.Post('/invoices/delete',{InvoiceId:row.InvoiceId});
this.IsLoading=false;
if(result!=null)
{
this.Data.splice(this.Data.indexOf(row),1);
}
}
}
}