coupon/work_order_records.php

105 lines
4.4 KiB
PHP
Executable File

<?php
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/functions.php';
requireLogin();
// 撤销工单(在输出 HTML 前处理)
if (isset($_GET['cancel']) && is_numeric($_GET['cancel'])) {
$id = (int)$_GET['cancel'];
$pid = getCurrentProductId();
$stmt = $pdo->prepare("SELECT attachment FROM work_orders WHERE id = ? AND creator_id = ? AND product_id = ? AND status = '未处理'");
$stmt->execute([$id, getCurrentUserId(), $pid]);
$order = $stmt->fetch();
if ($order) {
if ($order['attachment'] && file_exists(__DIR__ . '/' . $order['attachment'])) {
unlink(__DIR__ . '/' . $order['attachment']);
}
$pdo->prepare('DELETE FROM work_orders WHERE id = ? AND product_id = ?')->execute([$id, $pid]);
$_SESSION['flash_msg'] = '工单已撤销';
$_SESSION['flash_type'] = 'success';
} else {
$_SESSION['flash_msg'] = '无法撤销:工单不存在、非本人发起或状态已变更';
$_SESSION['flash_type'] = 'danger';
}
header('Location: work_order_records.php');
exit;
}
$pageTitle = '工单记录';
require __DIR__ . '/includes/header.php';
// 分页
$page = max(1, (int)($_GET['p'] ?? 1));
$perPage = 20;
$offset = ($page - 1) * $perPage;
$pid = getCurrentProductId();
$stmt = $pdo->prepare('SELECT COUNT(*) FROM work_orders WHERE creator_id = ? AND product_id = ?');
$stmt->execute([getCurrentUserId(), $pid]);
$total = (int)$stmt->fetchColumn();
$totalPages = max(1, ceil($total / $perPage));
$stmt = $pdo->prepare('SELECT * FROM work_orders WHERE creator_id = ? AND product_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?');
$stmt->execute([getCurrentUserId(), $pid, $perPage, $offset]);
$orders = $stmt->fetchAll();
?>
<div style="margin-bottom:16px;">
<a href="work_order_create.php" class="btn btn-primary">+ 发起工单</a>
</div>
<?php if (isset($_SESSION['flash_msg'])): ?>
<div class="alert alert-<?= h($_SESSION['flash_type'] ?? 'success') ?>"><?= h($_SESSION['flash_msg']) ?></div>
<?php unset($_SESSION['flash_msg'], $_SESSION['flash_type']); ?>
<?php endif; ?>
<div class="card">
<h2>我的工单</h2>
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>ID</th>
<th>内容</th>
<th>兑换码</th>
<th>附件</th>
<th>发起时间</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $o): ?>
<tr>
<td><?= $o['id'] ?></td>
<td style="max-width:200px;white-space:pre-wrap;word-break:break-all;"><?= h(mb_substr($o['content'], 0, 100)) ?><?= mb_strlen($o['content']) > 100 ? '...' : '' ?></td>
<td style="white-space:pre-wrap;"><?php if ($o['code']): $lines = explode("\n", $o['code']); echo h(implode("\n", $lines)); else: ?>-<?php endif; ?></td>
<td>
<?php if ($o['attachment']): ?>
<a href="<?= h($o['attachment']) ?>" target="_blank" class="btn btn-info btn-sm">查看</a>
<?php else: ?>
-
<?php endif; ?>
</td>
<td><?= formatDateTime($o['created_at']) ?></td>
<td><?= workOrderStatusBadge($o['status']) ?></td>
<td>
<?php if ($o['status'] === '未处理'): ?>
<a href="?cancel=<?= $o['id'] ?>&csrf_token=<?= csrfToken() ?>" class="btn btn-danger btn-sm" onclick="return confirm('确定撤销此工单?')">撤销</a>
<?php elseif ($o['status'] === '已驳回'): ?>
<span style="color:#999;font-size:12px;">已驳回</span>
<?php else: ?>
-
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($orders)): ?>
<tr><td colspan="7" style="text-align:center;color:#999;">暂无工单</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php renderPagination($page, $totalPages); ?>
</div>
<?php require __DIR__ . '/includes/footer.php'; ?>