👁 查看:manage_technicians.php
路径:/home/boguraj1/public_html/isp/manage_technicians.php
大小:9.8 KB · 修改:2026-06-15 23:29:20 · 权限:0644 · 可写
<?php
session_start();
require_once 'includes/auth.php';
requireAdmin();
require_once 'config/db.php';
$success = '';
$error = '';
// ---- Handle Delete ----
if (isset($_GET['delete'])) {
$del_id = (int) $_GET['delete'];
// Prevent deleting yourself
if ($del_id === (int) $_SESSION['user_id']) {
$error = 'You cannot delete your own account.';
} else {
$pdo->prepare("DELETE FROM users WHERE id = ? AND role = 'technician'")->execute([$del_id]);
$success = 'Technician deleted successfully.';
}
}
// ---- Handle Add ----
$form = ['full_name' => '', 'username' => '', 'password' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
$form['full_name'] = trim($_POST['full_name'] ?? '');
$form['username'] = trim($_POST['username'] ?? '');
$form['password'] = $_POST['password'] ?? '';
if ($form['full_name'] === '') {
$error = 'Full name is required.';
} elseif ($form['username'] === '') {
$error = 'Username is required.';
} elseif (strlen($form['password']) < 6) {
$error = 'Password must be at least 6 characters.';
} else {
// Check duplicate username
$dup = $pdo->prepare("SELECT id FROM users WHERE username = ?");
$dup->execute([$form['username']]);
if ($dup->fetch()) {
$error = "Username \"{$form['username']}\" is already taken.";
} else {
$pdo->prepare(
"INSERT INTO users (full_name, username, password, role) VALUES (?, ?, ?, 'technician')"
)->execute([$form['full_name'], $form['username'], password_hash($form['password'], PASSWORD_DEFAULT)]);
$success = "Technician \"{$form['full_name']}\" added successfully.";
$form = ['full_name' => '', 'username' => '', 'password' => ''];
}
}
}
// ---- Fetch all technicians ----
$technicians = $pdo->query(
"SELECT u.id, u.full_name, u.username, u.created_at,
COUNT(st.id) AS total_entries
FROM users u
LEFT JOIN stock_transactions st ON st.user_id = u.id
WHERE u.role = 'technician'
GROUP BY u.id
ORDER BY u.full_name ASC"
)->fetchAll();
include 'includes/header.php';
?>
<div class="row mb-3 align-items-center">
<div class="col">
<h4 class="page-title"><i class="bi bi-people me-2 text-primary"></i>Manage Technicians</h4>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
<li class="breadcrumb-item active">Technicians</li>
</ol>
</nav>
</div>
</div>
<?php if ($success !== ''): ?>
<div class="alert alert-success alert-dismissible fade show d-flex align-items-center gap-2">
<i class="bi bi-check-circle-fill"></i> <?= htmlspecialchars($success) ?>
<button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($error !== ''): ?>
<div class="alert alert-danger d-flex align-items-center gap-2">
<i class="bi bi-exclamation-triangle-fill"></i> <?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<div class="row g-4">
<!-- ===== Add Technician Form ===== -->
<div class="col-lg-4">
<div class="card">
<div class="card-header bg-primary text-white">
<i class="bi bi-person-plus me-1"></i> Add New Technician
</div>
<div class="card-body">
<form method="POST" action="" novalidate>
<input type="hidden" name="action" value="add">
<div class="mb-3">
<label class="form-label fw-semibold">
Full Name <span class="text-danger">*</span>
</label>
<input type="text" class="form-control" name="full_name"
value="<?= htmlspecialchars($form['full_name']) ?>"
placeholder="e.g., Rahim Uddin" required>
</div>
<div class="mb-3">
<label class="form-label fw-semibold">
Username <span class="text-danger">*</span>
</label>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-person"></i></span>
<input type="text" class="form-control" name="username"
value="<?= htmlspecialchars($form['username']) ?>"
placeholder="e.g., rahim01" required>
</div>
</div>
<div class="mb-4">
<label class="form-label fw-semibold">
Password <span class="text-danger">*</span>
</label>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-lock"></i></span>
<input type="password" class="form-control" name="password"
id="techPwd" placeholder="Min. 6 characters" required>
<button type="button" class="btn btn-outline-secondary" id="toggleTechPwd">
<i class="bi bi-eye"></i>
</button>
</div>
<div class="form-text">Minimum 6 characters.</div>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary fw-semibold">
<i class="bi bi-person-plus me-1"></i> Add Technician
</button>
</div>
</form>
</div>
</div>
</div>
<!-- ===== Technician List ===== -->
<div class="col-lg-8">
<div class="card">
<div class="card-header bg-secondary text-white">
<i class="bi bi-people me-1"></i> Technician Accounts
<span class="badge bg-light text-dark ms-1"><?= count($technicians) ?></span>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover mb-0 align-middle">
<thead>
<tr>
<th>#</th>
<th>Full Name</th>
<th>Username</th>
<th class="text-center">Total Entries</th>
<th>Created</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($technicians)): ?>
<tr>
<td colspan="6" class="text-center py-4 text-muted">
<i class="bi bi-people fs-2 d-block mb-2"></i>
No technicians yet. Add one using the form on the left.
</td>
</tr>
<?php else: ?>
<?php foreach ($technicians as $i => $t): ?>
<tr>
<td class="text-muted small"><?= $i + 1 ?></td>
<td class="fw-semibold"><?= htmlspecialchars($t['full_name']) ?></td>
<td>
<span class="badge bg-light text-dark border">
<i class="bi bi-person me-1"></i><?= htmlspecialchars($t['username']) ?>
</span>
</td>
<td class="text-center">
<a href="stock_history.php?submitted_by=<?= $t['id'] ?>"
class="badge bg-info text-white text-decoration-none"
title="View this technician's entries">
<?= $t['total_entries'] ?> entries
</a>
</td>
<td class="text-muted small">
<?= date('d M Y', strtotime($t['created_at'])) ?>
</td>
<td class="text-center">
<a href="manage_technicians.php?delete=<?= $t['id'] ?>"
class="btn btn-sm btn-danger"
onclick="return confirm('Delete technician \'<?= addslashes($t['full_name']) ?>\'? Their submission history will remain but won\'t be linked to any user.')">
<i class="bi bi-trash"></i> Delete
</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('toggleTechPwd').addEventListener('click', function () {
const pwd = document.getElementById('techPwd');
const icon = this.querySelector('i');
if (pwd.type === 'password') {
pwd.type = 'text';
icon.classList.replace('bi-eye', 'bi-eye-slash');
} else {
pwd.type = 'password';
icon.classList.replace('bi-eye-slash', 'bi-eye');
}
});
</script>
<?php include 'includes/footer.php'; ?>