<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access.
}

$profile_id    = ! empty( $args['profile_id'] ) ? $args['profile_id'] : '';
$profile_type  = ! empty( $args['profile_type'] ) ? $args['profile_type'] : '';
$platform_type = ! empty( $args['platform_type'] ) ? $args['platform_type'] : '';
$credit_system = ! empty( $args['credit_system'] ) ? $args['credit_system'] : '';

$date_format = get_option( 'date_format' );

$withdrawal_args = array(
	'post_type'      => 'gigneo_withdrawal',
	'post_status'    => array( 'publish', 'in-progress', 'on-hold', 'completed', 'rejected' ),
	'posts_per_page' => 10,
	'orderby'        => 'date',
	'order'          => 'DESC',
	// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	'meta_query'     => array(
		array(
			'key'     => '_gigneo_pm_profile_id',
			'value'   => $profile_id,
			'compare' => '=',
		),
	),
);

$withdrawal_query  = new WP_Query( $withdrawal_args );
$total_withdrawals = $withdrawal_query->found_posts;
?>
<div id="gn-wallet-withdrawal" class="gn-dashboard-section gn-section-withdrawal-requests gn-h-100">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Withdrawals', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<?php if ( $withdrawal_query->have_posts() ) { ?>
			<ul id="gn-wallet-withdrawal-body" class="gn-withdrawal-request-list gn-custom-scroll">
				<?php
				while ( $withdrawal_query->have_posts() ) {
					$withdrawal_query->the_post();
					?>
					<?php
					$withdrawal_id            = get_the_ID();
					$withdrawal_status        = get_post_status( $withdrawal_id );
					$withdrawal_status        = ( 'publish' === $withdrawal_status ) ? 'pending' : $withdrawal_status;
					$withdrawal_amount        = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_total' );
					$withdrawal_note          = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_withdrawal_note' );
					$withdrawal_status_class  = gigneo_status_class( $withdrawal_status );
					$withdrawal_timestamp_utc = get_post_time( 'Y-m-d H:i:s', true );
					$withdrawal_publish_date  = gigneo_datetime( $withdrawal_timestamp_utc, $date_format );
					$updated_timestamp_utc    = get_post_modified_time( 'Y-m-d H:i:s', true );
					$withdrawal_updated_date  = gigneo_datetime( $updated_timestamp_utc, $date_format );
					$withdrawal_status_label  = gigneo_status_label( $withdrawal_status );
					$profile_id               = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_profile_id' );
					$payment_method           = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_payout' );
					$payout_fee               = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_commission' );
					$net_amount               = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_subtotal' );
					$fee_label                = gigneo_option( '_gigneo_ts_withdrawal_fee_name' );
					$payout_tax               = gigneo_calculate_tax( $profile_id, $net_amount );
					$payable_amount           = floatval( $net_amount - $payout_tax );

					if ( empty( $payment_method ) ) {
						continue;
					}

					if ( ! empty( $payout_fee['percentage'] ) && ! empty( $payout_fee['fixed'] ) ) {
						$description = sprintf(
						// translators: 1: percentage fee, 2: fixed fee.
							esc_html__( 'Fee may apply: %1$s%% + %2$s per withdrawal.', 'gigneo' ),
							$payout_fee['percentage'],
							gigneo_price_format( $payout_fee['fixed'], true )
						);
					} elseif ( ! empty( $payout_fee['percentage'] ) ) {
						$description = sprintf(
						// translators: %s: percentage fee.
							esc_html__( 'Fee may apply: %s%% per withdrawal.', 'gigneo' ),
							$payout_fee['percentage']
						);
					} elseif ( ! empty( $payout_fee['fixed'] ) ) {
						$description = sprintf(
						// translators: %s: fixed fee.
							esc_html__( 'Fee may apply: %s per withdrawal.', 'gigneo' ),
							gigneo_price_format( $payout_fee['fixed'], true )
						);
					} else {
						$description = esc_html__( 'No withdrawal fee applies.', 'gigneo' );
					}
					?>
					<li class="gn-withdrawal-request">
						<figure class="gn-figure">
							<img src="<?php echo esc_url( gigneo_payout_logo_url( $payment_method['type'] ?? 0 ) ); ?>" alt="<?php echo esc_html( $payment_method['title'] ); ?>">
						</figure>
						<div class="gn-info">
							<h5 class="gn-title">
								<?php echo esc_html( $payment_method['title'] ); ?>
								<span class="gn-status-tag <?php echo esc_attr( $withdrawal_status_class ); ?>">
								<?php echo esc_html( $withdrawal_status_label ); ?>
							</span>
							</h5>
							<div class="gn-meta">
								<span class="gn-date"><?php echo esc_attr( $withdrawal_publish_date ); ?></span>
								<span class="gn-amount"><?php echo esc_attr( gigneo_price_format( $withdrawal_amount, true ) ); ?></span>
							</div>
						</div>
						<button type="button" data-gn-modal-toggle="gn-withdrawal-request-<?php echo esc_attr( $withdrawal_id ); ?>" class="gn-withdrawal-request-view">
							<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
						</button>
						<div id="gn-withdrawal-request-<?php echo esc_attr( $withdrawal_id ); ?>"
							class="gn-modal-wrapper">
							<div class="gn-modal-dialog">
								<div class="gn-modal-content">
									<div class="gn-modal-info">
										<div class="gn-modal-header">
											<h4 class="gn-title">
												<?php echo esc_html__( 'Withdraw request details', 'gigneo' ); ?>
													<span class="gn-status-tag <?php echo esc_attr( $withdrawal_status_class ); ?>">
												<?php echo esc_html( $withdrawal_status_label ); ?>
												</span>
											</h4>
											<p class="gn-description"><?php echo esc_html__( 'View detail information about the withdrawal request.', 'gigneo' ); ?></p>
											<button type="button" data-gn-modal-close="gn-withdrawal-request-<?php echo esc_attr( $withdrawal_id ); ?>" class="gn-modal-close">
												<i class="hi hi-x-mark"></i>
											</button>
										</div>
										<div class="gn-modal-body gn-custom-scrollbar">
											<?php if ( $withdrawal_note ) { ?>
												<div class="gn-notices-wrapper">
													<div
														class="gn-notice gn-notice-<?php echo esc_attr( $withdrawal_status ); ?>">
														<div class="gn-notice-content">
															<div class="gn-notice-icon">
																<i class="hi hi-exclamation-circle"
																	aria-hidden="true"></i>
															</div>
															<div class="gn-notice-info">
																<h3 class="gn-title"><?php echo esc_html__( 'Note', 'gigneo' ); ?></h3>
																<p class="gn-description"><?php echo esc_html( $withdrawal_note ); ?></p>
															</div>
														</div>
													</div>
												</div>
											<?php } ?>
											<div class="gn-info-grid-wrap">
												<ul class="gn-info-grid-content">
													<li>
														<div class="gn-info-grid">
															<span class="gn-icon"><i class="hi hi-hashtag"></i></span>
															<div class="gn-info">
																<h5 class="gn-title"><?php echo esc_html__( 'Withdrawal ID', 'gigneo' ); ?></h5>
																<p class="gn-detail"><?php echo esc_html( gigneo_format_id( $withdrawal_id, 'withdraw', false ) ); ?></p>
															</div>
														</div>
													</li>
													<li>
														<div class="gn-info-grid">
															<span class="gn-icon"><i class="hi hi-currency-dollar"></i></span>
															<div class="gn-info">
																<h5 class="gn-title"><?php echo esc_html__( 'Requested amount', 'gigneo' ); ?></h5>
																<p class="gn-detail"><?php echo esc_html( gigneo_price_format( $withdrawal_amount, true ) ); ?></p>
															</div>
														</div>
													</li>
													<li>
														<div class="gn-info-grid">
															<span class="gn-icon"><i class="hi hi-calendar"></i></span>
															<div class="gn-info">
																<h5 class="gn-title"><?php echo esc_html__( 'Requested date', 'gigneo' ); ?></h5>
																<p class="gn-detail"><?php echo esc_html( $withdrawal_publish_date ); ?></p>
															</div>
														</div>
													</li>
													<li>
														<div class="gn-info-grid">
															<span class="gn-icon"><i class="hi hi-calendar"></i></span>
															<div class="gn-info">
																<h5 class="gn-title"><?php echo esc_html__( 'Last update date', 'gigneo' ); ?></h5>
																<p class="gn-detail"><?php echo esc_html( $withdrawal_updated_date ); ?></p>
															</div>
														</div>
													</li>
												</ul>
											</div>
											<div class="gn-withdraw-payout">
												<h3 class="gn-title"><?php echo esc_html__( 'Payout method', 'gigneo' ); ?></h3>
												<div class="gn-payout-method">
													<figure class="gn-payout-img">
														<img src="<?php echo esc_url( gigneo_payout_logo_url( $payment_method['type'] ) ); ?>"
															alt="<?php echo esc_html( $payment_method['title'] ); ?>">
													</figure>
													<div class="gn-payout-info">
														<h4 class="gn-label">
															<?php
															echo esc_html( $payment_method['title'] );
															if ( ! empty( $payment_method['label'] ) ) {
																?>
																<span class="gn-status-tag"><?php echo esc_html( $payment_method['label'] ); ?></span>
															<?php } ?>
														</h4>
														<span class="gn-value"><?php echo esc_html( $description ); ?></span>
													</div>
												</div>
												<div class="gn-payout-info">
													<h3 class="gn-title"><?php echo esc_html__( 'Payout details', 'gigneo' ); ?></h3>
													<?php foreach ( $payment_method['fields'] as $field ) { ?>
														<div class="gn-info">
															<span class="gn-label"><?php echo esc_html( $field['label'] ); ?>:</span>
															<span class="gn-value"><?php echo esc_html( $field['value'] ); ?></span>
														</div>
													<?php } ?>
												</div>
											</div>
											<div class="gn-form-summary">
												<h3 class="gn-title"><?php echo esc_html__( 'Payment summary', 'gigneo' ); ?></h3>
												<div class="gn-summary-info">
													<div class="gn-info">
														<span class="gn-label"><?php echo esc_html__( 'Requested amount', 'gigneo' ); ?></span>
														<span class="gn-value gn-total"><?php echo esc_html( gigneo_price_format( $withdrawal_amount, true ) ); ?></span>
													</div>
													<?php if ( $payout_fee ) { ?>
														<div class="gn-info gn-fee">
															<span class="gn-label"><?php echo esc_html( $fee_label ); ?></span>
															<span class="gn-value"><?php echo esc_html( gigneo_price_format( $payout_fee['total'], true, false ) ); ?></span>
														</div>
													<?php } ?>
													<div class="gn-info gn-subtotal">
														<span class="gn-label"><?php echo esc_html__( 'Subtotal', 'gigneo' ); ?></span>
														<span class="gn-value"><?php echo esc_html( gigneo_price_format( $net_amount, true, false ) ); ?></span>
													</div>
													<?php if ( $payout_tax ) { ?>
														<div class="gn-info gn-fee">
															<span class="gn-label"><?php echo esc_html__( 'Tax', 'gigneo' ); ?></span>
															<span class="gn-value"><?php echo esc_html( gigneo_price_format( $payout_tax, true, false ) ); ?></span>
														</div>
													<?php } ?>
													<div class="gn-info gn-total">
														<span class="gn-label"><?php echo esc_html__( 'You\'ll receive', 'gigneo' ); ?></span>
														<span class="gn-value gn-payable"><?php echo esc_html( gigneo_price_format( $payable_amount, true ) ); ?></span>
													</div>
												</div>
											</div>
											<?php
											//Invoices
											$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_withdraw_id',
														'value'   => $withdrawal_id,
														'compare' => '=',
													),
													array(
														'key'     => '_gigneo_pm_profile_id',
														'value'   => $profile_id,
														'compare' => '=',
													),
												),
											);

											$invoice_query = new WP_Query( $invoice_args );
											if ( $invoice_query->have_posts() ) {
												?>
												<div class="gn-invoice-wrapper">
													<h3 class="gn-title"><?php echo esc_html__( 'Invoices', 'gigneo' ); ?></h3>
													<ul class="gn-invoice-list">
														<?php
														while ( $invoice_query->have_posts() ) {
															$invoice_query->the_post();
															$invoice_id            = get_the_ID();
															$invoice_status        = get_post_status( $invoice_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 ( 'service_commission' === $invoice_type ) {
																$invoice_type = ( 'freelancer' === $profile_type ) ? 'freelancer_fee' : 'employer_fee';
															}
															$invoice_label = gigneo_invoice_label( $invoice_type );
															?>
															<li class="gn-invoice">
																<div class="gn-info">
																	<h5 class="gn-title">
																		<?php echo esc_html( gigneo_format_id( $invoice_id, 'invoice', false ) ); ?>
																		<button type="button"
																				data-gn-invoice-id="<?php echo esc_attr( $invoice_id ); ?>"
																				class="gn-invoice-download">
																			<i class="hi hi-arrow-down-tray"
																				aria-hidden="true"></i>
																		</button>
																	</h5>
																	<?php if ( ! empty( $invoice_label ) ) { ?>
																		<p class="gn-details"><?php echo esc_html( $invoice_label ); ?></p>
																	<?php } ?>
																</div>
																<div class="gn-amount-info">
																	<h5 class="gn-amount gn-<?php echo esc_attr( $invoice_total < 0 ? 'danger' : 'success' ); ?>"><?php echo esc_html( gigneo_price_format( $invoice_total ?? 0, true ) ); ?></h5>
																	<p class="gn-meta">
																		<span class="gn-status-tag <?php echo esc_attr( gigneo_status_class( $invoice_status ) ); ?>"><?php echo esc_html( $invoice_status_label ); ?></span>
																		<span class="gn-date"><?php echo esc_html( gigneo_datetime( $invoice_publish_date, $date_format ) ); ?></span>
																	</p>
																</div>
															</li>
															<?php
														}
														wp_reset_postdata();
														?>
													</ul>
												</div>
											<?php } ?>
										</div>
										<div class="gn-modal-footer">
											<button type="button" class="gn-btn gn-btn-outline-grey"
													data-gn-modal-close="gn-withdrawal-request-<?php echo esc_attr( $withdrawal_id ); ?>">
												<?php echo esc_html__( 'Close', 'gigneo' ); ?>
											</button>
											<?php if ( 'pending' === $withdrawal_status ) { ?>
												<button type="button"
														data-gn-id="<?php echo esc_attr( $withdrawal_id ); ?>"
														class="gn-btn gn-btn-danger gn-cancel-withdrawal-request">
													<?php echo esc_html__( 'Cancel', 'gigneo' ); ?>
												</button>
											<?php } ?>
										</div>
									</div>
								</div>
							</div>
						</div>
					</li>
				<?php } ?>
			</ul>
			<?php
		} else {
			$not_found_args = array(
				'title'       => esc_html__( 'No withdrawals', 'gigneo' ),
				'description' => esc_html__( 'All your withdrawals will appear here once available.', 'gigneo' ),
			);
			gigneo_not_found( 'account_wallet_withdrawal', $not_found_args );
		}
		?>
	</div>
	<div class="gn-footer">
		<button type="button" class="gn-btn gn-btn-md gn-btn-block gn-btn-outline-grey" data-gn-modal-toggle="gn-withdraw-funds">
			<?php echo esc_html__( 'Withdraw funds', 'gigneo' ); ?>
			<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
		</button>
	</div>
</div>

