<?php
require 'function.php';
session_start();
// login
if (isset($_POST['login'])) {
    $username = $_POST['nama'];
    $pass = $_POST['password'];

    $cek = mysqli_query($conn, "SELECT * FROM user WHERE username='$username' and password='$pass'");
    $hitung = mysqli_num_rows($cek);

    if ($hitung > 0) {
        $ambilrole = mysqli_fetch_array($cek);
        $role = $ambilrole['role'];

        if ($role == 'admin') {
            // jika admin
            $_SESSION['log'] = 'Logged';
            $_SESSION['iden'] = 'Superadmin';
            $_SESSION['role'] = 'admin';
            header('location:dashboard.php');
        } else {
            // jika user
            $_SESSION['log'] = 'Logged';
            $_SESSION['iden'] = 'Sambo';
            $_SESSION['role'] = 'user';
            header('location:anggota_user.php');
        }
    } else if ($username == '' || $pass == '') {
        header('location:index.php?error=2');
    } else {
        header('location:index.php?error=1');
    }
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SIPS POLRES MOJOKERTO</title>

    <!-- Google Font: Source Sans Pro -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="app/plugins/fontawesome-free/css/all.min.css">
    <!-- icheck bootstrap -->
    <link rel="stylesheet" href="app/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="app/dist/css/adminlte.min.css">
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href="app/plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css" />
</head>

<body class="hold-transition login-page" style="background-image: url('assets/img/bg1.jpg'); background-size:cover">
    <div class="login-box">
        <div class="card card-outline card-primary">
            <div class="card-header text-center">
                <a href="#" class="h1"><b>SATUAN SAMAPTA</b>
                    <br>POLRES MOJOKERTO</br></a>
                <img src="assets/logo/logo.png" alt="SIPS" height="75" width="75">
            </div>
            <div class="card-body">
                <p class="login-box-msg">Masukan Username & Password Anda</p>

                <form method="post">
                    <div class="input-group mb-3">
                        <input type="text" class="form-control" placeholder="Username" name="nama">
                        <div class="input-group-append">
                            <div class="input-group-text">
                                <span class="fas fa-user"></span>
                            </div>
                        </div>
                    </div>
                    <div class="input-group mb-3">
                        <input type="password" class="form-control" placeholder="Password" name="password">
                        <div class="input-group-append">
                            <div class="input-group-text">
                                <span class="fas fa-lock"></span>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-4">
                            <button type="submit" class="btn btn-primary btn-block" name="login">Login</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- jQuery -->
        <script src="app/plugins/jquery/jquery.min.js"></script>
        <!-- Bootstrap 4 -->
        <script src="app/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
        <!-- AdminLTE App -->
        <script src="app/dist/js/adminlte.min.js"></script>
        <!-- SweetAlert2 -->
        <script src="app/plugins/sweetalert2/sweetalert2.min.js"></script>
    </div>
</body>
<?php
//buat swetalert <<HARUS dibawah link JS>>
if (isset($_GET['error'])) {
    $x = ($_GET['error']);
    if ($x == 1) {
        echo "<script>
        var Toast = Swal.mixin({
            toast: true,
            position: 'top',
            showConfirmButton: false,
            timer: 3000
        });
        Toast.fire({
            icon: 'error',
            title: 'Login Gagal'
        })
        </script>";
    } else if ($x == 2) {
        echo "<script>
        var Toast = Swal.mixin({
            toast: true,
            position: 'top',
            showConfirmButton: false,
            timer: 3000
        });
        Toast.fire({
            icon: 'warning',
            title: 'Silahkan Masukkan Username dan Password'
        })
        </script>";
    } else {
        echo '';
    }
}
?>

</html>