<div class="header_menu">
    <div class="container">
        <div class="header-nav clearfix">
            <?php echo $this->partial('shop/layout/headerMenu'); ?>
        </div>
    </div>
</div>
</div>

<div class="container">
    <div class="xm-plain-box">
        <div class="box-hd">
            <h3 class="title"><?php echo $this->translate('用户注册'); ?></h3>
        </div>
        <div class="box-bd">
            <div class="row">
                <div class="control-group">
                    <div class="message_one">
                    </div>
                </div>
                <?php if(isset($this->registerStatus) && !$this->registerStatus) { echo '<div><h4>'. $this->registerCloseMessage . '</h4></div>'; } else { ?>
                    <?php
                    $form = $this->form;
                    $form->prepare();
                    $form->setAttributes(['class' => 'form-horizontal']);

                    $form->get('userName')->setAttributes([
                        'class' => 'form-control',
                        'placeholder' => $this->translate('请输入登录名称')
                    ]);

                    $registerEmailState = $this->shopHelper()->customer('register_email_state', 'customer');
                    $registerPhoneState = $this->shopHelper()->customer('register_phone_state', 'customer');
                    $phoneCaptchaState  = $this->shopHelper()->customer('register_phone_captcha', 'customer');
                    ?>
                    <?php echo $this->form()->openTag($form); ?>
                    <?php echo $this->formElement($form->get('shopCsrf')); ?>
                    <?php echo $this->formElementErrors($form->get('shopCsrf'), ['class'=>'error-message']); ?>
                        <div class="control-group">
                            <label for="input01" class="control-label"><?php echo $this->translate('会员名称'); ?>：</label>
                            <div class="controls">
                                <?php echo $this->formElement($form->get('userName')); ?>
                                <?php echo $this->formElementErrors($form->get('userName'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <?php
                    if($registerEmailState == 1) {
                        $form->get('userEmail')->setAttributes([
                            'class' => 'form-control',
                            'placeholder' => $this->translate('请输入电子邮箱地址')
                        ]);
                            ?>
                            <div class="control-group">
                                <label for="input01" class="control-label"><?php echo $this->translate('电子邮箱'); ?>：</label>
                                <div class="controls">
                                    <?php echo $this->formElement($form->get('userEmail')); ?>
                                    <?php echo $this->formElementErrors($form->get('userEmail'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        <?php } ?>

                        <?php if($registerPhoneState == 1) { ?>
                        <div class="control-group">
                            <label for="input01" class="control-label"><?php echo $this->translate('手机号码'); ?><span class="must_add_value">*</span>：</label>
                            <div class="controls">
                                <?php
                                $form->get('userPhone')->setAttributes([
                                    'class' => 'form-control',
                                    'placeholder' => $this->translate('联系电话')
                                ]);
                                if(count($this->phoneArea) == 1) { ?>
                                    <?php echo $this->formElement($form->get('areaCode')); ?>
                                    <?php echo $this->formElementErrors($form->get('areaCode'), ['class'=>'error-message']); ?>
                                    <?php echo $this->formElement($form->get('userPhone')); ?>
                                    <?php echo $this->formElementErrors($form->get('userPhone'), ['class'=>'error-message']); ?>
                                <?php } else {
                                    $form->get('areaCode')->setAttributes([
                                        'style' => 'width:150px'
                                    ]);
                                    ?>
                                    <?php echo $this->formElement($form->get('areaCode')); ?>
                                    <?php echo $this->formElementErrors($form->get('areaCode'), ['class'=>'error-message']); ?>

                                    <?php echo $this->formElement($form->get('userPhone')); ?>
                                    <?php echo $this->formElementErrors($form->get('userPhone'), ['class'=>'error-message']); ?>
                                <?php } ?>
                            </div>
                        </div>
                        <?php if ($phoneCaptchaState == 1) { ?>
                            <div class="control-group">
                                <label for="input01" class="control-label"><?php echo $this->translate('手机验证码'); ?><span class="must_add_value">*</span>：</label>
                                <div class="controls">
                                    <?php echo $this->formElement($form->get('phoneCaptcha')->setAttributes(['class' => 'form-control', 'placeholder' => $this->translate('手机验证码')])); ?>
                                    <button id="get_phone_captcha" type="button" class="button button-code btn btn-info "><?php echo $this->translate('获取验证码'); ?></button>
                                    <?php echo $this->formElementErrors($form->get('phoneCaptcha'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        <?php } ?>
                        <?php } ?>
                        <div class="control-group">
                            <div class="controls">
                                <button class="btn btn-primary" type="submit"><?php echo $this->translate('提交(建立账户)'); ?></button>
                                &nbsp;&nbsp;
                                <button class="btn" type="button" onclick="location.href='<?php echo $this->url('shop-user', ['action' => 'otherUserRegister', 'id' => $this->loginType], ['query' => ['t' => 'bind']]); ?>'"><?php echo $this->translate('点击绑定现有账户'); ?></button>
                            </div>
                        </div>
                    <?php echo $this->form()->closeTag(); ?>

                    <script type="text/javascript">
                        $(document).ready(function() {
                            $("#<?php echo $form->getName(); ?>").validate({
                                rules: {
                                    <?php if ($registerEmailState == 1) { ?>
                                    userEmail: {
                                        required: true,
                                        email: true
                                    },
                                    <?php } ?>
                                    <?php if ($registerPhoneState == 1) { ?>
                                    userPhone: {
                                        required: true
                                    },
                                    <?php if ($phoneCaptchaState == 1) { ?>
                                    phoneCaptcha: {
                                        required: true
                                    },
                                    <?php } ?>
                                    <?php } ?>
                                    userName: {
                                        required: true
                                    }
                                },
                                messages: {
                                    <?php if ($registerEmailState == 1) { ?>
                                    userEmail: {
                                        required: "<?php echo $this->translate('请输入电子邮箱地址!'); ?>",
                                        email: "<?php echo $this->translate('请输入有效的电子邮箱地址!'); ?>"
                                    },
                                    <?php } ?>
                                    <?php if ($registerPhoneState == 1) { ?>
                                    userPhone: {
                                        required: "<?php echo $this->translate('请输入联系电话!'); ?>"
                                    },
                                    <?php if ($phoneCaptchaState == 1) { ?>
                                    phoneCaptcha: {
                                        required: "<?php echo $this->translate('请输入手机验证码!'); ?>"
                                    },
                                    <?php } ?>
                                    <?php } ?>
                                    userName: {
                                        required: "<?php echo $this->translate('请输入登录名称!'); ?>"
                                    }
                                }
                            });
                        });

                        <?php if ($registerPhoneState == 1 && $phoneCaptchaState == 1) { ?>
                        var oBtn = document.getElementById('get_phone_captcha');
                        var flag = true;

                        oBtn.addEventListener("click", function () {
                            $.ajax({
                                type: 'POST',
                                url : "<?php echo $this->url('shop/default', ['action'=>'sendPhoneCaptcha']); ?>",
                                data: {"phoneNumber": $('#userPhone').val(), "areaCode": $('#areaCode').val()},
                                success : function (msg) {
                                    if(typeof msg == "undefined" || msg == null || msg == '') {
                                        startSend(<?php echo $this->shopHelper()->getPhoneCaptchaSecond(); ?>);
                                    } else {
                                        layer.msg(msg);
                                    }
                                }
                            });
                        });

                        function startSend(expiredTime) {
                            var time = expiredTime;
                            oBtn.classList.add('disabled');
                            $('#userPhone').attr('readonly', true);
                            if (flag) {
                                flag = false;
                                var timer = setInterval(() => {
                                    time--;
                                    oBtn.innerText = '已发送 ' + time + ' 秒';
                                    if (time === 0) {
                                        clearInterval(timer);
                                        oBtn.innerText = '重新获取';
                                        oBtn.classList.remove('disabled');
                                        $('#userPhone').attr('readonly', false);
                                        flag = true;
                                    }
                                }, 1000)
                            }
                        }
                        <?php if(((int)$this->shopHelper()->getDbshopSessionData('expiredTime') - time()) > 0) { ?>
                        startSend(<?php echo (int)$this->shopHelper()->getDbshopSessionData('expiredTime') - time(); ?>);
                        <?php } ?>
                        <?php } ?>
                    </script>
                <?php } ?>
            </div>
        </div>
    </div>
</div>
