<?php
if (!function_exists('numberToWords')) {
function numberToWords($num) {

    $num = round($num);

    $ones = [
        0 => '', 1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four',
        5 => 'Five', 6 => 'Six', 7 => 'Seven', 8 => 'Eight',
        9 => 'Nine', 10 => 'Ten', 11 => 'Eleven', 12 => 'Twelve',
        13 => 'Thirteen', 14 => 'Fourteen', 15 => 'Fifteen',
        16 => 'Sixteen', 17 => 'Seventeen', 18 => 'Eighteen',
        19 => 'Nineteen'
    ];

    $tens = [
        2 => 'Twenty', 3 => 'Thirty', 4 => 'Forty',
        5 => 'Fifty', 6 => 'Sixty', 7 => 'Seventy',
        8 => 'Eighty', 9 => 'Ninety'
    ];

    if ($num == 0) return 'Zero';

    $result = '';

    if ($num >= 10000000) {
        $result .= numberToWords(floor($num / 10000000)) . ' Crore ';
        $num %= 10000000;
    }

    if ($num >= 100000) {
        $result .= numberToWords(floor($num / 100000)) . ' Lakh ';
        $num %= 100000;
    }

    if ($num >= 1000) {
        $result .= numberToWords(floor($num / 1000)) . ' Thousand ';
        $num %= 1000;
    }

    if ($num >= 100) {
        $result .= $ones[floor($num / 100)] . ' Hundred ';
        $num %= 100;
    }

    if ($num > 0) {
        if ($num < 20) {
            $result .= $ones[$num] . ' ';
        } else {
            $result .= $tens[floor($num / 10)];
            if ($num % 10 > 0) {
                $result .= ' ' . $ones[$num % 10];
            }
        }
    }

    return trim($result);
}
}

/* ================= STATE CODE ================= */
$state_codes = [

    // ===== STATES =====
    'andhra pradesh' => '37',
    'arunachal pradesh' => '12',
    'assam' => '18',
    'bihar' => '10',
    'chhattisgarh' => '22',
    'goa' => '30',
    'gujarat' => '24',
    'haryana' => '06',
    'himachal pradesh' => '02',
    'jharkhand' => '20',
    'karnataka' => '29',
    'kerala' => '32',
    'madhya pradesh' => '23',
    'maharashtra' => '27',
    'manipur' => '14',
    'meghalaya' => '17',
    'mizoram' => '15',
    'nagaland' => '13',
    'odisha' => '21',
    'punjab' => '03',
    'rajasthan' => '08',
    'sikkim' => '11',
    'tamil nadu' => '33',
    'telangana' => '36',
    'tripura' => '16',
    'uttar pradesh' => '09',
    'uttarakhand' => '05',
    'west bengal' => '19',

    // ===== UNION TERRITORIES =====
    'andaman and nicobar islands' => '35',
    'chandigarh' => '04',
    'dadra and nagar haveli and daman and diu' => '26',
    'delhi' => '07',
    'jammu and kashmir' => '01',
    'ladakh' => '38',
    'lakshadweep' => '31',
    'puducherry' => '34'
];
date_default_timezone_set('Asia/Kolkata');
$state_code = $state_codes[$state] ?? '';
$formatted_date = date('d-m-Y', strtotime($invoice['invoice_date']));
$customer_name = ucwords(strtolower($invoice['client_name']));
$formatted_time = date('g:i A');
$customer_address = ucwords(strtolower($invoice['address']));
$customer_city = ucwords(strtolower($invoice['city']));
$customer_state = ucwords(strtolower($invoice['state']));
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        @page {
            size: A4;
            margin: 0;
        }

        body {
            margin: 0;
            padding: 0;
            font-family: DejaVu Sans, sans-serif;


        }

        .page {
            position: relative;
            width: 210mm;
            height: 297mm;
        }

        .bg {
            position: absolute;
            top: 0;
            left: 0;
            width: 210mm;
            height: 297mm;
        }

        .content {
            position: absolute;
            top: 0;
            left: 0;
            width: 210mm;
            height: 297mm;
            font-size: 12px;
        }



        /* Invoice specific styles */
        .invoice-wrap {
            position: absolute;
            top: 49mm;
            /* header ke neeche start */
            left: 15mm;
            /* left golden border ke andar */
            width: 180mm;
            font-family: Arial, Helvetica, sans-serif;
            color: #3b2f2f;
        }

        .invoice-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 8px;
        }

        .company {
            font-size: 18px;
            font-weight: 700;
            color: #a66b00
        }

        .addr {
            font-size: 11px;
            color: #6b6b6b
        }

        .invoice-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 6px
        }

        .invoice-table th,
        .invoice-table td {
            border: 1px solid #e0cfa8;
            padding: 6px;
            font-size: 11px
        }

        .invoice-table th {
            background: #f7e9d0
        }

        .right-panel {
            width: 36%;
            float: right;
            box-sizing: border-box;
            padding-left: 8px
        }

        .left-panel {
            width: 62%;
            float: left;
            box-sizing: border-box
        }

        .totals {
            border: 1px dashed #d9c5a3;
            padding: 8px;
            margin-top: 6px;
            background: #fffaf1
        }

        .totals .row {
            display: flex;
            justify-content: space-between;
            margin: 4px 0;
            font-size: 12px
        }

        .charges-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px
        }

        .charges-table td {
            border: 1px solid #e9d9c0;
            padding: 6px;
            font-size: 11px
        }

        .signatures {
            display: flex;
            justify-content: space-between;
            margin-top: 18px
        }

        .footer {
            background: #fff2d9;
            padding: 8px;
            margin-top: 10px;
            text-align: center;
            font-size: 11px;
            color: #a15a00
        }

        .clear {
            clear: both
        }


        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            font-weight: 900;
        }

        strong,
        b {
            font-weight: 600;
        }

        .header h2 {
            font-weight: 900;
        }

        .table-invoice th {
            font-weight: 800;
        }

        .table-invoice {
            font-size: 9px;
        }

        .table-invoice th,
        .table-invoice td {
            padding: 3px 4px;
            /* 6px → 3px */
        }

        .table-invoice td {
            font-weight: 600;
        }


        .info-box {
            border: 1px solid #e5c87a;
            padding: 6px;
            background: #fffaf0;
        }

        .info-box h5 {
            margin: 0;
            color: #8b0000;
            font-size: 12px;
            border-bottom: 1px solid #e5c87a;
        }

        .items-wrapper {
            border: 1px solid #d4af37;
            background: #fffdf8;
            display: flex;
            flex-direction: column;
            margin-top: 15px;
        }

        .items-scroll {
            flex: 1;
            overflow-y: auto;
        }

        .table-invoice {
            width: 100%;
            border-collapse: collapse;
            font-size: 9px;
        }

        .table-invoice th {
            background: #f3e2a9;
            border: 1px solid #d4af37;
            padding: 6px;
            color: #5a2a27;
        }

        .table-invoice td {
            border: 1px solid #d4af37;
            padding: 5px 6px;
            text-align: center;
        }

        .total-table {
            margin-top: auto;
        }

        .total-table td {
            background: #fff1c1;
            font-weight: bold;
            border-top: 2px solid #d4af37;
        }

        .left-box,
        .right-box {
            border: 1px dashed #888;
            background: #fffdf8;
            padding: 6px;
            /* margin: 5px; */
        }

        .left-box .row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 6px;
        }

        .left-box .row strong {
            width: 60px;
            color: #7b1e1e;
        }

        .total-words {
            border: 1px dashed #888;
            padding: 6px 10px;
            margin-top: 10px;
            font-style: italic;
            background: #fffaf0;
        }

        .right-box table {
            width: 100%;
            border-collapse: collapse;
        }

        .right-box table tr {
            border-bottom: 1px dashed #888;
        }

        .right-box table td {
            padding: 2px 5px;
        }

        .right-box table td:first-child {
            font-weight: 400;
            color: #7b1e1e;
            font-size: 11px;
        }

        .right-box table td:last-child {
            text-align: right;
        }
        .sign-area {
            display: flex;
            justify-content: space-between;
            margin-top: 89px;
        }

        .sign-box {
            text-align: center;
            color: #7b1e1e;
        }
    </style>
</head>
<?php
$logoPath = __DIR__ . '/3.jpg';
$logoData = base64_encode(file_get_contents($logoPath));
$logoSrc = 'data:image/png;base64,' . $logoData;
?>
<?php
$rupeePath = __DIR__ . '/rupees.png';
$rupeeData = base64_encode(file_get_contents($rupeePath));
$rupeeSrc = 'data:image/png;base64,' . $rupeeData;
?>

<body>

    <div class="page">

        <!-- Background Full A4 -->
        <img class="bg" src="<?php echo $logoSrc; ?>" alt="Invoice Background">

        <!-- Overlay Content -->
        <div class="content">
            <!-- outer flex container to center invoice on the page -->
            <div class="invoice-wrap">
                <p style="text-align: center;">Tax Invoice</p>
                <div class="info-box">
                    <table width="100%" cellpadding="4" cellspacing="0"
                        style="border-collapse:collapse; font-size:10px;">
                        <!-- ROW 1 -->
                         <tr style="margin:2px 0; color: black;     font-size: 12px;">
                            <td style="margin-bottom:7px; font-weight: 400;"><strong>Invoice No:</strong>
                                <?= htmlspecialchars($invoice['invoice_no']) ?>
                            </td>


                            <td style="margin-bottom:7px;  font-weight: 600;"><strong>Name:</strong>
                                <?= htmlspecialchars($customer_name) ?>
                            </td>
                            <td style="margin-bottom:7px;  font-weight: 600;"><strong>Phone:</strong>
                                <?= htmlspecialchars($invoice['phone']) ?>
                            </td>
                            <td style="margin-bottom:7px;  font-weight: 600;"><strong>Date:</strong>
                                <?= $formatted_date ?>
                            </td>
                        </tr>

                        <!-- ROW 2 -->
                        <tr style="margin:2px 0; font-weight: 600; color: black;     font-size: 12px;">
                            <td colspan="2" style="margin-bottom:7px;">
                                <strong>Address:</strong>
                                <?= htmlspecialchars($customer_address) ?>,
                                <?= htmlspecialchars($customer_city) ?>,
                                <?= htmlspecialchars($customer_state) ?> -
                                <?= htmlspecialchars($invoice['pincode']) ?>
                            </td>
                        </tr>

                        <!-- ROW 3 -->
                        <tr style="margin:2px 0; font-weight: 600; color: black;     font-size: 12px;">
                            <td style="margin-bottom:7px;"><strong>PAN:</strong>
                                <?= htmlspecialchars($invoice['pan_no']) ?>
                            </td>
                            <td style="margin-bottom:7px;"><strong>GST:</strong>
                                <?= htmlspecialchars($invoice['gst_no']) ?>
                            </td>
                            <td colspan="2" style="margin-bottom:7px;"><strong>State Code:</strong>
                                <?= $state_code ?>
                            </td>
                        </tr>

                    </table>
                </div>


                <div class="items-wrapper">

                    <!-- ITEMS (SCROLLABLE) -->
                    <div class="items-scroll">
                        <table class="table-invoice">
                            <thead>
                                <tr>
                                    <th>Description</th>
                                    <th>H.S.N</th>
                                    <th>Gross Weight (gm)</th>
                                    <th>Net Weight (gm)</th>
                                    <th>Rate in gms</th>
                                    <th>Making</th>
                                    <th>Stone Value</th>
                                    <th>H.M</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($items as $k => $item) : ?>
                                <tr>
                                    <!-- <td>
                                    <?= $k + 1 ?>
                                </td> -->
                                    <td>
                                        <?= htmlspecialchars($item['description']) ?>
                                    </td>

                                    <!-- HSN -->
                                    <td>
                                        <?= htmlspecialchars($item['hsn']) ?>
                                    </td>

                                    <!-- Gross Weight -->
                                    <td>
                                        <?= number_format($item['gross_weight'], 3) ?>
                                    </td>

                                    <!-- Net Weight -->
                                    <td>
                                        <?= number_format($item['net_weight'], 3) ?>
                                    </td>

                                    <!-- Rate -->
                                    <td>
                                        <?= number_format($item['rate'], 2) ?>
                                    </td>

                                    <!-- Making -->
                                    <td>
                                        <?= number_format($item['making'], 2) ?>
                                    </td>

                                    <!-- Stone Value -->
                                    <td>
                                        <?= number_format($item['stone_value'], 2) ?>
                                    </td>

                                    <!-- H.M Value -->
                                    <td>
                                        <?= number_format($item['hm_value'], 2) ?>
                                    </td>

                                    <!-- Total -->
                                    <td>
                                        <?= number_format($item['total'], 2) ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- TOTAL (FIXED AT BOTTOM) -->
                    <table class="table-invoice total-table">
                        <tr>
                            <td colspan="8"><strong>TOTAL</strong></td>
                            <td><strong>
                                    <?= number_format($items_subtotal, 2) ?>
                                </strong></td>
                        </tr>
                    </table>

                </div>

                <table width="100%" cellpadding="6" cellspacing="0" style="border-collapse:collapse; font-size:13px;">
                    <tr>
                        <!-- LEFT : CUSTOMER DETAILS -->
                        <td width="50%" valign="top" style=" padding: 10px 0px; ">

                            <div class="left-box">
                                <!-- <div class="row"><strong>UPI:</strong> <img src="<?= $rupeeSrc ?>" style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($payments['UPI'], 2) ?>
                            </div>
                            <div class="row"><strong>Bank:</strong> <img src="<?= $rupeeSrc ?>" style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($payments['Bank'], 2) ?>
                            </div>
                            <div class="row"><strong>Metal:</strong> <img src="<?= $rupeeSrc ?>" style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($metal, 2) ?>
                            </div>
                            <div class="row"><strong>Cash:</strong> <img src="<?= $rupeeSrc ?>" style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($payments['Cash'], 2) ?>
                            </div>
                            <div class="row"><strong>Dues:</strong> <img src="<?= $rupeeSrc ?>" style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($dues, 2) ?>
                            </div> -->


                                <table class="table-invoice">
                                    <thead>
                                        <tr>
                                            <th>UPI:</th>
                                            <th>Cheque:</th>
                                            <th>Metal:</th>
                                            <th>Cash:</th>
                                            <th>Card:</th>
                                        </tr>
                                    </thead>

                                    <tbody>

                                        <tr>
                                            <td> <?= number_format($payments['UPI'], 2) ?></td>
                                            <td> <?= number_format($payments['Cheque'], 2) ?></td>
                                            <td> <?= number_format($payments['Metal'], 2) ?></td>
                                            <td> <?= number_format($payments['Cash'], 2) ?></td>
                                            <td> <?= number_format($payments['Card'], 2) ?></td>
                                        </tr>
                                    </tbody>

                                </table>
                                <div class="total-words" style="font-weight:bold; font-size:13px;">
                                    Total :
                                    <?= numberToWords(($round_final_amount + $hm_base_amount) + $other_charges) ?> Only
                                </div>
                            </div>
                        </td>

                        <!-- RIGHT : INVOICE DETAILS -->
                        <td width="50%" valign="top" style=" padding: 10px 0px; ">
                            <div class="right-box">
                                <table>
                                    <tr>
                                        <td>Total :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($items_subtotal, 2) ?>
                                        </td>
                                    </tr>
                                    <!-- <tr>
                                        <td>C-Gst 1.5% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($cgst, 2) ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>S-Gst 1.5% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($sgst, 2) ?>
                                        </td>
                                    </tr> -->
                                    <?php if ($cgst > 0): ?>
                                    <tr>
                                        <td>C-GST 1.5% :</td>
                                        <td><img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($cgst, 2) ?></td>
                                    </tr>
                                    <tr>
                                        <td>S-GST 1.5% :</td>
                                        <td><img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($sgst, 2) ?></td>
                                    </tr>
                                    <?php endif; ?>

                                    <?php if ($igst > 0): ?>
                                    <tr>
                                        <td>I-GST 3% :</td>
                                        <td><img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($igst, 2) ?></td>
                                    </tr>
                                    <?php endif; ?>
                                    <?php if ($state == 'west bengal'): ?>

                                    <tr>
                                        <td>HM CGST 9% :</td>
                                        <td><img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($hm_cgst, 2) ?></td>
                                    </tr>

                                    <tr>
                                        <td>HM SGST 9% :</td>
                                        <td><img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($hm_sgst, 2) ?></td>
                                    </tr>

                                    <?php else: ?>

                                    <tr>
                                        <td>HM IGST 18% :</td>
                                        <td><img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($hm_igst, 2) ?></td>
                                    </tr>

                                    <?php endif; ?>

                                   
                                    <tr>
                                        <td>Final Amount :</td>
                                        <td><img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">  <?= number_format($amount_before_discount + $hm_base_amount, 2) ?>
                                        </td>

                                    </tr>
                                     <?php if ($discount_amount > 0): ?>
                                    <tr>
                                        <td>Discount
                                            <?php if ($discount_type == 'percent'): ?>
                                            (<?= $discount_value ?>%)
                                            <?php endif; ?>
                                            :
                                        </td>
                                        <td style="color:#c00000;">
                                            - <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($discount_amount, 2) ?>
                                        </td>
                                    </tr>
                                    <?php endif; ?>
                                    <tr>
                                        <td>Round Off :</td>
                                        <td>
                                            <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($round_off, 2) ?>
                                        </td>
                                    </tr>

                                    <tr style="font-size:14px; font-weight:bold;">
                                        <td>Total :</td>
                                        <td>
                                            <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                           <?= number_format($round_final_amount + $hm_base_amount, 2) ?>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                </table>

                <?php if (count($other_charges_list) > 0) : ?>

                <table class="table-invoice" style="margin-top:3px; font-size:8px;">
                    <thead>
                        <tr>
                            <th>SL</th>
                            <th>Charge Name</th>
                            <th>Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($other_charges_list as $idx => $charge) : ?>
                        <tr>
                            <td>
                                <?= $idx + 1 ?>
                            </td>
                            <td>
                                <?= htmlspecialchars($charge['charge_name']) ?>
                            </td>
                            <td style="text-align:right;">
                                <?= number_format($charge['amount'], 2) ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>

                        <!--<tr>-->
                        <!--    <td colspan="2" style="text-align:right;">-->
                        <!--        <strong>Total Other Charges</strong>-->
                        <!--    </td>-->
                        <!--    <td style="text-align:right;">-->
                        <!--        <strong>-->
                        <!--            <?= number_format($other_charges, 2) ?>-->
                        <!--        </strong>-->
                        <!--    </td>-->
                        <!--</tr>-->

                       <?php
                     $grand_total_with_other = ($round_final_amount + $hm_base_amount) + $other_charges;
                    ?>

                        <tr style="background:#fff1c1; font-weight:bold; font-size:13px;">
                            <td colspan="2" style="text-align:right;">Grand Total</td>
                            <td style="text-align:right;">
                                ₹
                                <?= number_format($grand_total_with_other, 2) ?>
                            </td>
                        </tr>

                    </tbody>
                </table>

                <?php endif; ?>
<div class="sign-area">
                <table width="100%">
                    <tr>
                        <!-- LEFT : CUSTOMER DETAILS -->
                        <td width="50%" valign="top">
                            <div class="sign-box">
                                ___________________________<br>Customer Signature
                            </div>
                        </td>

                        <!-- RIGHT : INVOICE DETAILS -->
                        <td width="50%" valign="top" style="padding-left: 40px;">
                            <div class="sign-box">
                                ___________________________<br>For R P JEWELLERS & SONS
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            </div> <!-- .invoice-wrap -->
            
        </div> <!-- flex center -->
        <div style="
    position:absolute;
    bottom:20px;
    right:30px;
    font-size:10px;
">
            <strong>Invoice Time:</strong>
            <?= $formatted_time ?>
        </div>
</body>

</html>