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 { 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 Room();
  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(contentequirment,item : Equirment) {

    this.modelitemDetail = new itemDetail();
    this.modelitemDetail.equirment = item ;
    console.log("item",this.modelitemDetail);

    this.modalService.open(contentequirment, { 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 Room();
    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();
    console.log(this.itemDetailService);
  }

  saveBook() {
    this.roomDetailService.addroomDetail(this.modelroomDetail);
    this.modalService.dismissAll();
    this.ngOnInit();
    console.log(this.roomDetailService);
  }
}

