import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Equirment, SearchEquirmentModel } from '../models/equirment.model';
import { ItemDetail } from '../models/itemDetail.model';

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

  constructor(private http: HttpClient) { }
  url: string;
  listEquirment: Equirment[] = [
    {
      eqId: 1,
      eqName: "จอคอมพิวเตอร์",
      eqDesc: "หน้าจอขนาด 22 นิ้ว",
      eqType: "it",
      eqPic: "assets/img/mo1.jpg"
    },
    {
      eqId: 2,
      eqName: "เมาส์",
      eqDesc: "...",
      eqType: "it",
      eqPic: "assets/img/mou.jpg"
    },
    {
      eqId: 3,
      eqName: "โน๊ตบุ๊ค",
      eqDesc: "หน้าจอขนาด 20 นิ้ว",
      eqType: "it",
      eqPic: "assets/img/notebook.jpg"
    },
    {
      eqId: 4,
      eqName: "คีย์บอร์ด",
      eqDesc: "...",
      eqType: "it",
      eqPic: "assets/img/keyboard.png"
    }
  ]

  addEquirment(model: Equirment) {
    this.listEquirment.push(model);
  }

  // bookEquirment(models: ItemDetail){
  //   this.listEquirment.push(models);
  // }

  getListEquirment() {
    return this.listEquirment;
  }

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

  searchListEquirment(searchModel: SearchEquirmentModel) {
    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.listEquirment = response;
    }, error => {
      console.log(error)
      alert("ไม่สามารถค้นหาอุปกรณ์ที่ว่างได้ เนื่องจาก :\n" + error.message)
    })
  }

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

  //   })


  // }
  deleteEquirment(EquirmentModel: Equirment) {
    this.http.delete(this.url + "/" + EquirmentModel.eqId).subscribe(response => {
      console.log(response);
      this.getListRoom()
      alert("ลบข้อมูลอุปกรณ์สำเร็จ")
    })
  }

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

  EquirmentModel(eqId: string) {

  }

}