// import { CatalogRolesComponent } from '@/shared/catalog/roles';
import { AbstractFormDialog } from '@/shared/abstractClasses';
import { AddressComponent, BranchCodeComponent, NameComponent } 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 { IComplexRequest, IComplexResponse } from '../../models';
import { ConsumerComplexesService } from '../../services/complexes.service';

@Component({
  selector: 'consumer-complex-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    NameComponent,
    BranchCodeComponent,
    AddressComponent,
    FormFooterActionsComponent,
  ],
})
export class ConsumerComplexFormComponent extends AbstractFormDialog<
  IComplexRequest,
  IComplexResponse
> {
  private readonly service = inject(ConsumerComplexesService);

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

  initForm = () => {
    return this.fb.group({
      name: fieldControl.name(this.initialValues?.name || ''),
      branch_code: fieldControl.branch_code(this.initialValues?.branch_code || ''),
      address: fieldControl.address(this.initialValues?.address || ''),
    });
  };

  form = this.initForm();

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

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

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