<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}
$profile_id   = ! empty( $args['profile_id'] ) ? $args['profile_id'] : '';
$profile_type = ! empty( $args['profile_type'] ) ? $args['profile_type'] : '';

$wallet_balance        = gigneo_wallet_balance( $profile_id );
$requested_withdrawals = gigneo_requested_withdrawals( $profile_id );
$profile_payouts       = gigneo_profile_payouts( $profile_id, 'active' );
$minimum_withdrawal    = gigneo_option( '_gigneo_ts_minimum_withdraw_amount', 50 );
$profile_payouts_meta  = gigneo_post_meta( $profile_id, '_gigneo_pm_payouts' );
$withdrawal_fee_name   = gigneo_option( 'gigneo_ts_withdrawal_fee_name', esc_html__( 'Withdrawal Fee', 'gigneo' ) );
?>
<!--Withdraw Funds Modal-->
<div id="gn-withdraw-funds" 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 funds', 'gigneo' ); ?></h4>
					<p class="gn-description"><?php echo esc_html__( 'Enter the amount to withdraw from wallet.', 'gigneo' ); ?></p>
					<button type="button" data-gn-modal-close="gn-withdraw-funds" class="gn-modal-close">
						<i class="hi hi-x-mark" aria-hidden="true"></i>
					</button>
				</div>
				<form id="gn-withdraw-funds-form" class="gn-theme-form gn-withdraw-funds-form">
					<div class="gn-modal-body gn-custom-scrollbar">
						<div class="gn-stat-cards">
							<div class="gn-stat-card gn-wallet-balance">
								<div class="gn-head">
				<span class="gn-icon">
					<i class="hi hi-briefcase" aria-hidden="true"></i>
				</span>
								</div>
								<h3 class="gn-value"><?php echo esc_html( gigneo_price_format( $wallet_balance, true ) ); ?></h3>
								<h6 class="gn-label"><?php echo esc_html__( 'Wallet balance', 'gigneo' ); ?></h6>
							</div>
							<div class="gn-stat-card gn-requested-withdrawals">
								<div class="gn-head">
									<span class="gn-icon">
										<i class="hi hi-briefcase" aria-hidden="true"></i>
									</span>
								</div>
								<h3 class="gn-value"><?php echo esc_html( gigneo_price_format( $requested_withdrawals, true ) ); ?></h3>
								<h6 class="gn-label"><?php echo esc_html__( 'Requested withdrawals', 'gigneo' ); ?></h6>
							</div>
						</div>
						<fieldset class="gn-form-fields">
							<div class="gn-form-field">
								<label for="gn-withdraw-funds-amount" class="gn-required-field-label"><?php echo esc_html__( 'Amount', 'gigneo' ); ?></label>
								<input id="gn-withdraw-funds-amount" min="<?php echo esc_attr( $minimum_withdrawal ); ?>" class="gn-required-field" type="number" name="amount" autocomplete="off" placeholder="<?php echo esc_attr__( 'Enter amount', 'gigneo' ); ?>">
								<?php if ( ! empty( $minimum_withdrawal ) ) { ?>
									<p class="gn-field-description">
									<?php
									printf(
										// translators: %s is minimum withdraw amount
										esc_html__( 'Minimum withdrawal amount is %s.', 'gigneo' ),
										esc_html( gigneo_price_format( $minimum_withdrawal, true ) )
									);
									?>
									</p>
								<?php } ?>
							</div>
							<?php if ( ! empty( $profile_payouts ) && is_array( $profile_payouts ) ) { ?>
								<div class="gn-form-field">
									<div class="gn-payout-list-wrapper">
										<h6 class="gn-title gn-required-field-label">
											<?php echo esc_html__( 'Payout Method', 'gigneo' ); ?>
										</h6>
										<?php
										foreach ( $profile_payouts as $key => $payout ) {
											$commission_fixed      = $payout['commission']['fixed'] ?? '';
											$commission_percentage = $payout['commission']['percentage'] ?? '';
											$is_first              = array_key_first( $profile_payouts ) === $key;
											?>
											<div class="gn-radio">
												<input id="gn-payout-<?php echo esc_attr( $key ); ?>"<?php echo $is_first ? ' checked' : ''; ?>  type="radio" name="payout" data-gn-commission-fixed="<?php echo esc_attr( $commission_fixed ); ?>" data-gn-commission-percentage="<?php echo esc_attr( $commission_percentage ); ?>" value="<?php echo esc_attr( $key ); ?>" class="gn-payout-radio gn-required-field">
												<label for="gn-payout-<?php echo esc_attr( $key ); ?>">
														<span class="gn-payout-image">
															<img src="<?php echo esc_url( $payout['logo'] ); ?>" alt="<?php echo esc_attr( $payout['type'] ); ?>">
														</span>
													<span class="gn-payout-info">
															<span class="gn-title">
																<?php
																echo esc_attr( $payout['title'] );
																if ( ! empty( $payout['label'] ) ) {
																	?>
																	<span class="gn-label"><?php echo esc_attr( $payout['label'] ); ?></span>
																<?php } ?>
																</span>
															<span class="gn-description"><?php echo esc_html( $payout['description'] ); ?></span>
														</span>
												</label>
											</div>
										<?php } ?>
									</div>
								</div>
								<?php
							} else {
								$args = array(
									'title'       => esc_html__( 'No payout methods', 'gigneo' ),
									'description' => esc_html__( 'Set up a payout method to withdraw your wallet balance.', 'gigneo' ),
									'button_text' => esc_html__( 'Add payout', 'gigneo' ),
									'button_url'  => gigneo_dashboard_url( 'setting', 'billing' ),
								);

								gigneo_not_found( 'account_wallet_withdrawal_payout', $args );
							}
							?>
						</fieldset>
						<?php if ( empty( $profile_payouts ) && ! is_array( $profile_payouts ) ) { ?>
							<div class="gn-form-summary">
								<h6 class="gn-title"><?php echo esc_html__( 'Summary', 'gigneo' ); ?></h6>
								<div class="gn-summary-info">
									<div class="gn-info gn-withdrawal-amount">
										<span class="gn-label"><?php echo esc_html__( 'Withdrawal Amount:', 'gigneo' ); ?></span>
										<span id="gn-withdrawal-requested"
												class="gn-value"><?php echo esc_html( gigneo_price_format( 0, true ) ); ?></span>
									</div>
									<div class="gn-info gn-withdrawal-fee">
										<span class="gn-label"><?php echo esc_html( $withdrawal_fee_name ); ?></span>
										<span id="gn-withdrawal-fee"
												class="gn-value"><?php echo esc_html( gigneo_price_format( 0, true ) ); ?></span>
									</div>
									<div class="gn-info gn-total-amount-received">
										<span class="gn-label"><?php echo esc_html__( "You'll Receive:", 'gigneo' ); ?></span>
										<span id="gn-withdrawal-final-amount"
												class="gn-value"><?php echo esc_html( gigneo_price_format( 0, true ) ); ?></span>
									</div>
								</div>
							</div>
						<?php } ?>
					</div>
					<div class="gn-modal-footer">
						<input type="hidden" id="gn-wallet-balance" name="wallet_balance" value="<?php echo esc_attr( $wallet_balance ); ?>">
						<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-withdraw-funds"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
						<button type="submit" disabled class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Submit', 'gigneo' ); ?></button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
