<?php

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

$order_id = $args['p_id'] ?? '';

if ( ! $order_id ) {
	return;
}
$freelancer_profile_id = $args['freelancer_profile_id'] ?? '';
$employer_profile_id   = $args['profile_id'] ?? '';
$profile_id            = $args['profile_id'] ?? '';
$date_format           = $args['date_format'] ?? '';

$plan_data     = gigneo_post_meta( $order_id, '_gigneo_pm_price_plan' );
$service_price = ! empty( $plan_data['price'] ) ? gigneo_price_format( $plan_data['price'], true ) : 0;
$sub_total     = gigneo_post_meta( $order_id, '_gigneo_pm_subtotal' );
$total         = gigneo_post_meta( $order_id, '_gigneo_pm_total' );

// Employer commission
$employer_commission       = gigneo_post_meta( $order_id, '_gigneo_pm_commission' );
$employer_commission_title = gigneo_option( '_gigneo_ts_platform_fee_name', esc_html__( 'Platform fee', 'gigneo' ) );
$employer_commission_total = $employer_commission['total'] ?? 0;
$employer_total            = $total;

// Freelancer commission
$freelancer_commission       = gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_commission' );
$freelancer_commission       = empty( $freelancer_commission ) ? gigneo_commission( $sub_total, 'job', $freelancer_profile_id ) : $freelancer_commission;
$freelancer_commission_title = gigneo_option( '_gigneo_ts_platform_fee_name', esc_html__( 'Platform fee', 'gigneo' ) );
$freelancer_commission_total = $freelancer_commission['total'] ?? 0;
$freelancer_total            = $sub_total - $freelancer_commission_total;
?>
<form></form>
<div class="gn-order-payments-wrapper">
	<div id="gn-order-payments" class="gn-order-section gn-order-payments">
		<div class="gn-head">
			<h3 class="gn-title"><?php echo esc_html__( 'Payment overview', 'gigneo' ); ?></h3>
		</div>
		<div class="gn-body">
			<div class="gn-stat-cards">
				<div class="gn-stat-card">
					<span class="gn-label"><?php echo esc_html__( 'Order amount', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( gigneo_price_format( $sub_total, true ) ); ?></span>
				</div>
				<?php if ( ! empty( $employer_commission_total ) ) { ?>
				<div class="gn-stat-card">
					<span class="gn-label"><?php echo esc_html( $employer_commission_title ); ?> (<?php echo esc_html( gigneo_employer_singular() ); ?>)</span>
					<span class="gn-value gn-danger"><?php echo esc_html( gigneo_price_format( $employer_commission_total, true ) ); ?></span>
				</div>
				<?php } ?>
				<?php if ( ! empty( $freelancer_commission_total ) ) { ?>
				<div class="gn-stat-card">
					<span class="gn-label"><?php echo esc_html( $freelancer_commission_title ); ?> (<?php echo esc_html( gigneo_freelancer_singular() ); ?>)</span>
					<span class="gn-value gn-danger"><?php echo esc_html( gigneo_price_format( $freelancer_commission_total, true ) ); ?></span>
				</div>
				<?php } ?>
				<div class="gn-stat-card">
					<span class="gn-label"><?php echo esc_html( gigneo_employer_singular() ); ?> <?php echo esc_html__( 'Total spent', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( gigneo_price_format( $employer_total, true ) ); ?></span>
				</div>
				<div class="gn-stat-card">
					<span class="gn-label"><?php echo esc_html( gigneo_freelancer_singular() ); ?> <?php echo esc_html__( 'Earnings', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( gigneo_price_format( $freelancer_total, true ) ); ?></span>
				</div>
			</div>
		</div>
	</div>
<?php
$invoice_args = array(
	'post_type'      => 'gigneo_invoice',
	'post_status'    => 'any',
	'posts_per_page' => -1,
	'orderby'        => 'date',
	'order'          => 'DESC',
	// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	'meta_query'     => array(
		'relation' => 'AND',
		array(
			'key'     => '_gigneo_pm_order_id',
			'value'   => $order_id,
			'compare' => '=',
		),
	),
);

$invoice_query = new WP_Query( $invoice_args );

if ( $invoice_query->have_posts() ) {
	?>
	<div id="gn-all-invoices" class="gn-order-section gn-order-payments">
		<div class="gn-head">
			<h3 class="gn-title"><?php echo esc_html__( 'Invoices', 'gigneo' ); ?></h3>
		</div>
		<div class="gn-body">
			<ul class="gn-invoice-list">
				<?php
				while ( $invoice_query->have_posts() ) {
					$invoice_query->the_post();
					$invoice_id            = get_the_ID();
					$profile_id            = gigneo_post_meta( $invoice_id, '_gigneo_pm_profile_id' );
					$invoice_status        = get_post_status( $invoice_id );
					$profile_type          = gigneo_profile_type( $profile_id );
					$invoice_type          = gigneo_post_meta( $invoice_id, '_gigneo_pm_type' );
					$invoice_total         = gigneo_post_meta( $invoice_id, '_gigneo_pm_total' );
					$invoice_amount        = gigneo_post_meta( $invoice_id, '_gigneo_pm_credit' );
					$invoice_timestamp_utc = get_post_time( 'Y-m-d H:i:s', true );
					$invoice_publish_date  = gigneo_datetime( $invoice_timestamp_utc, $date_format );
					$invoice_status_label  = gigneo_status_label( $invoice_status );

					// Handle credit type
					if ( 'credit' === $invoice_type ) {
						$invoice_type = $invoice_amount > 0 ? 'credit_purchased' : 'credit_spent';
					}
					// If commission type, choose freelancer or employer version
					if ( 'job_commission' === $invoice_type || 'service_commission' === $invoice_type ) {
						$invoice_type = ( 'freelancer' === $profile_type )
								? 'freelancer_fee'
								: 'employer_fee';
					}
					// Determine user type based on profile_id
					$user_type     = ( $profile_id === $employer_profile_id ) ? gigneo_employer_singular() : gigneo_freelancer_singular();
					$invoice_label = gigneo_invoice_label( $invoice_type );
					?>
					<li class="gn-invoice">
						<span class="gn-icon">
							<i class="hi hi-document-text" aria-hidden="true"></i>
						</span>
						<div class="gn-info">
							<h5 class="gn-title">
								<?php echo esc_html( gigneo_format_id( $invoice_id, 'invoice', false ) ) . '.pdf'; ?>
								<span class="gn-status-tag <?php echo esc_attr( gigneo_status_class( $invoice_status ) ); ?>"><?php echo esc_html( $invoice_status_label ); ?></span>
							</h5>
							<ul class="gn-details">
								<li class="gn-date">
								<?php
								printf(
										// translators: %s is invoice publish date
									esc_html__( 'Generated %s', 'gigneo' ),
									esc_html( gigneo_datetime( $invoice_publish_date, $date_format ) )
								);
								?>
										</li>
								<?php if ( ! empty( $invoice_label ) ) { ?>
									<li class="gn-type"><?php echo esc_html( $invoice_label ); ?></li>
								<?php } ?>
								<li class="gn-user"><?php echo esc_html( $user_type ); ?></li>
							</ul>
						</div>
						<div class="gn-actions">
							<button type="button" class="gn-btn gn-service-order-invoice-download" data-gn-invoice-id="<?php echo esc_attr( $invoice_id ); ?>">
								<i class="hi hi-arrow-down-tray" aria-hidden="true"></i>
							</button>
						</div>
					</li>
				<?php } ?>
			</ul>
		</div>
	</div>
	<?php
	wp_reset_postdata();
}
?>
</div>
