<template>
    <div class="login loginHeight">
        <div class="title">
            <div class="text1">欢迎来到拉索</div>
            <div class="text2">看发现更多数字藏品</div>
        </div>
        <van-form @submit="submit" ref="code">
            <div class="field-box">
                <van-field
                    type="tel"
                    name="手机号码"
                    placeholder="请输入手机号"
                    v-model="form.phone"
                    :rules="[
                        { required: true, message: '请输入手机号码' },
                        {
                            pattern: phonePattern,
                            message: '手机号码格式错误'
                        }
                    ]"
                >
                </van-field>

                <van-field
                    type="code"
                    name="验证码"
                    placeholder="请输入验证码"
                    v-model="form.code"
                    :rules="[{ required: true, message: '请输入验证码' }]"
                >
                    <template #button>
                        <van-button
                            class="sub-code"
                            size="small"
                            plain
                            type="primary"
                            @click="sendPhone"
                            :disabled="isSend"
                        >
                            {{ isSend ? `已发送(${sendNum})S` : '获取验证码' }}
                        </van-button>
                    </template>
                </van-field>
            </div>
            <div class="button">
                <div class="xieyi">
                    <van-checkbox v-model="checked">
                        已阅读并同意
                        <span @click.stop="$router.push('/agreement?page=service')"> 《服务协议》 </span>
                        <span @click.stop="$router.push('/agreement')"> 《用户隐私协议》 </span>
                    </van-checkbox>
                </div>
                <!-- <van-button plain class="forget" @click="$router.replace('/forget')">
          忘记密码？
        </van-button> -->

                <van-button block size="large" native-type="submit" type="primary" class="sure" :disabled="!canLogin"
                    >登录</van-button
                >
            </div>
        </van-form>
    </div>
</template>

<script>
import phone from '../../mixins/phone';
let fromRoute = null;
export default {
    mixins: [phone],
    inject: ['bar'],
    data() {
        return {
            active: 'code',
            form: {
                phone: '',
                password: '',
                code: ''
            },
            checked: localStorage.getItem('agreeTerm') === 'true'
        };
    },
    computed: {
        canLogin() {
            return this.form.phone && this.form.code;
        }
    },
    beforeRouteEnter(to, from) {
        fromRoute = from;
    },
    mounted() {
        console.log(this.$store.state.loginBackUrl);
    },
    methods: {
        sendPhone(e) {
            if (!this.checkWebDriver(e)) {
                return;
            }
            this.$refs.code.validate('手机号码').then(() => {
                this.sendMsg(this.form.phone);
            });
        },
        submit() {
            if (!this.checked) {
                this.$toast('请先阅读并同意隐私政策');
                return;
            }
            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 ||
                        !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');
                    });
            }
        }
    },
    watch: {
        checked(val) {
            if (val) {
                localStorage.setItem('agreeTerm', true);
            } else {
                localStorage.removeItem('agreeTerm');
            }
        }
    }
};
</script>

<style lang="less" scoped>
.login {
    padding: 10px 40px 100px;
    background-repeat: no-repeat;
    background-size: 100% 100%;
}
.title {
    .text1 {
        font-size: 32px;
        font-weight: bold;
        color: #ffffff;
        line-height: 45px;
    }
    .text2 {
        font-size: 16px;
        color: #ffffff;
        line-height: 22px;
        opacity: 0.8;
        margin-top: 10px;
    }
}
.loginHeight {
    min-height: calc(100vh - 46px);
    min-height: calc(var(--app-height) - 46px);
}
/deep/input:-webkit-autofill {
    box-shadow: 0 0 0px 1000px @bg inset;
    -webkit-text-fill-color: #ffffff;
}
/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: 0px;
        right: 0px;
        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 0px;

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

    .van-cell + .van-cell {
        margin-top: 20px;
    }
    .van-field__body {
        height: 67px;
        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;
        height: 60px;
    }

    .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;
    padding-bottom: 18px;

    color: #5e6168;
    span {
        color: #fff;
    }
}
.van-checkbox {
    // background-color: #101010;
    padding: 5px 0;
    border-radius: 12px;
}
/deep/.van-checkbox__label {
    color: #5e6168;
}

/deep/.van-checkbox__icon {
    height: 14px;
}

/deep/.van-checkbox__icon--checked .van-icon {
    background: @prim;
    border-width: 0;
    width: 14px;
    height: 14px;
}
</style>
