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

@Component({
    selector: 'v-login',
    templateUrl: './login.page.html',
    styleUrls: ['./login.page.scss'],
    animations: [fadeIn]
})
export class LoginPage implements OnInit {
    req = {name: '', ps: ''};

    constructor(private tf: Transfer, private api: Api) {
    }

    ngOnInit() {
    }

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