import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Room, SearchRoomModel } from '../models/rooms.model';


@Injectable({ providedIn: 'root' })
export class RoomService {

  constructor(private http: HttpClient) { }
  url: string;
  listRoomModel: Room[] = [
    {
      rId: 1,
      rName: "ห้องเดียว",
      rType: '',
      rPic: "assets/img/room.jpg",
      roomLimit: 25,
      rDesc: "จำนวนคน 25 คน",
    },
    {
      rId: 2,
      rName: "ห้องคู่",
      rType: '',
      rPic: "assets/img/room.jpg",
      roomLimit: 50,
      rDesc: "จำนวนคน 50 คน",
    },
    {
      rId: 3,
      rName: "ห้องรวม",
      rType: '',
      rPic: "assets/img/room.jpg",
      roomLimit: 100,
      rDesc: "จำนวนคน 100 คน",
    },
    {
      rId: 4,
      rName: "รถ",
      rType: '',
      rPic: "assets/img/car.jpg",
      roomLimit: 0,
      rDesc: "รถ",
    }
  ]
  listRoomSearch: Room[] = [];

  getListRoom() {
    this.http.get(this.url).subscribe((response: any) => {
      console.log(response);
      this.listRoomModel = response.content;
    }, error => {
      console.log(error)
      // alert("ไม่สามารถดึงข้อมูลห้องประชุมได้ เนื่องจาก :\n" + error.message)
    })
  }

  searchListRoom(searchModel: SearchRoomModel) {
    this.http.get(this.url + '/limits?roomlimit='+searchModel.roomlimit+'&startDate='+searchModel.sdate.split("-")[2]+'-'+searchModel.sdate.split("-")[1]+'-'+searchModel.sdate.split("-")[0]+'&endDate='+searchModel.edate.split("-")[2]+'-'+searchModel.edate.split("-")[1]+'-'+searchModel.edate.split("-")[0]+'&startTime='+searchModel.stime+':00'+'&endTime='+searchModel.etime+':00').subscribe((response: any) => {
      console.log(response);
      this.listRoomSearch = response;
    }, error => {
      console.log(error)
      alert("ไม่สามารถค้นหาห้องประขุมที่ว่างได้ เนื่องจาก :\n" + error.message)
    })
  }

  addRoom(roomModel: Room) {
    this.http.post(this.url, roomModel).subscribe(response => {
      console.log(response);

    })


  }
  deleteRoom(roomModel: Room) {
    this.http.delete(this.url + "/" + roomModel.rId).subscribe(response => {
      console.log(response);
      this.getListRoom()
      alert("ลบข้อมูลห้องสำเร็จ")
    })
  }

  updateRoom(roomModel: Room) {
    this.http.put(this.url + "/" + roomModel.rId, roomModel).subscribe(response => {
      console.log(response);
      alert("อัพเดทข้อมูลห้องสำเร็จ")
    })
  }

  roomModel(rId: string) {

  }


}
