88 lines
3.6 KiB
PHP
88 lines
3.6 KiB
PHP
<?php
|
|
// 后台:工单部门管理(创建 / 删除)
|
|
require __DIR__ . '/../includes/init.php';
|
|
requireStaff();
|
|
require '_common.php';
|
|
$isAdmin = isAdminStaff();
|
|
|
|
$db = db();
|
|
$msg = '';
|
|
$err = '';
|
|
// 客服账号仅可查看,禁止写操作
|
|
if (!$isAdmin && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$err = '客服账号仅可查看,如需操作请联系管理员。';
|
|
}
|
|
// 删除
|
|
if (isset($_POST['delete'])) {
|
|
verifyCsrf();
|
|
$id = (int) ($_POST['delete'] ?? 0);
|
|
// 该部门下的工单改挂「未指定」(dept_id=0)
|
|
$db->prepare('UPDATE ' . tn('tickets') . ' SET dept_id = 0 WHERE dept_id = ?')->execute([$id]);
|
|
$db->prepare('DELETE FROM ' . tn('ticket_departments') . ' WHERE id = ?')->execute([$id]);
|
|
$msg = '部门已删除,其工单已移至「未指定」。';
|
|
}
|
|
|
|
// 新增
|
|
if (isset($_POST['add'])) {
|
|
verifyCsrf();
|
|
$name = trim($_POST['name'] ?? '');
|
|
$desc = trim($_POST['description'] ?? '');
|
|
$sort = (int) ($_POST['sort'] ?? 0);
|
|
if ($name === '') {
|
|
$err = '请填写部门名称';
|
|
} else {
|
|
$db->prepare('INSERT INTO ' . tn('ticket_departments') . ' (name, description, sort, created_at) VALUES (?,?,?,NOW())')
|
|
->execute([$name, $desc, $sort]);
|
|
$msg = '部门已添加';
|
|
}
|
|
}
|
|
|
|
$list = $db->query('SELECT * FROM ' . tn('ticket_departments') . ' ORDER BY sort, id')->fetchAll();
|
|
$counts = $db->query('SELECT dept_id, COUNT(*) c FROM ' . tn('tickets') . ' GROUP BY dept_id')->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
|
|
adminHeader('工单部门', 'departments');
|
|
?>
|
|
<h1 class="page-title"><i class="fas fa-sitemap"></i> 工单部门</h1>
|
|
|
|
<?php if ($msg): ?><p class="banner-ok"><i class="fas fa-check-circle"></i> <?= h($msg) ?></p><?php endif; ?>
|
|
<?php if ($err): ?><p class="form-err"><?= h($err) ?></p><?php endif; ?>
|
|
|
|
<div class="panel">
|
|
<h3>新增部门</h3>
|
|
<form method="post" action="" class="grid-form" style="max-width:640px;">
|
|
<?= csrfField() ?>
|
|
<label>部门名称 *<input type="text" name="name" required></label>
|
|
<label>排序<input type="number" name="sort" value="0"></label>
|
|
<label class="span2">说明<input type="text" name="description" placeholder="选填,如负责范围"></label>
|
|
<div class="form-actions span2"><button type="submit" name="add" class="btn btn-primary"><i class="fas fa-plus"></i> 添加部门</button></div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="panel">
|
|
<h3>部门列表(共 <?= count($list) ?> 个)</h3>
|
|
<?php if (empty($list)): ?>
|
|
<p class="empty">还没有部门。</p>
|
|
<?php else: ?>
|
|
<table class="data-table">
|
|
<thead><tr><th>ID</th><th>名称</th><th>说明</th><th>工单数</th><th>操作</th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($list as $d): ?>
|
|
<tr>
|
|
<td><?= (int) $d['id'] ?></td>
|
|
<td><?= h($d['name']) ?></td>
|
|
<td><?= h($d['description']) ?></td>
|
|
<td><?= (int) ($counts[$d['id']] ?? 0) ?></td>
|
|
<td class="row-actions">
|
|
<form method="post" action="" style="display:inline" onsubmit="return confirm('删除该部门?其下工单将移至「未指定」。');">
|
|
<?= csrfField() ?>
|
|
<button type="submit" name="delete" value="<?= (int) $d['id'] ?>" class="mini-btn danger"><i class="fas fa-trash"></i></button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php adminFooter(); ?>
|