import {Component} from '@angular/core';
import {Platform} from '@ionic/angular';
import {SplashScreen} from '@ionic-native/splash-screen/ngx';
import {StatusBar} from '@ionic-native/status-bar/ngx';
import {environment} from "../environments/environment";
import {AppVersion} from "@ionic-native/app-version/ngx";
import {Storage} from "@ionic/storage";
import {Router} from "@angular/router";
import {TabsService} from "./tabs/tabs.service";
import {CommonService} from "../provide/common.service";

@Component({
    selector: 'app-root',
    templateUrl: 'app.component.html'
})
export class AppComponent {

    user;

    constructor(
        private platform: Platform,
        private splashScreen: SplashScreen,
        private statusBar: StatusBar,
        private appVersion: AppVersion,
        private storage: Storage, public commonSer: CommonService,
        private router: Router, public tabSer: TabsService,
    ) {
        this.initializeApp();
        // console.log(App)
    }

    initializeApp() {
        this.platform.ready().then(() => {
            this.splashScreen.hide();
            this.statusBar.show();
            this.statusBar.overlaysWebView(false);
            this.statusBar.backgroundColorByHexString('#e12724');
            this.statusBar.styleLightContent();
            this.appVersion.getVersionNumber().then((version: string) => {
                alert(version);
            })

            this.loadLogin();

        });
    }

    loadLogin() {
        this.storage.get("userLoginInfo").then((value) => {
            if (value != null && value != '') {
                this.user = value;
                if (this.user.remember) {
                    this.tabSer.loginpost(this.user).subscribe(
                        (res) => {
                            if (res["code"] == '200') {
                                this.storage.set('user', res.data);
                                this.user.userid = res.data.id;
                                this.storage.set('userLoginInfo', this.user);
                                this.router.navigate(['/tabs/home'])
                            } else {
                                this.commonSer.alert('手机号或密码错误！');
                                this.router.navigate(['/login'])
                            }
                        },
                        (err) => {
                            this.commonSer.alert('网络异常！');
                        }
                    )
                } else {
                    this.router.navigate(['/login'])
                }
            } else {
                this.router.navigate(['/login'])
            }
        })
    }
}
