import { Component, OnInit } from '@angular/core';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { Equirment } from 'src/app/models/equirment.model';
import { ItemDetail } from 'src/app/models/ItemDetail.model';
import { RoomDetail } from 'src/app/models/RoomDetail.model';
import { MyRoom, Room } from 'src/app/models/rooms.model';
import { EquirmentService } from 'src/app/service/equirment.service';
import { ItemDetailService } from 'src/app/service/item-detail.service';
import { RoomService } from 'src/app/service/room.service';
import { RoomDetailService } from 'src/app/service/room-detail.service';


@Component({
  selector: 'app-equipment',
  templateUrl: './equipment.component.html',
  styleUrls: ['./equipment.component.scss']
})
export class EquipmentComponent implements OnInit {
  time = { hour: 13, minute: 30 };
  page = 1;
  pageSize = 10;

  closeResult = '';

  listEquirmentModel: Equirment[] = [];
  listRoomModel: Room[] = [];
  collectionSize = this.listEquirmentModel.length;

  modelEquirment = new Equirment();
  modelRoom = new MyRoom();
  modelItemDetail = new ItemDetail();
  modelRoomDetail = new RoomDetail();

  constructor(private modalService: NgbModal, private equirmentService: EquirmentService, private roomService: RoomService, private ItemDetailService : ItemDetailService, private RoomDetailService : RoomDetailService) {

  }

  ngOnInit() {
    this.listEquirmentModel = this.equirmentService.getListEquirment();
    this.listRoomModel = this.roomService.getListRoom();
  }


  Eopen(contentequirment) {
    this.modelEquirment = new Equirment();
    this.modalService.open(contentequirment, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  
  bookEquir(Lend,item : Equirment) {

    this.modelItemDetail = new ItemDetail();
    this.modelItemDetail.equirment = item ;
    console.log("item",this.modelItemDetail);

    this.modalService.open(Lend, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  bookRoom(Room,item : Room) {

    this.modelRoomDetail = new RoomDetail();
    this.modelRoomDetail.room = item ;
    console.log("room",this.modelRoomDetail);

    this.modalService.open(Room, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  Ropen(contentroom) {
    this.modelRoom = new MyRoom();
    this.modalService.open(contentroom, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }


  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return `with: ${reason}`;
    }
  }
  refreshCountries() {
    this.listEquirmentModel = this.listEquirmentModel
    this.listRoomModel = this.listRoomModel
      .map((item, i) => ({ id: i + 1, ...item }))
      .slice((this.page - 1) * this.pageSize, (this.page - 1) * this.pageSize + this.pageSize);
  }

  NgbdTimepickerBasic() {

  }

  saveEquirment() {

    this.equirmentService.addEquirment(this.modelEquirment);
    this.modalService.dismissAll();
    this.ngOnInit();

  }

  saveRoom() {

   this.roomService.addRoom(this.modelRoom);
   this.modalService.dismissAll();
   this.ngOnInit();
   
  }

  saveLend() {
    this.ItemDetailService.addItemDetail(this.modelItemDetail);
    this.modalService.dismissAll();
    this.ngOnInit();
  }

  saveBook() {
    this.RoomDetailService.addRoomDetail(this.modelRoomDetail);
    this.modalService.dismissAll();
    this.ngOnInit();
  }
}

