<template>
    <div class="login">
        <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'">
            <van-field
                type="tel"
                name="用户名"
                placeholder="请输入手机号码"
                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="请输入密码"
                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 class="button">
                <!-- <van-button plain class="forget" @click="$router.replace('/forget')">
          忘记密码？
        </van-button> -->

                <van-button
                    round
                    block
                    native-type="submit"
                    color="linear-gradient(to right, #FDFB60, #FF8F3E)"
                    class="sure"
                    >登录</van-button
                >
                <van-button class="del" round block plain @click="$router.replace('/register')"
                    >暂无账号，立即注册</van-button
                >
            </div>
        </van-form>

        <van-form @submit="submit" ref="code" v-else>
            <van-field
                type="tel"
                name="手机号码"
                placeholder="请输入手机号码"
                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="请输入验证码"
                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
                        type="primary"
                        @click="sendPhone"
                        :disabled="isSend"
                    >
                        {{ isSend ? `已发送(${sendNum})S` : '发送验证码' }}
                    </van-button>
                </template>
            </van-field>
            <div class="button">
                <!-- <van-button plain class="forget" @click="$router.replace('/forget')">
          忘记密码？
        </van-button> -->

                <van-button
                    round
                    block
                    native-type="submit"
                    color="linear-gradient(to right, #FDFB60, #FF8F3E)"
                    class="sure"
                    >登录</van-button
                >
                <van-button class="del" round block plain @click="$router.replace('/register')"
                    >暂无账号，立即注册</van-button
                >
            </div>
        </van-form>
    </div>
</template>

<script>
import phone from '../../mixins/phone';
export default {
    mixins: [phone],
    data() {
        return {
            active: 'phone',
            form: {
                phone: '',
                password: '',
                code: ''
            }
        };
    },
    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(() => {
                    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;
}

.tabs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .tab {
        font-size: 18px;
        color: #c8c9cc;
        line-height: 25px;

        &.active {
            font-size: 24px;
            font-weight: bold;
            color: #ffffff;
            line-height: 33px;
        }
    }
}

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

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

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

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

    .sure {
        color: @bg !important;
    }

    .van-button {
        font-weight: bold;
    }
}

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

.forget {
    position: absolute;
    font-weight: normal !important;
    right: 0;
    top: -65px;
}
</style>
