This commit is contained in:
ns77@siliconpin.com
2025-09-03 14:04:44 +00:00
parent 10881c6a5b
commit 533d1b572d
12 changed files with 410 additions and 218 deletions

View File

@@ -7,42 +7,56 @@
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// grafinn01
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_id = $_POST['user_id'];
$user_id = $_POST['user_id'];
$user_name = $_POST['user_name'] ?? '';
$user_phone = $_POST['user_phone'] ?? '';
$type = $_POST['type'] ?? 'agent';
$comiRate = $_POST['comi_rate'] ?? null;
$passwordPlain = $_POST['password'] ?? '';
if (empty($_POST['user_name']) || empty($_POST['user_phone']) || empty($_POST['password'])) {
die("All fields are required.");
}
// Validation
if (empty($user_name) || empty($user_phone) || empty($passwordPlain)) {
$error = "All fields are required.";
} elseif (!preg_match("/^[0-9]{10}$/", $user_phone)) {
$error = "Invalid phone number format.";
} else {
$password = password_hash($passwordPlain, PASSWORD_DEFAULT);
$user_name = $_POST['user_name'];
$user_phone = $_POST['user_phone'];
$type = $_POST['type'] ?? 'agent';
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
// Profile Picture Upload
$profilePicPath = null;
if (isset($_FILES['profile_pic']) && $_FILES['profile_pic']['error'] === UPLOAD_ERR_OK) {
$uploadDir = __DIR__ . "/picture/";
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
$fileTmp = $_FILES['profile_pic']['tmp_name'];
$fileName = time() . "_" . basename($_FILES['profile_pic']['name']);
$filePath = $uploadDir . $fileName;
if (move_uploaded_file($fileTmp, $filePath)) {
$profilePicPath = "picture/" . $fileName;
} else {
$error = "Failed to upload profile picture.";
}
}
if (!preg_match("/^[0-9]{10}$/", $user_phone)) {
die("Invalid phone number format");
}
if (!isset($error)) {
$table = $GLOBALS['arif_users'] ?? 'arif_users';
$sql = "INSERT INTO `$table`
(user_id, password, type, user_name, user_phone, comi_rate, profile_pic)
VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssssss", $user_id, $password, $type, $user_name, $user_phone, $comiRate, $profilePicPath);
$table = $GLOBALS['arif_users'] ?? 'arif_users';
$sql = "INSERT INTO `$table` (user_id, password, type, user_name, user_phone) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Prepare failed: " . $conn->error);
}
$stmt->bind_param("sssss", $user_id, $password, $type, $user_name, $user_phone);
if ($stmt->execute()) {
echo "<div class='alert alert-success'>User <strong>{$user_name}</strong> added successfully.</div>";
} else {
echo "<div class='alert alert-danger'>Failed to add user <strong>{$user_name}</strong>. Error: " . $stmt->error . "</div>";
}
$stmt->close();
$conn->close();
if ($stmt->execute()) {
$success = "User <strong>{$user_name}</strong> added successfully.";
} else {
$error = "Failed to add user: " . $stmt->error;
}
$stmt->close();
}
}
}
$getAgentListsQuery = "SELECT * FROM " . $GLOBALS['arif_users'] . " ORDER BY type, user_id";
@@ -65,7 +79,7 @@
<div class="container">
<h3>Add New Agent</h3><hr>
<form method="post">
<form method="post" enctype="multipart/form-data">
<div class="row">
<!-- Left Column -->
<div class="col-md-6">
@@ -84,17 +98,27 @@
</div>
<div class="form-group">
<label for="type">User Type</label>
<select class="form-control" id="type" name="type" required>
<option value="agent" selected>Agent</option>
<select onchange="showCommissionField();" class="form-control" id="user-type" name="type" required>
<option value="">-Select-</option>
<option value="agent" >Agent</option>
<option value="admin">Admin</option>
<option value="supervisor">Supervisor</option>
<option value="bm">BRanch Manager</option>
</select>
</div>
<div class="form-group" id="commission-field" style="display: none;">
<label for="comi_rate">Commission Rate (%)</label>
<input type="number" class="form-control" id="comi_rate" name="comi_rate" value="3" placeholder="" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
</div>
<div class="form-group">
<label for="profile_pic">Profile Picture</label>
<input type="file" class="form-control" id="profile_pic" name="profile_pic" accept="image/*">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success w-100">Add Agent</button>
@@ -115,10 +139,12 @@
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Profile</th>
<th>User ID</th>
<th>User Type</th>
<th>Name</th>
<th>Phone</th>
<th>Comi Rate (%)</th>
<th>Actions</th>
</tr>
</thead>
@@ -127,6 +153,14 @@
<?php foreach ($agentList as $user): ?>
<tr>
<td><?php echo htmlspecialchars($user['id']); ?></td>
<td>
<?php if(!empty($user['profile_pic'])): ?>
<img src="/CONTENT/ROOT_URI/Admin/<?php echo $user['profile_pic']; ?>" width="40" height="40" style="border-radius:50%;">
<?php else: ?>
<span>No Photo</span>
<?php endif; ?>
</td>
<td><?php echo htmlspecialchars($user['user_id']); ?></td>
<td class="badge-cell">
<span class="badge <?php echo $user['type'] === 'admin' ? 'badge-primary' : 'badge-secondary'; ?>">
@@ -135,6 +169,7 @@
</td>
<td><?php echo htmlspecialchars($user['user_name']); ?></td>
<td><?php echo htmlspecialchars($user['user_phone']); ?></td>
<td><?php echo htmlspecialchars($user['comi_rate']); ?></td>
<td>
<a href="edit_user?id=<?php echo $user['id']; ?>" class="btn btn-sm btn-warning">Edit</a>
<?php if($user['type'] !== 'admin') { ?>
@@ -234,6 +269,18 @@
}
notif.innerHTML = res_txt;
}
function showCommissionField(){
const selectedUserType = document.getElementById('user-type').value;
const commissionField = document.getElementById('commission-field');
if(selectedUserType === 'agent'){
commissionField.style.display = 'block';
}else{
commissionField.style.display = 'none';
}
}
</script>