<?php
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST['email']) && isset($_POST['password']) && isset($_POST['csrf_token']) && $csrf_generator->validate($_POST['csrf_token'])) {
            $user_email       = $_POST['email'];
		    $password         = $_POST['password'];
            $hourAgo          = time() - 60*60;
            $redirect         = "/admin";

            $stmt = $db->query("SELECT id, status, password, name, avatar, user_group FROM {$db->prefix}admin WHERE email = :email OR user_name = :email", array(':email'=>$user_email));
            if ($stmt->num_rows == 1) {
                if($stmt->row['status'] == 0){
                    $data["error_warning"] = 'Hesabınız pasif durumda. Detaylı bilgi için sistem yöneticinize ulaşın.';
                }else {
                    if(password_verify($password, $stmt->row['password'])) {
                        $session->data['logged_in'] = true;
                        $session->data['user_id'] = $stmt->row["id"];
                        $session->data['user_email'] = $user_email;
                        $session->data['user_avatar'] = $stmt->row["avatar"];
                        $session->data['user_name'] = $stmt->row["name"];
                        $session->data['user_group'] = $stmt->row["user_group"];
                        $db->query("UPDATE {$db->prefix}admin SET last_login=NOW() WHERE id = :id", array(':id'=>$stmt->row['id']));

                        if(isset($_POST['returnUrl']) && $_POST['returnUrl'] != ''){
                            $redirect = $_POST['returnUrl'];
                        }

                        header("Location: ".$redirect);
                        exit();
                    }else{
                        $data["error_warning"] = 'Lütfen parolanızı kontrol ediniz.';
                    }
                }
            }else{
                $data["error_warning"] = 'Kullanıcı bulunamadı.';
            }
        } else {
            $data["error_warning"] = 'Lütfen tüm alanları doldurunuz.';
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Giriş Yap | <?php echo $config->get('site_title'); ?></title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="" />
        <meta name="csrf_token" content="<?php echo $csrf_token; ?>" />
        <link rel="apple-touch-icon" sizes="180x180" href="<?php echo $config->get('site_url'); ?>admin/public/assets/images/favicons/apple-touch-icon.png">
        <link rel="icon" type="image/png" sizes="32x32" href="<?php echo $config->get('site_url'); ?>admin/public/assets/images/favicons/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="<?php echo $config->get('site_url'); ?>admin/public/assets/images/favicons/favicon-16x16.png">
        <link rel="manifest" href="<?php echo $config->get('site_url'); ?>admin/public/assets/images/favicons/site.webmanifest">
        <link rel="mask-icon" href="<?php echo $config->get('site_url'); ?>admin/public/assets/images/favicons/safari-pinned-tab.svg" color="#5bbad5">
        <link rel="shortcut icon" href="<?php echo $config->get('site_url'); ?>admin/public/assets/images/favicons/favicon.ico">
        <meta name="msapplication-TileColor" content="#da532c">
        <meta name="msapplication-config" content="<?php echo $config->get('site_url'); ?>admin/public/assets/images/favicons/browserconfig.xml">
        <meta name="theme-color" content="#ffffff">
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@300;400;500&display=swap" rel="stylesheet">
        <link href="<?php echo $config->get('site_url'); ?>admin/public/assets/css/bootstrap/bootstrap.min.css" rel="stylesheet">
        <link href="<?php echo $config->get('site_url'); ?>admin/public/assets/css/public.css" rel="stylesheet">
        <link href="<?php echo $config->get('site_url'); ?>admin/public/assets/css/login.css" rel="stylesheet">
    </head>
    <body>
        <div class="row g-0 vh-100">
            <div class="col-12 d-flex align-items-center justify-content-center">
                <form action="/admin/login" method="POST" id="login-form">
                    <div class="login-wrapper">
                        <div class="text-center">
                            <img src="<?php echo $config->get('site_url'); ?>admin/public/assets/images/logo.jpg" alt="logo" class="logo">
                        </div>
                        
                        <p class="mb-3 text-center">Devam etmek için giriş yapın.</p>
                        <div class="row">
                            <div class="col-12">
                                <div class="form-label-group">
                                    <label for="email" class="form-label">Email</label>
                                    <input type="text" class="form-control" id="email" name="email" spellcheck="false" autocomplete="off" autocorrect="off" autocapitalize="off" value="<?php echo isset($_POST["email"]) ? htmlspecialchars($_POST["email"]) : ""; ?>" required/>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-12">
                                <div class="form-label-group">
                                    <label for="password" class="form-label">Parola</label>
                                    <input type="password" class="form-control" id="password" name="password" spellcheck="false" autocomplete="off" autocorrect="off" autocapitalize="off" required/>
                                    <span class="password-visibility-toggler" data-password="password">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-eye" viewBox="0 0 16 16">
                                            <path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z"/>
                                            <path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z"/>
                                        </svg>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div id="login-errors">
                            <?php if(isset($data["error_warning"])): ?>
                                <div class="error text-danger mb-3"><?php echo $data["error_warning"]; ?></div>
                            <?php endif; ?>
                        </div>
                        <input type="hidden" name="source" value="admin">
                        <input type="hidden" name="returnUrl" value="/admin">
                        <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                        <button type="submit" class="btn btn-primary">Giriş yap</button>
                    </div>
                </form>
            </div>
        </div>
        <script src="<?php echo $config->get('site_url'); ?>admin/public/assets/js/bootstrap/bootstrap.bundle.min.js"></script>
        <script src="<?php echo $config->get('site_url'); ?>admin/public/assets/js/public.js?v=<?php echo $config->get('version'); ?>"></script>
    </body>
</html>
