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

@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, private mqtt: Mqtt) {
    }

    ngOnInit() {
    }

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