<?php include '../includes/config.php'; ?>
<?php
session_start();

if (!$_SESSION['email']) {
    header('location:login.php');
}
?>
<?php
include '../includes/config.php';

// get the latest invoice number
$stmt_invoice = $conn->prepare("SELECT MAX(invoice) AS max_invoice FROM manual_stock_sale;");
$stmt_invoice->execute();
$row_invoice = $stmt_invoice->fetch(PDO::FETCH_ASSOC);
$invoice_number = $row_invoice['max_invoice'];

// get the customer details
$stmt_customer = $conn->prepare("SELECT contacts.name, manual_stock_sale.date_sale, manual_stock_sale.saleman FROM contacts
INNER JOIN manual_stock_sale ON contacts.id = manual_stock_sale.customer_id
WHERE manual_stock_sale.invoice = :invoice_number;");
$stmt_customer->bindParam(':invoice_number', $invoice_number);
$stmt_customer->execute();
$row_customer = $stmt_customer->fetch(PDO::FETCH_ASSOC);

// get the products in the bill
$stmt_products = $conn->prepare("SELECT * FROM manual_stock_sale
WHERE manual_stock_sale.invoice = :invoice_number;");
$stmt_products->bindParam(':invoice_number', $invoice_number);
$stmt_products->execute();

?>

<!DOCTYPE html>
<html>

<head>
    <title>Sales Receipt</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Crimson+Text&display=swap" rel="stylesheet">
    <style>
        /* General Styles */
        body {
            font-family: Consolas, Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 0;
            margin: 0;
        }

        .container {
            max-width: 250px;
            /* Adjust as needed for your thermal printer's width */
            margin: 0 auto;
            padding: 7px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
            border: 1px solid #ccc;
            border-radius: 5px;
            overflow: hidden;
        }

        h1,
        h2,
        h3 {
            text-align: center;
            color: #333;
            background-color: #f2f2f2;
            padding: 3px;
            margin: 0;
            border-bottom: 1px solid #ccc;
            font-weight: bold;
        }

        h4 {
            padding: 0px;
        }

        p {
            text-align: left;
            color: #333;
            background-color: #f2f2f2;
            padding: 3px;
            margin: 0;
            border-bottom: 1px solid #ccc;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
            font-size: 12px;
        }

        table td,
        table th {
            padding: 5px;
            border: none;
            font-weight: bold;
            border-bottom: 1px solid #ccc;
            text-align: left;
            font-size: 14px;
        }

        table th {
            font-weight: bold;
            background-color: #f2f2f2;
            color: #333;
        }

        .total {
            text-align: right;
            font-weight: bold;
            margin-top: 6px;
            margin-bottom: 0.3em;
            font-size: 14px;
            margin-right: 1em;
        }

        .logo {
            width: auto;
        }

        .logo>img {
            width: 100%;
            object-fit: cover;
        }

        /* Print Styles */
        @media print {

            /* General Styles */
            body {
                font-family: Consolas, Arial, sans-serif;
                background-color: #f5f5f5;
                padding: 0;
                margin: 0;
            }

            .container {
                max-width: 250px;
                /* Adjust as needed for your thermal printer's width */
                margin: 0;
                padding: 3px;
                background-color: #fff;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
                border: 1px solid #ccc;
                border-radius: 5px;
                overflow: hidden;
            }

            h1,
            h2,
            h3 {
                text-align: center;
                color: #333;
                background-color: #f2f2f2;
                padding: 2px;
                margin: 0;
                border-bottom: 1px solid #ccc;
                font-weight: bold;
            }

            h4 {
                padding: 0px;
            }

            p {
                text-align: left;
                color: #333;
                background-color: #f2f2f2;
                padding: 3px;
                margin: 0;
                border-bottom: 1px solid #ccc;
            }

            table {
                width: 100%;
                border-collapse: collapse;
            }

            table td,
            table th {
                padding: 5px;
                border: 1px solid #ccc;
                /* Ensure borders are visible in print */
                font-size: 12px;
                /* Adjust font size for print readability */
            }

            table th {
                background-color: #f2f2f2;
                color: #333;
            }

            .total {
                text-align: right;
                font-weight: bold;
                margin-top: 6px;
                margin-bottom: 0.3em;
                font-size: 14px;
                margin-right: 1em;
            }

            .logo {
                width: auto;
            }

            .image_logo {
                position: absolute; 
            left: 0; 
            width: 60px; 
            top: 50%; 
            padding-left: 5px;
            transform: translateY(-50%);
            }
        }
        @media print {
  #back-link {
    display: none;
  }
}
    </style>
</head>

<body>
<a href="../add_sale.php" id="back-link">Back Sale</a>

    <div class="container" id="data">
        <!-- <div class="logo">
        <img src="invoice_logo.jpg">
        </div> -->
        <h3 style="
        font-family: Consolas, Arial, sans-serif; 
        font-size: 19px; 
        display: flex; 
        align-items: center; 
        justify-content: center; 
        text-align: center; 
        margin: 0; 
        position: relative; 
        padding-left: 20px; /* Space for the logo */
    ">
        <img class="image_logo" src="invoice_logo-removebg-preview.png" style="
            position: absolute; 
            left: 0; 
            width: 60px; 
            top: 50%; 
            padding-left: 5px;
            transform: translateY(-50%);
        " alt="Logo">
        <div style="
            display: flex; 
            flex-direction: column; 
            align-items: center; 
            width: 100%;
        ">
            <span>3B APPARELS</span>
           
        </div>
    </h3>

        <h4 style="display: flex; flex-direction: column; align-items: center; text-align: center; font-size: 11px; font-weight:bold; margin-top: 4px; margin-bottom:2px;">Address:324 Main GT Road SA Rehman Garoghawala Lahore, Pakistan <br>
            Cell: +92-300-4248-791 </h4>
        <p style="align-items: center; text-align: center;">ESTIMATE
        </p>
        <h1 style="display: flex; flex-direction: column; align-items: center; text-align: center; font-size: 13px;"><?php echo  date("l"); ?> &nbsp; <?php echo date("d/m/Y"); ?> &nbsp; <?php date_default_timezone_set('Asia/Karachi');
                                                                                                                                                                                            $current_time = date('h:i A');
                                                                                                                                                                                            echo $current_time; ?></h1>
        <p style="font-size: 14px;"><b>Invoice: <?php echo intval($invoice_number) ?></b></p>
        <p style="font-size: 14px;"><b>SaleMan: <?php echo $row_customer['saleman']; ?></b></p>
        <p style="font-size: 14px;"><b>Customer Name:<?php echo $row_customer['name']; ?> </b></p>
        <!--<p style="font-size: 14px;"><b>Customer Phone: <?php echo $row_customer['customer_phone']; ?></b></p>-->
        <table>
            <thead>
                <tr>
                    <th>SR#</th> <!-- Add SR# header -->
                    <th>Desc</th>
                    <th>Rate</th>
                    <th>QTY</th>
                    <th>Total</th>
                </tr>
            </thead>

            <tbody>
                <?php
                $sr_number = 1; // Initialize SR# counter
                while ($row_product = $stmt_products->fetch(PDO::FETCH_ASSOC)) { ?>
                    <tr>
                        <td><?php echo $sr_number++; ?></td> <!-- Display and increment SR# -->
                        <td><?php echo $row_product['product_name']; ?></td>
                        <!-- <td><?php echo $row_product['product_code']; ?></td> -->
                        <!-- <td><?php echo $row_product['brand_name']; ?></td>
                <td><?php echo $row_product['category']; ?></td> -->
                        <!-- <td><?php echo $row_product['unit']; ?></td> -->
                        <td><?php echo $row_product['price']; ?></td>
                        <td><?php echo $row_product['sale_qty']; ?> <?php echo $row_product['unit']; ?></td>
                        <td><?php echo $row_product['total_amount']; ?></td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
        <?php
include '../includes/config.php';

// Get the latest invoice number
$stmt_invoice = $conn->prepare("SELECT MAX(invoice) AS max_invoice FROM manual_stock_sale;");
$stmt_invoice->execute();
$row_invoice = $stmt_invoice->fetch(PDO::FETCH_ASSOC);
$invoice_number = $row_invoice['max_invoice'];

// Get the customer details
$stmt_customer = $conn->prepare("SELECT contacts.name FROM contacts
INNER JOIN manual_stock_sale ON contacts.id = manual_stock_sale.customer_id
WHERE manual_stock_sale.invoice = :invoice_number;");
$stmt_customer->bindParam(':invoice_number', $invoice_number);
$stmt_customer->execute();
$row_customer = $stmt_customer->fetch(PDO::FETCH_ASSOC);

// Get the products in the bill
$stmt_products = $conn->prepare("SELECT SUM(total_amount) AS total_amount_sum, discount_type, (receive_amount) AS net_cash_sum, (discount) AS discount_sum, SUM(order_tax) AS order_tax_sum, SUM(sale_qty) AS total_sale_qty
FROM manual_stock_sale
WHERE manual_stock_sale.invoice = :invoice_number");
$stmt_products->bindParam(':invoice_number', $invoice_number);
$stmt_products->execute();
$row_products = $stmt_products->fetch(PDO::FETCH_ASSOC);

// Calculate final amounts based on discount type
$total_amount = $row_products['total_amount_sum'];
$order_tax_sum = $row_products['order_tax_sum'];
$net_cash_sum = $row_products['net_cash_sum'];
$discount_sum = $row_products['discount_sum'];
$discount_type = $row_products['discount_type'];

// Apply discount based on type
if ($discount_type === 'Percentage') {
    $discount_value = ($discount_sum / 100) * $total_amount; // Calculate percentage discount
    $display_discount = $discount_sum . '%'; // Display percentage
} else { // Assuming Fixed as the other option
    $discount_value = $discount_sum; // Use fixed discount directly
    $display_discount = $discount_sum; // Display fixed discount without %
}

// Calculate the due balance
$due_balance = ($total_amount + $order_tax_sum) - ($net_cash_sum + $discount_value);
?>

<div class="total">
    Total Qty: <?php echo $row_products['total_sale_qty']; ?>
</div>
<div class="total">
    Total Amount: <?php echo $total_amount; ?>
</div>
<div class="total">
    Discount: <?php echo $display_discount; ?>
</div>
<div class="total">
    Previous Balance: 0
</div>
<div class="total">
    Cash: <?php echo $net_cash_sum; ?>
</div>
<div class="total">
    Due Balance: <?php echo $due_balance; ?>
</div>

        <p style="align-items: center; text-align: center;">Goods Once Sold are not returnable
        </p>
        <h2>Thank You</h2>
        <!-- <p style="text-align:center; font-size:14px; font-weight:bold;">(Developed by IT Genesis)<br>webisite:itgenesis.net & ph: +92 308 4926007</p> -->
        <button onclick="printContent()">Print</button>
    </div>
</body>
<script>
    function printContent() {
        var container = document.querySelector(".container");
        var printButton = document.querySelector("button");
        printButton.style.display = "none"; // Hide the button
        window.print(); // Print the content of the container
        printButton.style.display = "block"; // Show the button after printing is done
    }
</script>

</html>