/**
 * Created by pohodnaivan on 05.06.17.
 */
import {Component, OnInit} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {FormGroup} from '@angular/forms';
import {User} from '../../../models/Model';
import AuthService = require('../../../service/web/auth.service');
import {MdDialogRef, MdSnackBar} from '@angular/material';

@Component({
  selector: 'login',
  templateUrl: 'app/modules/main/login/login.component.html',
  styleUrls: ['app/modules/main/login/login.component.css']
})

export class LoginComponent implements OnInit {
  private fb: FormBuilder;
  private loginForm: FormGroup;
  private authService: AuthService;
  private dialogRef: MdDialogRef<LoginComponent>;
  constructor(fb: FormBuilder, authService: AuthService, dialogRef: MdDialogRef<LoginComponent>,
              private snackBar: MdSnackBar) {
    this.fb = fb;
    this.authService = authService;
    this.dialogRef = dialogRef;
  }

  ngOnInit() {
    this.loginForm = this.fb.group(new User());
  }

  login(): void {
    this.authService.login(this.loginForm.get('login').value, this.loginForm.get('password').value).then((user) => {
      if (!user) 

      if (this.dialogRef) {
        this.snackBar.open(`successful login! as ${user.name}`, null, {
          duration: 1000
        });
        this.dialogRef.close(user);
      }
    });
  }
}

{
        this.snackBar.open(`wrong login or password!`, null, {
          duration: 1000
        });
        return;
      }