import { AbstractForm } from '@/shared/abstractClasses';
import { AppCardComponent, UnitPriceComponent } from '@/shared/components';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { fieldControl } from '@/shared/constants';
import { Component, inject, signal } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { Message } from 'primeng/message';
import { IPosConfigGoldPricePayload, IPosConfigGoldPriceResponse } from './models';
import { PosConfigGoldPriceService } from './services/main.service';

@Component({
  selector: 'pos-config-gold-price-form',
  templateUrl: 'form.component.html',
  imports: [
    ReactiveFormsModule,
    FormFooterActionsComponent,
    AppCardComponent,
    Message,
    UnitPriceComponent,
  ],
})
export class PosConfigGoldPriceFormComponent extends AbstractForm<
  IPosConfigGoldPricePayload,
  IPosConfigGoldPriceResponse
> {
  private readonly service = inject(PosConfigGoldPriceService);

  loading = signal(true);

  initForm = () => {
    const form = this.fb.group({
      price: fieldControl.unit_price(),
    });

    return form;
  };

  form = this.initForm();

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

    this.form.controls.price.setValue((initialValues || 0) + '');
    this.loading.set(false);
  }

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