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


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

$cashier_id = $_SESSION['user_id'];
$today = date('Y-m-d');

// Fetch all today's sales
$stmt = $conn->prepare("
    SELECT st.*, s.store_name 
    FROM sales_transactions st
    LEFT JOIN stores s ON st.store_id = s.id
    WHERE st.sold_by = ? AND DATE(st.created_at) = ?
    ORDER BY st.created_at DESC
");
$stmt->bind_param('is', $cashier_id, $today);
$stmt->execute();
$sales = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

// Calculate totals
$total_sales = 0;
$total_invoices = count($sales);
$total_vat = 0;
$total_discount = 0;

foreach ($sales as $sale) {
    $total_sales += $sale['total_amount'];
    $total_vat += $sale['vat_total'];
    $total_discount += $sale['discount_total'];
}

function e($str) {
    return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Sales - Digital POS</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>
        body, html {
            margin: 0;
            padding: 0;
            height: 100%;
            font-family: 'Segoe UI', sans-serif;
            background: url('../../images/profile.jpg') no-repeat center center fixed;
            background-size: cover;
        }

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

        h1, h2 {
            color: white;
            margin-bottom: 20px;
            font-size: 26px;
        }

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

        .card {
            background: rgba(255,255,255,0.15);
            backdrop-filter: blur(10px);
            border-radius: 12px;
            padding: 20px;
            flex: 1;
            min-width: 220px;
            color: white;
            box-shadow: 0 8px 16px rgba(0,0,0,0.25);
        }

        .card h3 {
            margin: 0;
            color: #ff6f00;
            font-weight: 700;
            font-size: 16px;
        }

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

        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(255,255,255,0.15);
            backdrop-filter: blur(10px);
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 8px 16px rgba(0,0,0,0.25);
            color: white;
        }

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

        th {
            background: rgba(255, 255, 255, 0.2);
            color: #ff6f00;
            font-weight: 600;
        }

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

        .btn {
            padding: 8px 16px;
            background: #ff6f00;
            border-radius: 8px;
            border: none;
            color: #fff;
            font-weight: bold;
            text-decoration: none;
            box-shadow: 0 4px 8px rgba(255,111,0,0.6);
            transition: background-color 0.3s ease;
        }

        .btn:hover {
            background: #e65c00;
            box-shadow: 0 6px 12px rgba(230,92,0,0.8);
        }

        p {
            color: white;
            text-align: center;
            font-size: 16px;
        }

        @media (max-width: 768px) {
            .content {
                margin-left: 0;
                margin-top: 20px;
                border-radius: 0;
                padding: 15px;
            }
            .summary-cards {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>

<div class="content" data-aos="fade-up" data-aos-delay="100">
    <h1>My Sales (<?= date('d M Y') ?>)</h1>

    <div class="summary-cards">
        <div class="card">
            <h3>Total Sales</h3>
            <p>MWK <?= number_format($total_sales, 2) ?></p>
        </div>
        <div class="card">
            <h3>Total Invoices</h3>
            <p><?= $total_invoices ?></p>
        </div>
        <div class="card">
            <h3>Total VAT</h3>
            <p>MWK <?= number_format($total_vat, 2) ?></p>
        </div>
        <div class="card">
            <h3>Total Discounts</h3>
            <p>MWK <?= number_format($total_discount, 2) ?></p>
        </div>
    </div>

    <?php if ($total_invoices > 0): ?>
        <h2>Recent Sales</h2>
        <table>
            <thead>
                <tr>
                    <th>Receipt #</th>
                    <th>Store</th>
                    <th>Amount (MWK)</th>
                    <th>Payment</th>
                    <th>Time</th>
                    <th>Receipt</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sales as $sale): ?>
                    <tr>
                        <td><?= e($sale['receipt_number']) ?></td>
                        <td><?= e($sale['store_name']) ?></td>
                        <td><?= number_format($sale['total_amount'], 2) ?></td>
                        <td><?= ucfirst(e($sale['payment_method'])) ?></td>
                        <td><?= date('H:i', strtotime($sale['created_at'])) ?></td>
                        <td>
                            <a href="print_receipt.php?sale_id=<?= $sale['id'] ?>" target="_blank" class="btn">View</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php else: ?>
        <p>No sales recorded today.</p>
    <?php endif; ?>
</div>

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

</body>
</html>
