<template>
    <div
        class="login"
        :class="{ loginHeight: bar.value && bar.value.show }"
        :style="{ backgroundImage: `url(${require('../../assets/login.jpg')})` }"
    >
        <img class="logo" src="../../assets/lvzhopu-logo.png" alt="" />
        <div class="tabs">
            <div class="tab active">用户注册</div>
        </div>

        <van-form @submit="submit">
            <div class="field-box">
                <van-field
                    type="tel"
                    name="手机号码"
                    placeholder="请输入手机号码"
                    v-model="form.phone"
                    :maxlength="11"
                    :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="digit"
                    name="验证码"
                    placeholder="请输入验证码"
                    v-model="form.code"
                    :maxlength="4"
                    :rules="[{ required: true, message: '请输入验证码' }]"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_code.svg')" class="icon" />
                    </template>
                    <template #button>
                        <van-button
                            @click="sendPhone"
                            class="sub-code"
                            size="small"
                            plain
                            :disabled="isSend"
                            color="#26F50D"
                        >
                            {{ isSend ? `已发送(${sendNum})S` : '发送验证码' }}
                        </van-button>
                    </template>
                </van-field>
                <van-field
                    name="图形验证码"
                    placeholder="请输入图形验证码"
                    v-model="form.captcha"
                    :maxlength="6"
                    :rules="[{ required: true, message: '请输入图形验证码' }]"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_code.svg')" class="icon" />
                    </template>
                    <template #button>
                        <van-image @click="getCode" :src="codeImg" width="68" height="32" fit="contain" />
                    </template>
                </van-field>
                <van-field
                    type="password"
                    name="密码"
                    placeholder="请输入密码"
                    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>

                <van-field
                    type="password"
                    name="密码"
                    placeholder="请再次输入密码"
                    v-model="form.password2"
                    :rules="[
                        { required: true, message: '请再次填写密码' },
                        {
                            validator: val => {
                                if (val == form.password) {
                                    return true;
                                } else {
                                    return false;
                                }
                            },
                            message: '两次密码输入不一致'
                        }
                    ]"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_mima.svg')" class="icon" />
                    </template>
                </van-field>
                <van-field
                    v-if="showInvite"
                    type="text"
                    name="邀请码"
                    placeholder="请输入邀请码"
                    v-model="form.inviteCode"
                    :disabled="!!$store.state.inviteCode"
                >
                    <template #left-icon>
                        <img :src="require('@assets/svgs/login_icon_yao qingma.svg')" class="icon" />
                    </template>
                </van-field>
            </div>
            <div class="button">
                <van-button round block native-type="submit" color="#26F50D" class="sure"> 立即申领 </van-button>
                <van-button class="del" round block plain @click="$router.replace('/login')">
                    已有古藏站登陆许可 去登陆
                </van-button>
            </div>
        </van-form>

        <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>
    </div>
</template>

<script>
import phone from '../../mixins/phone';
let fromRoute = null;
export default {
    mixins: [phone],
    inject: ['bar'],
    data() {
        return {
            active: 'phone',
            form: {
                phone: '',
                password: '',
                password2: '',
                code: '',
                inviteCode: this.$store.state.inviteCode || '',
                captchaKey: '',
                captcha: ''
            },
            checked: false,
            showInvite: true,
            codeImg: ''
        };
    },
    beforeRouteEnter(to, from) {
        fromRoute = from;
        console.log(to, from);
    },
    methods: {
        getCode() {
            this.$http.get('/captcha/get').then(res => {
                this.form.captchaKey = res.key;
                this.codeImg = res.image;
            });
        },
        sendPhone() {
            this.$refs.form.validate('手机号码').then(() => {
                this.sendMsg(this.form.phone);
            });
        },
        submit() {
            if (!this.checked) {
                this.$toast('请先阅读并同意隐私政策');
                return;
            }
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            this.$http
                .post('/auth/phoneRegister', this.form)
                .then(res => {
                    localStorage.setItem('nineToken', res);
                    return this.$store.dispatch('getUserInfo');
                })
                .then(() => {
                    this.$toast.success('注册成功');
                    setTimeout(() => {
                        if (!fromRoute.name || fromRoute.name === 'userRegister' || fromRoute.name === 'userLogin') {
                            this.$router.replace('/home');
                        } else {
                            this.$router.back();
                        }
                    }, 1500);
                })
                .catch(e => {
                    if (e) {
                        this.$toast(e.error);
                    }
                });
        }
    },
    mounted() {
        if (this.$store.state.invitor && this.$store.state.productId) {
            this.showInvite = false;
        } else {
            this.showInvite = true;
        }

        this.getCode();
    }
};
</script>

<style lang="less" scoped>
.login {
    padding: 40px 50px 100px;
    background-repeat: no-repeat;
    background-size: 100% 100%;
}
.loginHeight {
    min-height: calc(100vh - 46px);
}
.tabs {
    display: flex;
    justify-content: center;
    align-items: center;
    .tab {
        font-size: 18px;
        color: #c8c9cc;
        line-height: 25px;

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

.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;
    }
}

/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;
    }
}

.button {
    margin-top: 60px;

    .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;
}
.xieyi {
    .flex();
    justify-content: center;
    margin-top: 50px;
    font-size: @font1;
    color: #fff;
    span {
        color: #25f50d;
    }
}
.van-checkbox {
    background-color: #101010;
    padding: 5px 10px;
    border-radius: 12px;
}
/deep/.van-checkbox__label {
    color: #fff;
}
/deep/.van-checkbox__icon--checked .van-icon {
    background: linear-gradient(135deg, @prim 0%, @warn 100%);
    border-width: 0;
}
/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: 0 auto 2px;
}
</style>
