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

/* ── ۱) گرفتن و بررسی ورودی تاریخ‌ها ────────────────────────────────────────── */
$from_date = $_GET['from_date'] ?? '';
$to_date   = $_GET['to_date']   ?? '';

if (!strtotime($from_date) || !strtotime($to_date)) {
    die("فرمت تاریخ نادرست است.");
}

/* ── ۲) کوئری آماده با JOINهای لازم ─────────────────────────────────────────── */
$stmt = $con->prepare("
    SELECT 
        student.s_name,
        payments.p_id,
        payments.class_time,
        payments.p_amount,
        payments.p_status,
        payments.start_date,
        payments.end_date,
        course_type.c_name,
        loan.borrow_amount,
        loan.received_amount,
        discount.d_amount
    FROM payments
    INNER JOIN student      ON payments.s_id = student.s_id
    INNER JOIN loan         ON payments.s_id = loan.s_id   AND payments.c_id = loan.c_id
    INNER JOIN course_type  ON payments.c_id = course_type.c_id
    LEFT  JOIN discount     ON payments.s_id = discount.s_id AND payments.c_id = discount.c_id
    WHERE payments.start_date BETWEEN ? AND ?
    ORDER BY payments.start_date ASC
");
$stmt->bind_param('ss', $from_date, $to_date);
$stmt->execute();
$result = $stmt->get_result();

/* ── ۳) آماده‌سازی آرایه داده‌ها و جمع مقادیر ──────────────────────────────── */
$data            = [];
$total_fee       = 0;
$total_discount  = 0;
$total_remaining = 0;

while ($row = $result->fetch_assoc()) {
    $data[]           = $row;
    $total_fee       += $row['received_amount']       ?? 0;
    $total_remaining += $row['borrow_amount']  ?? 0;
}
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>گزارش پرداخت محصلان</title>
    <link rel="shortcut icon" type="image/png" href="images/favicon.png">
    <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:#2196F3;color:#fff;border:none;padding:10px 20px;margin:5px;font-size:16px;border-radius:6px;cursor:pointer}
        .action-buttons button:hover{background:#1976D2}
        h2{color:#2196F3;border-right:5px solid #2196F3;padding-right:10px}
        table{width:100%;border-collapse:collapse;background:#fff;margin-top:20px}
        th,td{border:1px solid #ddd;padding:10px;text-align:center}
        th{background:#2196F3;color:#fff}
        tr:nth-child(even){background:#f1f1f1}
        .summary{font-size:18px;margin-top:30px}
        @media print{.action-buttons{display:none}}
    </style>
    <script>
        document.addEventListener("keydown",e=>{
            if(e.key==="Escape") setTimeout(()=>history.back(),100);
        });
    </script>
</head>
<body>

<header>
    <img src="images/logo1.png" alt="Logo">
    <h1>گزارش پرداخت محصلان</h1>
</header>

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

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

<table>
    <thead>
        <tr>
            <th style="font-size:20px;">نمبر</th>
            <th style="font-size:20px;">اسم محصل</th>
            <th style="font-size:20px;">صنف</th>
            <th style="font-size:20px;">تایم</th>
            <th style="font-size:20px;">فیس</th>
            <th style="font-size:20px;">تخفیف</th>
            <th style="font-size:20px;">تاریخ شروع</th>
            <th style="font-size:20px;">تاریخ ختم</th>
            <th style="font-size:20px;">مبلغ باقی</th>
            <th style="font-size:20px;">مبلغ رسیدات</th>
            <th style="font-size:20px;">حالت</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($data as $index=>$row): ?>
        <tr>
            <td><?= $index+1 ?></td>
            <td><?= htmlspecialchars($row['s_name']) ?></td>
            <td><?= htmlspecialchars($row['c_name']) ?></td>
            <td><?= htmlspecialchars($row['class_time']) ?></td>
            <td><?= number_format($row['p_amount'] ?? 0, 2) ?></td>
            <td><?= number_format($row['d_amount'] ?? 0, 2) ?></td>
            <td><?= htmlspecialchars($row['start_date']) ?></td>
            <td><?= htmlspecialchars($row['end_date']) ?></td>
            <td><?= number_format($row['borrow_amount'] ?? 0, 2) ?></td>
            <td><?= number_format($row['received_amount'] ?? 0, 2) ?></td>
            <td><?= htmlspecialchars($row['p_status']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<div class="summary">
    <p><strong>جمع فیس‌ها:</strong> <?= number_format($total_fee, 2) ?></p>
    
    <p><strong>جمع مبالغ باقی:</strong> <?= number_format($total_remaining, 2) ?></p>
</div>

</body>
</html>
