<?php
session_start();
if (!isset($_SESSION['role_id'])||$_SESSION['role_id']!=1){header("Location:../../index.php");exit();}
include('../../includes/config.php');
include('../../includes/header.php');
include('../../includes/footer.php');

$store_id   = $_GET['store_id'] ?? '';
$date_from  = $_GET['date_from'] ?? date('Y-m-01');
$date_to    = $_GET['date_to']   ?? date('Y-m-d');
$export     = $_GET['export'] ?? '';

$where = ['DATE(si.created_at)>=?','DATE(si.created_at)<=?'];
$params = [$date_from,$date_to];
$types='ss';
if($store_id!==''){ $where[]='st.store_id=?'; $params[]= $store_id; $types.='i'; }

$sql = "SELECT c.category_name AS category, SUM(si.vat_rate) AS vat_total
        FROM sales_items si
        JOIN sales_transactions st ON si.transaction_id = st.id
        JOIN products p ON si.product_id = p.id
        JOIN categories c ON p.category_id = c.id
        WHERE " . implode(' AND ', $where) . "
        GROUP BY c.category_name";

$stmt=$conn->prepare($sql);
$stmt->bind_param($types,...$params);
$stmt->execute();
$data=$stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

$stores = $conn->query("SELECT id,store_name FROM stores");
if($export==='excel'){
    header("Content-Type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=vat_summary.xls");
    echo "<table border=1><tr><th>Category</th><th>VAT Total</th></tr>";
    foreach($data as $r) echo "<tr><td>".htmlspecialchars($r['category'])."</td><td>{$r['vat_total']}</td></tr>";
    echo "</table>"; exit();
}
?>
<!DOCTYPE html><html><head><title>VAT Summary by Category</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body{background:url('../../images/profile.jpg')no-repeat center fixed;color:white;font:Segoe UI;}
.overlay{position:fixed;inset:0;background:rgba(0,0,0,0.6);z-index:-1;}
.content{margin:100px auto;max-width:800px;padding:25px;background:rgba(255,255,255,0.08);backdrop-filter:blur(15px);border-radius:15px;}
.filters{display:flex;gap:10px;margin-bottom:20px;}
.filters select,input,button{padding:8px;border-radius:6px;background:rgba(255,255,255,0.2);border:none;color:white;}
.export-btn{background:#ff6f00;}
summary,table{width:100%;}
table,th,td{border-collapse:collapse;background:rgba(0,0,0,0.3);color:white;}
th{background:#ff6f00;}
     .back-link {
    display: inline-block;
    margin-bottom: 20px;
    padding: 10px 20px;
    background:rgba(255, 111, 0, 0);
    color: white;
    font-weight: 600;
    border-radius: 8px;
    text-decoration: none;
    transition: background-color 0.3s ease;
}
.back-link:hover {
    background: #e65c00;
}

</style></head><body><div class="overlay"></div><div class="content">
              <a href="reports.php" class="back-link">&larr; Back to Reports</a>

<h2>VAT Collected by Category</h2>
<form class="filters" method="GET">
<select name="store_id"><option value="">All Stores</option>
<?php while($s=$stores->fetch_assoc()):?>
<option value="<?=$s['id']?>" <?=($store_id==$s['id']?'selected':'')?>><?=htmlspecialchars($s['store_name'])?></option>
<?php endwhile;?>
</select>
<input type="date" name="date_from" value="<?=$date_from?>"><input type="date" name="date_to" value="<?=$date_to?>">
<button type="submit">Filter</button>
<button type="button" class="export-btn" onclick="location.search+='&export=excel'">Export Excel</button>
<button type="button" class="export-btn" onclick="window.print()">Export PDF</button>
</form>
<canvas id="vatChart" height="100"></canvas>
<table><thead><tr><th>Category</th><th>VAT Total</th></tr></thead><tbody>
<?php foreach($data as $r): ?>
<tr><td><?=htmlspecialchars($r['category'])?></td><td><?=number_format($r['vat_total'],2)?></td></tr>
<?php endforeach;?>
</tbody></table></div>
<script>
const ctx=document.getElementById('vatChart').getContext('2d');
new Chart(ctx,{type:'pie',data:{labels:<?=json_encode(array_column($data,'category'))?>,datasets:[{data:<?=json_encode(array_column($data,'vat_total'))?>,backgroundColor:['#ff6f00','#ffba5c','#ffa500','#f57c00']]}});</script>
</body></html>
