<?php
// Get users created by this reseller
$reseller_users = getUsersByReseller($user_id);
$all_products = getAllProducts();
// Get current user data
$user_data = getUserById($user_id);
// Get current user quota info for agen
if (hasRole('agen')) {
$user_quota = $user_data['user_quota'] ?? 0;
$used_quota = $user_data['used_quota'] ?? 0;
$available_quota = $user_quota - $used_quota;
} else {
$user_quota = 0;
$available_quota = 0;
}
?>
<!-- Reseller Panel Page -->
<div class="mb-6">
<div class="flex justify-between items-center">
<div>
<h2 class="text-2xl font-bold text-gray-800 mb-2">Panel Reseller</h2>
<p class="text-gray-600">Kelola user dan berikan akses produk digital.</p>
</div>
<button onclick="openAddUserModal()"
class="bg-blue-primary hover:bg-blue-dark text-white px-4 py-2 rounded-lg transition duration-200 flex items-center">
<i class="fas fa-plus mr-2"></i>
Tambah User Baru
</button>
</div>
</div>
<!-- Users Table -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
<div class="px-6 py-4 border-b border-gray-200">
<h3 class="text-lg font-semibold text-gray-800">Daftar User</h3>
</div>
<?php if (empty($reseller_users)): ?>
<div class="p-8 text-center">
<div class="bg-gray-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4">
<i class="fas fa-users text-gray-400 text-2xl"></i>
</div>
<h3 class="text-lg font-semibold text-gray-800 mb-2">Belum Ada User</h3>
<p class="text-gray-600 mb-4">Anda belum menambahkan user apapun.</p>
<button onclick="openAddUserModal()"
class="bg-blue-primary hover:bg-blue-dark text-white px-4 py-2 rounded-lg transition duration-200">
<i class="fas fa-plus mr-2"></i>
Tambah User Pertama
</button>
</div>
<?php else: ?>
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Role</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Quota</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Bergabung</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Aksi</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
<?php foreach ($reseller_users as $reseller_user): ?>
<tr class="hover:bg-gray-50" data-user-id="<?php echo $reseller_user['id']; ?>">
<td class="px-6 py-4 whitespace-nowrap">
<div class="flex items-center">
<div class="bg-blue-100 w-10 h-10 rounded-full flex items-center justify-center mr-3">
<i class="fas fa-user text-blue-primary"></i>
</div>
<div>
<div class="text-sm font-medium text-gray-900">
<?php echo htmlspecialchars($reseller_user['full_name']); ?>
</div>
<div class="text-sm text-gray-500">
@<?php echo htmlspecialchars($reseller_user['username']); ?>
</div>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
<?php echo htmlspecialchars($reseller_user['email']); ?>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="role-badge <?php echo $reseller_user['role'] === 'reseller' ? 'bg-purple-100 text-purple-800' : 'bg-gray-100 text-gray-800'; ?> px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
<?php echo ucfirst($reseller_user['role']); ?>
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
<?php if ($reseller_user['role'] === 'reseller'): ?>
<span class="text-blue-600 font-medium"><?php echo $reseller_user['user_quota'] ?? 0; ?></span>
<?php else: ?>
<span class="text-gray-400">-</span>
<?php endif; ?>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="status-badge <?php echo $reseller_user['status'] === 'active' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?> px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
<?php echo ucfirst($reseller_user['status']); ?>
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
<?php echo formatDate($reseller_user['created_at']); ?>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium space-x-2">
<button onclick="openEditUserModal(<?php echo $reseller_user['id']; ?>, '<?php echo htmlspecialchars($reseller_user['username']); ?>', '<?php echo htmlspecialchars($reseller_user['email']); ?>', '<?php echo htmlspecialchars($reseller_user['full_name']); ?>', '<?php echo $reseller_user['role']; ?>', '<?php echo htmlspecialchars($reseller_user['whatsapp_number'] ?? ''); ?>')"
class="text-indigo-600 hover:text-indigo-900 transition duration-200">
<i class="fas fa-edit mr-1"></i>Edit
</button>
<button onclick="openProductModal(<?php echo $reseller_user['id']; ?>, '<?php echo htmlspecialchars($reseller_user['full_name']); ?>')"
class="text-blue-600 hover:text-blue-900 transition duration-200">
<i class="fas fa-key mr-1"></i>Kelola Akses
</button>
<?php if ($reseller_user['role'] === 'reseller'): ?>
<button onclick="openQuotaModal(<?php echo $reseller_user['id']; ?>, '<?php echo htmlspecialchars($reseller_user['full_name']); ?>', <?php echo $reseller_user['user_quota'] ?? 0; ?>)"
class="text-green-600 hover:text-green-900 transition duration-200">
<i class="fas fa-chart-pie mr-1"></i>Edit Quota
</button>
<?php endif; ?>
<form method="POST" class="inline" onsubmit="return confirm('Yakin ingin mengubah status user ini?')">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<input type="hidden" name="action" value="toggle_user_status">
<input type="hidden" name="user_id" value="<?php echo $reseller_user['id']; ?>">
<button type="submit" class="text-yellow-600 hover:text-yellow-900 transition duration-200">
<i class="fas fa-toggle-<?php echo $reseller_user['status'] === 'active' ? 'on' : 'off'; ?> mr-1"></i>
<?php echo $reseller_user['status'] === 'active' ? 'Nonaktifkan' : 'Aktifkan'; ?>
</button>
</form>
<form method="POST" class="inline" onsubmit="return confirm('Yakin ingin menghapus user ini? Tindakan ini tidak dapat dibatalkan.')">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<input type="hidden" name="action" value="delete_user">
<input type="hidden" name="user_id" value="<?php echo $reseller_user['id']; ?>">
<button type="submit" class="text-red-600 hover:text-red-900 transition duration-200">
<i class="fas fa-trash mr-1"></i>Hapus
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
<!-- Add User Modal -->
<div id="addUserModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden z-50 modal-backdrop">
<div class="flex items-center justify-center min-h-screen p-4">
<div class="bg-white rounded-lg shadow-xl max-w-md w-full">
<div class="px-6 py-4 border-b border-gray-200">
<h3 class="text-lg font-semibold text-gray-800">Tambah User Baru</h3>
</div>
<form id="addUserForm" method="POST" class="p-6 space-y-4">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<input type="hidden" name="action" value="add_user">
<div>
<label for="username" class="block text-sm font-medium text-gray-700 mb-1">Username</label>
<input type="text" id="username" name="username" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
</div>
<div>
<label for="email" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" id="email" name="email" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
</div>
<div>
<label for="full_name" class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap</label>
<input type="text" id="full_name" name="full_name" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
</div>
<div>
<label for="whatsapp_number" class="block text-sm font-medium text-gray-700 mb-1">Nomor WhatsApp</label>
<input type="text" id="whatsapp_number" name="whatsapp_number" placeholder="+62812345678"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
<p class="text-xs text-gray-500 mt-1">Format: +62812345678 (opsional)</p>
</div>
<div>
<label for="password" class="block text-sm font-medium text-gray-700 mb-1">Password</label>
<input type="password" id="password" name="password" required minlength="6"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
<p class="text-xs text-gray-500 mt-1">Minimal 6 karakter</p>
</div>
<div>
<label for="role" class="block text-sm font-medium text-gray-700 mb-1">Role</label>
<select id="role" name="role" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
<?php if (hasRole('agen')): ?>
<option value="reseller">Reseller</option>
<option value="user" selected>User</option>
<?php else: ?>
<option value="user" selected>User</option>
<?php endif; ?>
</select>
<p class="text-xs text-gray-500 mt-1">
<?php if (hasRole('agen')): ?>
Agen dapat menambahkan Reseller atau User
<?php else: ?>
Reseller hanya dapat menambahkan User
<?php endif; ?>
</p>
</div>
<?php if (hasRole('agen')): ?>
<?php endif; ?>
<div class="flex justify-end space-x-3 pt-4">
<button type="button" onclick="closeAddUserModal()"
class="px-4 py-2 text-gray-600 border border-gray-300 rounded-lg hover:bg-gray-50 transition duration-200">
Batal
</button>
<button type="submit"
class="px-4 py-2 bg-blue-primary text-white rounded-lg hover:bg-blue-dark transition duration-200">
<i class="fas fa-plus mr-1"></i>Tambah User
</button>
</div>
</form>
</div>
</div>
</div>
<!-- Product Access Modal -->
<div id="productModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden z-50 modal-backdrop">
<div class="flex items-center justify-center min-h-screen p-4">
<div class="bg-white rounded-lg shadow-xl max-w-2xl w-full max-h-96 overflow-y-auto">
<div class="px-6 py-4 border-b border-gray-200">
<h3 class="text-lg font-semibold text-gray-800">Kelola Akses Produk</h3>
<p class="text-sm text-gray-600 mt-1">User: <span id="modalUserName"></span></p>
</div>
<input type="hidden" id="modalUserId" value="">
<div class="p-6">
<div class="space-y-3" id="productsList">
<!-- Products will be loaded here -->
</div>
</div>
<div class="px-6 py-4 border-t border-gray-200 flex justify-end">
<button type="button" onclick="closeProductModal()"
class="px-4 py-2 text-gray-600 border border-gray-300 rounded-lg hover:bg-gray-50 transition duration-200">
Tutup
</button>
</div>
</div>
</div>
</div>
<!-- Quota Edit Modal -->
<div id="quotaModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden z-50 modal-backdrop">
<div class="flex items-center justify-center min-h-screen p-4">
<div class="bg-white rounded-lg shadow-xl max-w-md w-full">
<div class="px-6 py-4 border-b border-gray-200">
<h3 class="text-lg font-semibold text-gray-800">Edit Quota Reseller</h3>
<p class="text-sm text-gray-600 mt-1">User: <span id="quotaModalUserName"></span></p>
</div>
<form id="quotaForm" class="p-6">
<input type="hidden" id="quotaModalUserId" name="quotaModalUserId" value="">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<div class="mb-4">
<label for="userQuota" class="block text-sm font-medium text-gray-700 mb-2">Quota Reseller</label>
<input type="number" id="userQuota" name="user_quota" min="0" max="<?php echo $user_quota ?? 0; ?>"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="Masukkan quota reseller" required>
<p class="text-xs text-gray-500 mt-1">Quota tersedia agen: <?php echo $available_quota ?? 0; ?></p>
</div>
<div class="flex justify-end space-x-3">
<button type="button" onclick="closeQuotaModal()"
class="px-4 py-2 text-gray-600 border border-gray-300 rounded-lg hover:bg-gray-50 transition duration-200">
Batal
</button>
<button type="submit"
class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition duration-200">
Simpan
</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit User Modal -->
<div id="editUserModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden z-50 modal-backdrop">
<div class="flex items-center justify-center min-h-screen p-4">
<div class="bg-white rounded-lg shadow-xl max-w-md w-full">
<div class="px-6 py-4 border-b border-gray-200">
<h3 class="text-lg font-semibold text-gray-800">Edit User</h3>
<p class="text-sm text-gray-600 mt-1">Ubah informasi user</p>
</div>
<form id="editUserForm" class="p-6 space-y-4">
<input type="hidden" id="editUserId" name="edit_user_id" value="">
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<div>
<label for="editUsername" class="block text-sm font-medium text-gray-700 mb-1">Username</label>
<input type="text" id="editUsername" name="edit_username" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
</div>
<div>
<label for="editEmail" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" id="editEmail" name="edit_email" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
</div>
<div>
<label for="editFullName" class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap</label>
<input type="text" id="editFullName" name="edit_full_name" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
</div>
<div>
<label for="editWhatsappNumber" class="block text-sm font-medium text-gray-700 mb-1">Nomor WhatsApp</label>
<input type="text" id="editWhatsappNumber" name="edit_whatsapp_number" placeholder="+62812345678"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
<p class="text-xs text-gray-500 mt-1">Format: +62812345678 (opsional)</p>
</div>
<div>
<label for="editRole" class="block text-sm font-medium text-gray-700 mb-1">Role</label>
<select id="editRole" name="edit_role" required
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
<?php if (hasRole('agen')): ?>
<option value="reseller">Reseller</option>
<option value="user">User</option>
<?php else: ?>
<option value="user">User</option>
<?php endif; ?>
</select>
<p class="text-xs text-gray-500 mt-1">
<?php if (hasRole('agen')): ?>
Agen dapat mengubah role ke Reseller atau User
<?php else: ?>
Reseller hanya dapat mengubah role ke User
<?php endif; ?>
</p>
</div>
<div>
<label for="editPassword" class="block text-sm font-medium text-gray-700 mb-1">Password Baru (Opsional)</label>
<input type="password" id="editPassword" name="edit_password" minlength="6"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
<p class="text-xs text-gray-500 mt-1">Kosongkan jika tidak ingin mengubah password</p>
</div>
<div class="flex justify-end space-x-3 pt-4">
<button type="button" onclick="closeEditUserModal()"
class="px-4 py-2 text-gray-600 border border-gray-300 rounded-lg hover:bg-gray-50 transition duration-200">
Batal
</button>
<button type="submit"
class="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition duration-200">
<i class="fas fa-save mr-1"></i>Simpan Perubahan
</button>
</div>
</form>
</div>
</div>
</div>
<!-- Add CSRF token meta tag -->
<meta name="csrf-token" content="<?php echo generateCSRFToken(); ?>">
<!-- Include Panel JavaScript -->
<script src="js/panel.js"></script>