<?php
require 'conn/conn.php';
require 'conn/function.php';
require 'conn/sendmail.php';

$from = $_GET["from"];
$action=$_GET["action"];

if($action=="sendcode"){
    $email=t($_GET["email"]);
    $code=strtoupper(substr(md5($email),-4));
    if (strpos($email,"@")===false){
        die("{\"msg\":\"请输入一个正确格式的邮箱！\"}");
    }else{
        if(getrs("select * from sl_member where M_email='$email'")==""){
            $info=sendmail("您的邮箱验证码","您的7支付邮箱验证码为<b>".$code."</b>，请在注册页面正确填写。",$email);
            die("{\"msg\":\"$info\"}");
        }else{
            die("{\"msg\":\"该邮箱已被占用，请换邮箱重试！\"}");
        }
    }
    
}

if($action=="reg"){
    $M_emailcode=$_POST["M_emailcode"];
    $M_pwd=$_POST["M_pwd"];
    $M_pwd2=$_POST["M_pwd2"];
    $M_email=htmlspecialchars($_POST["M_email"]);
    
    if ($M_pwd!=$M_pwd2){
        die("{\"msg\":\"两次输入密码不一致！\"}");
    }
    if($C_regemail==1){
        if($M_emailcode!=strtoupper(substr(md5($_POST["M_email"]),-4))){
            die("{\"msg\":\"邮箱验证码错误！\"}");
        }
    }
    if(($_POST["M_code"]!=$_SESSION["CmsCode"] || $_POST["M_code"]=="" || $_SESSION["CmsCode"]=="")){
        die("{\"code\":\"error2\",\"msg\":\"请填写正确的计算结果！\"}");
    }else {
        if ($M_pwd!="" && $M_pwd2!="" && $M_email!=""){
            if (strpos($M_email,"@")===false){
                die("{\"msg\":\"请输入一个正确格式的邮箱！\"}");
            }else{
                if(getrs("select * from sl_member Where M_email='".$M_email."'","M_email")!=""){
                    die("{\"msg\":\"邮箱已被占用！\"}");
                }else{
                    sql("insert into sl_member(M_pwd,M_email,M_regtime,M_logo,M_ico,M_wechatcode) values('".md5($M_pwd)."','".$M_email."','".date('Y-m-d H:i:s')."','nopic.png','nopic.png','nopic.png')");
                    $M_id=getrs("select * from sl_member where M_email='$M_email'","M_id");
                    sql("insert into sl_psort(S_title,S_order,S_mid) values('默认分类',0,$M_id)");
                    die("{\"msg\":\"success\"}");
                }
            }
        }else{
            die("{\"msg\":\"请填全信息！\"}");
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>会员注册 - <?php echo $C_webtitle?></title>
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/tailwindcss/2.0.0-alpha.5/tailwind.min.css" rel="stylesheet">
    <style>
        .dlbox .weixin, .dlbox .weibo {
            display: inline-block;
            width: 32px;
            height: 32px;
            background-size: cover;
        }

        .bg-indigo-100 {
    background-color: #ebf4ff;
}.border-gray-200 {
    border-color: #edf2f7;
}.bg-gray-100 {
    background-color: #f7fafc;
}.bg-indigo-500 {
    background-color: #667eea;
}
a{color:#09f}
a:hover{color:#000}
.btn{position:absolute;top:15px;right:10px;background:#667eea;color:#fff;font-size:12px;padding:2px 5px;border-radius:5px;border:solid 1px #667eea}
.btn:hover{background:#fff;color:#667eea}
    </style>

</head>

<body class="min-h-screen bg-gray-100 text-gray-900 flex justify-center dlbox">
    <div class="max-w-screen-xl m-0 sm:m-20 bg-white shadow sm:rounded-lg flex justify-center flex-1">
        <div class="lg:w-1/2 xl:w-5/12 p-6 sm:p-12">
            <div class="mt-12 flex flex-col items-center">
                <a href="./"><img src="media/<?php echo $C_logo?>" style="width:260px;margin-bottom:30px"></a>
                <h1 class="text-2xl xl:text-3xl font-extrabold">用户注册</h1>
                <div class="w-full flex-1 mt-8">
                    <form method="post" id="form" class="met-form-validation">
                        <div class="mx-auto max-w-xs">
                            <div style="position:relative">
                                <input class="w-full px-8 py-4 rounded-lg font-medium bg-gray-100 border border-gray-200 placeholder-gray-500 text-sm focus:outline-none focus:border-gray-400 focus:bg-white" type="email" name="M_email" id="email" placeholder="电子邮箱" >
                                <?php 
                                    if($C_regemail==1){
                                        echo "<button class=\"btn\" type=\"button\" onclick=\"sendcode($('#email').val())\">获取验证码</button>";
                                    }
                                ?>
                            </div>

                            <?php 
                                if($C_regemail==1){
                                    echo "<input class=\"w-full px-8 py-4 rounded-lg font-medium bg-gray-100 border border-gray-200 placeholder-gray-500 text-sm focus:outline-none focus:border-gray-400 focus:bg-white mt-5\" type=\"text\" name=\"M_emailcode\" placeholder=\"邮箱验证码\">";
                                }
                            ?>

                            
                            <input class="w-full px-8 py-4 rounded-lg font-medium bg-gray-100 border border-gray-200 placeholder-gray-500 text-sm focus:outline-none focus:border-gray-400 focus:bg-white mt-5"  type="password" name="M_pwd" placeholder="设置密码">

                            <input class="w-full px-8 py-4 rounded-lg font-medium bg-gray-100 border border-gray-200 placeholder-gray-500 text-sm focus:outline-none focus:border-gray-400 focus:bg-white mt-5"  type="password" name="M_pwd2" placeholder="确认密码">
                            
                            <?php
                        echo "<div class=\"form-group form-material floating\" style=\"position: relative;margin-top:20px\">
                            ".calculation("M_code")."
                        </div>";
                        ?>

                            <button type="button" onclick="reg()" class="mt-5 tracking-wide font-semibold bg-indigo-500 text-gray-100 w-full py-4 rounded-lg hover:bg-indigo-700 ease-in-out flex items-center justify-center focus:shadow-outline focus:outline-none">
                                <span class="ml-3">注 册</span>
                            </button>
                            <div id="msg" style="font-size:12px;text-align:center;margin-top:15px;color:#f00"></div>
                            <p class="mt-6 text-xs text-gray-600 text-center">已经有账号了? 去 <a href="login.php">登录</a> <a href="forget.php">忘记密码？</a></p>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="flex-1 bg-indigo-100 text-center hidden lg:flex">
            <div class="m-12 xl:m-16 w-full bg-contain bg-center bg-no-repeat" style="background-image: url('member/images/dlbox.svg');"></div>
        </div>
    </div>
    <script>
    function reg(){
        $.ajax({
            url: '?action=reg',
            type: 'post',
            data: $("#form").serialize(),
            success: function(data) {
                data = JSON.parse(data);
                if (data.msg == "success") {
                    alert("注册成功，您可以登录了");
                    window.location.href="login.php";
                } else {
                    $("#msg").html(data.msg);
                }
            }
        });
    }
    function sendcode(email){
        $.ajax({
        url: '?action=sendcode&email=' + email,
        type: 'get',
        success: function(data) {
            data = JSON.parse(data);
            if (data.msg == "success") {
                alert("验证码已发送，请到邮箱查收");
            } else {
                alert(data.msg);
            }
        }
    });
    }
    
</script>
</body>
</html>