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

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

  constructor(private http: HttpClient) { }
  url: string = "http://915e-49-0-64-28.ngrok.io";
  listRoomModel: Room[] = []
  listRoomSearch: Room[] = [];

  editroom(model: Room) {
    this.listRoomModel.push(model);
  }

  addRoom(model: Room) {
    console.log("model", model)
    this.http.post(this.url + "/room/", model).subscribe(response => {
      console.log(response);

    })
  }

  getListRoom() {
    return this.http.get<Room[]>(this.url + "/room/lists");
  }

  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(model: Room) {
    this.http.delete(this.url + "/room/" + model.rId).subscribe(response => {
      console.log(response);
      this.getListRoom()
      alert("ลบข้อมูลห้องสำเร็จ")
    })
  }

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

  // updateRoomProfile(model : Room) {
  //   this.listRoomModel = this.listRoomModel.filter((value, key) => {
  //     if (value.rId === model.rId) {
  //         value = model;
  //     }
  //     return true;
  // });
  // }

  // deleteRoomProfile(row_obj: Room) {
  //   this.listRoomModel = this.listRoomModel.filter((value, key) => {
  //     return value.rId !== row_obj.rId;
  //   });
  // }

}
