<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">User Register</div>
            <el-form
                class="ms-content"
                :rules="registerRules"
                ref="registerForm"
                :model="registerForm"
                label-width="0"
            >
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input
                        size="small"
                        v-model="registerForm.username"
                        auto-complete="off"
                        placeholder="username"
                    >
                        <i slot="prefix" class="el-icon-user"></i>
                    </el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input
                        size="small"
                        v-model="registerForm.password"
                        auto-complete="off"
                        placeholder="password"
                        show-password
                    >
                        <i slot="prefix" class="el-icon-lock"></i>
                    </el-input>
                </el-form-item>
                <!-- 姓名 -->
                <el-form-item prop="name">
                    <el-input
                        size="small"
                        v-model="registerForm.name"
                        auto-complete="off"
                        placeholder="realname"
                    >
                        <i slot="prefix" class="el-icon-edit-outline"></i>
                    </el-input>
                </el-form-item>
                <!-- 手机 -->
                <el-form-item prop="phone">
                    <el-input
                        size="small"
                        v-model="registerForm.phone"
                        auto-complete="off"
                        placeholder="phone"
                    >
                        <i slot="prefix" class="el-icon-mobile-phone" />
                    </el-input>
                </el-form-item>
                <!-- 邮件 -->
                <el-form-item prop="email">
                    <el-input
                        size="small"
                        v-model="registerForm.email"
                        auto-complete="off"
                        placeholder="email"
                    >
                        <i slot="prefix" class="el-icon-message"></i>
                    </el-input>
                </el-form-item>
                <!-- ORCID -->
                <!-- <el-form-item prop="orcid" v-if="registerForm.usertype === '1'">
                    <el-input
                        size="small"
                        v-model="registerForm.orcid"
                        auto-complete="off"
                        placeholder="ORCID"
                    >
                        <i slot="prefix" class="el-icon-postcard" />
                    </el-input>
                </el-form-item> -->

                <!-- 验证码 -->
                <el-form-item prop="code">
                    <el-row :span="24">
                        <el-col :span="16">
                            <el-input
                                size="small"
                                v-model="registerForm.code"
                                auto-complete="off"
                                placeholder="captcha"
                            >
                                <i slot="prefix" class="el-icon-key" />
                            </el-input>
                        </el-col>
                        <el-col :span="8">
                            <div class="login-code">
                                <img
                                    :src="image"
                                    @click="refreshCode"
                                    alt="captcha"
                                    class="captchaimg"
                                />
                            </div>
                        </el-col>
                    </el-row>
                </el-form-item>
                <!-- 按钮 -->
                <el-row type="flex" justify="center">
                    <el-col :span="6">
                        <el-form-item>
                            <el-button
                                size="small"
                                type="primary"
                                @click.native.prevent="handleRegister"
                                class="register-submit"
                            >Register</el-button>
                        </el-form-item>
                    </el-col>
                    <el-col :span="14"></el-col>
                    <el-col :span="4">
                        <el-link :underline="false" type="primary" href="/" class="rebacklogin">Go login</el-link>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function() {
        return {
            baseUrl: this.Common.baseUrl,
            //验证码图片
            image: '',
            registerForm: {
                //用户名
                username: '',
                //密码
                password: '',
                //姓名
                name: '',
                //手机
                phone: '',
                //邮件
                email: '',
                //ORCID
                // orcid: '',
                //验证码的值
                code: '',
                //验证码key
                random_num: 0

            },
            registerRules: {
                username: [
                    {
                        required: true,
                        message: 'Please enter your username.',
                        trigger: 'blur'
                    }
                ],
                password: [
                    {
                        required: true,
                        message: 'Please enter your password.',
                        trigger: 'blur'
                    },
                    {
                        min: 6,
                        message: 'The password number cannot be less than 6 characters.',
                        trigger: 'blur'
                    }
                ],
                name: [
                    {
                        required: true,
                        message: 'Please enter your real name.',
                        trigger: 'blur'
                    }
                ],
                phone: [
                    {
                        required: true,
                        message: 'Please enter the correct mobile phone number.',
                        trigger: 'blur'
                    }
                ],
                email: [
                    {
                        required: true,
                        message: 'Please enter your email address.',
                        trigger: 'blur'
                    },
                    {
                        type: 'email',
                        message: 'Email address format error（example@gmail.com）.',
                        trigger: 'blur'
                    }
                ],
                code:[
                    {
                        required:true,
                        message:'Captcha code cannot be empty.',
                        trigger:'blur'
                    }
                ]
            }
        };
    },
    created: function() {
        this.getCaptcha();
    },
    methods: {
        refreshCode() {
            this.getCaptcha();
        },
        handleRegister() {
            this.$refs.registerForm.validate(valid => {
                if (valid) {
                    this.$api
                        .post('api/user/register', this.registerForm)
                        .then(res => {
                            if(res == 'existence'){//重复注册
                                this.$message.error('Failed to register, user name email is occupied!');
                                return false;
                            }else if(res == 'errcaptcha'){
                                this.$message.error('Verification code error!');
                                return false;
                            }else{
                                this.$message.success('register success');
                                this.$router.push('/');
                            }
                        })
                        .catch(err => {
                            console.log(err);
                        });
                } else {
                    this.$message.error('The registration information is incorrect. Please check and submit again');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        getCaptcha() {
            this.registerForm.random_num = Math.random();
            this.image = this.baseUrl+'api/User/testCaptcha?a=' + this.registerForm.random_num;
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 500px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.register-submit {
    margin: auto;
}
.rebacklogin {
    padding-top: 13px;
}
.captchaimg {
    margin-top: 2px;
    margin-left: 8px;
}
</style>