import { AbstractFormDialog } from '@/shared/abstractClasses';
import { CodeComponent, FieldInvoiceTemplatesComponent, 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';
import { Component, inject, Input } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { IGuildRequest, IGuildResponse } from '../models';
import { GuildsService } from '../services/main.service';

interface T {
  name: string;
  code: string;
}

@Component({
  selector: 'admin-guild-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    FormFooterActionsComponent,
    NameComponent,
    CodeComponent,
    FieldInvoiceTemplatesComponent,
  ],
})
export class GuildFormComponent extends AbstractFormDialog<IGuildRequest, IGuildResponse> {
  @Input() guildId?: string;
  private service = inject(GuildsService);

  form = this.fb.group({
    name: fieldControl.name(this.initialValues?.name, true),
    code: fieldControl.code(this.initialValues?.code, true),
    invoice_template: fieldControl.invoice_template(this.initialValues?.invoice_template.id, true),
  });

  override ngOnChanges() {
    this.form.patchValue(this.initialValues ?? {});
    if (this.editMode && !this.guildId) {
      throw 'missing some arguments';
    }
  }

  initForm() {
    // const form = this.fb.group({
    //   name: [
    //     { value: this.initialValues?.name, disabled: !this.initialValues?.name },
    //     Validators.required,
    //   ],
    //   code: [
    //     { value: this.initialValues?.code, disabled: this.submitLoading() },
    //     Validators.required,
    //   ],
    // });
    // this.code = form.get<string>('code')!.valueChanges.pipe(
    //   tap((code) => {
    //     const nameControl = form.get('name');
    //     code === '1234' ? nameControl?.disable() : nameControl?.enable();
    //   }),
    // );
    // return form;
  }

  override submitForm(payload: IGuildRequest) {
    if (this.editMode) {
      return this.service.update(this.guildId!, payload);
    }
    return this.service.create(payload);
  }
}
