import {Component, EventEmitter, Output, ViewChild} from "@angular/core";
import {BasicDialog} from "../sharedmodules/controls/Dialog/BasicDialog";
import {WpJsonApi} from "../sharedmodules/wp-elements/Services/WpJsonApi";
import {NgForm} from "@angular/forms";
import {ToastService} from "../sharedmodules/wp-elements/Services/ToastService";
@Component({
selector:'service-dialog',
template:`
`
})
export class ServiceDialogComponent {
@ViewChild('dialog') public dialog:BasicDialog;
@Output() public SavingStarted = new EventEmitter();
@Output() public SavingEnded = new EventEmitter();
@ViewChild('dialogNgForm') private _formDialog:NgForm;
public Taxable=rnparams.settings.TaxesOptions.Enable;
public TaxType=rnparams.settings.TaxesOptions.TaxType!=null?rnparams.settings.TaxesOptions.TaxType:'';
constructor(public ajax:WpJsonApi,public toast:ToastService){
}
public ServiceToEditOrCreate:Service={
ServiceId:0,
Rate:0,
Name:'',
Taxable:false,
TaxRate:0,
Notes:''
};
public Open(service:Service){
this.UndirtyFields();
this.ServiceToEditOrCreate=service;
this.dialog.Open();
}
async SaveNewService() {
this.MarkAllFieldsAsDirty();
if(this._formDialog.valid) {
this.dialog.IsLoading = true;
let result:Service=await this.ajax.Post('service/save',this.ServiceToEditOrCreate);
this.dialog.IsLoading=false;
if(result!=null) {
this.dialog.Close();
this.toast.SendSuccess('Service saved successfully');
this.SavingEnded.emit(result);
}else{
this.SavingEnded.emit(null);
}
}
}
private UndirtyFields(){
for(let property in this._formDialog.controls)
{
this._formDialog.controls[property].markAsPristine();
}
}
private MarkAllFieldsAsDirty() {
for(let property in this._formDialog.controls)
{
this._formDialog.controls[property].markAsDirty();
}
}
}