// import { CatalogRolesComponent } from '@/shared/catalog/roles';
import { AbstractFormDialog } from '@/shared/abstractClasses';
import { CatalogDeviceBrandSelectComponent } from '@/shared/catalog/deviceBrands';
import { NameComponent } from '@/shared/components';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { Component, inject, Input } from '@angular/core';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { IDeviceRequest, IDeviceResponse } from '../models';
import { SuperAdminDeviceService } from '../services/main.service';

@Component({
  selector: 'superAdmin-device-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    FormFooterActionsComponent,
    CatalogDeviceBrandSelectComponent,
    NameComponent,
  ],
})
export class UserComplexFormComponent extends AbstractFormDialog<IDeviceRequest, IDeviceResponse> {
  private readonly service = inject(SuperAdminDeviceService);

  @Input() deviceId?: string;

  initForm = () => {
    return this.fb.group({
      name: [this.initialValues?.name || '', [Validators.required]],
      brand_id: [this.initialValues?.brand.id || '', [Validators.required]],
    });
  };

  form = this.initForm();

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

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

  submitForm() {
    const formValue = this.form.value as IDeviceRequest;
    if (this.editMode) {
      return this.service.update(this.deviceId!, formValue);
    }
    return this.service.create(formValue);
  }
}
