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

$action=$_GET["action"];
if($action=="found"){
    $M_email=$_POST["M_email"];
    $M_code=$_POST["M_code"];
    if(($_POST["M_code"]!=$_SESSION["CmsCode"] || $_POST["M_code"]=="" || $_SESSION["CmsCode"]=="")){
        die("{\"code\":\"error\",\"msg\":\"请填写正确的计算结果！\"}");
    }else{
        
        $M=getrs("select * from sl_member Where M_email='" . t($M_email) . "'");
            if ($M!="") {
            $M_pwdcode=gen_key(20);
            sql("update sl_member set M_pwdcode='".$M_pwdcode."' where M_email='".$M_email."'");

            $info=sendmail("找回密码邮件","请点击链接重新设置密码<br><a href='".gethttp().$domain."/setpwd.php?M_pwdcode=".$M_pwdcode."'>".gethttp().$domain."/setpwd.php?M_pwdcode=".$M_pwdcode."</a><br>说明：重置密码后链接失效",$M_email);
            if($info=="success"){
                die("{\"code\":\"success\"}");
            }else{
                die("{\"code\":\"success\"}");
            }
        }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 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="电子邮箱" >

                            <?php
                            echo "<div class=\"form-group form-material floating\" style=\"position: relative;margin-top:20px\">
                                ".calculation("M_code")."
                            </div>";
                            ?>
                            <button type="button" onclick="found()" 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="./">返回首页</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 found(){
            $.ajax({
                url: '?action=found',
                type: 'post',
                data: $("#form").serialize(),
                success: function(data) {
                    data = JSON.parse(data);
                    if (data.code == "success") {
                        alert("请查收密码找回邮件!");
                        window.location.href="login.php";
                    } else {
                        $("#msg").html(data.msg);
                    }
                }
            });
        }
    </script>
</body>
</html>