<?php
require_once './database/shikanisha.kts.php';

session_start();
if (!isset($_SESSION['employee_id'])) {
    die('Not authorized');
}

$shift_id = isset($_GET['shift_id']) ? intval($_GET['shift_id']) : 0;

// Get shift info
$stmt = $conn->prepare("
    SELECT s.*, e.name as employee_name 
    FROM shifts s
    JOIN employees e ON s.employee_id = e.employee_id
    WHERE s.shift_id = ?
");
$stmt->bind_param("i", $shift_id);
$stmt->execute();
$shift = $stmt->get_result()->fetch_assoc();

if (!$shift) {
    die('Shift not found');
}

// Get payment methods summary
$stmt = $conn->prepare("
    SELECT payment_method, SUM(total_amount) as total_amount, SUM(transaction_count) as transaction_count
    FROM shift_summaries
    WHERE shift_id = ?
    GROUP BY payment_method
");
$stmt->bind_param("i", $shift_id);
$stmt->execute();
$payment_methods = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

// Calculate totals
$totals = [
    'total_amount' => 0,
    'transaction_count' => 0
];

foreach ($payment_methods as $method) {
    $totals['total_amount'] += $method['total_amount'];
    $totals['transaction_count'] += $method['transaction_count'];
}

// Get transactions
$stmt = $conn->prepare("
    SELECT tr.trans_id, tr.seller_name, tr.seller_date, tr.total, p.payment_method, p.amount
    FROM transaction_receipts tr
    JOIN payments p ON tr.trans_id = p.trans_id
    WHERE tr.seller_date >= ? AND tr.seller_date <= ? AND tr.seller_id = ?
    ORDER BY tr.seller_date DESC
");
$stmt->bind_param("ssi", $shift['start_time'], $shift['end_time'], $shift['employee_id']);
$stmt->execute();
$transactions = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shift Report #<?= $shift_id ?></title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        h1, h2 { color: #333; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .payment-method { margin-bottom: 15px; padding: 10px; background-color: #f9f9f9; }
        .totals { font-weight: bold; margin-top: 20px; }
    </style>
</head>
<body>
    <h1>Shift Report #<?= $shift_id ?></h1>
    <p><strong>Employee:</strong> <?= $shift['employee_name'] ?></p>
    <p><strong>Start Time:</strong> <?= $shift['start_time'] ?></p>
    <p><strong>End Time:</strong> <?= $shift['end_time'] ?></p>
    <p><strong>Starting Cash:</strong> $<?= number_format($shift['starting_cash'], 2) ?></p>
    <p><strong>Ending Cash:</strong> $<?= number_format($shift['ending_cash'], 2) ?></p>
    
    <h2>Payment Methods</h2>
    <?php foreach ($payment_methods as $method): ?>
        <div class="payment-method">
            <p><strong><?= $method['payment_method'] ?>:</strong> 
            $<?= number_format($method['total_amount'], 2) ?> (<?= $method['transaction_count'] ?> transactions)</p>
        </div>
    <?php endforeach; ?>
    
    <div class="totals">
        <p>Total Amount: $<?= number_format($totals['total_amount'], 2) ?></p>
        <p>Total Transactions: <?= $totals['transaction_count'] ?></p>
    </div>
    
    <h2>Transactions</h2>
    <table>
        <thead>
            <tr>
                <th>Transaction ID</th>
                <th>Seller</th>
                <th>Date</th>
                <th>Total</th>
                <th>Payment Method</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($transactions as $trans): ?>
                <tr>
                    <td><?= $trans['trans_id'] ?></td>
                    <td><?= $trans['seller_name'] ?></td>
                    <td><?= $trans['seller_date'] ?></td>
                    <td>$<?= number_format($trans['total'], 2) ?></td>
                    <td><?= $trans['payment_method'] ?></td>
                    <td>$<?= number_format($trans['amount'], 2) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    
    <script>
        window.onload = function() {
            window.print();
        }
    </script>
</body>
</html>