<?php

session_start();

require_once __DIR__ . "/config/database.php";

date_default_timezone_set("Africa/Kigali");

/*
|--------------------------------------------------------------------------
| DATABASE
|--------------------------------------------------------------------------
*/

$db = new Database();
$pdo = $db->connect();

/*
|--------------------------------------------------------------------------
| CHECK SESSION
|--------------------------------------------------------------------------
*/

if (
    !isset($_SESSION["print_job"]) ||
    !isset($_SESSION["print_job"]["order_id"])
) {

    header("Location:index.php");
    exit;

}

$orderId = (int)$_SESSION["print_job"]["order_id"];
$printerToken = $_SESSION["printer_qr"]["printer_token"] ?? "";

/*
|--------------------------------------------------------------------------
| LOAD ORDER
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT *
    FROM print_orders
    WHERE id=?
    LIMIT 1
");

$stmt->execute([$orderId]);

$order = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$order) {

    die("Order not found.");

}

/*
|--------------------------------------------------------------------------
| PAYMENT MUST BE SUCCESSFUL
|--------------------------------------------------------------------------
*/

if ($order["payment_status"] !== "paid") {

    die("Payment has not been completed.");

}

/*
|--------------------------------------------------------------------------
| LOAD PAYMENT
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT *
    FROM payments
    WHERE order_id=?
    ORDER BY id DESC
    LIMIT 1
");

$stmt->execute([$orderId]);

$payment = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$payment) {

    die("Payment record not found.");

}

/*
|--------------------------------------------------------------------------
| LOAD PRINTER
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT *
    FROM printers
    WHERE id=?
    LIMIT 1
");

$stmt->execute([
    $order["printer_id"]
]);

$printer = $stmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| RECEIPT NUMBER
|--------------------------------------------------------------------------
*/

$receiptNumber =
    "SPR-" .
    date("Ymd") .
    "-" .
    str_pad(
        $orderId,
        6,
        "0",
        STR_PAD_LEFT
    );

/*
|--------------------------------------------------------------------------
| COMPANY
|--------------------------------------------------------------------------
*/

$company = "SmartPrint";
?>
<!DOCTYPE html>
<html>
<head>

<title>SmartPrint Receipt</title>

<style>

body{
    font-family: Arial, sans-serif;
    background:#f2f2f2;
    padding:30px;
}


.receipt{

    width:420px;
    margin:auto;
    background:white;
    padding:25px;
    border-radius:10px;
    box-shadow:0 0 10px #ccc;

}


.header{

    text-align:center;

}


.header h2{

    margin:0;
    font-size:28px;

}


.line{

    border-top:1px dashed #777;
    margin:15px 0;

}


.row{

    display:flex;
    justify-content:space-between;
    margin:8px 0;

}


.label{

    font-weight:bold;

}


.amount{

    font-size:22px;
    font-weight:bold;
    text-align:center;
}


.btn{

    display:block;
    margin-top:20px;
    text-align:center;

}


.btn a{

    background:#0066ff;
    color:white;
    padding:12px 20px;
    text-decoration:none;
    border-radius:5px;

}


.footer{

    text-align:center;
    margin-top:20px;
    font-size:13px;
    color:#666;

}
.done-btn{

    width:100%;

    padding:15px 25px;

    border:none;

    border-radius:14px;

    background:linear-gradient(
        135deg,
        #00c853,
        #00a844
    );

    color:white;

    font-size:18px;

    font-weight:700;

    cursor:pointer;

    display:flex;

    justify-content:center;

    align-items:center;

    gap:10px;

    box-shadow:
    0 10px 25px rgba(0,200,83,.35);

    transition:.3s ease;

}


.done-btn i{

    font-size:22px;

}


.done-btn:hover{

    transform:translateY(-3px);

    background:linear-gradient(
        135deg,
        #00e676,
        #00b248
    );

    box-shadow:
    0 15px 35px rgba(0,200,83,.5);

}


.done-btn:active{

    transform:scale(.97);

}


</style>

</head>


<body>


<div class="receipt">


<div class="header">

<h2>
<?=htmlspecialchars($company)?>
</h2>

<p>
Commercial Self-Service Printing
</p>

</div>


<div class="line"></div>


<div class="row">
<span class="label">Receipt No</span>
<span>
<?=$receiptNumber?>
</span>
</div>


<div class="row">
<span class="label">Order No</span>
<span>
<?=$order["order_number"]?>
</span>
</div>


<div class="row">
<span class="label">Date</span>
<span>
<?=date("Y-m-d H:i:s")?>
</span>
</div>


<div class="row">
<span class="label">Printer</span>
<span>
<?=$printer["name"] ?? "N/A"?>
</span>
</div>



<div class="line"></div>


<h4>DOCUMENT DETAILS</h4>


<div class="row">

<span class="label">
File
</span>

<span>
<?=$order["file_name"]?>
</span>

</div>



<div class="row">

<span class="label">
Pages
</span>

<span>
<?=$order["pages"]?>
</span>

</div>



<div class="row">

<span class="label">
Copies
</span>

<span>
<?=$order["copies"]?>
</span>

</div>



<div class="row">

<span class="label">
Paper Size
</span>

<span>
<?=$order["paper_size"]?>
</span>

</div>



<div class="row">

<span class="label">
Print Type
</span>

<span>
<?=$order["print_type"]?>
</span>

</div>



<div class="row">

<span class="label">
Duplex
</span>

<span>
<?=$order["duplex"]?>
</span>

</div>



<div class="line"></div>


<h4>PAYMENT DETAILS</h4>



<div class="row">

<span class="label">
Amount Paid
</span>

<span>

<?=$order["price"]?>

RWF

</span>

</div>



<div class="row">

<span class="label">
Phone
</span>

<span>
<?=$payment["phone"] ?? "N/A"?>
</span>

</div>



<div class="row">

<span class="label">
Transaction ID
</span>

<span>

<?=$payment["transaction_id"] ?? "N/A"?>

</span>

</div>



<div class="row">

<span class="label">
Payment Status
</span>

<span>

<?=$order["payment_status"]?>

</span>

</div>



<div class="line"></div>


<div class="footer">

<p>
Thank you for using SmartPrint
</p>

<p>
Your document has been sent to printer.
</p>

</div>



<div class="btn">

<a href="receipt_pdf.php">
Download PDF Receipt
</a>

</div>


<div class="btn">

<button
class="done-btn"
onclick="window.location='upload.php?printer=<?= urlencode($printerToken) ?>'">

<i class="bi bi-printer-fill"></i>

Start New Print

</button>

</div>



</div>


</body>

</html>