import { Component, Input, OnInit, inject } from '@angular/core';
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

import { AlertService } from '../alert/alert.service';
import { FunctionService } from '../models/function.service';
import { SettingsModuleEnum } from '../models/named-parameters.model';
import { EmployeeService } from '../../../providers/employee/employee.service';
import { NgClass } from '@angular/common';
import { GridComponent, CommandColumnComponent, CellTemplateDirective, GridToolbarFocusableDirective, FocusableDirective, EditCommandDirective, ColumnComponent } from '@progress/kendo-angular-grid';

@Component({
  standalone: true,
  selector: 'employee-search',
  templateUrl: './employeesearch.component.html',
  imports: [FormsModule, ReactiveFormsModule, NgClass, GridComponent, CommandColumnComponent, CellTemplateDirective, GridToolbarFocusableDirective, FocusableDirective, EditCommandDirective, ColumnComponent]
})
export class EmployeeSearchComponent implements OnInit
{
  private readonly fnSvc = inject(FunctionService);
  private readonly service = inject(EmployeeService);
  private readonly formBuilder = inject(FormBuilder);
  private readonly alertService = inject(AlertService);
  private readonly activeModal = inject(NgbActiveModal);

  @Input() private fromParent;

  public setting: any;
  public items: any[] = [];
  public errors: string = '';
  public loading: boolean = false;
  public submitted: boolean = false;
  public empSearchForm: FormGroup;

  ngOnInit(): void
  {
    this.empSearchForm = this.formBuilder.group({ criteria: '' });

    this.loadSetting();
  }

  private loadSetting(): void
  {
    this.loading = true;

    this.service.getLabelSetting(SettingsModuleEnum.Employee_Search).subscribe(
      {
        next: (resp: any) =>
        {
          this.loading = false;
          if (resp.returnStatus === 200)
          {
            this.setting = resp;
            this.setValidatorsToControls();
          }
        },
        error: (errorMessage: string) =>
        {
          this.loading = false;
          this.errors = errorMessage;
        }
      });
  }

  get f() { return this.empSearchForm.controls; }

  public clearGrid(): void
  {
    this.items = [];
  }

  public loadEmployees(): void
  {
    this.clearGrid();
    this.submitted = true;
    if (this.empSearchForm.invalid)
      return;

    this.alertService.clear();

    const criteria: string = (this.f.criteria.value || '');
    const empType: number = parseInt(this.fromParent?.employeeType || '1');

    this.service.getEmployeesBySearch(empType, criteria, 8).subscribe(
      {
        next: (resp: any) =>
        {
          this.loading = false;
          if (resp.returnStatus === 200)
          {
            this.items = resp.value || [];
          }
          else
          {
            this.errors = resp.returnMessage;
          }
        },
        error: (errorMessage: string) =>
        {
          this.loading = false;
          this.errors = errorMessage;
        }
      });
  }

  private setValidatorsToControls(): void
  {
    if (this.setting && this.empSearchForm?.controls)
    {
      this.fnSvc.setValidatorsToToControl({ control: this.f.criteria, required: this.setting.label02Req, regExp: this.setting.label02RegExp, maxLen: 50 });
    }
  }

  public okay(dataItem: any): void
  {
    this.activeModal.close(dataItem);
  }

  public close(): void
  {
    this.activeModal.close();
  }
}
