<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Invoice NC300303</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: #000;
            margin: 40px;
        }

        .invoice-header {
            display: table;
            width: 100%;
            border-bottom: 2px solid #1a1a1a;
            padding-bottom: 20px;
            margin-bottom: 4px;
        }

        .invoice-header-left {
            display: table-cell;
            width: 45%;
            vertical-align: top;
        }

        .invoice-header-right {
            display: table-cell;
            width: 55%;
            vertical-align: top;
            text-align: right;
        }

        .logo-box {
            margin-bottom: 12px;
            padding: 8px 0;
        }

        .logo-box img {
            max-height: 56px;
            max-width: 160px;
            width: auto;
            height: auto;
            display: block;
            object-fit: contain;
        }

        .company-name {
            font-size: 16px;
            font-weight: 600;
            color: #1a1a1a;
            letter-spacing: 0.3px;
            margin: 0;
            line-height: 1.3;
        }

        .invoice-title {
            font-size: 28px;
            font-weight: 700;
            letter-spacing: 2px;
            margin: 0 0 16px 0;
            color: #1a1a1a;
        }

        .invoice-meta {
            font-size: 13px;
            line-height: 1.6;
            color: #333;
            display: inline-block;
            text-align: left;
            margin-left: auto;
        }

        .invoice-meta-row {
            display: table-row;
        }

        .invoice-meta-label {
            display: table-cell;
            padding: 2px 12px 2px 0;
            font-weight: 600;
            color: #1a1a1a;
            white-space: nowrap;
            vertical-align: top;
        }

        .invoice-meta-value {
            display: table-cell;
            padding: 2px 0;
            color: #1a1a1a;
            text-align: right;
            vertical-align: top;
        }

        .invoice-meta-table {
            display: table;
            border-collapse: collapse;
        }

        .bill-to-box {
            margin-top: 12px;
            padding: 10px 12px;
            background: #f8f8f8;
            border: 1px solid #e0e0e0;
            border-radius: 2px;
            text-align: left;
            display: inline-block;
            min-width: 200px;
        }

        .bill-to-box strong {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            color: #555;
            display: block;
            margin-bottom: 6px;
        }

        .bill-to-box .bill-to-content {
            font-size: 13px;
            line-height: 1.5;
            color: #1a1a1a;
        }

        .section {
            margin-top: 30px;
        }

        .contact-issued-row {
            display: table;
            width: 100%;
            margin-top: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 2px;
        }

        .contact-issued-row .contact-info-box,
        .contact-issued-row .issued-to-box {
            display: table-cell;
            width: 50%;
            vertical-align: top;
            padding: 14px 16px;
            font-size: 13px;
            line-height: 1.6;
            color: #1a1a1a;
        }

        .contact-issued-row .contact-info-box {
            border-right: 1px solid #e0e0e0;
        }

        .contact-issued-row .box-title {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            color: #555;
            font-weight: 600;
            margin-bottom: 8px;
            display: block;
        }

        .contact-info,
        .issued-to {
            font-size: 14px;
            line-height: 1.6;
        }

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

        table th,
        table td {
            border: 1px solid #000;
            padding: 10px;
            text-align: left;
        }

        table th {
            background: #000;
            color: #fff;
            font-weight: bold;
        }

        .totals {
            margin-top: 20px;
            width: 100%;
            font-size: 14px;
        }

        .totals td {
            padding: 5px 10px;
            text-align: right;
        }

        .totals .label {
            text-align: left;
        }

        .bold {
            font-weight: bold;
        }

        .signature {
            margin-top: 60px;
            font-size: 14px;
        }

        .signature-line {
            width: 200px;
            border-top: 1px solid #000;
            margin-bottom: 5px;
        }

        .thank-you {
            text-align: right;
            font-size: 15px;
            font-weight: bold;
            color: #bfbfbf;
            line-height: 1;
            margin-top: 20px;
        }
    </style>
</head>

<body>

    @php
        $company_info = siteinfo();
        $logo_src = null;
        if (!empty($company_info) && !empty($company_info->img_url)) {
            $logo_path = storage_path('app/public/company/' . $company_info->img_url);
            if (!file_exists($logo_path)) {
                $logo_path = public_path('storage/company/' . $company_info->img_url);
            }
            if (file_exists($logo_path)) {
                $mime = mime_content_type($logo_path);
                $logo_src = 'data:' . $mime . ';base64,' . base64_encode(file_get_contents($logo_path));
            }
        }
    @endphp
    <div class="invoice-header">
        <div class="invoice-header-left">
            @if ($logo_src)
                <div class="logo-box">
                    <img src="{{ $logo_src }}" alt="Company Logo">
                </div>
            @endif
            @if (!empty($company_info) && !empty($company_info->name))
                <p class="company-name">{{ $company_info->name }}</p>
            @endif
        </div>
        <div class="invoice-header-right">
            <h1 class="invoice-title">INVOICE</h1>
            <div class="invoice-meta">
                <div class="invoice-meta-table">
                    <div class="invoice-meta-row">
                        <span class="invoice-meta-label">Invoice No</span>
                        <span class="invoice-meta-value">{{ $invoice->invoice_no }}</span>
                    </div>
                    <div class="invoice-meta-row">
                        <span class="invoice-meta-label">Date Issued</span>
                        <span class="invoice-meta-value">{{ date('d-M-Y', strtotime($invoice->billing_date ?? '')) }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="contact-issued-row">
        <div class="contact-info-box">
            <span class="box-title">Contact Info</span>
            {{ $company_info->name ?? '' }}<br>
            {{ $company_info->email ?? '' }}<br>
            {{ $company_info->mobile ?? '' }}<br>
            {{ $company_info->address ?? '' }}
        </div>
        <div class="issued-to-box">
            <span class="box-title">Issued To</span>
            {{ $invoice->bandwidthSaleCustomer->customer_name ?? '' }}<br>
            {{ $invoice->bandwidthSaleCustomer->email ?? '' }}<br>
            {{ $invoice->bandwidthSaleCustomer->phone ?? '' }}<br>
            {{ $invoice->bandwidthSaleCustomer->address ?? '' }}
        </div>
    </div>

    <table>
        <thead>
            <tr>
                <th>DESCRIPTION OF SERVICES</th>
                <th>RATE</th>
                <th>VAT</th>
                <th>TOTAL(BDT)</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($invoice->sateItems as $item)
                <tr>
                    <td><strong>{{ $item->service->name ?? '' }}</strong> ({{ $item->quantity ?? '' }} MB)
                        {{ date('d-M-Y', strtotime($item->from_date ?? '')) }} to
                        {{ date('d-M-Y', strtotime($item->to_date ?? '')) }}</td>
                    <td style="text-align: right;">{{ number_format($item->rate, 2) }}</td>
                    <td style="text-align: right;">{{ number_format($item->vat, 2) }}</td>
                    <td style="text-align: right;">{{ number_format($item->line_total, 2) }}</td>
                </tr>
            @empty
            @endforelse
        </tbody>
        <tr>
            <td colspan="3" class="label" style="text-align: right;">SUB TOTAL</td>
            <td style="text-align: right;">{{ number_format($invoice->total, 2) }}</td>
        </tr>
        <tr>
            <td colspan="3" class="label" style="text-align: right;">DISCOUNT</td>
            <td style="text-align: right;">{{ number_format($invoice->discount, 2) }}</td>
        </tr>
        <tr class="bold">
            <td colspan="3" class="label" style="text-align: right;">GRAND TOTAL</td>
            <td style="text-align: right;">{{ number_format($invoice->total - $invoice->discount, 2) }}</td>
        </tr>
        <tr>
            <td colspan="3" class="label" style="text-align: right;">PAID AMOUNT</td>
            <td style="text-align: right;">{{ number_format($invoice->paid, 2) }}</td>
        </tr>
        <tr>
            <td colspan="3" class="label" style="text-align: right;">DUE</td>
            <td style="text-align: right;">
                {{ number_format($invoice->total - ($invoice->discount + $invoice->paid), 2) }}</td>
        </tr>
    </table>

    <div class="signature">
        <div class="signature-line"></div>
        Authorized Signature
    </div>

    <div class="thank-you">
        THANK<br>YOU
    </div>

</body>

</html>
