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

/* ── ۱) گرفتن پارامترهای ورودی ───────────────────────────────────────────── */
$from_date   = $_GET['from_date']   ?? '';
$to_date     = $_GET['to_date']     ?? '';
$c_id        = $_GET['c_id']        ?? '';
$student_id  = $_GET['student_id']  ?? '';

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

/* ── ۲) ساخت کوئری داینامیک بر اساس پارامترها ────────────────────────────── */
$query = "
    SELECT 
        loan_r_details.loan_id,
        course_type.c_name,
        loan_r_details.p_received_amount,
        loan_r_details.p_borrow_amount,
        loan_r_details.bill_no,
        loan_r_details.c_id,
        loan_r_details.s_id,
        loan_r_details.p_date
    FROM loan_r_details
    INNER JOIN student     ON loan_r_details.s_id = student.s_id
    INNER JOIN course_type ON loan_r_details.c_id = course_type.c_id
    WHERE loan_r_details.p_date BETWEEN ? AND ?
";

$params = [$from_date, $to_date];
$types  = 'ss';

if (!empty($c_id)) {
    $query .= " AND loan_r_details.c_id = ?";
    $params[] = $c_id;
    $types   .= 'i';
}
if (!empty($student_id)) {
    $query .= " AND loan_r_details.s_id = ?";
    $params[] = $student_id;
    $types   .= 'i';
}

$query .= " ORDER BY loan_r_details.loan_id ASC";

$stmt = $con->prepare($query);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();

/* ── ۳) جمع‌بندی داده‌ها ───────────────────────────────────────────────────── */
$data             = [];
$total_received   = 0;
$total_borrowed   = 0;

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
    $total_received += $row['p_received_amount'] ?? 0;
    $total_borrowed += $row['p_borrow_amount']   ?? 0;
}
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>گزارش پرداخت </title>
    <link rel="shortcut icon" 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>گزارش پرداخت ها :
        <?php
                                          
                                         $q4="SELECT s_id,s_name FROM student WHERE s_id='$student_id'";
                                         $r1=$con->query($q4);
                            while($row=$r1->fetch_array(MYSQLI_ASSOC))
                            {
                              echo $row["s_name"]; 
                          
                            }
                            ?>
    </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>نمبر </th>
            <th>صنف</th>
            <th>مبلغ رسید</th>
            <th>مبلغ باقی</th>
            <th>بل نمبر</th>
            <th>تاریخ پرداخت</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($data as $row): ?>
        <tr>
            <td><?= $row['loan_id'] ?></td>
            <td><?= htmlspecialchars($row['c_name']) ?></td>
            <td><?= number_format($row['p_received_amount'] ?? 0,2) ?></td>
            <td><?= number_format($row['p_borrow_amount'] ?? 0,2) ?></td>
            <td><?= htmlspecialchars($row['bill_no']) ?></td>
            <td><?= htmlspecialchars($row['p_date']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<div class="summary">
    <p><strong>جمع مبلغ رسید:</strong> <?= number_format($total_received, 2) ?></p>
    <p><strong>جمع مبلغ قرضه:</strong> <?= number_format($total_borrowed, 2) ?></p>
</div>

</body>
</html>
