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 { RoomService } from 'src/app/service/room.service';
import { roomDetailService } from 'src/app/service/room-detail.service';
import { FormBuilder, FormGroup } from '@angular/forms';

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

  closeResult = '';

  listEquirmentModel: Equirment[] = [];
  listRoomModel: Room[] = [];
  listitemDetail: itemDetail[] = [];
  listroomDetail: roomDetail[] = [];

  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 roomDetailService: roomDetailService, private fb: FormBuilder
  ) {
    this.myForm = this.fb.group({
      firstName: '',
      lastName: '',
      email: '',
      phone: '',
      address: this.fb.group({
        address1: '',
        address2: '',
        province: '',
        postalCode: ''
      })
    });
    this.myForm.valueChanges.subscribe(console.log);
  }

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

  bookEquir(editLend, item: Equirment) {

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

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

  bookRoom(editroom, item: Room) {

    this.modelroomDetail = new roomDetail();
    this.modelroomDetail.room = item;
    console.log("room", this.modelroomDetail);

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

  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)}`;
    });
  }

  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)}`;
    });
  }

  openItemDetail(content: string, item: itemDetail) {
    this.modelitemDetail = item;
    console.log(item);
    this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  openRoomDetail(content: string, item: roomDetail) {
    this.modelroomDetail = item;
    console.log(item);
    this.modalService.open(content, { 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();

  }

  editRoom() {
    this.roomDetailService.editroomDetail(this.modelroomDetail);
    this.modalService.dismissAll();
    this.ngOnInit();
  }
}
