import { Component, OnInit } from '@angular/core';
import { NgbDate, NgbCalendar, NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
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 { ItemDetailService } from 'src/app/service/item-detail.service';
import { RoomDetail } from 'src/app/models/roomDetail.model';
import { RoomDetailService } from 'src/app/service/room-detail.service';
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 { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-admin-pending',
  templateUrl: './admin-pending.component.html',
  styleUrls: ['./admin-pending.component.scss'],
  styles: [`
  .form-group.hidden {
    width: 0;
    margin: 0;
    border: none;
    padding: 0;
  }
  .custom-day {
    text-align: center;
    padding: 0.185rem 0.25rem;
    display: inline-block;
    height: 2rem;
    width: 2rem;
  }
  .custom-day.focused {
    background-color: #e6e6e6;
  }
  .custom-day.range, .custom-day:hover {
    background-color: rgb(2, 117, 216);
    color: white;
  }
  .custom-day.faded {
    background-color: rgba(2, 117, 216, 0.5);
  }
`]

})

export class AdminPendingComponent implements OnInit {
  time = { hour: 13, minute: 30 };

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

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

  closeResult = '';

  listitemDetail : ItemDetail[] = [];
  listroomDetail : RoomDetail[] = [];

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

  hoveredDate: NgbDate | null = null;

  fromDate: NgbDate | null;
  toDate: NgbDate | null;

  thStime: string;
  thEtime: string;
  thtimeChange (time:string,item:any){
    let timeSplit = time.split(":")
    item.hour = parseInt(timeSplit[0])
    item.minute = parseInt(timeSplit[1])
    console.log(this.modelRoomDetail)
  }


  constructor(private calendar: NgbCalendar, public formatter: NgbDateParserFormatter, private modalService: NgbModal, private ItemDetailService: ItemDetailService, private RoomDetailService: RoomDetailService
    , private equirmentService: EquirmentService, private roomService: RoomService) {
    this.fromDate = calendar.getToday();
    this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
    this.refreshitemDetail();
  }

  refreshitemDetail() {
    this.listitemDetail = this.listitemDetail
      .map((country, i) => ({ id: i + 1, ...country }))
      .slice((this.page - 1) * this.pageSize, (this.page - 1) * this.pageSize + this.pageSize);
  }

  onDateSelection(date: NgbDate) {
    if (!this.fromDate && !this.toDate) {
      this.fromDate = date;
    } else if (this.fromDate && !this.toDate && date && date.after(this.fromDate)) {
      this.toDate = date;
    } else {
      this.toDate = null;
      this.fromDate = date;
    }
  }

  open(content) {
    this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }
  date='10-12-2561'
  show(a:string){
    console.log(a)
  }
  sDate = ""
  eDate = ""
  openItemDetail(content : string ,item : ItemDetail) {
    this.modelItemDetail = item ;
    this.thStime= this.modelItemDetail.sTime.hour + ':' + this.modelItemDetail.sTime.minute 
    this.thEtime= this.modelItemDetail.eTime.hour + ':' + this.modelItemDetail.eTime.minute
    let SDate = this.modelItemDetail.sDate.split('/')
    this.sDate = SDate[2]+"-"+SDate[1]+"-"+SDate[0]
    let EDate = this.modelItemDetail.eDate.split('/')
    this.eDate = EDate[2]+"-"+EDate[1]+"-"+EDate[0]
    this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  rDate(item: string, item2: string){
    let Dates = item.split("-")
    let dates = Dates[2]+"/"+Dates[1]+"/"+Dates[0]
    
    if(item2=="sdate"){
      this.modelItemDetail.sDate = dates
    }
    if(item2=="edate"){
      this.modelItemDetail.eDate = dates
    }
    console.log(this.sDate)
  } 

  openRoomDetail(content : string ,item : RoomDetail) {
    this.modelRoomDetail = item ;
    this.thStime= this.modelRoomDetail.sTime.hour + ':' + this.modelRoomDetail.sTime.minute 
    this.thEtime= this.modelRoomDetail.eTime.hour + ':' + this.modelRoomDetail.eTime.minute 
    let SDate = this.modelRoomDetail.sDate.split('/')
    this.sDate = SDate[2]+"-"+SDate[1]+"-"+SDate[0]
    let EDate = this.modelRoomDetail.eDate.split('/')
    this.eDate = EDate[2]+"-"+EDate[1]+"-"+EDate[0]
    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}`;
    }
  }

  isHovered(date: NgbDate) {
    return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
  }

  isInside(date: NgbDate) {
    return this.toDate && date.after(this.fromDate) && date.before(this.toDate);
  }

  isRange(date: NgbDate) {
    return date.equals(this.fromDate) || (this.toDate && date.equals(this.toDate)) || this.isInside(date) || this.isHovered(date);
  }

  validateInput(currentValue: NgbDate | null, input: string): NgbDate | null {
    const parsed = this.formatter.parse(input);
    return parsed && this.calendar.isValid(NgbDate.from(parsed)) ? NgbDate.from(parsed) : currentValue;
  }

  myFormRoom: FormGroup;
  myFormEquir: FormGroup;

  ngOnInit(): void {
    this.listitemDetail = this.ItemDetailService.getListItemDetail();
    this.listroomDetail = this.RoomDetailService.getListRoomDetail();
    this.collectionSizeListRoom = this.listitemDetail.length;
    console.log(this.listitemDetail);
    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()
    });
    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]),
    })
  }

  saveEquirment() {
    this.equirmentService.addEquirment(this.modelEquirment);
    this.modalService.dismissAll();
    this.ngOnInit();
  }

  saveRoom() {
    this.roomService.addRoom(this.modelRoom);
    this.modalService.dismissAll();
    this.ngOnInit();
  }
}

export class NgbdDropdownBasic {
}
