<?php defined('BASEPATH') or exit('No direct script access allowed');

$dimensions = $pdf->getPageDimensions();

$info_left_column  = '';
$info_right_column = '';

// Left: company logo (same as invoice)
$info_left_column .= pdf_logo_url();

// Right: RECEIPT heading + reference + date (styled like invoice)
$info_right_column .= '<span style="font-weight:bold;font-size:27px;">' . "Official Receipt" . '</span><br />';

// Show “for invoice #XXXX”
$info_right_column .= '<b style="color:#4e4e4e;">' . _l('payment_for_string') . ' # ' . format_invoice_number($payment->invoice_data->id) . '</b>';

// Payment date (like invoice shows issue/due)
$info_right_column .= '<br />' . _l('payment_date') . ' ' . _d($payment->date);

// Write top row (logo + right info)
pdf_multi_row($info_left_column, $info_right_column, $pdf, ($dimensions['wk'] / 2) - $dimensions['lm']);

$pdf->Ln(1);

// Organization info (left) vs Bill To (right) — same pattern as invoice
$organization_info = '<div style="color:#424242;">' . format_organization_info() . '</div>';

$client_details = '<b>' . _l('invoice_bill_to') . ':</b>';
$client_details .= '<div style="color:#424242;">';
$client_details .= format_customer_info($payment->invoice_data, 'payment', 'billing');
$client_details .= '</div>';

$left_info  = $swap == '1' ? $client_details : $organization_info;
$right_info = $swap == '1' ? $organization_info : $client_details;

pdf_multi_row($right_info, $left_info, $pdf, ($dimensions['wk'] / 2) - $dimensions['lm']);

// Spacing before summary table
$pdf->Ln(hooks()->apply_filters('pdf_info_and_table_separator', 6));

// ===== Payment Summary table (styled like invoice tables) =====
$heading_row_bg_color  = !empty(getPdfOptions('payment', 'items_table', 'heading_row_bg_color')) ? getPdfOptions('payment', 'items_table', 'heading_row_bg_color') : '#3A4656';
$heading_row_text_color= !empty(getPdfOptions('payment', 'items_table', 'heading_row_text_color')) ? getPdfOptions('payment', 'items_table', 'heading_row_text_color') : '#ffffff';
$row_bg_color          = !empty(getPdfOptions('payment', 'items_table', 'row_bg_color')) ? getPdfOptions('payment', 'items_table', 'row_bg_color') : '#ffffff';
$row_text_color        = !empty(getPdfOptions('payment', 'items_table', 'row_text_color')) ? getPdfOptions('payment', 'items_table', 'row_text_color') : '#000000';

// Whether to show “Amount Due” column
$amountDue = isset($payment->invoice_data->total_left_to_pay) && (float)$payment->invoice_data->total_left_to_pay > 0;

// Build table
$tbl = '<table width="100%" bgcolor="#fff" cellspacing="0" cellpadding="5" border="0">';

// Header row
$tbl .= '<tr height="30" style="color:' . $heading_row_text_color . ';" bgcolor="' . $heading_row_bg_color . '">';
$tbl .= '<th width="' . ($amountDue ? 20 : 25) . '%;">' . _l('payment_table_invoice_number') . '</th>';
$tbl .= '<th width="' . ($amountDue ? 20 : 25) . '%;">' . _l('payment_table_invoice_date') . '</th>';
$tbl .= '<th width="' . ($amountDue ? 20 : 25) . '%;">' . _l('payment_table_invoice_amount_total') . '</th>';
$tbl .= '<th width="' . ($amountDue ? 20 : 25) . '%;">' . _l('payment_table_payment_amount_total') . '</th>';
if ($amountDue) {
    $tbl .= '<th width="20%">' . _l('invoice_amount_due') . '</th>';
}
$tbl .= '</tr>';

// Single row
$tbl .= '<tbody>';
$tbl .= '<tr style="color:' . $row_text_color . ';" bgcolor="' . $row_bg_color . '">';
$tbl .= '<td>' . format_invoice_number($payment->invoice_data->id) . '</td>';
$tbl .= '<td>' . _d($payment->invoice_data->date) . '</td>';
$tbl .= '<td>' . app_format_money($payment->invoice_data->total, $payment->invoice_data->currency_name) . '</td>';
$tbl .= '<td>' . app_format_money($payment->amount, $payment->invoice_data->currency_name) . '</td>';
if ($amountDue) {
    $tbl .= '<td style="color:#fc2d42">' . app_format_money($payment->invoice_data->total_left_to_pay, $payment->invoice_data->currency_name) . '</td>';
}
$tbl .= '</tr>';
$tbl .= '</tbody>';
$tbl .= '</table>';

$pdf->writeHTML($tbl, true, false, false, false, '');

// Below-table details (styled to match your invoice’s simple rows)


$payment_name = $payment->name;
if (!empty($payment->paymentmethod)) {
    $payment_name .= ' - ' . $payment->paymentmethod;
}

//$pdf->Cell(0, 0, $payment_name, 0, 1, 'L', 0, '', 0);
$pdf->Ln(6);
$pdf->SetFont($font_name, 'B', $font_size);
$pdf->Cell(0, 0, _l('payment_view_mode') . ' ' . $payment_name, 0, 1, 'L', 0, '', 0);
$pdf->SetFont($font_name, '', $font_size);

if (!empty($payment->transactionid)) {
    $pdf->Ln(4);
    $pdf->SetFont($font_name, 'B', $font_size);
    $pdf->Cell(0, 0, _l('payment_transaction_id') . ': ' . $payment->transactionid, 0, 1, 'L', 0, '', 0);
    $pdf->SetFont($font_name, '', $font_size);
}

// Total amount block – mimic the invoice “total” style
$pdf->Ln(8);
$total_bg_color  = !empty(getPdfOptions('payment', 'items_table', 'total_row_bg_color')) ? getPdfOptions('payment', 'items_table', 'total_row_bg_color') : '#f0f0f0';
$total_text_color= !empty(getPdfOptions('payment', 'items_table', 'total_row_text_color')) ? getPdfOptions('payment', 'items_table', 'total_row_text_color') : '#000000';
$tot = '<table cellpadding="6" style="font-size:' . ($font_size + 2) . 'px">';
$tot .= '<tr style="background-color:' . $total_bg_color . '; color:' . $total_text_color . '">';
$tot .= '<td align="right" width="85%"><strong>' . _l('payment_total_amount') . '</strong></td>';
$tot .= '<td align="right" width="15%">' . app_format_money($payment->amount, $payment->invoice_data->currency_name) . '</td>';
$tot .= '</tr>';
$tot .= '</table>';
$pdf->writeHTML($tot, true, false, false, false, '');

// Optional: client note from invoice (same behavior as invoice)
if (!empty($payment->invoice_data->clientnote)) {
    $pdf->Ln(4);
    $pdf->SetFont($font_name, 'B', $font_size);
    $pdf->Cell(0, 0, _l('invoice_note'), 0, 1, 'L', 0, '', 0);
    $pdf->SetFont($font_name, '', $font_size);
    $pdf->Ln(2);
    $pdf->writeHTMLCell('', '', '', '', $payment->invoice_data->clientnote, 0, 1, false, true, 'L', true);
}

// Optional: invoice terms reused on receipt (if you want it)
if (!empty($payment->invoice_data->terms)) {
    $pdf->Ln(4);
    $pdf->SetFont($font_name, 'B', $font_size);
    $pdf->Cell(0, 0, _l('terms_and_conditions') . ':', 0, 1, 'L', 0, '', 0);
    $pdf->SetFont($font_name, '', $font_size);
    $pdf->Ln(2);
    $pdf->writeHTMLCell('', '', '', '', $payment->invoice_data->terms, 0, 1, false, true, 'L', true);
}
