// import { CatalogRolesComponent } from '@/shared/catalog/roles';
import { AbstractFormDialog } from '@/shared/abstractClasses';
import {
  EconomicCodeComponent,
  FiscalIdComponent,
  GuildIdComponent,
  NameComponent,
  PartnerTokenComponent,
} from '@/shared/components';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { fieldControl } from '@/shared/constants/fields';
import { Component, inject, Input } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { IBusinessActivityRequest, IBusinessActivityResponse } from '../../models';
import { AdminConsumerBusinessActivitiesService } from '../../services/businessActivities.service';

@Component({
  selector: 'superAdmin-consumer-businessActivities-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    NameComponent,
    EconomicCodeComponent,
    FiscalIdComponent,
    PartnerTokenComponent,
    GuildIdComponent,
    FormFooterActionsComponent,
  ],
})
export class ConsumerBusinessActivitiesFormComponent extends AbstractFormDialog<
  IBusinessActivityRequest,
  IBusinessActivityResponse
> {
  private readonly service = inject(AdminConsumerBusinessActivitiesService);

  @Input({ required: true }) consumerId!: string;
  @Input() businessActivityId!: string;

  initForm = () => {
    return this.fb.group({
      name: fieldControl.name(this.initialValues?.name || ''),
      economic_code: fieldControl.economic_code(this.initialValues?.economic_code || ''),
      fiscal_id: fieldControl.fiscal_id(this.initialValues?.fiscal_id || ''),
      partner_token: fieldControl.partner_token(this.initialValues?.partner_token || ''),
      // @ts-ignore
      guild_id: fieldControl.guild_id(this.initialValues?.guild?.id || ''),
    });
  };

  form = this.initForm();

  get preparedTitle() {
    return `${this.editMode ? 'ویرایش' : 'افزودن'} فعالیت اقتصادی`;
  }

  override ngOnChanges(): void {
    this.form = this.initForm();
  }

  submitForm() {
    const formValue = this.form.value as IBusinessActivityRequest;
    if (this.editMode) {
      return this.service.update(this.consumerId, this.businessActivityId, formValue);
    }
    return this.service.create(this.consumerId, formValue);
  }
}
