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 { Type } from 'src/app/models/type.model';
import { MyRoom, 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, FormControl, FormGroup, Validators } from '@angular/forms';
import { TypeService } from 'src/app/service/type.service';

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


  closeResult = '';

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

  listEquirmentModelEdit: Equirment | undefined;
  listRoomModelEdit: Room | undefined;

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

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


  modelEquirment = new Equirment();
  modelRoom: Room
  modelItemDetail = new ItemDetail();
  modelRoomDetail = new RoomDetail();
  modelType = new Type();

  RoomForm = new FormGroup({
    rId: new FormControl(''),
    rName: new FormControl(''),
    rType: new FormControl(''),
    rPic: new FormControl(''),
    roomLimit: new FormControl(''),
    rDesc: new FormControl('')
  });

  myFormRoom: FormGroup;
  myFormEquir: FormGroup;
  myFormType: FormGroup;

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

  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]),
      eqType : 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(''),
    })

    this.myFormType = new FormGroup({
      typeName : new FormControl('',[Validators.required]),
    })
 
  }

  bookEquir(editLend, item: Equirment) {

    this.modelItemDetail = new ItemDetail();
    this.modelItemDetail.equirments = 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.modelRoom = new MyRoom({
    //   "rId": 2,
    //   "rName": "ห้องคู่",
    //   "rType": "ห้อง",
    //   "rPic": "assets/img/room.jpg",
    //   "roomLimit": 50,
    //   "rDesc": "จำนวนคน 50 คน"
    // });
    // this.modelRoom = new MyRoom(item);
    console.log("room", this.modelRoom);

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

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

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

  Eopen(contentequirment, index?: number) {
    this.index = index;
    let temp = {
      eqName: this.listEquirmentModel[this.index].eqName,
      eqType: this.listEquirmentModel[this.index].eqType,
      eqDesc: this.listEquirmentModel[this.index].eqDesc,
      eqId: this.listEquirmentModel[this.index].eqId,
      eqPic: this.listEquirmentModel[this.index].eqPic,
    }
    this.listEquirmentModelEdit = temp

    // 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)}`;
    });
  }
  index = -1;
  Ropen(contentroom, index?: number) {
    this.index = index;
    let temp = {
      rName: this.listRoomModel[this.index].rName,
      rType: this.listRoomModel[this.index].rType,
      rDesc: this.listRoomModel[this.index].rDesc,
      rId: this.listRoomModel[this.index].rId,
      rPic: this.listRoomModel[this.index].rPic,
      roomLimit: this.listRoomModel[this.index].roomLimit,
    }
    this.listRoomModelEdit = temp

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

  openItemDetail(contentequirment) {
    this.modelEquirment = new Equirment();
    this.myFormEquir = new FormGroup({
      eqName : new FormControl(this.modelEquirment.eqName,[Validators.required]),
      eqType : new FormControl(this.modelEquirment.eqType,[Validators.required]),
      eqDesc : new FormControl(this.modelEquirment.eqDesc,[Validators.required]),
      eqPic : new FormControl(this.modelEquirment.eqPic,[Validators.required]),
    })
    this.modalService.open(contentequirment, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  openRoomDetail(contentroom) {
    this.modelRoom = new MyRoom();
    this.myFormRoom = new FormGroup({
      rName : new FormControl(this.modelRoom.rName,[Validators.required]),
      rType : new FormControl(this.modelRoom.rType,[Validators.required]),
      rDesc : new FormControl(this.modelRoom.rDesc,[Validators.required]),
      rPic : new FormControl(this.modelRoom.rPic,[Validators.required]),
    })
    this.modalService.open(contentroom, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  openType(contenttype) {
    this.modelType = new Type ();
    this.myFormType = new FormGroup({
      typeId : new FormControl(this.modelType.typeId,[Validators.required]),
      typeName : new FormControl(this.modelType.typeName,[Validators.required]),
    })
    this.modalService.open(contenttype, { 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();

  }

  saveType(){
    this.typeService.addType(this.modelType);
    this.modalService.dismissAll();
    this.ngOnInit();
  }

  editRoom() {
    this.roomService.editroom(this.modelRoom);
    this.modalService.dismissAll();
    this.ngOnInit();
  }

  onSubmit() {
    // TODO: Use EventEmitter with form value
    console.warn(this.RoomForm.value);
  }
  
  updateRoomProfile() {
    this.listRoomModel[this.index].rName = this.myFormRoom.value.rName
    this.listRoomModel[this.index].rDesc = this.myFormRoom.value.rDesc
    this.listRoomModel[this.index].rType = this.myFormRoom.value.rType
    this.roomService.updateRoom(this.listRoomModel[this.index])
    this.modalService.dismissAll();
    this.ngOnInit();
  }

  updateItemProfile() {
    this.listEquirmentModel[this.index].eqName = this.myFormEquir.value.eqName
    this.listEquirmentModel[this.index].eqDesc = this.myFormEquir.value.eqDesc
    this.listEquirmentModel[this.index].eqType = this.myFormEquir.value.eqType
    this.equirmentService.updateEquirment(this.listEquirmentModel[this.index])
    this.modalService.dismissAll();
    this.ngOnInit();
  }

  deleteRoomProfile() {
    this.roomService.deleteRoom(this.modelRoom)
    this.modalService.dismissAll();
    this.ngOnInit();
  }
  
  deleteEquirProfile() {
    this.equirmentService.deleteEquirment(this.modelEquirment)
    this.modalService.dismissAll();
    this.ngOnInit();
  }

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

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

}
