<?php
include '../connection.php'; 
if(!isset($_SESSION)) session_start();
if(isset($_SESSION["ID"])){header('Location: logout.php');}
$username = isset($_POST["username"]) ? $_POST["username"] : "";
$password = isset($_POST["password"]) ? $_POST["password"] : "";
$error = array();
$flag = true;
if(isset($_POST["log-in"]))
{
    if (!$_SERVER['REQUEST_METHOD'] === 'POST')
    { 
        header( 'Location: ../ar');
    }
    if(empty($username) || empty($password))
    {
        $error["empty"] = "يرجى تعبئة جميع الحقول";
        $flag = false;
    }
    if($flag === true)
    {
        $password = sha1($password);
        $sqlUser = "SELECT ID , username,role FROM admin WHERE username='{$username}' AND password='{$password}' AND status=1";
        $executeSql = mysqli_query($connection,$sqlUser);
        $retriveData = mysqli_fetch_row($executeSql);
        if($retriveData == null)
        {
            $error["incorrect"] = "يرجى التأكد من إسم المستخدم او كلمة المرور";
            $flag = false;
        }
        else
        {
            $_SESSION["ID"] = $retriveData[0];
            $_SESSION["username"] = $retriveData[1];
            $_SESSION["role"] = $retriveData[2];
            header( 'Location: show-main-images.php');
        }
    }
    if($flag === false)
    {
        foreach($error as $e)
        {
            $listError = $e."<br>";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="icon" href="../assets/img/logo.png" type="image/x-icon">
    <title>خذ فرصة- تسجيل دخول</title>
    <?php include 'cssFile.php'; ?>
    <link rel="stylesheet" href="cssadmin.css">
    <style>
    body
{
    margin:0;
    padding:0;
    background: url(bg.png);
}
    </style>
</head>
<body dir='rtl'>
    <div class="container">
        <div class="row">
            <div class="col-md-4 col-sm-6 col-8 text-right" id='form-login'>
                <?php
                    if(isset($flag) && $flag === false)
                    {
                        echo "<div class='alert alert-danger'>$listError</div>";
                    }
                ?>
                <h3 class='text-center'>تسجيل دخول</h3>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
                    <div class="form-row" style='margin-top:10px'>
                        <label for="username">إسم المستخدم</label>
                        <input type="text" name="username" id="username" placeholder='إسم المستخدم' class="form-control">
                    </div>
                    <div class="form-row" style='margin-top:10px'>
                        <label for="password">كلمة المرور</label>
                        <input type="password" name="password" id="password" placeholder='كلمة المرور' class="form-control">
                    </div>
                    <div class="form-row">
                        <button type="submit" name='log-in' class="btn btn-success btn-block">تسجيل دخول</button>
                    </div>
                </form>
                <!-- End form -->
            </div>
        </div>
        <!-- End row -->
    </div>
<!-- End container -->

    <!-- assets / js -->
    <?php include 'jsFile.php'; ?>
</body>
</html>