<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$start_date = isset($_GET['start']) ? $_GET['start'] : date('Y-01-01');
$end_date = isset($_GET['end']) ? $_GET['end'] : date('Y-12-31');

// Tax Analysis
$sql = "SELECT 
            DATE_FORMAT(invoice_date, '%Y-%m') as month_grp,
            COUNT(*) as inv_count,
            SUM(total_amount) as total_revenue,
            SUM(tax_amount) as total_tax
        FROM invoices 
        WHERE invoice_date BETWEEN ? AND ? 
        GROUP BY month_grp
        ORDER BY month_grp ASC";
// Assuming `tax_amount` column exists. If not, I'll calculate it assuming included in total if needed, but schema usually has it.
// I'll stick to `tax_amount` as standard. If it errors, I'll fix.

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Tax Reports')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Tax Liability Reports</h4>
                                <div class="page-title-right">
                                    <form method="GET" class="d-flex gap-2">
                                        <input type="date" name="start" class="form-control form-control-sm"
                                            value="<?php echo $start_date; ?>">
                                        <input type="date" name="end" class="form-control form-control-sm"
                                            value="<?php echo $end_date; ?>">
                                        <button type="submit" class="btn btn-primary btn-sm">Update</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="taxTable" class="table table-bordered dt-responsive nowrap">
                                        <thead>
                                            <tr>
                                                <th>Period</th>
                                                <th>Invoices</th>
                                                <th>Taxable Revenue</th>
                                                <th>Tax Collected (VAT/Levy)</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $tot_rev = 0;
                                            $tot_tax = 0;
                                            while ($row = $result->fetch_assoc()):
                                                $tot_rev += $row['total_revenue'];
                                                $tot_tax += $row['total_tax'];
                                                $monthName = date('F Y', strtotime($row['month_grp'] . '-01'));
                                                ?>
                                                <tr>
                                                    <td><?php echo $monthName; ?></td>
                                                    <td><?php echo number_format($row['inv_count']); ?></td>
                                                    <td><?php echo number_format($row['total_revenue'] - $row['total_tax'], 2); ?>
                                                    </td>
                                                    <td><?php echo number_format($row['total_tax'], 2); ?></td>
                                                    <td><span class="badge bg-success">Recorded</span></td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                        <tfoot>
                                            <tr class="table-light fw-bold">
                                                <td>TOTAL</td>
                                                <td>-</td>
                                                <td><?php echo number_format($tot_rev - $tot_tax, 2); ?></td>
                                                <td><?php echo number_format($tot_tax, 2); ?></td>
                                                <td></td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#taxTable').DataTable({ dom: 'Bfrtip', buttons: ['excel', 'pdf', 'print'] });</script>
</body>

</html>