import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { RoomDetail, SearchRoomDetailModel } from '../models/RoomDetail.model';
import { Observable } from 'rxjs';


@Injectable({ providedIn: 'root' })
export class RoomDetailService {
    open(contenttype: any, arg1: { ariaLabelledBy: string; }) {
      throw new Error('Method not implemented.');
    }

    constructor(private http: HttpClient) { }
    url: string = "http://b872-2405-9800-bc03-3665-282a-83c6-a83e-fc1b.ngrok.io";
    listRoomDetail: RoomDetail[] = []

    editRoomDetail(model: RoomDetail) {
        this.listRoomDetail.push(model)
    }

    addRoomDetail(model: RoomDetail) {
        this.listRoomDetail.push(model);
    }

    getListRoomDetail() : Observable<RoomDetail[]> {
        return this.http.get<RoomDetail[]>(this.url + "/book/lists"); 
    }


    searchListRoomDetail(searchModel: SearchRoomDetailModel) {
        this.http.get(this.url + '&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.listRoomDetail = response;
        }, error => {
            console.log(error)
            alert("ไม่สามารถค้นหาการจอง เนื่องจาก :\n" + error.message)
        })
    }

    deleteRoomDetail(model: RoomDetail) {
        this.http.delete(this.url + "/book/" + model.id).subscribe(response => {
            console.log(response);
            this.getListRoomDetail()
            alert("ลบข้อมูลการจองสำเร็จ")
        })
    }

    updateRoomDetail(model: RoomDetail) {
        this.http.put(this.url + "/book/" + model.id, model).subscribe(response => {
            console.log(response);
            alert("อัพเดทข้อมูลการจองสำเร็จ")
        })
    }

}