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

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

  constructor(private http: HttpClient) { }
  url: string = "http://b872-2405-9800-bc03-3665-282a-83c6-a83e-fc1b.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(searchroomModel: SearchRoomModel) {
    this.http.get(this.url + '/status?sroomStasus=' + searchroomModel.sroomStatus + '&startDate=' + searchroomModel.sDate + '&endDate=' + searchroomModel.eDate).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;
  //   });
  // }

}
