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

$from = $_GET["from"];
$action = $_GET["action"];
if($from==""){
    $from="member/index.php";
}
if ($action == "unlogin") {
    $_SESSION["M_login"] = "";
    $_SESSION["M_id"] = "";
    $_SESSION["M_pwd"] = "";
    $_SESSION["from"] = "";
    if($from==""){
        box("退出成功!", "login.php", "success");
    }else{
        box("退出成功!", $from, "success");
    }
}

if ($action == "login") {
    $M_email = t($_POST["M_email"]);
    $M_pwd = $_POST["M_pwd"];
    $M_code = $_POST["M_code"];
    $L_add = $_POST["add"];

    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='" . $M_email . "'  and M_pwd='" . md5($M_pwd) . "'");
        if($M!=""){
            if($M["M_stop"]==1){
                die("{\"code\":\"error\",\"msg\":\"账户已封停，原因：".$M["M_reason"]."，请联系客服处理！\"}");
            }else{
                $M_id = $M["M_id"];
                $M_openid = $M["M_openid"];
                $_SESSION["M_email"] = $M["M_email"];
                $_SESSION["M_id"] = $M["M_id"];
                $_SESSION["M_pwd"] = $M["M_pwd"];
                if ($from == "") {
                    die("{\"code\":\"success\",\"url\":\"member\"}");
                } else {
                    die("{\"code\":\"success\",\"url\":\"$from\"}");
                }
            }
        }else{
            die("{\"code\":\"error\",\"msg\":\"帐号或密码错误！\"}");
        }
    }
}
if($_SESSION["M_email"]!=="" && $_SESSION["M_pwd"]!="" && $_SESSION["M_id"]!=""){
    die("<script>window.location.href=\"$from\"</script>");
}
?>

<!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;
        }

        .dlbox .weixin {
            background-image: url(images/weixin.png);
        }

        .dlbox .weibo {
            background-image: url(images/weibo.png);
        }
        .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" 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="电子邮箱" >
                            <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="密码">
                            
                            <?php
                        echo "<div class=\"form-group form-material floating\" style=\"position: relative;margin-top:20px\">
                            ".calculation("M_code")."
                        </div>";
                        ?>

                            <button type="button" 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" onclick="login()">
                                <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?1');"></div>
        </div>
    </div>
    <script>
        function login(){
            $.ajax({
                url: '?action=login',
                type: 'post',
                data: $("#form").serialize(),
                success: function(data) {
                    data = JSON.parse(data);
                    if (data.code == "success") {
                        window.location.href=data.url;
                    } else {
                        $("#msg").html(data.msg);
                    }
                }
            });
        }
    </script>
</body>
</html>