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

include('../../includes/config.php');
include('../../includes/header.php');
include('../../includes/footer.php');

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

if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date_from)) $date_from = date('Y-m-01');
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date_to)) $date_to = date('Y-m-d');

// Fetch invoices joined with cashier and store info
$stmt = $conn->prepare("
    SELECT st.id AS transaction_id, st.created_at, st.payment_method, st.total_amount, st.vat_total,
           u.first_name, u.last_name,
           s.store_name
    FROM sales_transactions st
    JOIN users u ON st.sold_by = u.id
    JOIN stores s ON st.store_id = s.id
    WHERE DATE(st.created_at) BETWEEN ? AND ?
    ORDER BY st.created_at DESC
");
$stmt->bind_param("ss", $date_from, $date_to);
$stmt->execute();
$invoices = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

$page_title = "Accountant Invoices";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title><?= htmlspecialchars($page_title) ?></title>
    <link rel="icon" type="image/png" href="../../images/logo.png" />
    <style>
        body {
            margin: 0;
            /* Reserve 80px space for a fixed header */
            padding-top: 80px;
            background: url('../../images/profile.jpg') no-repeat center center fixed;
            background-size: cover;
            font-family: 'Segoe UI', sans-serif;
            color: #eee;
        }
        .content {
            margin: 2rem auto 0 auto;
            padding: 40px;
            max-width: 1850px;
            background: rgba(255,255,255,0.1);
            border-radius: 16px;
            backdrop-filter: blur(10px);
            box-shadow: 0 4px 15px rgba(255,165,0,0.3);
        }
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1.5rem;
        }
        h1.page-title {
            color: #ffa500;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 700;
            font-size: 2rem;
        }
        h1.page-title img {
            height: 40px;
            width: auto;
        }
        a.back-link {
            color: #ffa500;
            text-decoration: none;
            font-weight: 600;
            border: 1.5px solid #ffa500;
            padding: 6px 14px;
            border-radius: 8px;
            transition: background-color 0.3s ease, color 0.3s ease;
            white-space: nowrap;
        }
        a.back-link:hover {
            background-color: #ffa500;
            color: #222;
        }
        form.filters {
            margin-bottom: 30px;
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            justify-content: center;
        }
        form.filters input,
        form.filters button {
            padding: 10px 15px;
            border-radius: 8px;
            border: none;
            font-weight: 600;
            background: rgba(255,255,255,0.15);
            color: #fff;
            min-width: 140px;
            text-align: center;
        }
        form.filters button {
            background: #ffa500;
            color: #222;
            cursor: pointer;
            min-width: auto;
            padding-left: 20px;
            padding-right: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
            background: rgba(255,255,255,0.05);
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(255,165,0,0.25);
        }
        thead tr {
            background: rgba(255,165,0,0.25);
            color: #fff;
        }
        th, td {
            padding: 12px 15px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            text-align: center;
        }
        a.invoice-link {
            color: #4dd0e1;
            text-decoration: underline;
        }
        a.invoice-link:hover {
            color: #81d4fa;
        }
        @media (max-width: 600px) {
            form.filters {
                flex-direction: column;
                gap: 10px;
            }
            form.filters input, form.filters button {
                min-width: 100%;
            }
        }
    </style>
</head>
<body>
<div class="content">
    <div class="page-header">
        <h1 class="page-title">
            <img src="../../images/logo.png" alt="Logo" />
            <?= htmlspecialchars($page_title) ?>
        </h1>
        <a href="reports_dashboard.php" class="back-link">&laquo; Back to Reports Dashboard</a>
    </div>

    <form method="GET" class="filters" novalidate>
        <input type="date" name="date_from" value="<?= htmlspecialchars($date_from) ?>" required>
        <input type="date" name="date_to" value="<?= htmlspecialchars($date_to) ?>" required>
        <button type="submit">Filter</button>
    </form>

    <table>
        <thead>
            <tr>
                <th>Date</th>
                <th>Invoice ID</th>
                <th>Cashier</th>
                <th>Store</th>
                <th>Payment Method</th>
                <th>Total</th>
                <th>VAT</th>
            </tr>
        </thead>
        <tbody>
        <?php if (empty($invoices)): ?>
            <tr><td colspan="7">No invoices found.</td></tr>
        <?php else: ?>
            <?php foreach ($invoices as $row): ?>
                <tr>
                    <td><?= date('Y-m-d', strtotime($row['created_at'])) ?></td>
                    <td>
                        <a class="invoice-link" href="receipt_view.php?id=<?= $row['transaction_id'] ?>" target="_blank">
                            #<?= $row['transaction_id'] ?>
                        </a>
                    </td>
                    <td><?= htmlspecialchars($row['first_name'] . ' ' . $row['last_name']) ?></td>
                    <td><?= htmlspecialchars($row['store_name']) ?></td>
                    <td><?= htmlspecialchars(ucfirst($row['payment_method'] ?? 'N/A')) ?></td>
                    <td><?= number_format($row['total_amount'], 2) ?> MWK</td>
                    <td><?= number_format($row['vat_total'], 2) ?> MWK</td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
    </table>
</div>
</body>
</html>
