import {Component, OnInit} from '@angular/core';
import {fadeIn} from '../../animations/fade-in';
import {TransferService} from '../../service/transfer.service';
import {ApiService} from '../../service/api.service';

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss'],
    animations: [fadeIn]
})
export class LoginComponent implements OnInit {
    req = {name: '', ps: ''};
    constructor(private tf: TransferService, private api: ApiService) {
    }

    ngOnInit() {
    }

    login() {
        const req = {
            userName: this.req.name,
            password: this.req.ps
        };
        this.api.login.dologin([req, (data) => {
            if (data.code === 1) {
                window.localStorage.setItem('token', data.message);
                const query = {refresh: new Date().getTime()};
                this.tf.transfer({
                    from: 'app-extra',
                    to: 'app-home',
                    url: '/main',
                    query: query,
                    hash: ''
                });
            } else {
                this.api.presentMsgToast(data && data.message ? data.message : '登录失败');
            }
        }]);
    }
}
