<?php
include 'includes/config.php';
session_start();

if (!$_SESSION['email']) {
    header('location:login.php');
}
?>
<?php

if (isset($_POST['submit'])) {
    $user_name = $_POST['user_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $options = isset($_POST['options']) ? $_POST['options'] : [];

    $options_password = [
        'cost' => 10,
    ];
    $hashed_password = password_hash($password, PASSWORD_BCRYPT, $options_password);

    try {
        $sql_check = "SELECT id FROM users WHERE email = :email OR user_name = :user_name";
        $query_check = $conn->prepare($sql_check);
        $query_check->bindParam(':email', $email, PDO::PARAM_STR);
        $query_check->bindParam(':user_name', $user_name, PDO::PARAM_STR);
        $query_check->execute();

        if ($query_check->fetch(PDO::FETCH_ASSOC)) {
            $error_msg = "⚠️ Email or User Name already exists.";
        } else {
            $sql_insert_user = "INSERT INTO users (user_name, email, password) VALUES (:user_name, :email, :password)";
            $query_insert_user = $conn->prepare($sql_insert_user);
            $query_insert_user->bindParam(':user_name', $user_name, PDO::PARAM_STR);
            $query_insert_user->bindParam(':email', $email, PDO::PARAM_STR);
            $query_insert_user->bindParam(':password', $hashed_password, PDO::PARAM_STR);

            $conn->beginTransaction();
            $query_insert_user->execute();
            $user_id = $conn->lastInsertId();

            if ($user_id) {
                $sql_insert_rights = "INSERT INTO user_rights (user_id, user_auth) VALUES (:user_id, :user_auth)";
                $query_insert_rights = $conn->prepare($sql_insert_rights);

                foreach ($options as $option) {
                    $query_insert_rights->bindParam(':user_id', $user_id, PDO::PARAM_INT);
                    $query_insert_rights->bindParam(':user_auth', $option, PDO::PARAM_INT);
                    $query_insert_rights->execute();
                }

                $conn->commit();
                $succMsg = "User and User Rights added successfully.";
            } else {
                $conn->rollBack();
                $error_msg = "Error adding user.";
            }
        }
    } catch (PDOException $e) {
        $conn->rollBack();
        $error_msg = "Error: " . $e->getMessage();
    }
}
?>





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

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Add Expense</title>
    <!-- Favicon -->
    <link rel="shortcut icon" href="images/favicon.ico" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Typography CSS -->
    <link rel="stylesheet" href="css/typography.css">
    <!-- Style CSS -->
    <link rel="stylesheet" href="css/style.css">
    <!-- Responsive CSS -->
    <link rel="stylesheet" href="css/responsive.css">
    <style>
        ion-icon {
            font-size: 20px;
            margin-bottom: -5px;
        }
    </style>
</head>

<body>
    <!-- loader Start -->
    <div id="loading">
        <div id="loading-center">
        </div>
    </div>
    <!-- loader END -->
    <!-- Wrapper Start -->
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php
        include 'includes/sidebar.php';
        ?>
        <!-- TOP Nav Bar -->
        <?php include 'includes/header.php'; ?>
        <!-- TOP Nav Bar END -->
        <!-- Page Content  -->
        <div id="content-page" class="content-page">
            <section class="login-content">
                <div class="container">
                    <div class="row align-items-center justify-content-center height-self-center">
                        <div class="col-lg-8">
                            <div class="card auth-card">
                                <div class="card-body p-0">
                                    <div class="d-flex align-items-center auth-content">
                                        <div class="col-lg-7 align-self-center">
                                            <div class="p-3">
                                                <div>
                                                    <?php
                                                    if (isset($succMsg)) {
                                                        echo "<div style='color: green;'>$succMsg</div>";
                                                    }
                                                    if (isset($error_msg)) {
                                                        echo "<div style='color: red;'>$error_msg</div>";
                                                    }
                                                    ?>
                                                </div>
                                                <h2 class="mb-2">Add Users</h2>
                                                <!-- <p>Login to stay connected.</p> -->
                                                <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" data-toggle="validator">
                                                    <div class="row">
                                                        <div class="col-lg-12">
                                                            <div class="floating-label form-group">
                                                                <label>User Name</label>
                                                                <input class="floating-input form-control" name="user_name" type="text" placeholder="Enter User Name" autocomplete="off">

                                                            </div>
                                                        </div>
                                                        <div class="col-lg-12">
                                                            <div class="floating-label form-group">
                                                                <label>Email</label>
                                                                <input class="floating-input form-control" name="email" type="email" placeholder="Enter Email" autocomplete="off">

                                                            </div>
                                                        </div>
                                                        <div class="col-lg-12">
                                                            <div class="floating-label form-group">
                                                                <label>Password</label>
                                                                <input class="floating-input form-control" name="password" type="password" placeholder="Enter Password" autocomplete="off">

                                                            </div>
                                                        </div>
                                                        <div class="col-lg-12">
                                                            <label>Departments</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="1"> Contacts Department</label>
                                                            </div>

                                                        </div>
                                                        <div class="col-lg-12" id="options-container">
                                                            <label>Products Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="2"> List Product </label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="3"> Add Product</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="4"> Print Labels</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="5"> Units</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="6"> Categroies</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="7"> Brand</label>
                                                            </div>

                                                        </div>

                                                        <div class="col-lg-12" id="options-container">
                                                            <label>Purchase Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="8"> List Purchase</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="9"> Add Purchase</label>
                                                            </div>
                                                        </div>


                                                        <div class="col-lg-12" id="options-container">
                                                            <label>Sell Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="10"> All Sales</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="11"> Add Sales</label>
                                                            </div>

                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="12"> List Pos</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="13"> Pos</label>
                                                            </div>
                                                        </div>



                                                        <div class="col-lg-12" id="options-container">
                                                            <label>Stock Transfer Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="14"> List Stock Receive</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="15"> List Stock Transfer</label>
                                                            </div>

                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="16"> Add Stock Transfer</label>
                                                            </div>

                                                        </div>

                                                        <div class="col-lg-12" id="options-container">
                                                            <label>Expense Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="17"> List Expense</label>
                                                            </div>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="18"> Add Expense</label>
                                                            </div>

                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="19"> Expense Categroies</label>
                                                            </div>

                                                        </div>


                                                        <div class="col-lg-12" id="options-container">
                                                            <label>Reports Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="20"> Reports</label>
                                                            </div>

                                                        </div>

                                                        <div class="col-lg-12" id="options-container">
                                                            <label>Accounting Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="21"> Accounting Department</label>
                                                            </div>

                                                        </div>

                                                        <div class="col-lg-12" id="options-container">
                                                            <label>User Management Department</label>
                                                            <div>
                                                                <label><input type="checkbox" name="options[]" value="22"> User Management</label>
                                                            </div>

                                                        </div>



                                                    </div>
                                            </div>
                                            <button type="submit" name="submit" class="btn btn-success">Add User</button>
                                            </form>

                                        </div>
                                    </div>
                                    <br>
                                    <!-- <div class="col-lg-5 content-right">
                                        <img src="../assets/images/login/01.png" class="img-fluid image-right" alt="">
                                    </div> -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
        </div>
        </section>
    </div>
    <!-- Wrapper END -->
    <!-- Footer -->
    <?php include 'includes/footer.php'; ?>
    <nav class="iq-float-menu">
        <input type="checkbox" href="#" class="iq-float-menu-open" name="menu-open" id="menu-open" />
        <label class="iq-float-menu-open-button" for="menu-open">
            <span class="lines line-1"></span>
            <span class="lines line-2"></span>
            <span class="lines line-3"></span>
        </label>
        <button class="iq-float-menu-item bg-info" data-toggle="tooltip" data-placement="top" title="Direction Mode"
            data-mode="rtl"><i class="ri-text-direction-r"></i></button>
        <button class="iq-float-menu-item bg-danger" data-toggle="tooltip" data-placement="top" title="Color Mode"
            id="dark-mode" data-active="true"><i class="ri-sun-line"></i></button>
        <button class="iq-float-menu-item bg-warning" data-toggle="tooltip" data-placement="top" title="Comming Soon"><i
                class="ri-palette-line"></i></button>
    </nav>
    <!-- Footer END -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="js/jquery.min.js"></script>
    <!-- Rtl and Darkmode -->
    <script src="js/rtl.js"></script>
    <script src="js/customizer.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Appear JavaScript -->
    <script src="js/jquery.appear.js"></script>
    <!-- Countdown JavaScript -->
    <script src="js/countdown.min.js"></script>
    <!-- Counterup JavaScript -->
    <script src="js/waypoints.min.js"></script>
    <script src="js/jquery.counterup.min.js"></script>
    <!-- Wow JavaScript -->
    <script src="js/wow.min.js"></script>
    <!-- Apexcharts JavaScript -->
    <script src="js/apexcharts.js"></script>
    <!-- Slick JavaScript -->
    <script src="js/slick.min.js"></script>
    <!-- Select2 JavaScript -->
    <script src="js/select2.min.js"></script>
    <!-- Owl Carousel JavaScript -->
    <script src="js/owl.carousel.min.js"></script>
    <!-- Magnific Popup JavaScript -->
    <script src="js/jquery.magnific-popup.min.js"></script>
    <!-- Smooth Scrollbar JavaScript -->
    <script src="js/smooth-scrollbar.js"></script>
    <!-- lottie JavaScript -->
    <script src="js/lottie.js"></script>
    <!-- Chart Custom JavaScript -->
    <script src="js/chart-custom.js"></script>
    <!-- Custom JavaScript -->
    <script src="js/custom.js"></script>
</body>
</html>