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

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

  constructor(private http: HttpClient) { }
  url: string;
  listItemDetail: ItemDetail[] = [
    {
      id : 1,
      equirment : {
        eqId: 2,
        eqName: "เมาส์",
        eqDesc: "...",
        eqType: "it",
        eqPic: "assets/img/mou.jpg"
      },
      userName: 'เกม',
      sDate: '2021/12/09',
      eDate: '2021/12/15',
      sTime: '22:30',
      eTime: '22:30',
      eStatus: 0,
      Quantity: '1',
      eTelephone: '0123456789',
      email: 'qwerty@qwerty',
      remark: 'test',
      allDay: false,
      returnItem: true
    },
    {
      id : 2,
      equirment : {
        eqId: 3,
        eqName: "โน๊ตบุ๊ค",
        eqDesc: "หน้าจอขนาด 20 นิ้ว",
        eqType: "it",
        eqPic: "assets/img/notebook.jpg"
      },
      userName: 'แป๊ะ',
      sDate: '2021/12/09',
      eDate: '2021/12/13',
      sTime: '22:30',
      eTime: '22:30',
      eStatus: 1,
      Quantity: '1',
      eTelephone: '0123456789',
      email: 'qwerty@qwerty',
      remark: 'test',
      allDay: false,
      returnItem: true
    },

  ]

  addItemDetail(model: ItemDetail) {
    console.log(this.listItemDetail);
    this.listItemDetail.push(model);
    this.listItemDetail.reverse
  }



  getListItemDetail() {
    return this.listItemDetail;
  }

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

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

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

  //   })


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

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

  updateItemDetail(row_obj: ItemDetail) {
    this.listItemDetail = this.listItemDetail.filter((value, key) => {
        if (value.id === row_obj.id) {
            value = row_obj;
        }
        return true;
    });
}

  ItemDetailModel(eqId: string) {

  }

}