<?php
/**
 * Clean Sheet PDF template — mPDF-compliant table architecture.
 * 100% Google Fonts Inter TTF suite & pixel-accurate geometry matching clean_sheet_preview.html.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals

/**
 * Template inputs:
 * $invoice: array
 * $client:  array|null
 */
$currency     = $invoice['currency'] ?? 'USD';
$issue_ts     = ! empty( $invoice['issue_date'] ) && '0000-00-00' !== $invoice['issue_date'] ? strtotime( $invoice['issue_date'] ) : false;
$date_format  = get_option( 'date_format', 'F j, Y' );
$issue_date   = ( false !== $issue_ts ) ? strtoupper( wp_date( $date_format, $issue_ts ) ) : '—';
$due_ts       = ! empty( $invoice['due_date'] ) && '0000-00-00' !== $invoice['due_date'] ? strtotime( $invoice['due_date'] ) : false;
$due_date     = ( false !== $due_ts ) ? strtoupper( wp_date( $date_format, $due_ts ) ) : '—';
$line_items   = $invoice['line_items'] ?? [];

$doc_type   = $invoice['document_type'] ?? 'invoice';
$doc_title  = ! empty( $invoice['document_title'] )
	? $invoice['document_title']
	: ( 'estimate' === $doc_type ? __( 'ESTIMATE', 'zoventic-invoices' ) : __( 'INVOICE', 'zoventic-invoices' ) );
$due_label  = ( 'estimate' === $doc_type ) ? __( 'EXPIRY:', 'zoventic-invoices' ) : __( 'DUE:', 'zoventic-invoices' );
$inv_status = strtolower( (string) ( $invoice['status'] ?? 'draft' ) );

// Company identity from Settings.
$company_name = (string) ZINV_Settings::get( 'company_name', '' );
if ( '' === trim( $company_name ) ) {
	$company_name = get_bloginfo( 'name' );
}

$company_addr = (string) ZINV_Settings::get( 'address', '' );
if ( '' === trim( $company_addr ) ) {
	$company_addr = get_bloginfo( 'description' );
}

$company_logo  = (string) ZINV_Settings::get( 'company_logo', '' );
$company_tax   = (string) ZINV_Settings::get( 'tax_id', '' );
$company_email = (string) ZINV_Settings::get( 'company_email', '' );
$company_phone = (string) ZINV_Settings::get( 'company_phone', '' );

$accent      = '#4F46E5';
$show_footer = true;

// --- Watermark Engine ---
$enable_watermark = (bool) ZINV_Settings::get( 'enable_watermark', false );
$watermark_type   = (string) ZINV_Settings::get( 'watermark_type', 'SAMPLE' );
$watermark_custom = (string) ZINV_Settings::get( 'watermark_custom_text', '' );
$watermark_text   = 'CUSTOM' === $watermark_type ? $watermark_custom : $watermark_type;

// Field accessor: works for arrays and stdClass objects.
$zinv_field = static function ( $row, string $key, $default = '' ) {
	if ( is_array( $row ) ) {
		return $row[ $key ] ?? $default;
	}
	if ( is_object( $row ) ) {
		return $row->$key ?? $default;
	}
	return $default;
};

// Clean integer/decimal quantity formatting.
$clean_qty = static function ( $val ) {
	$num = (float) $val;
	return ( floor( $num ) == $num ) ? (string) (int) $num : (string) $num;
};

// Shipping / Delivery details (if present).
$ship_to_name    = (string) ( $invoice['ship_to_name'] ?? $invoice['shipping_name'] ?? '' );
$ship_to_address = (string) ( $invoice['ship_to_address'] ?? $invoice['shipping_address'] ?? '' );
$has_shipping    = ! empty( $ship_to_name ) || ! empty( $ship_to_address );

// Payment Instructions from settings.
$bank_name     = (string) ZINV_Settings::get( 'bank_name', '' );
$bank_routing  = (string) ZINV_Settings::get( 'bank_routing', '' );
$bank_account  = (string) ZINV_Settings::get( 'bank_account', '' );
$bank_swift    = (string) ZINV_Settings::get( 'bank_swift', '' );
$bank_holder   = (string) ZINV_Settings::get( 'bank_holder', '' );
$manual_instr  = (string) ZINV_Settings::get( 'manual_instructions', '' );
$has_bank_info = ! empty( $bank_name ) || ! empty( $bank_account ) || ! empty( $bank_swift ) || ! empty( $bank_holder ) || ! empty( $manual_instr );

// Split company address lines cleanly.
$company_lines = [];
if ( ! empty( $company_addr ) ) {
	$raw_lines = preg_split( '/\r\n|\r|\n/', trim( $company_addr ) );
	foreach ( $raw_lines as $l ) {
		if ( '' !== trim( $l ) ) {
			$company_lines[] = trim( $l );
		}
	}
}

$comp_contact = [];
if ( ! empty( $company_email ) ) {
	$comp_contact[] = $company_email;
}
if ( ! empty( $company_phone ) ) {
	$comp_contact[] = $company_phone;
}
if ( ! empty( $comp_contact ) ) {
	$company_lines[] = implode( ' · ', $comp_contact );
}
if ( ! empty( $company_tax ) ) {
	$company_lines[] = 'Tax ID: ' . $company_tax;
}

// Format client address lines cleanly matching reference design.
$client_lines = [];
if ( $client ) {
	if ( ! empty( $client['company'] ) ) {
		$client_lines[] = (string) $client['company'];
	}
	if ( ! empty( $client['address_line1'] ) ) {
		$client_lines[] = (string) $client['address_line1'];
	}
	if ( ! empty( $client['address_line2'] ) ) {
		$client_lines[] = (string) $client['address_line2'];
	}

	$loc_parts = [];
	$city_state = trim( ( $client['city'] ?? '' ) . ( ! empty( $client['state'] ) ? ( ! empty( $client['city'] ) ? ', ' : '' ) . $client['state'] : '' ) );
	if ( ! empty( $client['postal_code'] ) ) {
		$city_state = trim( $city_state . ' ' . $client['postal_code'] );
	}
	if ( ! empty( $city_state ) ) {
		$loc_parts[] = $city_state;
	}
	if ( ! empty( $client['country'] ) ) {
		$loc_parts[] = (string) $client['country'];
	}
	if ( ! empty( $loc_parts ) ) {
		$client_lines[] = implode( ', ', $loc_parts );
	}

	$contact_parts = [];
	if ( ! empty( $client['email'] ) ) {
		$contact_parts[] = 'Email: ' . $client['email'];
	}
	if ( ! empty( $client['phone'] ) ) {
		$contact_parts[] = 'Phone: ' . $client['phone'];
	}
	if ( ! empty( $contact_parts ) ) {
		$client_lines[] = implode( ' · ', $contact_parts );
	}

	if ( ! empty( $client['tax_id'] ) ) {
		$client_lines[] = 'Tax ID: ' . $client['tax_id'];
	}
}

// Split shipping lines cleanly.
$shipping_lines = [];
if ( ! empty( $ship_to_address ) ) {
	$raw_ship = preg_split( '/\r\n|\r|\n/', trim( $ship_to_address ) );
	foreach ( $raw_ship as $sl ) {
		if ( '' !== trim( $sl ) ) {
			$shipping_lines[] = trim( $sl );
		}
	}
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
    @page {
        margin: 36pt 45pt 30pt 45pt;
    }
    * { box-sizing: border-box; }
    body {
        font-family: 'inter-medium', 'inter', sans-serif;
        font-size: 8.6pt;
        color: #0F172A;
        margin: 0;
        padding: 0;
    }
    table {
        border-collapse: collapse;
        border-spacing: 0;
        width: 100%;
    }
</style>
</head>
<body>

<!-- ─── 1. HEADER ─── -->
<table width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <!-- Company block — LEFT -->
        <td width="55%" valign="top" align="left" style="vertical-align: top; text-align: left;">
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <td valign="top" style="padding-right: 12pt; vertical-align: top;">
                        <?php if ( ! empty( $company_logo ) ) : ?>
                            <img src="<?php echo esc_url( $company_logo ); ?>" style="max-width: 36pt; max-height: 36pt;" alt="" />
                        <?php else : ?>
                            <table cellpadding="0" cellspacing="0">
                                <tr>
                                    <td align="center" valign="middle" style="width: 36pt; height: 36pt; background-color: <?php echo esc_attr( $accent ); ?>; color: #ffffff; text-align: center; vertical-align: middle; font-family: 'inter-extrabold', 'inter', sans-serif; font-size: 15pt; border-radius: 7.5pt;">
                                        <?php echo esc_html( strtoupper( substr( $company_name, 0, 1 ) ) ); ?>
                                    </td>
                                </tr>
                            </table>
                        <?php endif; ?>
                    </td>
                    <td valign="top" align="left" style="vertical-align: top; text-align: left;">
                        <table width="100%" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style="font-family: 'inter-extrabold', 'inter', sans-serif; font-size: 13.1pt; color: #0F172A; letter-spacing: -0.02em; line-height: 14pt; padding-bottom: 2.25pt;">
                                    <?php echo esc_html( $company_name ); ?>
                                </td>
                            </tr>
                            <?php foreach ( $company_lines as $idx => $cline ) : ?>
                                <tr>
                                    <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; line-height: 13.3pt; padding-bottom: 1.5pt;">
                                        <?php echo esc_html( $cline ); ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
        <!-- Invoice title + meta — RIGHT -->
        <td width="45%" valign="top" align="right" style="vertical-align: top; text-align: right;">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="right" style="font-family: 'inter-extrabold', 'inter', sans-serif; font-size: 22.5pt; color: #0F172A; letter-spacing: -0.025em; line-height: 22.5pt; text-align: right; padding-bottom: 7.5pt;">
                        <?php echo esc_html( strtoupper( $doc_title ) ); ?>
                    </td>
                </tr>
                <tr>
                    <td align="right" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 7.9pt; text-transform: uppercase; letter-spacing: 0.05em; color: #334155; line-height: 13pt; text-align: right; padding-bottom: 2pt;">
                        NUMBER: <span style="font-family: 'inter-extrabold', 'inter', sans-serif; color: <?php echo esc_attr( $accent ); ?>; margin-left: 4.5pt;"><?php echo esc_html( (string) ( $invoice['invoice_number'] ?? '' ) ); ?></span>
                    </td>
                </tr>
                <tr>
                    <td align="right" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 7.9pt; text-transform: uppercase; letter-spacing: 0.05em; color: #334155; line-height: 13pt; text-align: right; padding-bottom: 2pt;">
                        DATE: <span style="font-family: 'inter-bold', 'inter', sans-serif; color: #0F172A; margin-left: 4.5pt;"><?php echo esc_html( $issue_date ); ?></span>
                    </td>
                </tr>
                <tr>
                    <td align="right" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 7.9pt; text-transform: uppercase; letter-spacing: 0.05em; color: #334155; line-height: 13pt; text-align: right; padding-bottom: 2pt;">
                        <?php echo esc_html( $due_label ); ?> <span style="font-family: 'inter-bold', 'inter', sans-serif; color: #0F172A; margin-left: 4.5pt;"><?php echo esc_html( $due_date ); ?></span>
                    </td>
                </tr>
                <?php if ( 'paid' === $inv_status && ( ! $enable_watermark || empty( $watermark_text ) ) ) : ?>
                    <tr>
                        <td align="right" style="padding-top: 6pt; text-align: right;">
                            <span style="display: inline-block; background-color: #ECFDF5; color: #059669; border: 1.125pt solid #059669; border-radius: 3.75pt; padding: 2.25pt 6pt; font-family: 'inter-extrabold', 'inter', sans-serif; font-size: 7.9pt; text-transform: uppercase; letter-spacing: 0.06em; line-height: 10pt;">&#10003; PAID</span>
                        </td>
                    </tr>
                <?php endif; ?>
            </table>
        </td>
    </tr>
</table>

<!-- GAP: Header to Addresses (48px = 36pt) -->
<table width="100%" cellpadding="0" cellspacing="0"><tr><td height="36pt" style="font-size: 1pt; line-height: 1pt;">&nbsp;</td></tr></table>

<!-- ─── 2. ADDRESSES ─── -->
<table width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="<?php echo $has_shipping ? '48%' : '100%'; ?>" valign="top" style="border-top: 1px solid #CBD5E1; padding-top: 7.5pt; vertical-align: top;">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; text-transform: uppercase; letter-spacing: 0.08em; color: #334155; padding-bottom: 6pt;">
                        <?php echo ( 'estimate' === $doc_type ) ? 'PREPARED FOR' : 'BILL TO'; ?>
                    </td>
                </tr>
                <?php if ( $client ) : ?>
                    <tr>
                        <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 10.9pt; color: #0F172A; letter-spacing: -0.01em; line-height: 13pt; padding-bottom: 3pt;">
                            <?php echo esc_html( (string) ( $client['name'] ?? '' ) ); ?>
                        </td>
                    </tr>
                    <?php foreach ( $client_lines as $idx => $cline ) : ?>
                        <tr>
                            <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; line-height: 13.3pt; padding-bottom: 1.5pt;">
                                <?php echo esc_html( $cline ); ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </table>
        </td>
        <?php if ( $has_shipping ) : ?>
            <td width="4%"></td>
            <td width="48%" valign="top" style="border-top: 1px solid #CBD5E1; padding-top: 7.5pt; vertical-align: top;">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; text-transform: uppercase; letter-spacing: 0.08em; color: #334155; padding-bottom: 6pt;">
                            SHIP TO
                        </td>
                    </tr>
                    <tr>
                        <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 10.9pt; color: #0F172A; letter-spacing: -0.01em; line-height: 13pt; padding-bottom: 3pt;">
                            <?php echo esc_html( $ship_to_name ); ?>
                        </td>
                    </tr>
                    <?php foreach ( $shipping_lines as $idx => $sline ) : ?>
                        <tr>
                            <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; line-height: 13.3pt; padding-bottom: 1.5pt;">
                                <?php echo esc_html( $sline ); ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </table>
            </td>
        <?php endif; ?>
    </tr>
</table>

<!-- GAP: Addresses to Line Items (44px = 33pt) -->
<table width="100%" cellpadding="0" cellspacing="0"><tr><td height="33pt" style="font-size: 1pt; line-height: 1pt;">&nbsp;</td></tr></table>

<!-- ─── 3. LINE ITEMS TABLE ─── -->
<table width="100%" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th width="52%" align="left"   style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; color: #334155; text-transform: uppercase; letter-spacing: 0.06em; padding: 7.5pt 10.5pt; background-color: #F8FAFC; border-top: 1px solid #CBD5E1; border-bottom: 1px solid #CBD5E1; text-align: left;">Description</th>
            <th width="12%" align="center" style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; color: #334155; text-transform: uppercase; letter-spacing: 0.06em; padding: 7.5pt 10.5pt; background-color: #F8FAFC; border-top: 1px solid #CBD5E1; border-bottom: 1px solid #CBD5E1; text-align: center;">Qty</th>
            <th width="18%" align="right"  style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; color: #334155; text-transform: uppercase; letter-spacing: 0.06em; padding: 7.5pt 10.5pt; background-color: #F8FAFC; border-top: 1px solid #CBD5E1; border-bottom: 1px solid #CBD5E1; text-align: right;">Rate</th>
            <th width="18%" align="right"  style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; color: #334155; text-transform: uppercase; letter-spacing: 0.06em; padding: 7.5pt 10.5pt; background-color: #F8FAFC; border-top: 1px solid #CBD5E1; border-bottom: 1px solid #CBD5E1; text-align: right;">Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ( $line_items as $item ) :
            $item_sku = (string) $zinv_field( $item, 'sku', '' );
            $raw_qty  = $zinv_field( $item, 'quantity', 0 );
        ?>
            <tr>
                <td align="left" valign="middle" style="padding: 10.5pt 10.5pt; text-align: left; border-bottom: 1px solid #F1F5F9; vertical-align: middle;">
                    <table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 9.4pt; color: #0F172A; line-height: 12pt; text-align: left;">
                                <?php echo esc_html( (string) $zinv_field( $item, 'description', '' ) ); ?>
                            </td>
                        </tr>
                        <?php if ( ! empty( $item_sku ) ) : ?>
                            <tr>
                                <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 7.5pt; color: #475569; line-height: 9pt; padding-top: 1.5pt; text-align: left;">
                                    <?php echo esc_html( $item_sku ); ?>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </table>
                </td>
                <td align="center" valign="middle" style="padding: 10.5pt 10.5pt; text-align: center; font-family: 'inter-bold', 'inter', sans-serif; font-size: 9pt; color: #0F172A; border-bottom: 1px solid #F1F5F9; vertical-align: middle;"><?php echo esc_html( $clean_qty( $raw_qty ) ); ?></td>
                <td align="right"  valign="middle" style="padding: 10.5pt 10.5pt; text-align: right;  font-family: 'inter-medium', 'inter', sans-serif; font-size: 9pt; color: #0F172A; border-bottom: 1px solid #F1F5F9; vertical-align: middle;"><?php echo esc_html( ZINV_PDF_Generator::format_money( (string) $zinv_field( $item, 'unit_price', '0' ), $currency ) ); ?></td>
                <td align="right"  valign="middle" style="padding: 10.5pt 10.5pt; text-align: right;  font-family: 'inter-bold', 'inter', sans-serif; font-size: 9pt; color: #0F172A; border-bottom: 1px solid #F1F5F9; vertical-align: middle;"><?php echo esc_html( ZINV_PDF_Generator::format_money( (string) $zinv_field( $item, 'total', '0' ), $currency ) ); ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<!-- GAP: Line Items to Totals (36px = 27pt) -->
<table width="100%" cellpadding="0" cellspacing="0"><tr><td height="27pt" style="font-size: 1pt; line-height: 1pt;">&nbsp;</td></tr></table>

<!-- ─── 4. TOTALS ─── -->
<?php
$subtotal       = (string) ( $invoice['subtotal'] ?? '0' );
$tax_total      = (string) ( $invoice['tax_total'] ?? '0' );
$discount_total = (string) ( $invoice['discount_total'] ?? '0' );
$shipping_total = (string) ( $invoice['shipping_total'] ?? '0' );
$amount_due     = isset( $invoice['amount_due'] ) ? (string) $invoice['amount_due'] : (string) ( $invoice['total'] ?? '0' );
?>
<table width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="63%"></td>
        <td width="37%" valign="top">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="left"  style="padding: 3pt 0; font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #334155; line-height: 11pt;">Subtotal</td>
                    <td align="right" style="padding: 3pt 0; font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; text-align: right; line-height: 11pt;"><?php echo esc_html( ZINV_PDF_Generator::format_money( $subtotal, $currency ) ); ?></td>
                </tr>
                <?php if ( is_numeric( $discount_total ) && bccomp( $discount_total, '0', 4 ) > 0 ) : ?>
                    <tr>
                        <td align="left"  style="padding: 3pt 0; font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #334155; line-height: 11pt;">Discount</td>
                        <td align="right" style="padding: 3pt 0; font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.6pt; color: #E11D48; text-align: right; line-height: 11pt;">-<?php echo esc_html( ZINV_PDF_Generator::format_money( $discount_total, $currency ) ); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if ( is_numeric( $shipping_total ) && bccomp( $shipping_total, '0', 4 ) > 0 ) : ?>
                    <tr>
                        <td align="left"  style="padding: 3pt 0; font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #334155; line-height: 11pt;">Shipping</td>
                        <td align="right" style="padding: 3pt 0; font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; text-align: right; line-height: 11pt;"><?php echo esc_html( ZINV_PDF_Generator::format_money( $shipping_total, $currency ) ); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if ( is_numeric( $tax_total ) && bccomp( $tax_total, '0', 4 ) > 0 ) : ?>
                    <tr>
                        <td align="left"  style="padding: 3pt 0; font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #334155; line-height: 11pt;">Tax</td>
                        <td align="right" style="padding: 3pt 0; font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; text-align: right; line-height: 11pt;"><?php echo esc_html( ZINV_PDF_Generator::format_money( $tax_total, $currency ) ); ?></td>
                    </tr>
                <?php endif; ?>
                <!-- Total Due Spacer + Row -->
                <tr>
                    <td colspan="2" height="6pt" style="font-size: 1pt; line-height: 1pt;">&nbsp;</td>
                </tr>
                <tr>
                    <td align="left"  style="border-top: 2px solid #0F172A; padding-top: 7.5pt; font-family: 'inter-bold', 'inter', sans-serif; font-size: 10.5pt; color: #0F172A; letter-spacing: -0.01em; line-height: 14pt;"><?php echo ( 'estimate' === $doc_type ) ? 'Estimated Total' : 'Total Due'; ?></td>
                    <td align="right" style="border-top: 2px solid #0F172A; padding-top: 7.5pt; font-family: 'inter-extrabold', 'inter', sans-serif; font-size: 13.5pt; color: #0F172A; letter-spacing: -0.02em; text-align: right; line-height: 14pt;"><?php echo esc_html( ZINV_PDF_Generator::format_money( $amount_due, $currency ) ); ?></td>
                </tr>
            </table>
        </td>
    </tr>
</table>

<!-- GAP: Totals to Bottom Section (44px = 33pt) -->
<table width="100%" cellpadding="0" cellspacing="0"><tr><td height="33pt" style="font-size: 1pt; line-height: 1pt;">&nbsp;</td></tr></table>

<!-- ─── 5. PAYMENT INSTRUCTIONS & NOTES ─── -->
<?php
$has_notes_or_terms = ! empty( $invoice['notes'] ) || ! empty( $invoice['terms'] );
if ( $has_bank_info || $has_notes_or_terms ) :
?>
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <?php if ( $has_bank_info && $has_notes_or_terms ) : ?>
                <td width="48%" valign="top" style="border-top: 1px solid #CBD5E1; padding-top: 15pt; vertical-align: top;">
                    <table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; text-transform: uppercase; letter-spacing: 0.08em; color: #334155; padding-bottom: 6pt;">
                                PAYMENT INSTRUCTIONS
                            </td>
                        </tr>
                        <?php if ( ! empty( $bank_name ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Bank:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_name ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_routing ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Routing:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_routing ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_account ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Account No:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_account ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_swift ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">SWIFT / BIC:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_swift ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_holder ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Beneficiary:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_holder ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $manual_instr ) ) : ?>
                            <tr>
                                <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; line-height: 13.3pt; padding-top: 4.5pt;">
                                    <?php echo nl2br( esc_html( $manual_instr ) ); ?>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </table>
                </td>
                <td width="4%"></td>
                <td width="48%" valign="top" style="border-top: 1px solid #CBD5E1; padding-top: 15pt; vertical-align: top;">
                    <table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; text-transform: uppercase; letter-spacing: 0.08em; color: #334155; padding-bottom: 6pt;">
                                NOTES &amp; TERMS
                            </td>
                        </tr>
                        <?php if ( ! empty( $invoice['notes'] ) ) : ?>
                            <tr>
                                <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; line-height: 13.3pt; padding-bottom: 4.5pt;">
                                    <?php echo nl2br( esc_html( (string) $invoice['notes'] ) ); ?>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $invoice['terms'] ) ) : ?>
                            <tr>
                                <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 7.9pt; color: #334155; line-height: 12.0pt;">
                                    <span style="font-family: 'inter-bold', 'inter', sans-serif;">Terms:</span> <?php echo nl2br( esc_html( (string) $invoice['terms'] ) ); ?>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </table>
                </td>
            <?php elseif ( $has_bank_info ) : ?>
                <td width="100%" valign="top" style="border-top: 1px solid #CBD5E1; padding-top: 15pt; vertical-align: top;">
                    <table width="50%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; text-transform: uppercase; letter-spacing: 0.08em; color: #334155; padding-bottom: 6pt;">
                                PAYMENT INSTRUCTIONS
                            </td>
                        </tr>
                        <?php if ( ! empty( $bank_name ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Bank:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_name ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_routing ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Routing:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_routing ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_account ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Account No:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_account ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_swift ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">SWIFT / BIC:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_swift ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $bank_holder ) ) : ?>
                            <tr>
                                <td style="padding-bottom: 2.25pt;">
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="34%" style="font-family: 'inter-semibold', 'inter', sans-serif; font-size: 8.25pt; color: #334155; line-height: 11pt;">Beneficiary:</td>
                                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 8.25pt; color: #0F172A; line-height: 11pt;"><?php echo esc_html( $bank_holder ); ?></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </table>
                </td>
            <?php else : ?>
                <td width="100%" valign="top" style="border-top: 1px solid #CBD5E1; padding-top: 15pt; vertical-align: top;">
                    <table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="font-family: 'inter-bold', 'inter', sans-serif; font-size: 7.1pt; text-transform: uppercase; letter-spacing: 0.08em; color: #334155; padding-bottom: 6pt;">
                                NOTES &amp; TERMS
                            </td>
                        </tr>
                        <?php if ( ! empty( $invoice['notes'] ) ) : ?>
                            <tr>
                                <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 8.6pt; color: #0F172A; line-height: 13.3pt; padding-bottom: 4.5pt;">
                                    <?php echo nl2br( esc_html( (string) $invoice['notes'] ) ); ?>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php if ( ! empty( $invoice['terms'] ) ) : ?>
                            <tr>
                                <td style="font-family: 'inter-medium', 'inter', sans-serif; font-size: 7.9pt; color: #334155; line-height: 12.0pt;">
                                    <span style="font-family: 'inter-bold', 'inter', sans-serif;">Terms:</span> <?php echo nl2br( esc_html( (string) $invoice['terms'] ) ); ?>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </table>
                </td>
            <?php endif; ?>
        </tr>
    </table>
<?php endif; ?>

<!-- GAP: Bottom Section to Footer (36px = 27pt) -->
<table width="100%" cellpadding="0" cellspacing="0"><tr><td height="27pt" style="font-size: 1pt; line-height: 1pt;">&nbsp;</td></tr></table>

<!-- ─── 6. FOOTER ─── -->
<?php if ( $show_footer ) : ?>
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td align="center" style="border-top: 1px solid #E2E8F0; padding-top: 12pt; text-align: center; font-family: 'inter-medium', 'inter', sans-serif; font-size: 7.1pt; color: #475569; text-transform: uppercase; letter-spacing: 0.15em; line-height: 10pt;">
                <?php
                $footer_text = (string) ZINV_Settings::get( 'footer_text', '' );
                if ( empty( $footer_text ) ) {
                    $footer_text = 'Thank you for your business · ' . $company_name;
                }
                echo esc_html( $footer_text );
                ?>
            </td>
        </tr>
    </table>
<?php endif; ?>

</body>
</html>
