<template>
    <div class="passport-layout-web-reg web-v3-layout-box">
        <div class="web-v3-common-logo main-logo">
            <img src="../../assets/franchise1/logo.png" class="logo-image" alt="">
        </div>
        <div class="is-h5">
            <div class="login-com-lang">
                <div class="newLogin_lang-box">

                </div>
            </div>
            <div class="language-options-h5-mask">

            </div>
            <div class="newLogin_lang-options newLogin_lang-options-down"></div>
        </div>

        <div class="web-reg-left">
            <img class="isv-img" src="../../assets/franchise1/left_list.png" alt="">
        </div>
        <div class="web-reg-right">
            <div class="web-req-right-top">
                <div class="web-main-content">
                    <div class="login-content-container">
                        <div class="new-account-login-box new-account-login-box-hide-back">
                            <div class="new-account-login-module new-account-module-nopadding">
                                <div class="step-box enter-credential ud__modal__content">
                                    <!-- <div class="ud__modal__header">
    
                                    </div> -->
                                    <el-form :model="form" :rules="rules"
                                        class="el-row demo-ruleForm" ref="formRef">
                                        <div class="right-form">
                                            <GuipFormItem column="column" addClass="w388">
                                                <div slot="formLeft" class="form-top-icon">
                                                    <img src="../../assets/franchise1/storeNum.svg" alt="">
                                                    <span>订单号</span>
                                                </div>
                                                <GuipInput slot="formDom" ref="GuipInput" v-model="form.tid" prop="tid" placeholder="拍1件金额3000元订单" desc="2000元加盟费+1000元充值账户余额"/>
                                            </GuipFormItem>
                                            <GuipFormItem column="column" addClass="w388">
                                                <div slot="formLeft" class="form-top-icon">
                                                    <img src="../../assets/franchise1/nicheng.svg" alt="">
                                                    <span>平台昵称</span>
                                                </div>
                                                <GuipInput slot="formDom" ref="GuipInput" v-model="form.nick" prop="nick" placeholder="为自己取个平台昵称" desc="仅用于后台内使用、区分子账号"/>
                                            </GuipFormItem>
                                            <GuipFormItem column="column" addClass="w388">
                                                <div slot="formLeft" class="form-top-icon">
                                                    <img src="../../assets/franchise1/nicheng.svg" alt="">
                                                    <span>联系人姓名</span>
                                                </div>
                                                <GuipInput slot="formDom" ref="GuipInput" v-model="form.name" prop="name" placeholder="您的真实姓名"/>
                                            </GuipFormItem>

                                            <!-- <PhoneCode v-model="form.phoneCode" :phone-prop="'phone'"
                                                :code-prop="'code'" @send-code="handleSendCode" /> -->
                                            <GuipFormItem column="column" addClass="w388">
                                                <div slot="formLeft" class="form-top-icon">
                                                    <img src="../../assets/franchise1/storeNum.svg" alt="">
                                                    <span>手机号码</span>
                                                </div>
                                                <el-form-item slot="formDom" prop="phone"
                                                    style="width: 100%;">
                                                    <div style="width: 100%;" class="flex-between">
                                                        <el-input v-model="form.phone" @input="clearPhoneError"
                                                            placeholder="请输入手机号" style="width: 277px;" clearable
                                                            @blur="validatePhone">
                                                        </el-input>
                                                        <GuipButton type="reverseMild"
                                                            :btnstyle="{ width: '99px', height: '38px' }"
                                                            :disabled="!canSend || countdown > 0" @click="sendCode"
                                                            class="send-btn">
                                                            {{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}</GuipButton>
                                                    </div>
                                                </el-form-item>
                                            </GuipFormItem>
                                            <!-- 验证码输入框 -->
                                            <GuipFormItem column="column" addClass="w388">
                                                <div slot="formLeft" class="form-top-icon">
                                                    <img src="../../assets/franchise1/codeNumber.svg" alt="">
                                                    <span>验证码</span>
                                                </div>
                                                <el-form-item slot="formDom" prop="sms_code">
                                                    <el-input v-model="form.sms_code" placeholder="请输入验证码" clearable
                                                        @blur="validateCode" @input="clearCodeError"></el-input>
                                                </el-form-item>
                                            </GuipFormItem>
                                        </div>
                                    </el-form>
                                    <div class="ud__modal__footer">
                                        <div class="step-box__footer">
                                            <GuipButton :btnstyle="btnstyleObj" type="primary" @click="goAddFranchise">
                                                加盟并进入后台</GuipButton>
                                            <p class="attention gap10">
                                                <el-checkbox v-model="checked"></el-checkbox>
                                                <b>代理商需要阅读并同意<a href="">《哈哈哈哈》</a></b>
                                            </p>
                                        </div>

                                    </div>
                                </div>
                            </div>

                        </div>

                    </div>

                </div>
            </div>
            <Footer></Footer>
        </div>

    </div>
</template>
<script>
// import PhoneCode from '@/components/PhoneCode.vue';
import GuipButton from '@/components/GuipButton.vue';
import GuipInput from '@/components/GuipInput.vue';
import GuipFormItem from '@/components/GuipFormItem.vue';
import Footer from '@/components/Footer.vue';

export default {
    data() {
        // 手机号验证规则
        const validatePhone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('请输入手机号'))
            }
            if (!/^1[3-9]\d{9}$/.test(value)) {
                return callback(new Error('手机号格式不正确'))
            }
            callback()
        }

        // 验证码验证规则
        const validateCode = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('请输入验证码'))
            }
            // 校验可改
            if (!/^\d{4,6}$/.test(value)) {
                return callback(new Error('验证码必须是4-6位数字'))
            }
            callback()
        }

        return {
            checked: true,
            btnstyleObj: {
                width: '388px',
                height: '46px'
            },
            form: {
                tid: '',
                name: '',
                phone: '',
                sms_code: '',
                nick: '',
                recommended_aid: '',
            },
            countdown: 0,
            timer: null,
            rules: {
                tid: [
                    { required: true, message: '请输入订单号', trigger: 'blur' }
                ],
                nick: [
                    { required: true, message: '请输入平台昵称', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入真实姓名', trigger: 'blur' }
                ],
                phone: [
                    { validator: validatePhone, trigger: 'blur' }
                ],
                sms_code: [
                    { validator: validateCode, trigger: 'blur' }
                ]
            },

        };
    },
    components: {
        GuipButton,
        GuipInput,
        GuipFormItem,
        // PhoneCode,
        Footer
    },
    mounted(){
        if(this.$route.aid){
            this.form.recommended_aid = this.$route.aid;
        }
    },
    computed: {
        canSend() {
            return /^1[3-9]\d{9}$/.test(this.form.phone)
        }
    },
    methods: {
        // 校验手机号
        validatePhone() {
            this.$refs.formRef.validateField('phone')
        },

        // 校验验证码
        validateCode() {
            this.$refs.formRef.validateField('code')
        },

        // 手机号输入时清除错误
        clearPhoneError() {
            if (/^1[3-9]\d{9}$/.test(this.form.phone)) {
                this.$refs.formRef.clearValidate('phone')
            }
        },

        // 验证码输入时清除错误
        clearCodeError() {
            if (/^\d{6}$/.test(this.form.code)) {
                this.$refs.formRef.clearValidate('code')
            }
        },
        // 发送验证码
        sendCode() {
            if (!this.canSend || this.countdown > 0) return

            // 开始倒计时
            this.countdown = 60
            this.timer = setInterval(() => {
                this.countdown--
                if (this.countdown <= 0) {
                    clearInterval(this.timer)
                }
            }, 1000)

            const that = this
            this.$http('POST', '/agentnew/ajax_send_verify_code', {
                phone: that.form.phone,
            }).then(response => {
                if (!response.status) {
                    that.$message.warning(response.info);
                }else{
                    that.$message.success(response.info);
                }
            }).catch(error => {
                console.error(error, 'error')
            })
        },

        goAddFranchise(){
            if(!this.checked){
                this.$message.warning('请阅读并同意协议');
                return false;
            }

            const that = this
            that.$refs.formRef.validate((valid) => {
                if (valid) {
                    this.$http('POST', '/agentnew/ajax_submit_register_info', {
                        tid: that.form.tid,
                        name: that.form.name,
                        phone: that.form.phone,
                        sms_code: that.form.sms_code,
                        nick: that.form.nick,
                        recommended_aid: that.form.recommended_aid,
                    }).then(response => {
                        if (!response.status) {
                            that.$message.warning(response.info);
                        }else{
                            that.$message.success(response.info);
                        }
                    }).catch(error => {
                        console.error(error, 'error')
                    })
                } else {
                    console.log('表单校验失败');
                    return false;
                }
            });
        }
    },
    beforeDestroy() {
        if (this.timer) clearInterval(this.timer);
    },
}
</script>
<style lang="scss" scoped>
@import '../../style/franchise';
::v-deep .el-form-item {
    margin-bottom: 0;
}
.right-form {
    padding: 40px 40px 0;
}

.form-item1 {
    margin-bottom: 24px;
}

.attention {
    margin-top: 20px;

    b {
        font-size: 12px;
        font-weight: normal;

        a {
            text-decoration: none;
            color: #006AFF;
        }
    }
}

.web-req-right-top {
    flex: 1;
}
</style>