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

$action=$_GET["action"];
$M_pwdcode=t($_GET["M_pwdcode"]);

if($M_pwdcode==""){
    die("{\"code\":\"error\",\"msg\":\"重置码不可为空！\"}");
}
$M=getrs("Select * from sl_member where M_pwdcode='".$M_pwdcode."'");

if ($M!="") {
    $M_email=$M["M_email"];
}else{
    die("{\"code\":\"error\",\"msg\":\"重置码错误\"}");
}

if($action=="setpwd"){
    if($_POST["M_newpwd"]==$_POST["M_newpwd2"] && $_POST["M_newpwd"]!=""){
        sql("update sl_member set M_pwd='".md5($_POST["M_newpwd"])."',M_pwdcode='' where M_pwdcode='".$M_pwdcode."'");

        die("{\"code\":\"success\",\"url\":\"member\"}");
    }else{
        die("{\"code\":\"error\",\"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}
    </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:50px"></a>
                <h1 class="text-2xl xl:text-3xl font-extrabold">重设密码</h1>
                <div class="w-full flex-1 mt-8">
                    <form method="post" class="met-form-validation" id="form">
                        <div class="mx-auto max-w-xs">
                            <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" placeholder="电子邮箱" value="<?php echo $M_email?>" >
                            <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_newpwd" 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_newpwd2" placeholder="确认密码">

                            <button type="button" onclick="setpwd()" 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="reg.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 setpwd(){
            $.ajax({
                url: '?action=setpwd&M_pwdcode=<?php echo $M_pwdcode?>',
                type: 'post',
                data: $("#form").serialize(),
                success: function(data) {
                    data = JSON.parse(data);
                    if (data.code == "success") {
                        alert("重置密码成功，请返回登录！")
                        window.location.href=data.url;
                    } else {
                        $("#msg").html(data.msg);
                    }
                }
            });
        }
    </script>
</body>
</html>