<?php
require_once '../config.php';
require_once '../functions.php';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $remember = isset($_POST['remember']);

    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND status = 'active'");
    $stmt->execute([$username]);
    $user = $stmt->fetch();

    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['role'] = 'user';
        $_SESSION['profile_image'] = $user['profile_image']; // Store profile image
        if ($remember) {
            setcookie('user_remember', $user['id'], time() + COOKIE_LIFETIME, '/');
        }
        header("Location: " . base_url('public/user/dashboard.php'));
        exit;
    } else {
        $error = "Invalid credentials or inactive account";
    }
}
?>
<!doctype html>
<html lang="en" dir="ltr">

<head>

    <!-- META DATA -->
    <meta charset="UTF-8">
    <meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=0'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="<?php echo APP_NAME; ?>">
    <meta name="author" content="Spruko Technologies Private Limited">
    <meta name="keywords" content="user, login, sms">

    <!-- FAVICON -->
    <link rel="shortcut icon" type="image/x-icon" href="<?php echo asset_url('assets/images/brand/favicon.ico'); ?>" />

    <!-- TITLE -->
    <title>User Login - <?php echo APP_NAME; ?></title>

    <!-- BOOTSTRAP CSS -->
    <link id="style" href="<?php echo asset_url('assets/plugins/bootstrap/css/bootstrap.min.css'); ?>"
        rel="stylesheet" />

    <!-- STYLE CSS -->
    <link href="<?php echo asset_url('assets/css/style.css'); ?>" rel="stylesheet" />
    <link href="<?php echo asset_url('assets/css/dark-style.css'); ?>" rel="stylesheet" />
    <link href="<?php echo asset_url('assets/css/transparent-style.css'); ?>" rel="stylesheet">
    <link href="<?php echo asset_url('assets/css/skin-modes.css'); ?>" rel="stylesheet" />

    <!--- FONT-ICONS CSS -->
    <link href="<?php echo asset_url('assets/css/icons.css'); ?>" rel="stylesheet" />

    <!-- COLOR SKIN CSS -->
    <link id="theme" rel="stylesheet" type="text/css" media="all"
        href="<?php echo asset_url('assets/colors/color1.css'); ?>" />

</head>

<body class="app sidebar-mini ltr login-img">

    <!-- BACKGROUND-IMAGE -->
    <div class="">

        <!-- GLOABAL LOADER -->
        <div id="global-loader">
            <img src="<?php echo asset_url('assets/images/loader.svg'); ?>" class="loader-img" alt="Loader">
        </div>
        <!-- /GLOABAL LOADER -->

        <!-- PAGE -->
        <div class="page">
            <div class="">
                <div class="col col-login mx-auto mt-7">
                    <div class="text-center">
                        <img src="<?php echo asset_url('assets/images/marketing-engine-logo.png'); ?>"
                            class="header-brand-img" alt="" style="height:auto; width:200px">
                    </div>
                </div>

                <div class="container-login100">
                    <div class="wrap-login100 p-6">
                        <form class="login100-form validate-form" method="POST">
                            <span class="login100-form-title pb-5">
                                Marketing Engine
                                <h6 class="text-sm mt-3 text-muted">SMs and Email Marketing</h6>
                            </span>
                            <div class="panel panel-primary">

                                <div class="panel-body tabs-menu-body p-0 pt-5">
                                    <div class="tab-content">
                                        <div class="tab-pane active" id="tab5">
                                            <?php if ($error): ?>
                                                <div class="alert alert-danger" role="alert"><?php echo $error; ?></div>
                                            <?php endif; ?>

                                            <div class="wrap-input100 validate-input input-group"
                                                data-bs-validate="Username is required">
                                                <a href="javascript:void(0)"
                                                    class="input-group-text bg-white text-muted">
                                                    <i class="zmdi zmdi-account text-muted" aria-hidden="true"></i>
                                                </a>
                                                <input class="input100 border-start-0 form-control ms-0" type="text"
                                                    name="username" placeholder="Username" required>
                                            </div>
                                            <div class="wrap-input100 validate-input input-group" id="Password-toggle">
                                                <a href="javascript:void(0)"
                                                    class="input-group-text bg-white text-muted">
                                                    <i class="zmdi zmdi-eye text-muted" aria-hidden="true"></i>
                                                </a>
                                                <input class="input100 border-start-0 form-control ms-0" type="password"
                                                    name="password" placeholder="Password" required>
                                            </div>
                                            <div class="text-end pt-4">
                                                <p class="mb-0"><a href="javascript:void(0)"
                                                        class="text-primary ms-1">Forgot Password?</a></p>
                                            </div>
                                            <div class="container-login100-form-btn">
                                                <button type="submit" class="login100-form-btn btn-primary">
                                                    Login
                                                </button>
                                            </div>
                                            <div class="text-center pt-3">
                                                <p class="text-dark mb-0">Are you an Admin?<a href="index.php"
                                                        class="text-primary ms-1">Admin Login</a></p>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>

                        </form>
                    </div>
                </div>
                <!-- CONTAINER CLOSED -->
            </div>
        </div>
        <!-- End PAGE -->

    </div>
    <!-- BACKGROUND-IMAGE CLOSED -->

    <!-- JQUERY JS -->
    <script src="<?php echo asset_url('assets/js/jquery.min.js'); ?>"></script>

    <!-- BOOTSTRAP JS -->
    <script src="<?php echo asset_url('assets/plugins/bootstrap/js/popper.min.js'); ?>"></script>
    <script src="<?php echo asset_url('assets/plugins/bootstrap/js/bootstrap.min.js'); ?>"></script>

    <!-- SHOW PASSWORD JS -->
    <script src="<?php echo asset_url('assets/js/show-password.min.js'); ?>"></script>

    <!-- Perfect SCROLLBAR JS-->
    <script src="<?php echo asset_url('assets/plugins/p-scroll/perfect-scrollbar.js'); ?>"></script>

    <!-- Color Theme js -->
    <script src="<?php echo asset_url('assets/js/themeColors.js'); ?>"></script>

    <!-- CUSTOM JS -->
    <script src="<?php echo asset_url('assets/js/custom.js'); ?>"></script>

</body>

</html>