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;
  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: "รถ",
    }

    ,
    {
      rId: 5,
      rName: "ห้องคู่",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 50,
      rDesc: "จำนวนคน 50 คน",
    },
    {
      rId: 6,
      rName: "ห้องรวม",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 100,
      rDesc: "จำนวนคน 100 คน",
    },
    {
      rId: 7,
      rName: "รถ",
      rType : "รถ",
      rPic: "assets/img/car.jpg",
      roomLimit: 0,
      rDesc: "รถ",
    }
    ,
    {
      rId: 8,
      rName: "ห้องคู่",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 50,
      rDesc: "จำนวนคน 50 คน",
    },
    {
      rId: 9,
      rName: "ห้องรวม",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 100,
      rDesc: "จำนวนคน 100 คน",
    },
    {
      rId: 10,
      rName: "รถ",
      rType : "รถ",
      rPic: "assets/img/car.jpg",
      roomLimit: 0,
      rDesc: "รถ",
    }
    ,
    {
      rId: 11,
      rName: "ห้องคู่",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 50,
      rDesc: "จำนวนคน 50 คน",
    },
    {
      rId: 12,
      rName: "ห้องรวม",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 100,
      rDesc: "จำนวนคน 100 คน",
    },
    {
      rId: 13,
      rName: "รถ",
      rType : "รถ",
      rPic: "assets/img/car.jpg",
      roomLimit: 0,
      rDesc: "รถ",
    }
    ,
    {
      rId: 14,
      rName: "ห้องคู่",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 50,
      rDesc: "จำนวนคน 50 คน",
    },
    {
      rId: 15,
      rName: "ห้องรวม",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 100,
      rDesc: "จำนวนคน 100 คน",
    },
    {
      rId: 16,
      rName: "รถ",
      rType : "รถ",
      rPic: "assets/img/car.jpg",
      roomLimit: 0,
      rDesc: "รถ",
    }
    ,
    {
      rId: 17,
      rName: "ห้องคู่",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 50,
      rDesc: "จำนวนคน 50 คน",
    },
    {
      rId: 18,
      rName: "ห้องรวม",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 100,
      rDesc: "จำนวนคน 100 คน",
    },
    {
      rId: 19,
      rName: "รถ",
      rType : "รถ",
      rPic: "assets/img/car.jpg",
      roomLimit: 0,
      rDesc: "รถ",
    }
    ,
    {
      rId: 20,
      rName: "ห้องคู่",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 50,
      rDesc: "จำนวนคน 50 คน",
    },
    {
      rId: 21,
      rName: "ห้องรวม",
      rType : "ห้อง",
      rPic: "assets/img/room.jpg",
      roomLimit: 100,
      rDesc: "จำนวนคน 100 คน",
    },
    {
      rId: 22,
      rName: "รถ",
      rType : "รถ",
      rPic: "assets/img/car.jpg",
      roomLimit: 0,
      rDesc: "รถ",
    }
  ]
  listRoomSearch: Room[] = [];

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

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

  getListRoom() {
    return this.listRoomModel;
    // 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("อัพเดทข้อมูลห้องสำเร็จ")
    })
  }

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

  roomModel(rId: string) {

  }


}
