<!-- By : Ghanim Software -->
<!-- Di Larang Memperjual Source Code ini -->
<?php
	@ob_start();
	session_start();
    include 'config.php';
	if(!isset($_SESSION['status'])){
    } else {
        header('location:index.php');
    };
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="baru.ico">
<link rel="icon" href="icon.ico" type="image/ico">
  <title>APLIKASI KASIR TOKO KOSMETIK</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="assets/css/bootstrap.min.css">
  <link rel="stylesheet" href="assets/css/style.css">
  <link href="assets/fontawesome/css/all.min.css" rel="stylesheet" type="text/css">
</head>
<body class="bg-light">
<body class="hold-transition login-page" style="background:url(assets/img/bg2.png)
no-repeat center center fixed; background-size: cover;
 -webkit-background-size: cover; 
 -moz-background-size: cover; -o-background-size: cover;">
<?php 
    if(isset($_POST['login'])){
        $user = trim(mysqli_real_escape_string($conn, $_POST['username']));
        $pass = $_POST['password'];
        
        // Debug: Tampilkan username yang diterima (hanya untuk debugging, nanti dihapus)
        // echo "Debug - Username diterima: '$user'<br>";
        
        // First check the new users table
        $queryuser = mysqli_query($conn,"SELECT * FROM users WHERE username='$user'");
        
        if (!$queryuser) {
            die("Query error: " . mysqli_error($conn));
        }
        
        $cariuser = mysqli_fetch_assoc($queryuser);
            
        if($cariuser && password_verify($pass, $cariuser['password'])) {
            $_SESSION['id_login'] = $cariuser['id'];
            $_SESSION['username'] = $cariuser['username'];
            $_SESSION['role'] = $cariuser['role'];
            $_SESSION['name'] = $cariuser['name'];
            $_SESSION['nama_toko'] = 'INDOMART'; // Default from login table
            $_SESSION['alamat'] = 'JALAN TAMBUN RENGAS CAKUNG TIMUR JAKARTA TIMUR 13910'; // Default from login table
            $_SESSION['telepon'] = '082125627978'; // Default from login table
            $_SESSION['status'] = "login";

            echo '<script>alert("Anda Berhasil Login");window.location="index.php"</script>';
        } else {
            // If not found in users table, check the old login table for backward compatibility
            $queryuser_old = mysqli_query($conn,"SELECT * FROM login WHERE username='$user'");
            
            if (!$queryuser_old) {
                die("Query error: " . mysqli_error($conn));
            }
            
            $cariuser_old = mysqli_fetch_assoc($queryuser_old);
            
            if($cariuser_old && password_verify($pass, $cariuser_old['password'])) {
                $_SESSION['id_login'] = $cariuser_old['id_login'];
                $_SESSION['username'] = $cariuser_old['username'];
                $_SESSION['role'] = 'admin'; // Default to admin for old login
                $_SESSION['nama_toko'] = $cariuser_old['nama_toko'];
                $_SESSION['alamat'] = $cariuser_old['alamat'];
                $_SESSION['telepon'] = $cariuser_old['telepon'];
                $_SESSION['status'] = "login";

                echo '<script>alert("Anda Berhasil Login");window.location="index.php"</script>';
            } else {
                echo '<script>alert("Username atau password salah");history.go(-1);</script>';
            }
        } // Tutup else dari if($cariuser && password_verify...)
    } // Tutup if(isset($_POST['login']))
?>
<div class="container">
<br><br><br><br><br><br>

<div class="row justify-content-center">

    <div class="col-sm-8 col-md-6 col-lg-4">
    <h1 class="h3 text-center mb-3" style="font-weight:600;"> <span class="text-primary"></span></h1>
	<img alt="logo" src="assets/img/logo23.png" height="110" width="390"/><p>
            <div class="card-body bg-white shadow-sm">
				<form method="POST">
                <label for="user">Username</label>
                <div class="input-group mb-2">
                    <div class="input-group-prepend">
                        <div class="input-group-text"><i class="fa fa-user"></i></div>
                    </div>
                    <input type="text" class="form-control" id="user" name="username" placeholder="Username" required autocomplete="off">
                </div>

                <label for="pass">Password</label>
                <div class="input-group mb-2">
                    <div class="input-group-prepend">
                        <div class="input-group-text"><i class="fa fa-lock"></i></div>
                    </div>
                    <input type="password" class="form-control" id="pass" name="password" placeholder="Password" required autocomplete="off">
                </div>
                <div class="row">
                <div class="col-6 pr-1 mt-2">
                    <button class="btn btn-danger btn-block" type="reset">
                       <i class="fa fa-trash-restore-alt mr-1"></i> Reset</button>
                </div>
                <div class="col-6 pl-1 mt-2">
                    <button class="btn btn-primary btn-block" name="login" type="submit">
                       <i class="fa fa-sign-in-alt mr-1"></i> Login</button>
                </div>
                </div>
            </form>
        </div>
		
        <div class="text-center small mt-4">
            <p class="text-muted" >Aplikasi kasir © <?php echo date('Y');?> - By : <a href="#" class="span text-primary">GHANIM</a></p>
        </div>
    </div>

</div>


</div> <!-- end container fluid -->

    <script src="assets/js/jquery.slim.min.js"></script>
    <script src="assets/js/popper.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>
</body>
</html>