<?php
session_start();
include('../../includes/config.php');
include __DIR__ . '/../../includes/header.php';
include __DIR__ . '/../../includes/sidebar.php';
include __DIR__ . '/../../includes/footer.php';

// Redirect if not a cashier
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 3) {
    header("Location: ../../index.php");
    exit();
}

$store_id = $_SESSION['store_id'];
$cashier_id = $_SESSION['user_id'];

// Today's summary
$summary_sql = "
    SELECT 
        COUNT(id) AS total_invoices,
        COALESCE(SUM(total_amount), 0) AS total_sales,
        COALESCE(SUM(total_amount - vat_total), 0) AS total_net
    FROM sales_transactions
    WHERE store_id = ? AND sold_by = ? AND DATE(created_at) = CURDATE()
";
$sum_stmt = $conn->prepare($summary_sql);
$sum_stmt->bind_param("ii", $store_id, $cashier_id);
$sum_stmt->execute();
$summary = $sum_stmt->get_result()->fetch_assoc();
$sum_stmt->close();

// Recent invoices (today)
$invoices_sql = "
    SELECT id, receipt_number, total_amount, vat_total, discount_total, payment_method, created_at
    FROM sales_transactions
    WHERE store_id = ? AND sold_by = ? AND DATE(created_at) = CURDATE()
    ORDER BY created_at DESC
    LIMIT 10
";
$inv_stmt = $conn->prepare($invoices_sql);
$inv_stmt->bind_param("ii", $store_id, $cashier_id);
$inv_stmt->execute();
$invoices = $inv_stmt->get_result();
$inv_stmt->close();

function e($str) {
    return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Cashier Dashboard</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aos@2.3.4/dist/aos.css" />
    <style>
        /* Styling preserved from previous message (glassmorphism dashboard) */
        body, html {
            margin: 0; padding: 0;
            font-family: 'Segoe UI', sans-serif;
            background: url('../../images/profile.jpg') no-repeat center center fixed;
            background-size: cover;
        }

        body::before {
            content: '';
            position: fixed;
            top: 0; left: 0;
            width: 100%; height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: -1;
        }

        .content {
            margin-left: 200px;
            margin-top: 70px;
            padding: 30px;
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            color: white;
            min-height: 100vh;
        }

        h3 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #ffa500;
        }

        .summary-cards {
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
        }

        .card {
            background: rgba(255,255,255,0.1);
            padding: 20px;
            border-radius: 12px;
            min-width: 220px;
            flex: 1;
            box-shadow: 0 8px 16px rgba(0,0,0,0.3);
        }

        .card h4 {
            margin: 0;
            font-size: 18px;
            color: #ff6f00;
        }

        .card p {
            font-size: 1.4rem;
            margin-top: 10px;
            font-weight: bold;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 30px;
            background: rgba(255,255,255,0.1);
            backdrop-filter: blur(6px);
            border-radius: 12px;
            overflow: hidden;
            color: white;
        }

        th, td {
            padding: 12px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        th {
            background: rgba(255, 255, 255, 0.2);
            color: #fff;
            text-align: left;
        }

        tbody tr:hover {
            background: rgba(255,255,255,0.05);
        }

        .btn-view {
            background: #ffa500;
            color: black;
            padding: 6px 12px;
            border-radius: 6px;
            text-decoration: none;
            font-weight: bold;
            font-size: 0.9rem;
        }

        .btn-view:hover {
            background: #cc7a00;
        }

        @media(max-width: 768px) {
            .content {
                margin: 80px 15px 15px 15px;
            }
            .summary-cards {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>

<div class="content">
    <h3>Today's Sales Summary</h3>
    <div class="summary-cards">
        <div class="card">
            <h4>Total Sales</h4>
            <p>MWK <?= number_format($summary['total_sales'], 2) ?></p>
        </div>
        <div class="card">
            <h4>Total Invoices</h4>
            <p><?= $summary['total_invoices'] ?></p>
        </div>
        <div class="card">
            <h4>Net Revenue</h4>
            <p>MWK <?= number_format($summary['total_net'], 2) ?></p>
        </div>
    </div>

    <h3 style="margin-top: 40px;">Recent Invoices (Today)</h3>
    <table>
        <thead>
            <tr>
                <th>Receipt #</th>
                <th>Total</th>
                <th>Discount</th>
                <th>VAT</th>
                <th>Method</th>
                <th>Date</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php if ($invoices->num_rows > 0): ?>
                <?php while ($row = $invoices->fetch_assoc()): ?>
                    <tr>
                        <td><?= e($row['receipt_number']) ?></td>
                        <td>MWK <?= number_format($row['total_amount'], 2) ?></td>
                        <td>MWK <?= number_format($row['discount_total'], 2) ?></td>
                        <td>MWK <?= number_format($row['vat_total'], 2) ?></td>
                        <td><?= e($row['payment_method']) ?></td>
                        <td><?= date('d M Y, H:i', strtotime($row['created_at'])) ?></td>
                        <td>
                            <a class="btn-view" href="print_receipt.php?sale_id=<?= $row['id'] ?>" target="_blank">View</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="7">No sales found for today.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<script src="https://cdn.jsdelivr.net/npm/aos@2.3.4/dist/aos.js"></script>
<script>AOS.init({ once: true });</script>

</body>
</html>
