<?php
include_once "../config/database.php";

$invoice = mysqli_real_escape_string($conn, $_GET['invoice']);

$query = mysqli_query($conn, "SELECT * FROM sales WHERE invoice_no='$invoice'");
$sale = mysqli_fetch_assoc($query);

if(!$sale){
    die("Invoice not found");
}

$items = mysqli_query($conn, "
    SELECT sale_items.*, products.product_name 
    FROM sale_items 
    JOIN products ON sale_items.product_id = products.id
    WHERE sale_items.sale_id='".$sale['id']."'
");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Print Receipt</title>

    <style>
        body{
            font-family: 'Segoe UI', sans-serif;
            background:#f4f6f9;
            padding:20px;
        }

        .receipt{
            max-width:340px;
            margin:auto;
            background:#fff;
            padding:20px;
            border-radius:12px;
            box-shadow:0 5px 20px rgba(0,0,0,0.08);
        }

        .header{
            text-align:center;
            border-bottom:2px dashed #ddd;
            padding-bottom:10px;
            margin-bottom:10px;
        }

        .header h2{
            margin:0;
            color:#6f42c1;
            font-weight:700;
        }

        .info{
            font-size:14px;
            margin-bottom:10px;
        }

        .info p{
            margin:3px 0;
        }

        table{
            width:100%;
            font-size:14px;
            margin-top:10px;
        }

        th, td{
            padding:6px 0;
        }

        th{
            text-align:left;
            border-bottom:1px solid #ddd;
        }

        .text-end{
            text-align:right;
        }

        .total-box{
            margin-top:10px;
            padding-top:10px;
            border-top:2px dashed #ddd;
        }

        .total{
            font-weight:bold;
            font-size:16px;
            color:#198754;
        }

        .payment{
            margin-top:8px;
            font-size:14px;
        }

        .payment span{
            font-weight:600;
            color:#0d6efd;
        }

        .footer{
            text-align:center;
            margin-top:15px;
            border-top:1px dashed #ddd;
            padding-top:10px;
            font-size:13px;
        }

        .thanks{
            font-weight:bold;
            color:#dc3545;
            margin-top:5px;
        }

        @media print{
            body{
                background:#fff;
                padding:0;
            }
            .receipt{
                box-shadow:none;
                border-radius:0;
            }
        }
    </style>
</head>

<body onload="window.print()">

<div class="receipt">

    <div class="header">
        <h2>Beauty POS</h2>
        <small>Professional Billing</small>
    </div>

    <div class="info">
        <p><strong>Invoice:</strong> <?php echo $sale['invoice_no']; ?></p>
        <p><strong>Date:</strong> <?php echo date("d M Y", strtotime($sale['created_at'])); ?></p>
    </div>

    <table>
        <thead>
            <tr>
                <th>Item</th>
                <th class="text-end">Qty</th>
                <th class="text-end">Price</th>
            </tr>
        </thead>
        <tbody>
            <?php while($row = mysqli_fetch_assoc($items)){ ?>
            <tr>
                <td><?php echo $row['product_name']; ?></td>
                <td class="text-end"><?php echo $row['quantity']; ?></td>
                <td class="text-end">₹<?php echo number_format($row['price'],2); ?></td>
            </tr>
            <?php } ?>
        </tbody>
    </table>

    <div class="total-box">
        <p class="total">Total: ₹<?php echo number_format($sale['total'],2); ?></p>
    </div>

    <div class="payment">
        <p><strong>Payment:</strong> <span><?php echo $sale['payment_method']; ?></span></p>

        <?php if($sale['payment_method']=="MTN MOMO"){ ?>
            <p>Number: 673210025</p>
            <p>Name: Mecca enterprise</p>
        <?php } ?>

        <?php if($sale['payment_method']=="ORANGE MONEY"){ ?>
            <p>Number: 695505787</p>
            <p>Name: Kemuh Mecca</p>
        <?php } ?>
    </div>

    <div class="footer">
        <div>Powered by Beauty POS</div>
        <div class="thanks">Thank You! Visit Again 💖</div>
    </div>

</div>

</body>
</html>