// import { CatalogRolesComponent } from '@/shared/catalog/roles';
import { AbstractForm } from '@/shared/abstractClasses';
import { AddressComponent, BranchCodeComponent, NameComponent } from '@/shared/components';
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 { PartnerComplexesService } from '../../services/complexes.service';

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

  @Input({ required: true }) consumerId!: string;
  @Input() 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.consumerId,
        this.businessActivityId,
        this.complexId,
        formValue,
      );
    }
    return this.service.create(this.consumerId, this.businessActivityId, formValue);
  }
}
