<?php
session_start();
require_once 'includes/auth.php';
requireTechnician();
require_once 'config/db.php';
date_default_timezone_set('Asia/Dhaka');
$user_id = (int) $_SESSION['user_id'];
// My recent 20 submissions
$my_entries = $pdo->prepare("
SELECT st.id,
st.quantity,
st.notes,
st.is_edited,
st.edited_at,
st.created_at,
p.name AS product_name,
p.unit
FROM stock_transactions st
JOIN products p ON p.id = st.product_id
WHERE st.user_id = ? AND st.type = 'deducted'
ORDER BY st.created_at DESC
LIMIT 20
");
$my_entries->execute([$user_id]);
$entries = $my_entries->fetchAll();
// Total quantity I've used (all time)
$total_used = $pdo->prepare(
"SELECT COALESCE(SUM(quantity), 0) FROM stock_transactions WHERE user_id = ? AND type = 'deducted'"
);
$total_used->execute([$user_id]);
$total_used = (int) $total_used->fetchColumn();
// Total entries count
$entry_count = count($entries); // approximate (limited to 20 above, get real count)
$real_count = $pdo->prepare(
"SELECT COUNT(*) FROM stock_transactions WHERE user_id = ? AND type = 'deducted'"
);
$real_count->execute([$user_id]);
$real_count = (int) $real_count->fetchColumn();
include 'includes/header.php';
?>
<!-- Welcome Banner -->
<div class="row mb-4">
<div class="col-12">
<div class="card border-0" style="background: linear-gradient(135deg, #0d6efd, #0a58ca);">
<div class="card-body py-4 text-white">
<h4 class="fw-bold mb-1">
<i class="bi bi-hand-wave me-2"></i>
Welcome, <?= htmlspecialchars($_SESSION['full_name'] ?? $_SESSION['username']) ?>!
</h4>
<p class="mb-3 opacity-75">Use the button below to log your product usage.</p>
<a href="tech_usage.php" class="btn btn-warning fw-bold">
<i class="bi bi-plus-circle me-1"></i> Submit New Usage
</a>
</div>
</div>
</div>
</div>
<!-- Stats -->
<div class="row g-3 mb-4">
<div class="col-6 col-md-4">
<div class="card stat-card border-primary h-100">
<div class="card-body d-flex justify-content-between align-items-center">
<div>
<p class="text-muted small mb-1">Total Submissions</p>
<h2 class="fw-bold text-primary mb-0"><?= $real_count ?></h2>
</div>
<i class="bi bi-clipboard-check stat-icon text-primary"></i>
</div>
</div>
</div>
<div class="col-6 col-md-4">
<div class="card stat-card border-warning h-100">
<div class="card-body d-flex justify-content-between align-items-center">
<div>
<p class="text-muted small mb-1">Total Items Used</p>
<h2 class="fw-bold text-warning mb-0"><?= number_format($total_used) ?></h2>
</div>
<i class="bi bi-box-arrow-up stat-icon text-warning"></i>
</div>
</div>
</div>
<div class="col-6 col-md-4">
<div class="card stat-card border-success h-100">
<div class="card-body d-flex justify-content-between align-items-center">
<div>
<p class="text-muted small mb-1">Today</p>
<?php
$today_count_stmt = $pdo->prepare(
"SELECT COUNT(*) FROM stock_transactions
WHERE user_id = ? AND type = 'deducted'
AND DATE(CONVERT_TZ(created_at, '+00:00', '+06:00')) = CURDATE()"
);
$today_count_stmt->execute([$user_id]);
$today_count = (int) $today_count_stmt->fetchColumn();
?>
<h2 class="fw-bold text-success mb-0"><?= $today_count ?></h2>
</div>
<i class="bi bi-calendar-check stat-icon text-success"></i>
</div>
</div>
</div>
</div>
<!-- My Submission History -->
<div class="card">
<div class="card-header bg-secondary text-white d-flex justify-content-between align-items-center">
<span><i class="bi bi-clock-history me-1"></i> My Recent Submissions</span>
<span class="badge bg-light text-dark"><?= $real_count ?> total</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>Product</th>
<th class="text-center">Qty Used</th>
<th>Notes / Reason</th>
<th>Date & Time</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?php if (empty($entries)): ?>
<tr>
<td colspan="6" class="text-center py-5 text-muted">
<i class="bi bi-inbox fs-1 d-block mb-2"></i>
You haven't submitted any usage yet.
<a href="tech_usage.php">Submit your first entry →</a>
</td>
</tr>
<?php else: ?>
<?php foreach ($entries as $i => $e): ?>
<tr>
<td class="text-muted small"><?= $i + 1 ?></td>
<td class="fw-semibold">
<?= htmlspecialchars($e['product_name']) ?>
<span class="text-muted small">(<?= htmlspecialchars($e['unit']) ?>)</span>
</td>
<td class="text-center fw-bold text-danger">
<?= number_format($e['quantity']) ?>
</td>
<td class="text-muted small"><?= htmlspecialchars($e['notes'] ?: '—') ?></td>
<td class="small text-muted">
<?= date('d M Y', strtotime($e['created_at'])) ?>
<br>
<?= date('h:i A', strtotime($e['created_at'])) ?>
<?php if ($e['is_edited']): ?>
<br>
<span class="badge bg-secondary" style="font-size:0.68em;"
title="Last edited: <?= date('d M Y h:i A', strtotime($e['edited_at'])) ?>">
<i class="bi bi-pencil"></i> Edited
</span>
<?php endif; ?>
</td>
<td class="text-center">
<a href="tech_usage_edit.php?id=<?= $e['id'] ?>"
class="btn btn-sm btn-outline-primary"
title="Edit this entry">
<i class="bi bi-pencil-square"></i> Edit
</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<?php if ($real_count > 20): ?>
<div class="card-footer text-muted small">
Showing latest 20 of <?= $real_count ?> submissions.
</div>
<?php endif; ?>
</div>
<?php include 'includes/footer.php'; ?>