import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { User } from '../_models/index';
import {environment} from '../../environments/environment';

@Injectable()
export class UserService {
    constructor(private http: HttpClient) { }

    getAll() {
        return this.http.get<User[]>(environment.apiUrl + '/admin/users');
    }

    getUsers(filter: any){
        const url = environment.apiUrl + 'users?perPage=' + filter.perPage + '&page=' + filter.page;
        console.log(url);

        return this.http.get(url);
    }

    getById(id: number) {
        return this.http.get(environment.apiUrl + 'users/' + id);
    }

    getByEmail(email: any) {
        return this.http.get(environment.apiUrl + 'users/email/' + email);
    }

    create(user: User) {
        return this.http.post(environment.apiUrl + 'users', user);
    }

    updateUser(user: any, mypass: string) {
        if (mypass != "") {
            user.password = mypass;
        }
        return this.http.put(environment.apiUrl + 'users/'+user.id, user);
    }

    delete(id: number) {
        return this.http.delete(environment.apiUrl + 'users/' + id);
    }
}
