<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}

$withdrawal_id            = isset( $args['p_id'] ) ? sanitize_text_field( $args['p_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' );
$total_amount             = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_total' );
$profile_id               = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_profile_id' );
$user_id                  = gigneo_post_meta( $profile_id, '_gigneo_pm_user_id' );
$withdrawal_note          = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_withdrawal_note' );
$withdrawal_status        = get_post_status( $withdrawal_id );
$fee_label                = gigneo_option( '_gigneo_ts_withdrawal_fee_name', true );
$profile_name             = gigneo_profile_name( $profile_id );
$profile_type             = gigneo_profile_type( $profile_id );
$role_label               = ( 'freelancer' === $profile_type ) ? gigneo_freelancer_singular() : gigneo_employer_singular();
$withdrawal_timestamp_utc = get_post_time( 'Y-m-d H:i:s', true );
$withdrawal_status        = ( 'publish' === $withdrawal_status ) ? 'pending' : $withdrawal_status;
$status_label             = gigneo_status_label( $withdrawal_status );
$status_class             = gigneo_status_class( $withdrawal_status );

$user_info               = get_userdata( $user_id );
$date_format             = get_option( 'date_format' );
$wallet_balance          = gigneo_wallet_balance( $profile_id, false );
$withdrawal_publish_date = gigneo_datetime( $withdrawal_timestamp_utc, $date_format );
$payout_tax              = gigneo_calculate_tax( $profile_id, $net_amount );
$payable_amount          = floatval( $net_amount - $payout_tax );
$profile_url             = add_query_arg(
	array(
		's'         => $profile_id,
		'post_type' => 'gigneo_' . $profile_type,
	),
	admin_url( 'edit.php' )
);

$description = '';
// Create description
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' ),
		esc_html( $payout_fee['percentage'] ),
		esc_html( gigneo_price_format( $payout_fee['fixed'], true ) )
	);
} elseif ( ! empty( $payout_fee['percentage'] ) ) {
	$description = sprintf(
	// translators: %s is percentage fee
		esc_html__( 'Fee may apply: %s%% per withdrawal.', 'gigneo' ),
		esc_html( $payout_fee['percentage'] )
	);
} elseif ( ! empty( $payout_fee['fixed'] ) ) {
	$description = sprintf(
	// translators: %s is fixed fee
		esc_html__( 'Fee may apply: %s per withdrawal.', 'gigneo' ),
		esc_html( gigneo_price_format( $payout_fee['fixed'], true ) )
	);
} else {
	$description = esc_html__( 'No withdrawal fee applies.', 'gigneo' );
}
?>
<div class="gn-withdraw-detail-modal gn-modal-wrapper "
	id="gn-withdraw-detail-modal-<?php echo esc_attr( $withdrawal_id ); ?>">
	<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__( 'Withdrawal request info', 'gigneo' ); ?>
						<span class="gn-withdrawal-status gn-status-tag <?php echo esc_attr( $status_class ); ?>"><?php echo esc_html( $status_label ); ?></span>
					</h4>
					<p class="gn-description">
						<?php echo esc_html__( 'Review the details of this withdrawal request before taking action.', 'gigneo' ); ?>
					</p>
					<button type="button" class="gn-modal-close"
							data-gn-modal-close="gn-withdraw-detail-modal-<?php echo esc_attr( $withdrawal_id ); ?>">
						<i class="hi hi-x-mark" aria-hidden="true"></i>
					</button>
				</div>
				<div class="gn-modal-body">
					<div class="gn-info-grid-wrap">
						<div class="gn-info-grid">
									<span class="gn-icon"><i class="hi hi-hashtag" aria-hidden="true"></i></span>
							<div class="gn-info">
								<h5 class="gn-title"><?php echo esc_html( __( 'Withdrawal ID', 'gigneo' ) ); ?></h5>
								<p class="gn-detail"><span
										class="gn-withdraw-id"><?php echo esc_html( gigneo_format_id( $withdrawal_id, 'withdraw', false ) ); ?></span>
								</p>
							</div>
						</div>
						<div class="gn-info-grid">
									<span class="gn-icon"><i class="hi hi-calendar" aria-hidden="true"></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>
						<div class="gn-info-grid">
									<span class="gn-icon"><i class="hi hi-user" aria-hidden="true"></i></span>
							<div class="gn-info">
								<h5 class="gn-title"><?php echo esc_html__( 'Requested by', 'gigneo' ); ?></h5>
								<p class="gn-detail"><a class="gn-profile gn-link" href="<?php echo esc_url( $profile_url ); ?>"><?php echo esc_html( $profile_name ); ?></a>(<span class="gn-type"><?php echo esc_html( $role_label ); ?></span>)</p>
							</div>
						</div>
						<?php
						if ( $user_info ) {
							?>
							<div class="gn-info-grid">
									<span class="gn-icon"><i class="hi hi-envelope" aria-hidden="true"></i></span>
								<div class="gn-info">
									<h5 class="gn-title"><?php echo esc_html__( 'Email', 'gigneo' ); ?></h5>
									<p class="gn-detail">
										<a href="mailto:<?php echo esc_attr( $user_info->user_email ); ?>"><?php echo esc_html( $user_info->user_email ); ?></a>
									</p>
								</div>
							</div>
						<?php } ?>
						<div class="gn-info-grid">
									<span class="gn-icon"><i class="hi hi-wallet" aria-hidden="true"></i></span>
							<div class="gn-info">
								<h5 class="gn-title"><?php echo esc_html__( 'Wallet balance', 'gigneo' ); ?></h5>
								<p class="gn-detail">
									<?php echo esc_html( gigneo_price_format( $wallet_balance, true ) ); ?>
								</p>
							</div>
						</div>
						<div class="gn-info-grid">
									<span class="gn-icon"><i class="hi hi-currency-dollar" aria-hidden="true"></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( $total_amount, true ) ); ?></p>
							</div>
						</div>
					</div>
					
					<?php if ( ! empty( $payment_method ) && ! empty( $payment_method['fields'] ) ) { ?>
					<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'] ); ?></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>
							<div class="gn-payout-info">
								<?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>
					<?php } ?>
					<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( $total_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 gn-subtotal"><?php echo esc_html( gigneo_price_format( $net_amount, true ) ); ?></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__( 'Amount payable', 'gigneo' ); ?></span>
								<span class="gn-value gn-payable"><?php echo esc_html( gigneo_price_format( $payable_amount, true ) ); ?></span>
							</div>
						</div>
					</div>
					<fieldset class="gn-form-fields">
						<div class="row g-4">
							<div class="col-md-12">
								<div class="gn-form-field gn-select2">
									<label for="gn-respond-action-<?php echo esc_attr( $withdrawal_id ); ?>"
											class="gn-required-field-label"><?php echo esc_html__( 'Status', 'gigneo' ); ?></label>
									<select id="gn-respond-action-<?php echo esc_attr( $withdrawal_id ); ?>" data-minimum-results-for-search="Infinity" aria-hidden="true"
											name="gn-respond-action" class="gn-required-field">
										<option
												value="publish" <?php selected( $withdrawal_status, 'published' ); ?>>
											<?php echo esc_html__( 'Pending', 'gigneo' ); ?>
										</option>
										<option value="in-progress" <?php selected( $withdrawal_status, 'in-progress' ); ?>>
											<?php echo esc_html__( 'In Progress', 'gigneo' ); ?>
										</option>
										<option value="completed" <?php selected( $withdrawal_status, 'completed' ); ?>>
											<?php echo esc_html__( 'Completed', 'gigneo' ); ?>
										</option>
										<option value="rejected" <?php selected( $withdrawal_status, 'rejected' ); ?>>
											<?php echo esc_html__( 'Rejected', 'gigneo' ); ?>
										</option>
										<option value="on-hold" <?php selected( $withdrawal_status, 'on-hold' ); ?>>
											<?php echo esc_html__( 'On Hold', 'gigneo' ); ?>
										</option>
									</select>
								</div>
							</div>
							<div class="col-md-12">
								<div class="gn-form-field gn-text-area">
									<label for="gn-withdrawal-note-<?php echo esc_attr( $withdrawal_id ); ?>"><?php echo esc_html__( 'Note', 'gigneo' ); ?></label>
									<textarea id="gn-withdrawal-note-<?php echo esc_attr( $withdrawal_id ); ?>" class="gn-textarea-counter" name="withdraw-note"
												autocomplete="off" rows="2" placeholder="<?php echo esc_attr__( 'Enter optional note for withdrawal.', 'gigneo' ); ?>"><?php echo esc_html( $withdrawal_note ); ?></textarea>
								</div>
							</div>
						</div>
					</fieldset>
				</div>
				<div class="gn-modal-footer">
					<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-withdraw-detail-modal-<?php echo esc_attr( $withdrawal_id ); ?>"><?php echo esc_html__( 'Cancel', 'gigneo' ); ?></button>
					<?php if ( 'pending' === $withdrawal_status || 'on-hold' === $withdrawal_status || 'in-progress' === $withdrawal_status ) { ?>
					<button type="button" data-gn-withdrawal-id="<?php echo esc_attr( $withdrawal_id ); ?>" class="gn-withdraw-submit gn-btn gn-btn-primary"><?php echo esc_html__( 'Submit', 'gigneo' ); ?></button>
					<?php } ?>
				</div>
			</div>
		</div>
	</div>
</div>
