<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}
$profile_id                = $args['profile_id'] ?? gigneo_active_profile_id();
$active_payout_list        = gigneo_withdrawal_payout_list();
$bank_field_list           = gigneo_payout_bank_fields();
$bank_required_fields      = gigneo_option( '_gigneo_ts_payment_bank_required_fields', array( 'name', 'acc_name', 'acc_number' ) );
$custom_payout_fields      = apply_filters( 'gigneo_custom_payout_fields_list', array() );
$custom_payout_field_added = array();

//Post Meta
$profile_payouts      = gigneo_profile_payouts( $profile_id );
$profile_payouts_meta = gigneo_post_meta( $profile_id, '_gigneo_pm_payouts' );

?>
<div id="gn-billing-payouts" class="gn-dashboard-section gn-section-billing-payouts">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Payout methods', 'gigneo' ); ?></h3>
		<button class="gn-link gn-link-primary" data-gn-modal-toggle="gn-select-payout">
			<i class="hi hi-plus" aria-hidden="true"></i>
			<?php echo esc_html__( 'Add', 'gigneo' ); ?>
		</button>
	</div>
	<div class="gn-body">
		<?php if ( ! empty( $profile_payouts ) ) { ?>
			<div class="gn-payout-list-wrapper">
				<ul class="gn-payout-list">
					<?php
					foreach ( $profile_payouts as $pkey => $profile_payout ) {
						?>
						<li class="gn-payout gn-payout-<?php echo esc_attr( $profile_payout['type'] ); ?>">
							<div class="gn-payout-inner">
								<figure class="gn-payout-image">
									<img src="<?php echo esc_url( $profile_payout['logo'] ); ?>" alt="<?php echo esc_html__( 'Payout', 'gigneo' ); ?>">
								</figure>
								<div class="gn-payout-info">
									<h4 class="gn-title">
										<?php echo esc_html( $profile_payout['title'] ); ?>
										<?php if ( ! empty( $profile_payout['label'] ) ) { ?>
											<span class="gn-label"><?php echo esc_attr( $profile_payout['label'] ); ?></span>
										<?php } ?>
										<span class="gn-status-tag gn-<?php echo esc_attr( $profile_payout['status'] ); ?>"><?php echo 'active' === $profile_payout['status'] ? esc_html__( 'Active', 'gigneo' ) : esc_html__( 'Inactive', 'gigneo' ); ?></span>
									</h4>
									<p class="gn-description"><?php echo esc_html( $profile_payout['description'] ); ?></p>
								</div>
								<div class="gn-actions">
									<?php
									$profile_payout_fields_data = array(
										'payout_id' => $pkey,
									);
									foreach ( $profile_payouts_meta[ $pkey ] as $pf_key => $pf_value ) {
										if ( 'label' === $pf_key || 'status' === $pf_key ) {
											$profile_payout_fields_data[ "payout_$pf_key" ] = $pf_value;
										} else {
											$profile_payout_fields_data[ $pf_key ] = $pf_value;
										}
									}
									?>
									<button type="button" data-gn-modal-toggle="gn-save-payout-<?php echo esc_attr( $profile_payout['type'] ); ?>" class="gn-btn gn-btn-light" data-gn-fields="<?php echo esc_attr( wp_json_encode( $profile_payout_fields_data ) ); ?>">
										<i class="hi hi-pencil-edit" aria-hidden="true"></i>
									</button>
									<button type="button" class="gn-btn gn-delete-payout-btn" data-gn-id="<?php echo esc_attr( $pkey ); ?>">
										<i class="hi hi-trash" aria-hidden="true"></i>
									</button>
								</div>
							</div>
						</li>
						<?php
					}
					?>
				</ul>
			</div>
			<?php
		} else {
			$not_found_args = array(
				'title'       => esc_html__( 'No payout methods', 'gigneo' ),
				'description' => esc_html__( 'Add a payout method to manage your withdrawals efficiently.', 'gigneo' ),
			);
			gigneo_not_found( 'gn-billing-payouts', $not_found_args );
		}
		?>
	</div>
	<!--Add Payouts Modal-->
	<div id="gn-select-payout" class="gn-modal-wrapper gn-select-payout-modal">
		<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__( 'Payout methods', 'gigneo' ); ?></h4>
						<p class="gn-description"><?php echo esc_html__( 'Setup payout method for withdrawals.', 'gigneo' ); ?></p>
						<button type="button" data-gn-modal-close="gn-select-payout" class="gn-modal-close">
							<i class="hi hi-x-mark" aria-hidden="true"></i>
						</button>
					</div>
					<form id="gn-select-payout-method-form" class="gn-user-billing-payouts-setting">
						<div class="gn-modal-body gn-custom-scrollbar">
							<?php if ( ! empty( $active_payout_list ) ) { ?>
								<ul class="gn-radio-payout-list">
									<?php
									$first_payout = true;
									foreach ( $active_payout_list as $pkey => $payout ) {
										$payout_logo = gigneo_payout_logo_url( $pkey );
										?>
										<li class="gn-payout gn-radio gn-<?php echo esc_attr( $pkey ); ?>">
											<label>
												<input type="radio" name="payout_type" value="<?php echo esc_attr( $pkey ); ?>" <?php checked( $first_payout ); ?>>
												<span class="gn-title"><?php echo esc_html( $payout ); ?></span>
												<img src="<?php echo esc_url( $payout_logo ); ?>" alt="<?php echo esc_attr( $payout ); ?>"/>
											</label>
										</li>
										<?php
										$first_payout = false;
									}
									?>
								</ul>
							<?php } ?>
						</div>
						<div class="gn-modal-footer">
							<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-select-payout"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
							<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Continue', 'gigneo' ); ?></button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
	<?php
	if ( ! empty( $active_payout_list ) ) {
		foreach ( $active_payout_list as $pkey => $payout ) {
			?>
			<!--Add Payout-->
			<div id="gn-save-payout-<?php echo esc_attr( $pkey ); ?>" class="gn-modal-wrapper gn-edit-payout-modal">
				<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
									printf(
									// translators: %s is the payout method name.
										esc_html__( '%s details', 'gigneo' ),
										esc_html( $payout )
									);
									?>
								</h4>
								<p class="gn-description"><?php echo esc_html__( 'Setup payout details for withdrawals.', 'gigneo' ); ?></p>
								<button type="button" data-gn-modal-close="gn-save-payout-<?php echo esc_attr( $pkey ); ?>" class="gn-modal-close">
									<i class="hi hi-x-mark" aria-hidden="true"></i>
								</button>
							</div>
							<form id="gn-save-payout-method-form" class="gn-theme-form gn-user-billing-payouts-setting">
								<div class="gn-modal-body gn-custom-scrollbar">
									<fieldset class="gn-form-fields">
										<div class="row g-4">
											<div class="col-md-12">
												<div class="gn-form-field">
													<label for="gn-payout-label"><?php echo esc_html__( 'Label', 'gigneo' ); ?></label>
													<input id="gn-payout-label" type="text" name="payout_label" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter label', 'gigneo' ); ?>">
												</div>
											</div>
											<div class="col-md-12">
												<div class="gn-form-field gn-select2">
													<label class="gn-required-field-label" for="gn-payout-status"><?php echo esc_html__( 'Status', 'gigneo' ); ?></label>
													<select id="gn-payout-status" name="payout_status" class="gn-required-field" data-minimum-results-for-search="Infinity">
														<option value="active"><?php echo esc_html__( 'Active', 'gigneo' ); ?></option>
														<option value="inactive"><?php echo esc_html__( 'Inactive', 'gigneo' ); ?></option>
													</select>
												</div>
											</div>
											<?php if ( 'paypal' === $pkey ) { ?>
												<div class="col-md-12">
													<div class="gn-form-field">
														<label class="gn-required-field-label" for="gn-paypal-email"><?php echo esc_html__( 'Paypal account', 'gigneo' ); ?></label>
														<input id="gn-paypal-email" class="gn-required-field" type="email" name="paypal_account" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter paypal email', 'gigneo' ); ?>">
													</div>
												</div>
											<?php } elseif ( 'payoneer' === $pkey ) { ?>
												<div class="col-md-12">
													<div class="gn-form-field">
														<label class="gn-required-field-label" for="gn-payoneer-account"><?php echo esc_html__( 'Payoneer account', 'gigneo' ); ?></label>
														<input id="gn-payoneer-account" class="gn-required-field" type="email" name="payoneer_account" autocomplete="off" value="" placeholder="<?php echo esc_attr__( 'Enter Payoneer email', 'gigneo' ); ?>">
													</div>
												</div>
												<?php
											} elseif ( 'bank' === $pkey ) {
												if ( ! empty( $bank_field_list ) && is_array( $bank_field_list ) ) {
													foreach ( $bank_field_list as $bf_key => $bank_field ) {
														$bank_field_required = in_array( $bf_key, $bank_required_fields, true );
														?>
														<div class="col-md-12">
															<div class="gn-form-field">
																<label for="gn-bank-<?php echo esc_attr( $bf_key ); ?>">
																	<?php
																	echo esc_html( $bank_field );
																	if ( $bank_field_required ) {
																		?>
																		<i class="gn-required-icon" aria-hidden="true"></i>
																	<?php } ?>
																</label>
																<input id="gn-bank-<?php echo esc_attr( $bf_key ); ?>" class="<?php echo esc_attr( $bank_field_required ? 'gn-required-field' : '' ); ?>" type="text" name="bank_<?php echo esc_attr( $bf_key ); ?>" autocomplete="off" value="" placeholder="
																<?php
																printf(
																	// translators: %s is field name.
																	esc_attr__( 'Enter %s', 'gigneo' ),
																	esc_attr( strtolower( $bank_field ) )
																);
																?>
																">
															</div>
														</div>
														<?php
													}
												}
											} elseif ( ! empty( $custom_payout_fields[ $pkey ] ) ) {
												if ( is_array( $custom_payout_fields[ $pkey ] ) ) {
													foreach ( $custom_payout_fields[ $pkey ] as $field ) {
														$custom_payout_field_name     = $field['name'] ?? '';
														$custom_payout_field_slug     = $field['slug'] ?? '';
														$custom_payout_field_required = 'yes' === $field['required'];
														if ( ! empty( $custom_payout_field_name ) && ! empty( $custom_payout_field_slug ) ) {
															if ( ! isset( $custom_payout_field_added[ "{$pkey}_{$custom_payout_field_slug}" ] ) ) {
																?>
																<div class="col-md-12">
																	<div class="gn-form-field">
																		<label for="gn-<?php echo esc_attr( $pkey . '-' . $custom_payout_field_slug ); ?>">
																			<?php
																			echo esc_html( $custom_payout_field_name );
																			if ( $custom_payout_field_required ) {
																				?>
																				<i class="gn-required-icon"
																					aria-hidden="true"></i>
																			<?php } ?>
																		</label>
																		<input id="gn-<?php echo esc_attr( $pkey . '-' . $custom_payout_field_slug ); ?>"
																				class="<?php echo esc_attr( $custom_payout_field_required ? 'gn-required-field' : '' ); ?>"
																				type="text"
																				name="<?php echo esc_attr( $pkey . '_' . $custom_payout_field_slug ); ?>"
																				autocomplete="off" value=""
																				placeholder="
																				<?php
																				printf(
																						// translators: %s is field name
																					esc_html__( 'Enter %s', 'gigneo' ),
																					esc_html( strtolower( $custom_payout_field_name ) )
																				);
																				?>
																				">
																	</div>
																</div>
																<?php
																$custom_payout_field_added[ "{$pkey}_{$custom_payout_field_slug}" ] = $custom_payout_field_name;
															}
														}
													}
												}
											}
											?>
										</div>
										<input type="hidden" name="payout_id" value="">
									</fieldset>
								</div>
								<div class="gn-modal-footer">
									<input type="hidden" name="type" value="<?php echo esc_attr( $pkey ); ?>">
									<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-save-payout-<?php echo esc_attr( $pkey ); ?>"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
									<button type="submit" class="gn-btn gn-btn-primary"><?php echo esc_html__( 'Save', 'gigneo' ); ?></button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
			<?php
		}
	}
	?>
</div>
