<template>
    <view class="flex direction-column height-100pre justify-bewteen bg-lightblue-to-white">
        <view class="status-bar-height"></view>
        <view class="flex direction-column grow-1">
            <view class="title padding-top">
                <view class="title-name">生产流程管理</view>
            </view>
            <view class="flex direction-column padding child-gap-40">
                <uni-easyinput placeholder="输入账号" v-model="username"></uni-easyinput>
                <uni-easyinput placeholder="输入密码" v-model="password" type="password" @confirm="login"></uni-easyinput>
                <view class="flex padding-top-40">
                    <button type="lightblue" border="round" class="grow-1" @tap="login">登录</button>
                </view>
            </view>
        </view>
        <div class="xieyi" @tap="openProtocol()">
            <text>登录表示您同意</text>
            <text>《用户协议》</text>
            <text>和</text>
            <text>《隐私政策》</text>
        </div>
    </view>
</template>

<script>
    import extend from '../../utils/extend.js';
    import request from '../../utils/request.js';
    import token from '../../utils/token.js';
    import user from '../../utils/user.js';
    import url from '../../utils/url.js';

    export default {
        data() {
            return {
                username: '',
                password: '',
                backType: '',
                prevPage: ''
            }
        },
        onLoad(opts) {
            let info = user.getRemember();
            if (extend.isObject(info)) {
                this.username = info.username;
                this.password = info.password;
            }
            if (extend.isObject(opts)) {
                switch (opts.type) {
                    case 'switchTab':
                        this.backType = opts.type;
                        break;
                    default:
                        this.backType = 'navigateBack';
                        break;
                }
                if (extend.isNonEmptyString(opts.page)) {
                    this.prevPage = opts.page;
                }
            }
        },
        methods: {
            openSetting() {
                uni.navigateTo({
                    url: '/pages/login/serverSetting'
                });
            },

            async login() {
                let data = {
                    client_id: 'kapok',
                    client_secret: 'mall',
                    grant_type: 'password',
                    username: this.username,
                    password: this.password
                };
                uni.showLoading({
                    title: '登陆中...',
                    mask: true,
                });
                let res = await token.getByServer({
                    url: 'oauth2/token',
                    data: data
                });
                uni.hideLoading();
                if (res.success) {
                    // 登录成功
                    user.setRemember({
                        username: this.username,
                        password: this.password
                    });

                    let userRes = await user.getByServer({
                        url: 'auman/user/info'
                    });

                    if (userRes.success) {
                        let url = extend.isNonEmptyString(this.prevPage) ? this.prevPage : '/pages/home/index';
                        uni.switchTab({
                            url: url
                        });
                    } else {
                        uni.showModal({
                            title: '错误',
                            content: res.message,
                            mask: true
                        });
                    }
                } else {
                    uni.showModal({
                        title: '错误',
                        content: res.message,
                        mask: true
                    });
                }
            },

            back() {
                let url = extend.isNonEmptyString(this.prevPage) ? this.prevPage : '/pages/home/index';
                switch (this.backType) {
                    case 'switchTab':
                        uni.switchTab({
                            url: url
                        });
                        break;
                    default:
                        uni.navigateBack();
                        break;
                }
            },

            openProtocol() {
                uni.navigateTo({
                    url: '/pages/my/protocol'
                });
            }
        }
    }
</script>

<style lang="scss">
    .page-bg {
        background-color: #f8f8f8;
    }

    .title {
        display: flex;
        height: 400upx;
        justify-content: center;
        align-items: center;

        .title-name {
            font-size: 60upx;
            font-weight: bold;
        }
    }

    .xieyi {
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #888888;
        height: 60upx;
        padding-bottom: 30upx;
    }
</style>