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';
import { Observable } from 'rxjs';
import { Type } from '../models/type.model';

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

  constructor(private http: HttpClient) { }
  url: string = "http://0eb9-110-169-220-190.ngrok.io";
  listEquirment: Equirment[] = []

  addEquirment(model: Equirment) {
    // this.listEquirment.push(model);
    console.log("model", model)
    this.http.post(this.url + "/eqment/", model).subscribe(response => {
      console.log(response);
    })
  }

  addType(model : Type) {
    this.http.post(this.url + "/type/", model).subscribe(response => {
      console.log(response);
    })
  }

  getListEquirment(): Observable<Equirment[]> {
    return this.http.get<Equirment[]>(this.url + "/eqment/lists");
  }

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

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

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