import { Component, Input, OnInit } from "@angular/core";
import { CommonModule } from "@angular/common";
import {
  NgbAccordionModule,
  NgbActiveModal,
  NgbDropdownModule,
} from "@ng-bootstrap/ng-bootstrap";
import {
  FormBuilder,
  FormGroup,
  ReactiveFormsModule,
  Validators,
} from "@angular/forms";
import { HelpersService } from "src/app/cores/services/helpers.service";

@Component({
  selector: "app-kerusakan",
  standalone: true,
  imports: [
    CommonModule,
    NgbDropdownModule,
    NgbAccordionModule,
    ReactiveFormsModule,
  ],
  templateUrl: "./kerusakan.component.html",
  styleUrls: ["./kerusakan.component.scss"],
})
export class KerusakanComponent implements OnInit {
  @Input() action = "create";
  @Input() data: any;

  form: FormGroup = {} as FormGroup;

  constructor(
    public activeModal: NgbActiveModal,
    private formBuilder: FormBuilder,
    public helpers: HelpersService
  ) {}

  ngOnInit(): void {
    this.initForm();
  }

  initForm(): void {
    this.form = this.formBuilder.group({
      user_type: ["Retak", [Validators.required]],
      user_level: ["Berat", [Validators.required]],
    });
  }

  onSubmit(): void {
    if (this.form.valid) {
      this.activeModal.close(this.form.value);
    }
  }

  onUserLevel(event: any): void {
    const selectedValue = event.target.value;
    const userLevel = <FormGroup>this.form.controls["user_level"];

    userLevel.patchValue(selectedValue);
  }

  onUserType(event: any): void {
    const selectedValue = event.target.value;
    const userType = <FormGroup>this.form.controls["user_type"];

    userType.patchValue(selectedValue);
  }
}
