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;

  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.equirmentService.getListEquirment().subscribe( result => {
      this.listEquirmentModel = result;
    });
    this.roomService.getListRoom().subscribe( result => {
      this.listRoomModel = result;
    });
    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]),
      sDate : new FormControl('',[Validators.required]),
      eDate : new FormControl(''),
      sTime : new FormControl(''),
      eTime : new FormControl(''),
      Quantity : new FormControl('',[Validators.required]),
      remark : new FormControl(''),
    })
  }


  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.equirments = item ;

    this.myFormEquir = new FormGroup({
      userNameForm : new FormControl(this.modelItemDetail.userName,[Validators.required]),
      eTelephone : new FormControl(this.modelItemDetail.eTelephone,[Validators.required]),
      email : new FormControl(this.modelItemDetail.email,[Validators.required, Validators.email]),
      eqName : new FormControl(this.modelItemDetail.equirments.eqName,[Validators.required]),
      eqDesc : new FormControl(this.modelItemDetail.equirments.eqDesc,[Validators.required]),
      sDate : new FormControl(this.modelItemDetail.sDate,[Validators.required]),
      eDate : new FormControl(''),
      sTime : new FormControl(''),
      eTime : new FormControl(''),
      Quantity : new FormControl('',[Validators.required]),
      remark : new FormControl(''),
    })

    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 ;

    this.myFormRoom = new FormGroup({
      userNameForm : new FormControl(this.modelRoomDetail.userName,[Validators.required]),
      rTelephone : new FormControl(this.modelRoomDetail.rTelephone,[Validators.required]),
      email : new FormControl(this.modelRoomDetail.email,[Validators.required, Validators.email]),
      rName : new FormControl(this.modelRoomDetail.room.rName,[Validators.required]),
      rDesc : new FormControl(this.modelRoomDetail.room.rDesc,[Validators.required]),
      rType : new FormControl(this.modelRoomDetail.room.rType,[Validators.required]),
      sDate : new FormControl(this.modelRoomDetail.sDate,[Validators.required]),
      eDate : new FormControl(this.modelRoomDetail.eDate,[Validators.required]),
      sTime : new FormControl(''),
      eTime : new FormControl(''),
      remark : new FormControl(''),
      allDay: new FormControl('')
    });

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

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

}