<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Invoice - <?= htmlspecialchars($row->bill_no) ?></title>
<style>
    * { box-sizing: border-box; }
    body {
        font-family: 'Segoe UI', Arial, sans-serif;
        color: #1a1a1a;
        margin: 0;
        background: #f4f4f4;
    }
    .invoice-wrapper {
        max-width: 1100px;
        margin: 20px auto;
        background: #fff;
        padding: 40px 50px;
        border: 1px solid #ddd;
    }
    .invoice-title {
        text-align: center;
        font-size: 32px;
        font-weight: 700;
        margin-bottom: 25px;
        color: #111;
    }
    .logo-row {
        margin-bottom: 12px;
    }
    .logo-row img {
        width: 90px;
        height: 90px;
    }
    .top-section {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 25px;
        gap: 20px;
    }
    .company-block { max-width: 380px; }
    .company-block .details { font-size: 13.5px; line-height: 1.55; }
    .company-block .company-name {
        font-size: 17px;
        font-weight: 700;
        color: #111;
        margin-bottom: 2px;
    }
    .to-block { font-size: 13.5px; line-height: 1.55; max-width: 280px; }
    .to-block .label { font-weight: 700; margin-bottom: 4px; }
    .to-block .customer-name {
        font-size: 17px;
        font-weight: 700;
        color: #111;
        margin-bottom: 2px;
    }
    .meta-table {
        border-collapse: collapse;
        font-size: 13.5px;
    }
    .meta-table td {
        border: 1px solid #333;
        padding: 6px 14px;
    }
    .meta-table td:first-child { font-weight: 700; background: #fafafa; }

    table.items-table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 10px;
        font-size: 13.5px;
    }
    table.items-table th, table.items-table td {
        border: 1px solid #333;
        padding: 8px 10px;
    }
    table.items-table thead th {
        background: #fafafa;
        font-weight: 700;
        text-align: left;
    }
    table.items-table td.num, table.items-table th.num { text-align: right; }

    .bottom-section {
        display: flex;
        justify-content: space-between;
        margin-top: 28px;
        gap: 30px;
    }
    .note-terms { font-size: 13px; max-width: 480px; }
    .note-terms h4 { margin: 0 0 6px 0; font-size: 14px; }
    .note-terms p { margin: 4px 0; }
    .bank-details { font-size: 13px; margin-top: 18px; }
    .bank-details h4 { margin: 0 0 6px 0; font-size: 14px; }
    .bank-details p { margin: 3px 0; }

    table.totals-table {
        border-collapse: collapse;
        font-size: 13.5px;
        min-width: 460px;
        height: fit-content;
    }
    table.totals-table td {
        border: 1px solid #333;
        padding: 8px 14px;
    }
    table.totals-table td.label { font-weight: 700; background: #fafafa; }
    table.totals-table td.value { text-align: right; }

    .words-row {
        margin-top: 18px;
        border-top: 1px solid #333;
        border-bottom: 1px solid #333;
        padding: 10px 0;
        font-size: 13.5px;
    }
    .sign-section {
        display: flex;
        justify-content: space-between;
        margin-top: 60px;
        font-size: 13.5px;
    }
    .sign-section .sign-line {
        border-top: 1px solid #333;
        padding-top: 6px;
        width: 200px;
        text-align: center;
    }
    .print-btn {
        display: inline-block;
        margin-top: 25px;
        padding: 8px 20px;
        background: #2563eb;
        color: #fff;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 14px;
    }
    .back-btn {
        display: inline-block;
        margin-top: 25px;
        margin-right: 10px;
        padding: 8px 20px;
        background: #f1f5f9;
        color: #374151;
        border: 1px solid #d1d5db;
        border-radius: 4px;
        cursor: pointer;
        font-size: 14px;
    }
    @media print {
        body { background: #fff; }
        .invoice-wrapper { border: none; margin: 0; padding: 15px 30px; }
        .print-btn, .back-btn { display: none; }
    }
</style>
</head>
<body>

<div class="invoice-wrapper">

    <div class="invoice-title">Invoice</div>

    <div class="logo-row">
        <img src="<?= base_url('assets/images/raju_logo.jpg') ?>" alt="Raju Collection Logo">
    </div>

    <div class="top-section">
        <div class="company-block">
            <div class="details">
                <div class="company-name">Raju Collection</div>
                <div>Fabric And Tailoring</div>
                <div>New Vashinaka Chowk, Deepkalp Complex,<br>Radhanagari Road, Kolhapur - 416003</div>
                <div>Phone No&nbsp;: &nbsp;9021473232, 9421645847</div>
            </div>
        </div>

        <div class="to-block">
            <div class="label">To</div>
            <div class="customer-name"><?= htmlspecialchars($row->customer_name ?? '-') ?></div>
            <div><?= htmlspecialchars($row->address ?? '') ?></div>
            <div>Phone No&nbsp;: &nbsp;<?= htmlspecialchars($row->mobile ?? '-') ?></div>
        </div>

        <table class="meta-table">
            <tr><td>Invoice ID</td><td><?= htmlspecialchars($row->bill_no) ?></td></tr>
            <tr><td>Bill Date</td><td><?= date('d-m-Y', strtotime($row->bill_date)) ?></td></tr>
            <tr><td>Due Date</td><td><?= date('Y-m-d', strtotime($row->bill_date)) ?></td></tr>
        </table>
    </div>

    <table class="items-table">
        <thead>
            <tr>
                <th>Product</th>
                <th>Unit</th>
                <th class="num">Qty</th>
                <!--<th class="num">Meter</th>-->
                <th class="num">Rate</th>
                <th class="num">Amount</th>
                <th class="num">CGST</th>
                <th class="num">SGST</th>
                <th class="num">Total</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($items)): foreach ($items as $item): ?>
            <tr>
                <td><?= htmlspecialchars($item->product_name ?? '-') ?></td>
                <td><?= htmlspecialchars($item->unit_name ?? '-') ?></td>
                <td class="num"><?= (int)$item->qty ?></td>
                <!--<td class="num"><?= ($item->meter > 0) ? number_format($item->meter, 2) : '—' ?></td>-->
                <td class="num"><?= number_format($item->rate, 2) ?></td>
                <td class="num"><?= number_format($item->amount, 2) ?></td>
                <td class="num"><?= number_format($item->gst_percent / 2, 1) ?>%</td>
                <td class="num"><?= number_format($item->gst_percent / 2, 1) ?>%</td>
                <td class="num"><?= number_format($item->total, 2) ?></td>
            </tr>
            <?php endforeach; else: ?>
            <tr><td colspan="9" style="text-align:center;">No items found.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>

    <?php
        // GST total items varun calculate karun CGST/SGST madhe adhi-adha split
        $total_gst = 0;
        if (!empty($items)) {
            foreach ($items as $item) {
                $total_gst += ($item->amount * $item->gst_percent / 100);
            }
        }
        $cgst_amt = $total_gst / 2;
        $sgst_amt = $total_gst / 2;
    ?>

    <div class="bottom-section">
        <div>
            <div class="note-terms">
                <h4>Note :</h4>
                <p><?= htmlspecialchars($row->notes ?? '') ?></p>

                <h4 style="margin-top:18px;">Term &amp; Conditions</h4>
                <?php if (!empty($reportheader) && !empty($reportheader[0]->terms_condition)): ?>
                    <?= $reportheader[0]->terms_condition ?>
                <?php endif; ?>
            </div>

            <div class="bank-details">
                <h4>Bank Details</h4>
                <?php if (!empty($reportheader) && !empty($reportheader[0]->bank_details)): ?>
                    <?= $reportheader[0]->bank_details ?>
                <?php endif; ?>
            </div>
        </div>

        <table class="totals-table">
            <tr>
                <td class="label">Sub Total:</td>
                <td class="value">&#8377; <?= number_format($row->subtotal, 2) ?></td>
            </tr>
            <tr>
                <td class="label">CGST:</td>
                <td class="value">&#8377; <?= number_format($cgst_amt, 2) ?></td>
            </tr>
            <tr>
                <td class="label">SGST:</td>
                <td class="value">&#8377; <?= number_format($sgst_amt, 2) ?></td>
            </tr>
            <tr>
                <td class="label">Discount:</td>
                <td class="value">&#8377; <?= number_format($row->discount, 2) ?></td>
            </tr>
            <tr>
                <td class="label">Net Payable:</td>
                <td class="value"><strong>&#8377; <?= number_format($row->net_payable, 2) ?></strong></td>
            </tr>
        </table>
    </div>

    <div class="words-row">
        In Words&nbsp;: &nbsp;<?= ucfirst(number_to_words($row->net_payable)) ?> Only.
    </div>

    <div class="sign-section">
        <div class="sign-line">Customer Sign</div>
        <div class="sign-line">
            <?php if (!empty($reportheader) && !empty($reportheader[0]->signature)): ?>
                <?= $reportheader[0]->signature ?>
            <?php else: ?>
                (Raju Collection)<br>
                Authorised Signatory
            <?php endif; ?>
        </div>
    </div>

    <div style="text-align:right;">
        <button class="back-btn" onclick="history.back()">&larr; Back</button>
        <button class="print-btn" onclick="window.print()">Print</button>
    </div>

</div>

</body>
</html>