<?php
require_once 'app/helpers/auth.php';
require_once 'app/helpers/rbac.php';
requireLogin();

if (!can('user.view')) {
    echo "<div class='alert alert-danger m-4'>Access denied</div>";
    exit;
}

$page_js = 'users.js';

include 'views/layouts/header.php';
include 'views/layouts/sidebar.php';
?>

<script>
window._CAN_EDIT = <?= can('user.edit') ? 'true' : 'false' ?>;
window._CAN_DELETE = <?= can('user.delete') ? 'true' : 'false' ?>;
</script>

<main class="main-content">
<?php include 'views/layouts/topbar.php'; ?>

<div class="container-fluid p-4">

    <div class="d-flex justify-content-between mb-3">
        <h4>User Management</h4>
        <?php if (can('user.create')): ?>
        <button class="btn btn-primary" id="btnAddUser">Add User</button>
        <?php endif; ?>
    </div>

    <div class="card">
        <div class="card-body">

            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Role</th>
                        <th>Alerts</th>
                        <th>Status</th>
                        <?php if (can('user.edit') || can('user.delete')): ?>
                        <th>Action</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody id="userTable"></tbody>
            </table>

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

</main>

<!-- MODAL -->
<div class="modal fade" id="userModal">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h5>User</h5>
                <button class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">

                <input type="hidden" id="user_id">

                <input class="form-control mb-2" id="name" placeholder="Name">
                <input class="form-control mb-2" id="email" placeholder="Email">
                <input class="form-control mb-2" id="phone" placeholder="Phone for SMS alerts">
                <input type="password" class="form-control mb-2" id="password" placeholder="Password">

                <select id="role_id" class="form-control mb-2"></select>

                <div class="row g-2 mb-2">
                    <div class="col-6">
                        <select id="email_alerts_enabled" class="form-control">
                            <option value="1">Email alerts on</option>
                            <option value="0">Email alerts off</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <select id="sms_alerts_enabled" class="form-control">
                            <option value="1">SMS alerts on</option>
                            <option value="0">SMS alerts off</option>
                        </select>
                    </div>
                </div>

                <select id="is_active" class="form-control">
                    <option value="1">Active</option>
                    <option value="0">Inactive</option>
                </select>

            </div>

            <div class="modal-footer">
                <button class="btn btn-success" id="saveUser">Save</button>
            </div>

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

<?php include 'views/layouts/footer.php'; ?>
