import { AbstractForm } from '@/shared/abstractClasses';
import { AppCardComponent } from '@/shared/components';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { UikitLabelComponent } from '@/uikit';
import { Component, inject, signal } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { Message } from 'primeng/message';
import { ToggleSwitch } from 'primeng/toggleswitch';
import { IPosConfigRapidInvoicePayload, IPosConfigRapidInvoiceResponse } from './models';
import { PosConfigRapidInvoiceService } from './services/main.service';

@Component({
  selector: 'pos-config-rapid-invoice-form',
  templateUrl: 'form.component.html',
  imports: [
    ReactiveFormsModule,
    FormFooterActionsComponent,
    AppCardComponent,
    Message,
    UikitLabelComponent,
    ToggleSwitch,
  ],
})
export class PosConfigRapidInvoiceFormComponent extends AbstractForm<
  IPosConfigRapidInvoicePayload,
  IPosConfigRapidInvoiceResponse
> {
  private readonly service = inject(PosConfigRapidInvoiceService);

  loading = signal(true);

  initForm = () => {
    const form = this.fb.group({
      rapidInvoice: [false],
    });

    return form;
  };

  form = this.initForm();

  override async ngOnInit() {
    this.loading.set(true);
    const initialValues = await this.service.get();

    this.form.controls.rapidInvoice.setValue(initialValues || false);
    this.loading.set(false);
  }

  override submitForm() {
    const formValue = this.form.value.rapidInvoice as IPosConfigRapidInvoicePayload;
    this.toastService.success({ text: 'تغییرات با موفقیت اعمال شد.' });
    return this.service.submit(formValue);
  }
}
