import { Component, OnInit } from "@angular/core";
import { FormControl } from "@angular/forms";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import { StatusService } from "../shared/status/status.service";
import {UserService} from '../user.service';
import "rxjs/Rx";

@Component({
  selector: "app-admin",
  templateUrl: "./admin.component.html",
  styleUrls: ["./admin.component.less"]
})
export class AdminComponent implements OnInit {
  name = new FormControl("");
  ip = new FormControl("");
  ipDelete = new FormControl("");
  devices = [];
  constructor(private http: HttpClient, private user: UserService, private statusService: StatusService) {}

  onAddDevice() {
    if (this.name.value.length > 0 && this.ip.value.length > 0) {
      this.http
        .post("http://localhost:3000/device", {
          name: this.name.value,
          ip: this.ip.value
        })
        .subscribe(data => {
          this.name.reset();
          this.ip.reset();
        });
    }
  }

  onDeviceDelete(i) {
 
    if (this.devices[i].ip.length > 0) {
      this.http
        .delete("http://localhost:3000/device/" + this.devices[i].ip)
        .subscribe(data => {
         
        });
    }
    
    
  }


  onDeviceEdit() {
    
 
    
  }



  ngOnInit() {

      this.statusService.getDB().subscribe(data => {
      this.devices = data;
      
    });
  }


}