<?php
include("../db/cn.php");

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$creditor = null;

if ($id > 0) {
    $query = "SELECT cr_name, amount, paid_by, date_time 
              FROM credit_paid 
              WHERE id = $id LIMIT 1";
    $result = mysqli_query($connection, $query);

    if ($result && mysqli_num_rows($result) > 0) {
        $creditor = mysqli_fetch_assoc($result);
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Creditor Receiving Print</title>
  <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

  <style>
    body {
      font-family: 'Quicksand', sans-serif;
      margin: 20px;
    }
    @media print {
      .no-print {
        display: none !important;
      }
    }
  </style>
</head>
<body>
<div class="container mt-4">
    <div class="text-center mb-4">
        <h2>Creditor Receiving</h2>
        <h5>Receipt</h5>
    </div>

    <?php if ($creditor): ?>
    <table class="table table-bordered text-center text-capitalize font-weight-bold">
        <thead class="table-dark">
            <tr>
                <th>Creditor Name</th>
                <th>Amount</th>
                <th>Paid By</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><?= htmlspecialchars($creditor['cr_name']); ?></td>
                <td><?= number_format($creditor['amount'], 2); ?></td>
                <td><?= htmlspecialchars($creditor['paid_by']); ?></td>
                <td><?= htmlspecialchars($creditor['date_time']); ?></td>
            </tr>
        </tbody>
    </table>

    <div class="text-center no-print mt-4">
        <button class="btn btn-primary" onclick="window.print()">Print</button>
        <a href="view_creditor.php" class="btn btn-secondary">Back</a>
    </div>
    <?php else: ?>
        <div class="alert alert-danger">No record found.</div>
        <a href="view_creditor.php" class="btn btn-secondary">Back</a>
    <?php endif; ?>
</div>
</body>
</html>
