<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

require_once __DIR__ . '/../../includes/auth.php';
requireLogin();
requirePermission('inventory_returns_view');

require_once __DIR__ . '/../../config/database.php';

require_once __DIR__ . '/../../includes/header.php';
require_once __DIR__ . '/../../includes/sidebar.php';

/* -------------------------------
SESSION DATA (TENANT)
--------------------------------*/

$company_id = $_SESSION['company_id'] ?? 0;

/* -------------------------------
RETURN STATISTICS (TENANT SAFE)
--------------------------------*/

$stmt = $pdo->prepare("
SELECT
COUNT(*) as total_returns,
SUM(CASE WHEN status='Pending' THEN 1 ELSE 0 END) as pending_returns,
SUM(CASE WHEN status='Approved' THEN 1 ELSE 0 END) as approved_returns,
SUM(CASE WHEN status='Closed' THEN 1 ELSE 0 END) as closed_returns
FROM inventory_returns
WHERE company_id = ?
");

$stmt->execute([$company_id]);

$stats = $stmt->fetch();

/* -------------------------------
RECENT RETURNS (TENANT SAFE)
--------------------------------*/

$stmt = $pdo->prepare("
SELECT return_number,status,created_at,id
FROM inventory_returns
WHERE company_id = ?
ORDER BY created_at DESC
LIMIT 10
");

$stmt->execute([$company_id]);

$recent = $stmt->fetchAll();

?>

<div class="main-content">

<div class="page-header">
<h2>Returns Dashboard</h2>
</div>

<!-- Stats Cards -->

<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;margin-bottom:30px;">

<div class="stat-card">
<h3>Total Returns</h3>
<p style="font-size:28px;font-weight:bold;">
<?php echo $stats['total_returns'] ?? 0; ?>
</p>
</div>

<div class="stat-card">
<h3 style="color:#f2531e;">Pending</h3>
<p style="font-size:28px;font-weight:bold;">
<?php echo $stats['pending_returns'] ?? 0; ?>
</p>
</div>

<div class="stat-card">
<h3 style="color:#007bff;">Approved</h3>
<p style="font-size:28px;font-weight:bold;">
<?php echo $stats['approved_returns'] ?? 0; ?>
</p>
</div>

<div class="stat-card">
<h3 style="color:#28a745;">Closed</h3>
<p style="font-size:28px;font-weight:bold;">
<?php echo $stats['closed_returns'] ?? 0; ?>
</p>
</div>

</div>


<!-- Quick Actions -->

<div class="stat-card" style="margin-bottom:30px;">

<h3>Quick Actions</h3>

<br>

<a href="create.php">
<button class="btn-primary">Create New Return</button>
</a>

<a href="index.php">
<button class="btn-small">View All Returns</button>
</a>

</div>


<!-- Recent Returns -->

<div class="stat-card">

<h3>Recent Returns</h3>

<table class="table">

<thead>

<tr>
<th>Return Number</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>

</thead>

<tbody>

<?php if(empty($recent)): ?>

<tr>
<td colspan="4">No returns found.</td>
</tr>

<?php else: ?>

<?php foreach($recent as $r): ?>

<tr>

<td><?php echo $r['return_number']; ?></td>

<td>

<?php
$color="#999";

if($r['status']=="Pending") $color="#f2531e";
if($r['status']=="Approved") $color="#007bff";
if($r['status']=="Closed") $color="#28a745";
?>

<span style="
padding:4px 10px;
border-radius:20px;
color:#fff;
background:<?php echo $color; ?>;
font-size:12px;
">

<?php echo $r['status']; ?>

</span>

</td>

<td><?php echo $r['created_at']; ?></td>

<td>

<a href="view.php?id=<?php echo $r['id']; ?>">
<button class="btn-small">View</button>
</a>

</td>

</tr>

<?php endforeach; ?>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>