<template>
    <div
        class="login"
        :class="{ loginHeight: bar.value.show }"
        :style="{ backgroundImage: `url(${require('../../assets/login.jpg')})` }"
    >
        <img class="logo" v-if="active === 'phone'" src="../../assets/lvzhopu-logo.png" alt="" />
        <img class="logo" v-else src="../../assets/lvzhopu-logo2.png" alt="" />
        <div class="tabs">
            <div class="tab" :class="{ active: active === 'phone' }" @click="active = 'phone'">账号密码登陆</div>
            <div class="tab" :class="{ active: active === 'code' }" @click="active = 'code'">验证码登陆</div>
        </div>

        <van-form @submit="submit" ref="form" v-if="active === 'phone'">
            <div class="field-box">
                <van-field
                    type="tel"
                    name="用户名"
                    placeholder="Account"
                    v-model="form.phone"
                    :rules="[
                        { required: true, message: '请输入手机号码' },
                        {
                            pattern: phonePattern,
                            message: '手机号码格式错误'
                        }
                    ]"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_zhanghao.svg')" class="icon" />
                    </template>
                </van-field>
                <van-field
                    type="password"
                    name="密码"
                    placeholder="Password"
                    v-model="form.password"
                    :rules="[{ required: true, message: '请填写密码' }]"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_mima.svg')" class="icon" />
                    </template>
                </van-field>
            </div>

            <div class="button">
                <!-- <van-button plain class="forget" @click="$router.replace('/forget')">
          忘记密码？
        </van-button> -->

                <van-button block native-type="submit" color="#26F50D" class="sure">现在出发！</van-button>
                <van-button class="del" block plain @click="$router.replace('/register')"
                    >暂无绿洲宇宙登陆许可 立即申领</van-button
                >
            </div>
        </van-form>

        <van-form @submit="submit" ref="code" v-else>
            <div class="field-box">
                <van-field
                    type="tel"
                    name="手机号码"
                    placeholder="Account"
                    v-model="form.phone"
                    :rules="[
                        { required: true, message: '请输入手机号码' },
                        {
                            pattern: phonePattern,
                            message: '手机号码格式错误'
                        }
                    ]"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_zhanghao.svg')" class="icon" />
                    </template>
                </van-field>

                <van-field
                    type="code"
                    name="验证码"
                    placeholder="Verify"
                    v-model="form.code"
                    :rules="[{ required: true, message: '请输入验证码' }]"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_code.svg')" class="icon" />
                    </template>
                    <template #button>
                        <van-button
                            class="sub-code"
                            size="small"
                            plain
                            color="#26F50D"
                            @click="sendPhone"
                            :disabled="isSend"
                        >
                            {{ isSend ? `已发送(${sendNum})S` : '发送验证码' }}
                        </van-button>
                    </template>
                </van-field>
            </div>
            <div class="button">
                <!-- <van-button plain class="forget" @click="$router.replace('/forget')">
          忘记密码？
        </van-button> -->

                <van-button block native-type="submit" color="#26F50D" class="sure">现在出发！</van-button>
                <van-button class="del" block plain @click="$router.replace('/register')"
                    >暂无绿洲宇宙登陆许可 立即申领</van-button
                >
            </div>
        </van-form>

        <!-- <div class="xieyi">
            <van-checkbox v-model="checked">
                已阅读并同意
                <span @click.stop="$router.push('/agreement?page=service')"> &lt;&lt;用户服务协议>></span>
                和
                <span @click.stop="$router.push('/agreement')"> &lt;&lt;平台隐私协议>></span>
            </van-checkbox>
        </div> -->
    </div>
</template>

<script>
import phone from '../../mixins/phone';
let fromRoute = null;
export default {
    mixins: [phone],
    inject: ['bar'],
    data() {
        return {
            active: 'phone',
            form: {
                phone: '',
                password: '',
                code: ''
            },
            checked: false
        };
    },
    beforeRouteEnter(to, from) {
        fromRoute = from;
    },
    mounted() {
        console.log(this.$store.state.loginBackUrl);
    },
    methods: {
        sendPhone() {
            this.$refs.code.validate('手机号码').then(() => {
                this.sendMsg(this.form.phone);
            });
        },
        submit() {
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            this.check().then(() => {
                this.$toast.success('登录成功');
                setTimeout(() => {
                    if (this.$store.state.loginBackUrl) {
                        this.$router.replace(this.$store.state.loginBackUrl);
                    } else if (!fromRoute.name || fromRoute.name === 'userRegister' || fromRoute.name === 'userLogin') {
                        this.$router.replace('/home');
                    } else {
                        this.$router.back();
                    }
                }, 1500);
            });
        },
        check() {
            if (this.active === 'phone') {
                return this.loginByPhone(this.form.phone, this.form.password);
            } else {
                return this.$http
                    .post('/auth/phoneLogin', {
                        phone: this.form.phone,
                        code: this.form.code
                    })
                    .catch(e => {
                        if (e) {
                            this.$toast(e.error);
                        }
                        return Promise.reject();
                    })
                    .then(res => {
                        localStorage.setItem('nineToken', res);
                        return this.$store.dispatch('getUserInfo');
                    });
            }
        }
    }
};
</script>

<style lang="less" scoped>
.login {
    padding: 40px 50px 100px;
    background-repeat: no-repeat;
    background-size: 100% 100%;
}
.loginHeight {
    min-height: calc(100vh - 46px);
}
/deep/input:-webkit-autofill {
    box-shadow: 0 0 0px 1000px #101010 inset;
    -webkit-text-fill-color: #898989;
}
/deep/.van-field__control {
    color: #fff;
    font-size: 14px;
}
.logo {
    width: 94px;
    height: 16px;
    display: block;
    margin-bottom: 2px;
}
.tabs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 10px;
    .tab {
        font-size: 16px;
        color: #c8c9cc;
        line-height: 30px;

        &.active {
            font-size: 20px;
            font-weight: bold;
            color: #25f50d;
            line-height: 30px;
        }
    }
}

/deep/.field-box {
    background: #101010;
    box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.5);
    border-radius: 12px;
    .van-cell:after {
        left: 56px;
        right: 16px;
        border: none;
        height: 1px;
        background: rgba(137, 137, 137, 0.3);
    }
    .van-cell + .van-cell {
        margin-top: 0 !important;
    }
}

.icon {
    display: block;
    margin-top: 16px;
}
/deep/ .van-form {
    margin-top: 28px;
    .van-cell {
        padding: 0px 16px;

        .van-field__left-icon {
            margin-right: 16px;
        }
    }

    .van-cell + .van-cell {
        margin-top: 20px;
    }
    .van-field__body {
        height: 56px;
        align-items: center;
    }
}
.button {
    margin-top: 60px;
    position: relative;

    .del {
        margin-top: 20px;
        border-color: #ffffff;
        color: #ffffff;
        background-color: #101010;
    }

    .sure {
        color: #000000 !important;
    }

    .van-button {
        font-weight: bold;
        border-radius: 12px;
    }
}

.sub-code {
    padding-right: 0;
    border-width: 0;
}

.forget {
    position: absolute;
    font-weight: normal !important;
    right: 0;
    top: -65px;
}
.xieyi {
    .flex();
    justify-content: center;
    margin-top: 50px;
    font-size: @font1;

    span {
        color: @prim;
    }
}

/deep/.van-checkbox__icon--checked .van-icon {
    background: linear-gradient(135deg, @prim 0%, @warn 100%);
    border-width: 0;
}
</style>
