<template>
    <div class="login">
        <div class="login-par">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
                <!-- <div class="login-logo">
                    <img src="../assets/image/login_logo.png" style="width:80px;height:80px;"/>
                </div> -->
                <h3 class="title">{{ $t('zhuCe') }}</h3>
                <el-form-item class="login-input" prop="companyName">
                    <el-input type="text" v-model="ruleForm.companyName" autocomplete="off" :placeholder="$t('gongSiMing')" clearable prefix-icon="el-icon-s-home"></el-input>
                </el-form-item>
                <el-form-item class="login-input" prop="name">
                    <el-input type="text" v-model="ruleForm.name" autocomplete="off" :placeholder="$t('lable.name')" clearable prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item class="login-input" prop="phone">
                    <el-input type="text" v-model="ruleForm.phone" autocomplete="off" :placeholder="$t('lable.phone')" clearable prefix-icon="el-icon-mobile-phone"></el-input>
                </el-form-item>
                <el-form-item class="login-input" prop="vcode">
                    <el-input type="text" v-model="ruleForm.vcode" autocomplete="off" :placeholder="$t('yanZhengMa')" @keyup.enter.native="sendVcode()" clearable prefix-icon="iconfont firerock-iconyanzhengma">
                        <el-button slot="append" @click="sendVcode" :disabled="ruleForm.phone=='' || showTimer">{{ $t('faSongYanZhengMa') }}<span v-if="showTimer">({{countNum}})</span></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item class="login-input" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off" :placeholder="$t('sheZhiMiMaChangDuBuDiYu_6Wei')" clearable prefix-icon="iconfont firerock-iconmima">
                    </el-input>
                </el-form-item>
                <el-form-item class="login-input" prop="repwd">
                    <el-input type="password" v-model="ruleForm.repwd" autocomplete="off" :placeholder="$t('zhongFuMiMa')" clearable prefix-icon="iconfont firerock-iconmima">
                    </el-input>
                </el-form-item>
                
                <el-form-item class="login-input" prop="type" style="text-align:center;">
                    <el-divider ></el-divider>
                    <div style="margin-top:10px;">
                    <span>{{ $t('xuanZeBanBen') }} </span><el-select v-model="ruleForm.type" style="width:250px;">
                        <el-option v-for="item in typeList" :value="item.id" :label="item.name" :key="item.id"></el-option>
                    </el-select>
                    </div>
                </el-form-item>
                <div class="login-button" style="width:100%;margin-top:20px;">
                    <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit" :loading="logining">{{ $t('zhuCe') }}</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                typeList:[{id:1,name:this.$t('gongShiTongJiJiChuBan')},
                {id:2,name:this.$t('xiangMuGuanLiZhuanYeBan')},{id:3,name:this.$t('qiYeBanGongQiJianBan')},{id:4,name:this.$t('gongChengGuanLiZhuanYeBan')}],
                logining: false,
                showTimer: false,
                countNum: 60,
                // 登录信息
                ruleForm: {
                    companyName: '',
                    name: '',
                    phone: '',
                    type:2,
                },
                rules: {
                    companyName: [{ required: true, message: this.$t('qingShuRuGongSiMing'), trigger: 'blur' },],
                    name: [{ required: true, message: this.$t('defaultText.PleaseEnterYourName'), trigger: 'blur' },],
                    phone: [{ required: true, message: this.$t('defaultText.PleaseEnterYourPhone'), trigger: 'blur' },],
                    vcode: [{ required: true, message: this.$t('qingShuRuYanZhengMa'), trigger: 'blur' },],
                    password: [{ required: true, message: this.$t('qingSheZhiMiMaChangDuBuDiYu_6Wei'), trigger: 'blur' },],
                    repwd: [{ required: true, message: this.$t('qingZhongFuShuRuMiMa'), trigger: 'blur' },]
                },
            };
        },
        mounted() {
            
        },
        methods: {
            //开始倒计时
            countDown() {
                if (this.countNum > 0) {
                    this.countNum--;
                } else {
                    clearInterval(this.timer);
                    this.showTimer = false;
                }
            },
            sendVcode() {
                if (this.ruleForm.phone.length != 11) {
                    this.$message({
                                    message: this.$t('shouJiHaoMaGeShiBuZhengQue'),
                                    type: 'error'
                                });
                } else {
                    this.http.post('/user/sendVcode', {mobile: this.ruleForm.phone} , res => {
                            this.logining = false;
                            if (res.code == "ok") {
                                this.$message({
                                    message: this.$t('faSongChengGong'),
                                    type: 'success'
                                });
                                this.showTimer = true;
                                this.countNum = 60;
                                this.timer = setInterval(this.countDown, 1000);
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.logining = false;
                            this.$message({
                                message: error,
                                type: 'error'
                            });
                        })
                }
            },
            handleReset2() {
                this.$refs.ruleForm.resetFields();
            },

            handleSubmit(ev) {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if (this.ruleForm.password.length < 6) {
                            this.$message({
                                    message: this.$t('miMaChangDuBuNengShaoYu_6Wei'),
                                    type: 'error'
                                });
                            return;
                        }
                        //检查两次密码是否一致
                        if (this.ruleForm.password != this.ruleForm.repwd) {
                            this.$message({
                                    message: this.$t('liangCiShuRuDeMiMaBuYiZhi'),
                                    type: 'error'
                                });
                            return;
                        }
                        let formObj = this.ruleForm
                        if(this.$route.query != {}) {
                            formObj = {
                                ...this.$route.query,
                                ...formObj
                            }
                        }
                        var _this = this;
                        this.logining = true;
                        this.http.post(this.port.manage.register, formObj, res => {
                            this.logining = false;
                            if (res.code == "ok") {
                                this.$message({
                                    message: this.$t('zhuCeChengGong'),
                                    type: 'success'
                                });
                                this.$router.push({ path: '/login' });
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.logining = false;
                            this.$message({
                                message: error,
                                type: 'error'
                            });
                        })
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
@import "../assets/scss/handle";
    .login {
        height: 100%;
        .login-par {
            width: 100%;
            min-height: 100%;
            background: #f0f2f5 url('../assets/image/background.png') no-repeat 50%;
            background-size: 100%;
            padding: 110px 0 144px;
            position: relative;
            box-sizing: border-box;
            .login-logo {
                text-align: center;
                margin: 0 0 20px 0;
            }
            .login-container {
                -webkit-border-radius: 5px;
                border-radius: 5px;
                -moz-border-radius: 5px;
                background-clip: padding-box;
                width: 450px;
                height: 495px;
                padding: 25px 35px 25px 35px;
                background: #fff;
                border: 1px solid;
                box-shadow: 0 0 5px #cac6c6;
                border-top: 10px solid;
                @include border_color("border_color");
                margin:auto;
                .title {
                    font-size: 20px;
                    margin: 0px auto 30px auto;
                    text-align: center;
                    color: #505458;
                }
                .remember {
                    margin: 0px 0px 35px 0px;
                }
                .login-input {
                    margin: 15px 0 0 0;
                }
                .login-button {
                    margin: 50px 0 0 0;
                }
                .login-button .el-button {
                    padding: 14px;
                }
            }
        }
    }
</style>