<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$footer_text        = gigneo_option( '_gigneo_ts_invoice_footer_text', '' );
$invoice_logo       = gigneo_option( '_gigneo_ts_invoice_logo' );
$invoice_logo_url   = ! empty( $invoice_logo ) ? wp_get_attachment_url( $invoice_logo ) : '';
$invoice_logo_url   = ! empty( $invoice_logo_url ) ? $invoice_logo_url : GIGNEO_DIR_URL . 'assets/img/logo-dark.png';
$country_state_list = gigneo_countries_with_states();
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title><?php echo esc_html__( 'Invoice', 'gigneo' ); ?></title>
	<?php wp_print_styles( 'gigneo-invoice-style' ); ?>
</head>
<body>
<div class="gn-invoice-container">
	<div class="gn-invoice-header">
		<div class="gn-invoice-title"><?php echo esc_html__( 'Invoice', 'gigneo' ); ?></div>
		<img src="<?php echo esc_url( $invoice_logo_url ); ?>" alt="<?php echo esc_attr__( 'Invoice', 'gigneo' ); ?>">
	</div>
	<div class="gn-invoice-details">
		<div class="gn-invoice-id">
			<span class="gn-title"><?php echo esc_html__( 'Invoice No:', 'gigneo' ); ?></span>
			<span class="gn-amount"><?php echo esc_html( $data['invoice_id'] ) ?? ''; ?></span>
		</div>
		<?php if ( ! empty( $data['order_id'] ) ) { ?>
			<div class="gn-order-id">
				<span class="gn-title"><?php echo esc_html__( 'Order No:', 'gigneo' ); ?></span>
				<span class="gn-amount"><?php echo esc_html( $data['order_id'] ); ?></span>
			</div>
		<?php } ?>
		<?php if ( ! empty( $data['issue_date'] ) ) { ?>
			<div class="gn-date">
				<span class="gn-title"><?php echo esc_html__( 'Issue Date:', 'gigneo' ); ?></span>
				<span class="gn-amount"><?php echo esc_html( $data['issue_date'] ); ?></span>
			</div>
		<?php } ?>
		<?php if ( ! empty( $data['timezone'] ) ) { ?>
			<div class="gn-date">
				<span class="gn-title"><?php echo esc_html__( 'Timezone:', 'gigneo' ); ?></span>
				<span class="gn-amount"><?php echo esc_html( $data['timezone'] ); ?></span>
			</div>
		<?php } ?>
		<?php if ( ! empty( $data['invoice_status'] ) ) { ?>
			<div class="gn-invoice-status">
				<span class="gn-title"><?php echo esc_html__( 'Status:', 'gigneo' ); ?></span>
				<span class="gn-status-tag <?php echo esc_attr( gigneo_status_class( $data['invoice_status'] ) ); ?>"><?php echo esc_html( gigneo_status_label( $data['invoice_status'] ) ); ?></span>
			</div>
		<?php } ?>
	</div>
	<div class="gn-invoice-billing">
		<div class="gn-invoice-billing-from">
			<div class="gn-title"><?php echo esc_html__( 'Billed From:', 'gigneo' ); ?></div>
			<div class="gn-details">
				<?php if ( ! empty( $data['from']['name'] ) ) { ?>
					<h5 class="gn-name"><?php echo esc_html( $data['from']['name'] ); ?></h5>
					<?php
				}
				if ( ! empty( $data['from']['country_state'] ) && isset( $country_state_list[ $data['from']['country_state'] ] ) ) {
					?>
					<p class="gn-country-state"><?php echo esc_html( $country_state_list[ $data['from']['country_state'] ] ); ?></p>
					<?php
				}
				if ( ! empty( $data['from']['address'] ) ) {
					?>
					<p class="gn-country-state"><?php echo esc_html( $data['from']['address'] ); ?></p>
					<?php
				}
				if ( ! empty( $data['from']['email'] ) ) {
					?>
					<p class="gn-email"><?php echo esc_html( $data['from']['email'] ); ?></p>
					<?php
				}
				if ( ! empty( $data['from']['phone'] ) ) {
					?>
					<p class="gn-email"><?php echo esc_html( $data['from']['phone'] ); ?></p>
				<?php } ?>
			</div>
		</div>
		<div class="gn-invoice-billing-to">
			<div class="gn-title"><?php echo esc_html__( 'Billed To:', 'gigneo' ); ?></div>
			<div class="gn-details">
				<?php if ( ! empty( $data['to']['name'] ) ) { ?>
					<h5 class="gn-name"><?php echo esc_html( $data['to']['name'] ); ?></h5>
					<?php
				}
				if ( ! empty( $data['to']['country_state'] ) && isset( $country_state_list[ $data['to']['country_state'] ] ) ) {
					?>
					<p class="gn-country-state"><?php echo esc_html( $country_state_list[ $data['to']['country_state'] ] ); ?></p>
					<?php
				}
				if ( ! empty( $data['to']['address'] ) ) {
					?>
					<p class="gn-country-state"><?php echo esc_html( $data['to']['address'] ); ?></p>
					<?php
				}
				if ( ! empty( $data['to']['email'] ) ) {
					?>
					<p class="gn-email"><?php echo esc_html( $data['to']['email'] ); ?></p>
					<?php
				}
				if ( ! empty( $data['to']['phone'] ) ) {
					?>
					<p class="gn-email"><?php echo esc_html( $data['to']['phone'] ); ?></p>
				<?php } ?>
			</div>
		</div>
	</div>
	<table class="gn-invoice-table">
		<thead>
		<tr>
			<th class="gn-title"><?php echo esc_html__( 'Title', 'gigneo' ); ?></th>
			<?php if ( 'commission' !== $data['type'] ) { ?>
				<th class="gn-quantity"><?php echo esc_html__( 'Quantity', 'gigneo' ); ?></th>
				<th class="gn-price"><?php echo esc_html__( 'Unit Price', 'gigneo' ); ?></th>
			<?php } ?>
			<th class="gn-amount"><?php echo esc_html__( 'Amount', 'gigneo' ); ?></th>
		</tr>
		</thead>
		<tbody>
		<?php
		if ( ! empty( $data['items'] ) ) {
			foreach ( $data['items'] as $item ) {
				?>
				<tr>
					<td class="gn-title"><?php echo esc_html( $item['title'] ?? '' ); ?></td>
					<?php if ( 'commission' !== $data['type'] ) { ?>
						<td class="gn-quantity"><?php echo esc_html( $item['quantity'] ?? '' ); ?></td>
						<td class="gn-amount"><?php echo esc_html( $item['amount'] ?? '' ); ?></td>
					<?php } ?>
					<td class="gn-total"><?php echo esc_html( $item['total'] ); ?></td>
				</tr>
				<?php
			}
		}
		?>
		</tbody>
	</table>
	<div class="gn-invoice-total-wrapper">
		<div class="gn-invoice-totals">
			<div class="gn-subtotal">
				<span class="gn-title"><?php echo esc_html__( 'Subtotal', 'gigneo' ); ?></span>
				<span class="gn-amount"><?php echo esc_html( $data['subtotal'] ); ?></span>
			</div>
			<?php
			if ( ! empty( $data['tax'] ) ) {
				foreach ( $data['tax'] as $tax_info ) {
					?>
					<div class="gn-subtotal">
						<span class="gn-title"><?php echo esc_html( $tax_info['label'] ); ?></span>
						<span class="gn-amount"><?php echo esc_html( $tax_info['formatted_amount'] ); ?></span>
					</div>
					<?php
				}
			}
			?>
			<div class="gn-total">
				<span class="gn-title"><?php echo esc_html__( 'Total', 'gigneo' ); ?></span>
				<span class="gn-amount"><?php echo esc_html( $data['total'] ); ?></span>
			</div>
		</div>
	</div>
	<?php if ( ! empty( $footer_text ) ) { ?>
		<div class="gn-invoice-footer">
			<p class="gn-footer-text">
				<?php echo wp_kses_post( $footer_text ); ?>
			</p>
		</div>
	<?php } ?>
</div>
</body>
</html>
