<?php

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

/**
 * 請求書 PDF テンプレート
 *
 * @var array $invoice      請求書データ
 * @var array $payment      決済データ
 * @var array $registration 申込データ
 * @var array $event        イベントデータ
 */
?>
<h1>請 求 書</h1>

<table class="header" cellpadding="0" cellspacing="0">
	<tr>
		<td class="header-left">
			<div class="addressee">
				<?php echo esc_html( $invoice['addressee_name'] ); ?>
				<?php if ( ! empty( $invoice['addressee_dept'] ) ) : ?>
					<br><?php echo esc_html( $invoice['addressee_dept'] ); ?>
				<?php endif; ?>
				<span class="honorific"><?php echo esc_html( $invoice['honorific'] ?? '御中' ); ?></span>
			</div>
			<p style="font-size: 10pt; color: #666; margin-top: 10px;">請求書番号: <?php echo esc_html( $invoice['invoice_no'] ); ?></p>
		</td>
		<td class="header-right">
			<p>発行日: <?php echo esc_html( wp_date( 'Y年n月j日', strtotime( $invoice['issued_at'] ) ) ); ?></p>
			<?php if ( ! empty( $event['organizer_info'] ) ) : ?>
				<p style="margin-top: 10px; font-size: 10pt; color: #333;"><?php echo nl2br( esc_html( $event['organizer_info'] ) ); ?></p>
			<?php endif; ?>
		</td>
	</tr>
</table>

<table class="detail">
	<tr>
		<th>イベント名</th>
		<td><?php echo esc_html( $event['title'] ?? '' ); ?></td>
	</tr>
	<tr>
		<th>開催日</th>
		<td><?php echo esc_html( $event ? wp_date( 'Y年n月j日', strtotime( $event['date_start'] ) ) : '' ); ?></td>
	</tr>
	<tr>
		<th>登録番号</th>
		<td><?php echo esc_html( $registration['registration_no'] ?? '' ); ?></td>
	</tr>
	<tr>
		<th>金額（税抜）</th>
		<td>&yen;<?php echo number_format( (int) ( $payment['amount'] ?? 0 ) ); ?></td>
	</tr>
	<tr>
		<th>消費税（10%）</th>
		<td>&yen;<?php echo number_format( (int) ( $payment['tax'] ?? 0 ) ); ?></td>
	</tr>
	<tr class="amount-row">
		<th>合計金額</th>
		<td>&yen;<?php echo number_format( (int) ( $payment['amount'] ?? 0 ) + (int) ( $payment['tax'] ?? 0 ) ); ?></td>
	</tr>
</table>

<table class="detail">
	<tr>
		<th>お支払方法</th>
		<td>
			<?php
			$conf_method_labels = [
				'stripe' => __( 'Credit Card (Stripe)', 'conferencemanager' ),
				'bank'   => __( 'Bank Transfer', 'conferencemanager' ),
			];
			echo esc_html( $conf_method_labels[ $payment['method'] ?? '' ] ?? __( 'Other', 'conferencemanager' ) );
			?>
		</td>
	</tr>
	<?php if ( ! empty( $payment['paid_at'] ) ) : ?>
	<tr>
		<th>お支払日</th>
		<td><?php echo esc_html( wp_date( 'Y年n月j日', strtotime( $payment['paid_at'] ) ) ); ?></td>
	</tr>
	<?php endif; ?>
</table>

<?php if ( ( $payment['method'] ?? '' ) === 'bank' && ( ! empty( $event['payment_account_name'] ) || ! empty( $event['payment_account_info'] ) ) ) : ?>
<div style="margin-top: 30px; padding: 15px; border: 1px solid #ccc; background: #fafafa;">
	<h3 style="margin: 0 0 10px; font-size: 12pt;">お振込先</h3>
	<?php if ( ! empty( $event['payment_account_name'] ) ) : ?>
		<p style="font-weight: bold; margin: 0 0 5px;"><?php echo esc_html( $event['payment_account_name'] ); ?></p>
	<?php endif; ?>
	<?php if ( ! empty( $event['payment_account_info'] ) ) : ?>
		<p style="margin: 0;"><?php echo nl2br( esc_html( $event['payment_account_info'] ) ); ?></p>
	<?php endif; ?>
</div>
<?php endif; ?>

<?php if ( ! empty( $invoice['tax_registration_no'] ) ) : ?>
<p class="tax-no">適格請求書発行事業者登録番号: <?php echo esc_html( $invoice['tax_registration_no'] ); ?></p>
<?php endif; ?>

<div class="footer">
	<p>本請求書は Conference Manager により自動生成されました。</p>
</div>
