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


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

    constructor(private http: HttpClient) { }
    url: string;
    listRoomDetail: RoomDetail[] = [
        {
            id: 1,
            room: {
                rId: 1,
                rName: "ห้องเดียว",
                rType: "ห้อง",
                rPic: "assets/img/room.jpg",
                roomLimit: 25,
                rDesc: "จำนวนคน 25 คน",
            },
            userName: 'เกม',
            sDate: '2021-12-15',
            eDate: '2021-12-15',
            sTime: '22:30',
            eTime: '22:30',
            rStatus: 0,
            rTelephone: '0123456789',
            email: 'qwerty@qwerty',
            remark: 'test',
            allDay: false
        },
        {
            id: 2,
            room: {
                rId: 2,
                rName: "ห้องคู่",
                rType: "ห้อง",
                rPic: "assets/img/room.jpg",
                roomLimit: 50,
                rDesc: "จำนวนคน 50 คน",
            },
            userName: 'แป๊ะ',
            sDate: '2021-12-15',
            eDate: '2021-12-15',
            sTime: '22:30',
            eTime: '22:30',
            rStatus: 1,
            rTelephone: '0123456789',
            email: 'qwerty@qwerty',
            remark: 'test',
            allDay: true
        },

    ]

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

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

    // updateRoomDetail(model: RoomDetail){
    //     let temp = this.listRoomDetail.filter( x => 
    //         x.Id == model.Id
    //     )
    //     console.log("temp",temp);
    // }

    updateRoomDetail(row_obj: RoomDetail) {
        this.listRoomDetail = this.listRoomDetail.filter((value, key) => {
            if (value.id === row_obj.id) {
                value = row_obj;
            }
            return true;
        });
    }


    getListRoomDetail() {
        return this.listRoomDetail;
    }




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

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

    // addEquirment(EquirmentModel: Equirment) {
    //   this.http.post(this.url, EquirmentModel).subscribe(response => {
    //     console.log(response);

    //   })


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

    // updateRoomDetail(RoomDetailModel: RoomDetail) {
    //     this.http.put(this.url + "/" + RoomDetailModel.Id, RoomDetailModel).subscribe(response => {
    //         console.log(response);
    //         alert("อัพเดทข้อมูลอุปกรณ์สำเร็จ")
    //     })
    // }

}