<template>
    <Cn-page :loading="loading">
        <view slot="body">
            <view class="index">
                <template v-if="step == 1">
                    <uni-forms ref="form" :rules="rules" :modelValue="formData">
                        <uni-forms-item name="phone">
                            <uni-easyinput type="number" v-model="formData.phone" maxlength="11" placeholder="请输入手机号" />
                        </uni-forms-item>
                        <uni-forms-item name="code">
                            <view class="login-box-input">
                                <uni-easyinput type="text" v-model="formData.code" placeholder="请输入验证码" maxlength="6" />
                                <view class="ml40" style="
                                        margin-left: 40rpx;
                                        font-size: 28rpx;
                                        color: #666;
                                        width: 200rpx;
                                        text-align: center;
                                    " v-if="waitTime > 0">{{ waitTime + 's后重新获取' }}
                                </view>
                                <button class="login-box-input-btn" v-else @click="getCode" size="mini">
                                    获取验证码
                                </button>
                            </view>
                        </uni-forms-item>
                    </uni-forms>
                    <button type="default" class="submit-btn" :loading="loadingButton" :disabled="loadingButton"
                        @click="firstSubmit">注册</button>
                    <view class="login-box-tips">
                        <view style="color: #999">
                            <checkbox style="transform: scale(0.7)" :checked="checkbox" @click="checkbox = !checkbox" />
                            我已阅读并同意
                        </view>
                        <navigator url="/pages/mine/agreement" hover-class="none">《用户协议》</navigator>
                        <navigator url="/pages/mine/policy" hover-class="none">《隐私政策》</navigator>
                    </view>
                </template>
                <template v-else>
                    <uni-forms ref="form" :rules="rules" :modelValue="formData">
                        <uni-forms-item name="password">
                            <uni-easyinput type="text" v-model="formData.password" placeholder="请输入登录密码" />
                        </uni-forms-item>
                        <uni-forms-item name="password2">
                            <uni-easyinput type="text" v-model="formData.password2" placeholder="请再次确认密码" />
                        </uni-forms-item>
                    </uni-forms>
                    <button type="default" class="submit-btn" :loading="loadingButton" :disabled="loadingButton"
                        @click="secondSubmit">提交</button>
                    <view class="login-box-tips">
                        <view style="color: #999">说明：密码长度为6-18位</view>
                    </view>
                </template>
            </view>
        </view>
    </Cn-page>
</template>
<script>
import { apiRegister, apiGetYms, apiSetPsd, autoLogin } from '@/common/api/user'

export default {
    name: 'jiaban',
    data() {
        return {
            checkbox: false,
            step: 1,
            loading: false,
            waitTime: 0,
            inter: null,
            loadingButton: false,
            // 表单数据
            formData: {
                phone: '',
                code: '',
                password: '',
                password2: '',
            },
            rules: {
                phone: {
                    rules: [
                        {
                            required: true,
                            errorMessage: '请填写手机号',
                        },
                    ],
                },
                code: {
                    rules: [
                        {
                            required: true,
                            errorMessage: '请填写验证码',
                        },
                    ],
                },
                password: {
                    rules: [
                        {
                            required: true,
                            errorMessage: '请填写新密码',
                        },
                    ],
                },
                password2: {
                    rules: [
                        {
                            required: true,
                            errorMessage: '请填写确认密码',
                        },
                    ],
                },
            },
        }
    },
    onLoad() { },
    methods: {
        getCode() {
            if (!this.checkbox) {
                return this.$util.toast('请先阅读并同意用户协议和隐私政策！')
            }
            if (!this.formData.phone) {
                return this.$util.toast('请输入手机号！')
            }
            uni.showLoading({
                title: '请稍等...',
                mask: true,
            })
            apiGetYms({
                phone: this.formData.phone,
                type: 1,
            })
                .then((res) => {
                    uni.hideLoading()
                    console.log(res)
                    this.waitTime = 60
                    this.inter = setInterval(() => {
                        if (this.waitTime == 0) {
                            clearInterval(this.inter)
                        } else {
                            this.waitTime--
                        }
                    }, 1000)
                })
                .catch((err) => {
                    uni.hideLoading()
                    console.log(err)
                })
        },
        firstSubmit() {
            if (!this.checkbox) {
                return this.$util.toast('请先阅读并同意用户协议和隐私政策！')
            }

            this.$refs.form.validate().then((valid, errors) => {
                this.loadingButton = true
                apiRegister({
                    phone: this.formData.phone,
                    code: this.formData.code,
                }).then((res) => {
                    console.log(res)
                    this.$util.toast(res.msg)
                    uni.setStorageSync('userInfo', res.data)
                    // this.step = 2
                    // 直接登录
                    autoLogin(this.formData.phone).then((res) => {
                        this.$util.loginSuccess(res.data)
                    })
                }).finally(() => {
                    this.loadingButton = false
                })
            })
        },
        secondSubmit() {
            this.$refs.form.validate().then((valid, errors) => {
                this.loadingButton = true
                console.log(this.formData.password.length)
                if (this.formData.password.length < 6 || this.formData.password.length > 18) {
                    return this.$util.toast('密码长度为6-18位')
                } else if (this.formData.password != this.formData.password2) {
                    return this.$util.toast('两次密码不一致')
                }
                apiSetPsd({
                    phone: this.formData.phone,
                    password: this.formData.password,
                }).then((res) => {
                    console.log(res)
                    this.$util.toast(res.msg)
                    uni.setStorageSync('userInfo', res.data)
                    uni.reLaunch({
                        url: '/pages/user/login',
                    })
                }).finally(() => {
                    this.loadingButton = false
                })
            })
        },
    },
}
</script>
<style lang="scss">
.index {
    padding: 20rpx;

    .submit-btn {
        background: $uni-theme-color;
        color: #fff;
    }

    .login-box-tips {
        margin-top: 60rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 20rpx;
    }

    .login-box-input {
        display: flex;
        align-items: center;
        border-bottom: 1rpx solid #f0f0f0;

        .login-box-input-icon {
            width: 40rpx;
            margin-right: 40rpx;
        }

        .login-box-input-main {
            font-size: 28rpx;
            flex: 1;
            height: 100rpx;
            line-height: 100rpx;
        }

        .login-box-input-btn {
            padding: 0 40rpx;
            margin-left: 40rpx;
            background: $uni-theme-color;
            color: #fff;
        }

        .login-box-input-img {
            margin-left: 40rpx;
            background: skyblue;
            height: 80rpx;
            width: 200rpx;
        }
    }
}
</style>
