<?php

// Use safe PDF creation to prevent conflicts with other plugins
$receipt = wpgv_create_pdf_safe('P', 'pt', array(595, 900));
$receipt->SetAutoPageBreak(0);
$receipt->AddPage();
// Ensure $buyingfor is defined (may be passed from different callers as GET or POST)
if (!isset($buyingfor)) {
	if (isset($_GET['buyingfor'])) {
		$buyingfor = sanitize_text_field(base64_decode($_GET['buyingfor']));
	} elseif (isset($_GET['buying_for'])) {
		$buyingfor = sanitize_text_field(base64_decode($_GET['buying_for']));
	} else {
		$buyingfor = 'someone_else';
	}
}
if (get_locale() == "el") {
	wpgv_add_font_safe($receipt, 'Arial_Greek', '', 'Arial_Greek.php');
	$receipt->SetFont('Arial_Greek', '', 14);
} elseif (get_locale() == "cs_CZ") {
	wpgv_add_font_safe($receipt, 'arial_cz', '', 'arial.php');
	$receipt->SetFont('arial_cz', '', 14);
}

$receipt->SetTextColor(0, 0, 0);

//Title
$receipt->SetXY(30, 50);
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B', 16);
}
$receipt->SetFontSize(20);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->MultiCell(0, 0, wpgv_em_cs_CZ(__('Customer Receipt', 'gift-voucher')), 0, 'C');
} else {
	$receipt->MultiCell(0, 0, wpgv_em(__('Customer Receipt', 'gift-voucher')), 0, 'C');
}


$receipt->SetFontSize(12);

//Company Name
if (get_locale() != "el") {
	if (get_locale() != "el") {
		$receipt->SetFont('Arial', 'B');
	}
}
$receipt->SetXY(30, 100);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Company Name', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Company Name', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 100);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($setting_options->company_name), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, ' ' . wpgv_em($setting_options->company_name), 0, 1, 'L', 0);
}


//Company Email
if (get_locale() != "el") {
	if (get_locale() != "el") {
		$receipt->SetFont('Arial', 'B');
	}
}
$receipt->SetXY(30, 120);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Company Email', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Company Email', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 120);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($setting_options->pdf_footer_email), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, ' ' . wpgv_em($setting_options->pdf_footer_email), 0, 1, 'L', 0);
}


//Company Website
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 140);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Company Website', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Company Website', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 140);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($setting_options->pdf_footer_url), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, ' ' . wpgv_em($setting_options->pdf_footer_url), 0, 1, 'L', 0);
}


//Order Number
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 160);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Order Number', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Order Number', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 160);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__(' #' . $lastid, 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__(' #' . $lastid, 'gift-voucher')), 0, 1, 'L', 0);
}


//Order Date
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 180);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Order Date', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Order Date', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 180);
$receipt->Cell(0, 0, ' ' . date_i18n('d.m.Y'), 0, 1, 'L', 0);

//For
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 200);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Your Name', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Your Name', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 200);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($for), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, ' ' . wpgv_em($for), 0, 1, 'L', 0);
}


//From
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 220);
if ($buyingfor != 'yourself') {
	if (get_locale() == "cs_CZ") {
		$receipt->SetFont('arial_cz', '', 16);
		$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Recipient Name', 'gift-voucher')), 0, 1, 'L', 0);
	} else {
		$receipt->Cell(0, 0, wpgv_em(__('Recipient Name', 'gift-voucher')), 0, 1, 'L', 0);
	}

	$receipt->SetFont('Arial', '');
	$receipt->SetXY(250, 220);
	if (get_locale() == "cs_CZ") {
		$receipt->SetFont('arial_cz', '', 16);
		$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($from), 0, 1, 'L', 0);
	} else {
		$receipt->Cell(0, 0, ' ' . wpgv_em($from), 0, 1, 'L', 0);
	}
}

//Email
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 240);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Email', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Email', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 240);

$receipt->Cell(0, 0, ' ' . wpgv_em($email), 0, 1, 'L', 0);

//Amount
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 260);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Amount', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Amount', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 260);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, ' ' . wpgv_price_format_cs_CZ($value), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, ' ' . iconv('utf-8', 'cp1252', wpgv_price_format($value)), 0, 1, 'L', 0);
}


//Coupon Code
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 280);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Coupon Code', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Coupon Code', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 280);
$receipt->Cell(0, 0, ' ' . wpgv_em($code), 0, 1, 'L', 0);

//Coupon Expiry date
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 300);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Coupon Expiry date', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Coupon Expiry date', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 300);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($expiry), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, ' ' . wpgv_em($expiry), 0, 1, 'L', 0);
}


//Payment Method
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 320);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Payment Method', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Payment Method', 'gift-voucher')), 0, 1, 'L', 0);
}


$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 320);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($paytext), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, ' ' . wpgv_em($paytext), 0, 1, 'L', 0);
}


//Payment Status
if (get_locale() != "el") {
	$receipt->SetFont('Arial', 'B');
}
$receipt->SetXY(30, 340);
if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	$receipt->Cell(0, 0, wpgv_em_cs_CZ(__('Payment Status', 'gift-voucher')), 0, 1, 'L', 0);
} else {
	$receipt->Cell(0, 0, wpgv_em(__('Payment Status', 'gift-voucher')), 0, 1, 'L', 0);
}

$receipt->SetFont('Arial', '');
$receipt->SetXY(250, 340);

if (get_locale() == "cs_CZ") {
	$receipt->SetFont('arial_cz', '', 16);
	// Use provided payment status if available, otherwise fall back to voucher record or 'Not Paid'
	$display_status = (isset($payment_status) && $payment_status != '') ? $payment_status : (isset($voucher_options->payment_status) ? $voucher_options->payment_status : __('Not Paid', 'gift-voucher'));
	$receipt->Cell(0, 0, ' ' . wpgv_em_cs_CZ($display_status), 0, 1, 'L', 0);
} else {
	$display_status = (isset($payment_status) && $payment_status != '') ? $payment_status : (isset($voucher_options->payment_status) ? $voucher_options->payment_status : __('Not Paid', 'gift-voucher'));
	$receipt->Cell(0, 0, ' ' . wpgv_em($display_status), 0, 1, 'L', 0);
}
