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';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';


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

  readonly = false;

  closeResult = '';
  
  page = 1;
  pageSize = 10;
  collectionSize = 0;

  pageListRoom = 1;
  pageSizeListRoom = 10;
  collectionSizeListRoom = 0;

  listEquirmentModel: Equirment[] = [];
  listRoomModel: Room[] = [];
  listItemDetail: ItemDetail[] = [];
  listRoomDetail: RoomDetail[] = [];

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

  myFormRoom: FormGroup;
  myFormEquir: FormGroup;

  ctrl = new FormControl(null, Validators.required);

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

  validate(){
    console.log(this.myFormRoom.controls.userNameForm.status)
    console.log(this.myFormRoom)
    // var form = document.getElementsByClassName('needs-validation')[0] as HTMLFormElement;
    // if (form.checkValidity() === false) {
    //   event.preventDefault();
    //   event.stopPropagation();
    // }
    // form.classList.add('was-validated');
  }

  ngOnInit() {
    this.listEquirmentModel = this.equirmentService.getListEquirment();
    this.listRoomModel = this.roomService.getListRoom();
    this.collectionSizeListRoom = this.listRoomModel.length
    this.collectionSize = this.listEquirmentModel.length
    this.myFormRoom = new FormGroup({
      userNameForm : new FormControl('',[Validators.required]),
      rTelephone : new FormControl('',[Validators.required]),
      email : new FormControl('',[Validators.required, Validators.email]),
      rName : new FormControl('',[Validators.required]),
      rDesc : new FormControl('',[Validators.required]),
      rType : new FormControl('',[Validators.required]),
      sDate : new FormControl('',[Validators.required]),
      eDate : new FormControl('',[Validators.required]),
      sTime : new FormControl('',[Validators.required]),
      eTime : new FormControl('',[Validators.required]),
      Remark : new FormControl(),
      allDay: new FormControl()
    });
    this.myFormEquir = new FormGroup({
      userNameForm : new FormControl('',[Validators.required]),
      eTelephone : new FormControl('',[Validators.required]),
      email : new FormControl('',[Validators.required, Validators.email]),
      eqName : new FormControl('',[Validators.required]),
      eqDesc : new FormControl('',[Validators.required]),
      eqType : new FormControl('',[Validators.required]),
      eqPic  : new FormControl('',[Validators.required]),
      sDate : new FormControl('',[Validators.required]),
      eDate : new FormControl('',[Validators.required]),
      sTime : new FormControl('',[Validators.required]),
      eTime : new FormControl('',[Validators.required]),
      Quantity : new FormControl('',[Validators.required]),
      Remark : new FormControl('',[Validators.required]),
    })
  }


  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.modelItemDetail.sDate = this.modelItemDetail.sDate.split("-")[0]+"/"+this.modelItemDetail.sDate.split("-")[1]+"/"+this.modelItemDetail.sDate.split("-")[2];
    this.modelItemDetail.eDate = this.modelItemDetail.eDate.split("-")[0]+"/"+this.modelItemDetail.eDate.split("-")[1]+"/"+this.modelItemDetail.eDate.split("-")[2];
    this.modelItemDetail.eStatus = 0 ;
    this.ItemDetailService.addItemDetail(this.modelItemDetail);
    this.modalService.dismissAll();
    this.ngOnInit();
  }

  saveBook() {
    this.modelRoomDetail.sDate = this.modelRoomDetail.sDate.split("-")[0]+"/"+this.modelRoomDetail.sDate.split("-")[1]+"/"+this.modelRoomDetail.sDate.split("-")[2];
    this.modelRoomDetail.eDate = this.modelRoomDetail.eDate.split("-")[0]+"/"+this.modelRoomDetail.eDate.split("-")[1]+"/"+this.modelRoomDetail.eDate.split("-")[2];
    this.modelRoomDetail.rStatus = 0 ;
    console.log("save",this.modelRoomDetail)
    this.RoomDetailService.addRoomDetail(this.modelRoomDetail);
    this.modalService.dismissAll();
    this.ngOnInit();
    
  }

}

