import { Component, Input } from "@angular/core";
import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

@Component({
  selector: "app-modal-add-edit",
  templateUrl: "./modal-add-edit.component.html",
  styleUrls: ["./modal-add-edit.component.css"],
})
export class ModalAddEditComponent {
  @Input() newAttribute: any = {};
  myForm: FormGroup;

  constructor(public activeModal: NgbActiveModal, private fb: FormBuilder) {
    this.createForm();
  }

  createForm() {
    this.myForm = this.fb.group({
      name: ['', Validators.required],
      position: ['', Validators.required],
      age: [null, [Validators.required, Validators.min(18)]],
      department: ['', Validators.required]
    });
  }

  addRow() {
    if (this.myForm.valid) {
      this.activeModal.close(this.myForm.value);
    }
  }

  addDepartment() {
    // Example function to simulate adding a department
    let newDept = prompt("Enter new department name:");
    if (newDept) {
      // Normally you would add to a database or a service
      alert(`Department ${newDept} added! (simulated)`);
    }
  }
}
