<?php
require_once '../includes/auth.php';
require_once '../db.php';
require_once '../includes/header.php';
require_once '../includes/sidebar.php';

$sql = "
    SELECT si.*, c.name AS customer_name
    FROM sales_invoices si
    JOIN clients c ON c.id = si.client_id
    ORDER BY si.id DESC
";
$result = $conn->query($sql);
?>

<div class="content">
    <h2>Sales Invoices</h2>

    <a href="create.php" class="btn">+ Create Sales Invoice</a>

    <table class="list-table">
        <tr>
            <th>Invoice No</th>
            <th>Date</th>
            <th>Customer</th>
            <th>Gross</th>
            <th>Overall Discount</th>
            <th>GST</th>
            <th>Net Total</th>
            <th>Received</th>
            <th>Balance</th>
            <th>Status</th>
            <th>Action</th>
        </tr>

        <?php if ($result->num_rows > 0): ?>
            <?php while ($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?= htmlspecialchars($row['invoice_no']) ?></td>
                    <td><?= htmlspecialchars($row['invoice_date']) ?></td>
                    <td><?= htmlspecialchars($row['customer_name']) ?></td>

                    <td>₹ <?= number_format($row['gross_total'], 2) ?></td>

                    <td>
                        <?php
                        if ($row['overall_discount_type'] === 'percent') {
                            echo $row['overall_discount_value'] . '%';
                        } elseif ($row['overall_discount_type'] === 'amount') {
                            echo '₹ ' . number_format($row['overall_discount_value'], 2);
                        } else {
                            echo '-';
                        }
                        ?>
                    </td>

                    <td>₹ <?= number_format($row['total_gst'], 2) ?></td>
                    <td><b>₹ <?= number_format($row['net_total'], 2) ?></b></td>
                    <td>₹ <?= number_format($row['received_amount'], 2) ?></td>
                    <td>₹ <?= number_format($row['balance_amount'], 2) ?></td>
                    <td><?= ucfirst($row['status']) ?></td>

                    <td>
						<?php if ($row['status'] == 'draft'): ?>
							<a href="edit.php?id=<?= $row['id'] ?>" title="Edit">✏️</a>
							<a href="javascript:void(0)" 
							   onclick="openPrintModal('sales', <?= $row['id'] ?>, 'invoice', 'thermal')"
							   title="Thermal Invoice">🧾</a>
							<a href="view.php?id=<?= $row['id'] ?>" title="view">👁</a>
							<a href="delete.php?id=<?= $row['id'] ?>" 
							   onclick="return confirm('Delete this draft invoice?')" 
							   title="Delete">🗑</a>
						<?php else: ?>
							<a href="javascript:void(0)" 
							   onclick="openPrintModal('sales', <?= $row['id'] ?>, 'invoice', 'thermal')" 
							   title="Thermal Invoice">🧾</a>
							<a href="view.php?id=<?= $row['id'] ?>" title="view">👁</a>

							<a href="<?= BASE_URL ?>/sales_return/create.php?invoice_id=<?= $row['id'] ?>" 
							   title="Sales Return">🔄</a>
						<?php endif; ?>
						</td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr><td colspan="11">No Sales Invoices Found.</td></tr>
        <?php endif; ?>
    </table>
</div>

<!-- Thermal Print Modal -->


<?php require_once '../includes/footer.php'; ?>


<script>
let currentInvoiceId = 0;

function loadLayout() {
    const type = document.querySelector('input[name="type"]:checked').value;

    fetch(`layout_loader.php?id=<?= $id ?>&type=${type}&paper=a4`)
        .then(r => r.text())
        .then(html => {
            document.getElementById('invoicePreview').innerHTML = html;
            renderCodes();
        });
}




document.querySelectorAll('input[name="thermalType"]').forEach(radio => {
    radio.addEventListener('change', function() {
        loadThermalLayout(this.value);
    });
});

</script>