<!DOCTYPE html>
<html lang="en">
<head>
     <?= $this->include('admin/inc/title') ?>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap 4 CSS -->
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">

    <style>
        
        .card-box {
            background: #fff;
            border-radius: 6px;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0,0,0,.1);
        }
        .error {
            color: red;
            font-size: 13px;
            font-weight: bold;
        }
        table th {
            background: #007bff;
            color: #fff;
        }
    </style>
</head>

<body>

 <?= $this->include('admin/inc/header') ?>

<!-- 🔹 MAIN CONTENT -->
<div class="container mt-4">

    <!-- Flash Messages -->
    <?php if (session()->getFlashdata('success')) { ?>
        <div class="alert alert-success text-center">
            <?= session()->getFlashdata('success') ?>
        </div>
    <?php } ?>

    <?php if (session()->getFlashdata('error')) { ?>
        <div class="alert alert-danger text-center">
            <?= session()->getFlashdata('error') ?>
        </div>
    <?php } ?>

    <h3 class="mb-4">Manage Users</h3>

    <div class="row">

        <!-- 🔹 ADD / EDIT USER FORM -->
        <div class="col-md-4">
            <div class="card-box">

                <h5 class="text-center text-danger mb-3">
                    <?= isset($editUser) ? 'Edit User' : 'Add User' ?>
                </h5>

                <form method="post" action="<?= isset($editUser)? base_url('users/update/'.$editUser['id']): base_url('users/store') ?>">
			
                    <div class="form-group">
                        <input type="text" name="name" class="form-control" placeholder="Full Name" value="<?= old('name', $editUser['name'] ?? '') ?>">
                    </div>

                    <div class="form-group">
                        <input type="text" name="mobile_number" class="form-control"
                               placeholder="Mobile Number"
                               value="<?= old('mobile_number', $editUser['mobile_number'] ?? '') ?>">
                    </div>

                    <div class="form-group">
                        <input type="text" name="username" class="form-control"
                               placeholder="Username"
                               value="<?= old('username', $editUser['username'] ?? '') ?>">
                    </div>

                    <div class="form-group">
                        <input type="password" name="password" class="form-control"
                               placeholder="Password" value="<?= old('password', $editUser['password'] ?? '') ?>">
                    </div>

                    <button type="submit" class="btn btn-success btn-block">
                        <?= isset($editUser) ? 'Update User' : 'Add User' ?>
                    </button>

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

        <!-- 🔹 USER LIST -->
        <div class="col-md-8">
            <div class="card-box">

                <h5 class="text-center text-danger mb-3">User List</h5>

                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>S.No</th>
                            <th>Name</th>
                            <th>Mobile</th>
                            <th>Username</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>

                    <?php if (!empty($users)) {
                        $i = 1;
                        foreach ($users as $user) { ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= esc($user['name']) ?></td>
                                <td><?= esc($user['mobile_number']) ?></td>
                                <td><?= esc($user['username']) ?></td>
                                <td>
                                    <div class="dropdown">
                                        <button class="btn btn-sm btn-secondary dropdown-toggle"
                                                type="button"
                                                data-toggle="dropdown"
                                                data-boundary="window">
                                            Actions
                                        </button>

                                        <div class="dropdown-menu">
                                            <a class="dropdown-item"
                                               href="<?= base_url('users/edit/'.$user['id']) ?>">
                                                Edit
                                            </a>

                                            <a class="dropdown-item text-danger"
                                               href="<?= base_url('users/delete/'.$user['id']) ?>"
                                               onclick="return confirm('Are you sure you want to delete this user?')">
                                                Delete
                                            </a>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                    <?php } } else { ?>
                        <tr>
                            <td colspan="5" class="text-center text-danger">
                                No users found
                            </td>
                        </tr>
                    <?php } ?>

                    </tbody>
                </table>

            </div>
        </div>

    </div>
	
	<div class="row mt-5">
		<div class="col-md-4"></div>
		<div class="col-md-4"><a href="<?= base_url('home')?>"><button class="btn btn-danger btn-md">Return to Home</button></a></div>
		<div class="col-md-4"></div>
        </div>
</div>

<!-- 🔹 REQUIRED JS (ORDER IS IMPORTANT) -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js"></script>

<!-- 🔹 AUTO FADE ALERTS -->
<script>
    setTimeout(function () {
        $('.alert').fadeOut('slow');
    }, 5000);
</script>
 <?= $this->include('admin/inc/footer') ?>
</body>
</html>
