<?php
include('dbconnection.php');

// Get and sanitize inputs
$from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
$to_date = isset($_GET['to_date']) ? $_GET['to_date'] : '';

if (!strtotime($from_date) || !strtotime($to_date)) {
    die("Invalid date format.");
}

$stmt = $con->prepare("
    SELECT 
        cash_point.cp_id,
        cash_point.date,
        cash_point.deposit_amount,
        course_type.c_name,
        cash_point.bill_no,
        employee.emp_name
    FROM cash_point
    INNER JOIN course_type ON cash_point.details = course_type.c_id
    INNER JOIN employee ON cash_point.emp_id = employee.emp_id
    WHERE cash_point.date BETWEEN ? AND ?
    ORDER BY cash_point.date ASC
");

$stmt->bind_param('ss', $from_date, $to_date);
$stmt->execute();
$result = $stmt->get_result();


$data = [];
$total_withdraw = 0;
$total_deposit = 0;

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
    $total_deposit += $row['deposit_amount'] ?? 0;
}
?>

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" type="image/png" href="images/favicon.png">
    <title>گزارش پول صیف</title>
    <script>
        document.addEventListener("keydown", function(event) {
            if (event.key === "Escape") {
                setTimeout(() => {
                    window.history.back();
                }, 100);
            }
        });
    </script>
    <style>
        body {
            font-family: 'Tahoma', sans-serif;
            background: #f5f7fa;
            margin: 30px;
            color: #333;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #2196F3;
            padding-bottom: 10px;
        }

        header img {
            width: 60px;
            vertical-align: middle;
        }

        header h1 {
            display: inline-block;
            margin: 0;
            color: #2196F3;
            font-size: 26px;
        }

        .action-buttons {
            text-align: center;
            margin-bottom: 20px;
        }

        .action-buttons button {
            background-color: #2196F3;
            color: white;
            border: none;
            padding: 10px 20px;
            margin: 5px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
        }

        .action-buttons button:hover {
            background-color: #1976D2;
        }

        h2 {
            color: #2196F3;
            border-right: 5px solid #2196F3;
            padding-right: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
            margin-top: 20px;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }

        th {
            background-color: #2196F3;
            color: white;
        }

        tr:nth-child(even) {
            background-color: #f1f1f1;
        }

        .summary {
            font-size: 18px;
            margin-top: 30px;
        }

        @media print {
            .action-buttons {
                display: none;
            }
        }
    </style>
</head>
<body>

<header>
    <img src="images/logo1.png" alt="Logo">
    <h1>گزارش پول صیف</h1>
</header>

<div class="action-buttons">
    <button onclick="window.print()">🖨️ پرنت</button>
    <button onclick="window.location.href='index.php'">🏠 صفحه اصلی</button>
</div>

<h2>از تاریخ: <?= htmlspecialchars($from_date) ?> الی <?= htmlspecialchars($to_date) ?></h2>

<table>
    <thead>
        <tr>
            
            <th>واریز پول</th>
            <th>جزئیات</th>
            <th>بل نمبر</th>
            <th>تاریخ</th>
          
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data as $row): ?>
        <tr>
            
            <td><?= number_format($row['deposit_amount'] ?? 0, 2) ?></td>
            <td>صنف <?= htmlspecialchars($row['c_name']) ?></td>
            <td><?= number_format($row['bill_no']) ?></td>
            <td><?= $row['date'] ?></td>
          
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<div class="summary" dir="rtl">
    <p><strong>مجموع واریز:</strong> <?= number_format($total_deposit, 2) ?></p>
</div>

</body>
</html>
