import {Component, ViewChild} from '@angular/core';
import {IonicPage, LoadingController, NavController, NavParams, Slides} from 'ionic-angular';
import {TabsPage} from "../tabs/tabs";
import {LoginService} from "./login.service";
import {Storage} from "@ionic/storage";
import {AppService} from "../../app/app.service";
import {CommonService} from "../../core/common.service";
import {CheckCodeComponent} from "../../components/check-code/check-code";
import {Keyboard} from "@ionic-native/keyboard";
import {StatusBar} from "@ionic-native/status-bar";
import {DatePipe} from "@angular/common";
import {RandomWordService} from "../../secret/randomWord.service";
import {GlobalData} from "../../core/GlobleData";

@IonicPage()
@Component({
    selector: 'page-login',
    templateUrl: 'login.html',
})
export class LoginPage {
    @ViewChild('checkCode') checkCode: CheckCodeComponent;

    codeNum;
    user = {
        mobilePhone: "",
        password: "",
        captcha: ""
    };
    loading;
    obj = {
        area: "",
        roles: ""
    }

    constructor(public navCtrl: NavController, public navParams: NavParams, private loadCtrl: LoadingController,
                private datePipe: DatePipe,
                private globle: GlobalData,
                private randomWord: RandomWordService,
                private loginSer: LoginService, private storage: Storage, private appSer: AppService,
                private commonSer: CommonService, private keyboard: Keyboard, public statusBar: StatusBar) {
    }

    ionViewDidLoad() {
        this.checkCode.drawPic();
    }

    login() {
        if (!this.user.mobilePhone || !this.user.password || !this.user.captcha) {
            this.commonSer.toast('请填写完整的登录信息');
            return
        }
        if (this.user.captcha != this.codeNum) {
            this.commonSer.toast("验证码错误!");
            return;
        }
        const data = {
            "mobilePhone": this.user.mobilePhone,
            "password": this.user.password
        };
        this.loading = this.loadCtrl.create();
        this.loading.present();
        this.loginSer.login(data).subscribe(
            (res) => {
                if (res.code == '0') {
                    this.storage.set('token', res.token);
                    this.globle.token = res.token;
                    setTimeout(() => {
                        this.getUserInfo();
                    }, 100)
                } else {
                    this.loading.dismiss();
                }
            }
        )
    }

    //获取用户信息
    getUserInfo() {
        this.loginSer.userInfo(this.obj).subscribe(
            (res) => {
                if (res.code == 0) {
                    this.storage.set("mineInfo", res.user);
                    this.globle.userObj = res.user;
                    this.obj.area = res.user.area;
                    this.obj.roles = res.user.roles;
                    this.commonSer.toast('登录成功');
                    this.loading.dismiss();
                    this.getArea(res.user);
                    this.getRoleMenu();
                }
            }
        )
    }

    //获取区属
    getArea(userObj) {
        const data = {};
        this.loginSer.getArea(data).subscribe(
            (res) => {
                if (res.list) {
                    res.list.map(e => {
                        if (e.code == userObj.area) {
                            this.globle.area = e;
                            this.storage.set('area', e);
                        }
                    });
                }
            }
        )
    }

    //获取权限
    getRoleMenu() {
        this.loginSer.getRoleMenu(this.obj).subscribe(
            (res) => {
                this.storage.set('roles', res.data);
                this.navCtrl.setRoot(TabsPage);
            }
        )
    }

    getCode(e) {
        this.codeNum = e;
    }

    flashCode() {
        this.checkCode.drawPic();
    }

}
