<?php

if ( !defined( 'ABSPATH' ) ) {
    exit( 'Sorry!' );
}
global $post, $bsd_sca;
// 3.7.5: only offer vendor accounts connected in the store's active Stripe
// mode (plus legacy/undetected) so a live store can't pick a test account.
$spp_active_mode = $bsd_sca->active_stripe_mode();
$get_accounts = $bsd_sca->get_stored_accounts( $spp_active_mode );
$level = 'product';
$get_accounts = apply_filters( 'spp_get_stored_accounts', $get_accounts, $level );
$upgrade_url = SPP_PLUGIN_UPGRADE_URL;
if ( function_exists( 'bsdwcscsp_fs' ) ) {
    $upgrade_url = bsdwcscsp_fs()->get_upgrade_url();
}
$_bsd_spscwt_product_connected_account = get_post_meta( $post->ID, '_bsd_spscwt_product_connected_account', true );
$_bsd_spscwt_product_type = get_post_meta( $post->ID, '_bsd_spscwt_product_type', true );
$_stripe_connect_split_pay_transfer_percentage = get_post_meta( $post->ID, '_stripe_connect_split_pay_transfer_percentage', true );
$_bsd_spscwt_product_amount = get_post_meta( $post->ID, '_bsd_spscwt_product_amount', true );
$_bsd_spscwt_shipping_type = get_post_meta( $post->ID, '_bsd_spscwt_shipping_type', true );
$_bsd_prod_shipping_percentage = get_post_meta( $post->ID, '_bsd_prod_shipping_percentage', true );
$_bsd_spscwt_shipping_amount = get_post_meta( $post->ID, '_bsd_spscwt_shipping_amount', true );
if ( !is_array( $_bsd_spscwt_product_type ) ) {
    $_bsd_spscwt_product_type = array();
}
if ( !is_array( $_stripe_connect_split_pay_transfer_percentage ) ) {
    $_stripe_connect_split_pay_transfer_percentage = array();
}
if ( !is_array( $_bsd_spscwt_product_amount ) ) {
    $_bsd_spscwt_product_amount = array();
}
if ( !is_array( $_bsd_spscwt_shipping_type ) ) {
    $_bsd_spscwt_shipping_type = array();
}
if ( !is_array( $_bsd_prod_shipping_percentage ) ) {
    $_bsd_prod_shipping_percentage = array();
}
if ( !is_array( $_bsd_spscwt_shipping_amount ) ) {
    $_bsd_spscwt_shipping_amount = array();
}
?>
<div id='stripe_connect_split_pay_product_data' class='panel woocommerce_options_panel'>
	<?php 
/*
 * 3.7.4 form-presence marker (sc-7216 — product-save erasure regression).
 *
 * Every save handler in this plugin (legacy bsd_product_data_fields_save +
 * namespaced WooCommerceIntegration::saveProductDataFields) MUST short-circuit
 * when this marker is absent from $_POST. Pre-3.7.4, both handlers fired on
 * every WooCommerce product save and unconditionally overwrote / deleted the
 * SPP meta keys based on whether $_POST carried the SPP fields — which it
 * doesn't when the SPP tab isn't rendered (variable products' parent save,
 * max_input_vars truncation on large forms, JS render failure, or any
 * third-party plugin doing a programmatic wp_update_post / WC REST save).
 *
 * The marker is the unambiguous signal that the SPP product-tab partial
 * actually rendered and the user actually saw + interacted with our fields.
 * If the marker is missing, the SPP meta is left alone — no writes, no
 * deletes, no empty arrays, no data loss.
 *
 * An intentional "clear all rows" workflow still works: the marker is
 * present, the row arrays come through as empty, and the handlers
 * intentionally clear the meta.
 */
?>
	<input type="hidden" name="bsd_spp_product_tab_rendered" value="1" />
	<div class="table-field-main-with-addbtn">
		<div class="table-field-main-max-height">
			<select style="display: none;" id="account_dropdown_html">
				<?php 
if ( !empty( $get_accounts ) ) {
    ?>
					<option value=""><?php 
    esc_html_e( 'Select', 'bsd-split-pay-stripe-connect-woo' );
    ?></option>
					<?php 
    foreach ( $get_accounts as $gak ) {
        ?>
						<option value="<?php 
        echo esc_attr( $gak['bsd_account_id'] );
        ?>"><?php 
        echo esc_html( ( empty( $gak['bsd_account_name'] ) ? $gak['bsd_account_id'] : $gak['bsd_account_name'] ) );
        ?></option>
						<?php 
    }
}
?>

			</select>
			<table class="table-field-main bsd_connect_acc_id_table_simple_prod">
				<?php 
?>
					<table class="table-field-main bsd_connect_acc_id_table_simple_prod ">
						<tbody>
						<tr class="bsd_connect_acc_id_row_simple_prod disabled">
							<td>
								<div class="table-field-row-cover">
									<div class="table-field-row table-field-row-1">
										<div class="table-field-label">
											<span class="table-field-label-th"><?php 
esc_html_e( 'Connected Stripe Account ID', 'bsd-split-pay-stripe-connect-woo' );
?></span>
										</div>
										<div class="table-field-row-inner">
											<div class="table-field-col-12">
														
														<span class="select2 select2-container select2-container--default"
																dir="ltr" style="width: auto;">
															<span class="selection">
																<span class="select2-selection select2-selection--single"
																		aria-haspopup="true" aria-expanded="false"
																		tabindex="0"
																		aria-labelledby="select2-sspscwsca_select_0-container"
																		role="combobox">
																	<span class="select2-selection__rendered"
																			id="select2-sspscwsca_select_0-container"
																			role="textbox" aria-readonly="true" title="">
																		<span class="select2-selection__clear">×</span></span>
																			<span class="select2-selection__arrow"
																					role="presentation">
																				<b role="presentation"></b>
																			</span>
																		</span>
																	</span>
																<span class="dropdown-wrapper"
																		aria-hidden="true"></span>
															</span>
											</div>
										</div>
										<div class="table-icon">
											<button class="copy_account_id round-icon-del-copy round-icon-copy"
													type="button" data-select_id="sspscwsca_select_0">
												<img src="<?php 
echo esc_url( SPP_PLUGIN_URI );
?>assets/copy-account-id.svg"
													alt="copy-account-id"
													title="<?php 
esc_attr_e( 'Copy Account ID', 'bsd-split-pay-stripe-connect-woo' );
?>">
											</button>
										</div>
									</div>
									<div class="table-field-row table-field-row-2">
										<div class="table-field-label">
											<span class="table-field-label-th"><?php 
esc_html_e( 'Product-Specific Transfer Amount', 'bsd-split-pay-stripe-connect-woo' );
?></span>
											<p class="bsd-scsp-helper-text"><?php 
esc_html_e( 'Overrides the', 'bsd-split-pay-stripe-connect-woo' );
?>
												<a href="<?php 
echo esc_url( admin_url( 'admin.php?page=bsd-split-pay-stripe-connect-woo-settings' ) );
?>"
													target="_blank" rel="noopener noreferrer"><?php 
esc_html_e( 'Global Transfer Value', 'bsd-split-pay-stripe-connect-woo' );
?></a> <?php 
esc_html_e( 'settings.', 'bsd-split-pay-stripe-connect-woo' );
?>
											</p>
										</div>
										<div class="table-field-row-inner">
											<div class="table-field-col-6">
												<select class="bsd_spscwt_type" id="bsd_spscwt_type_0">
													<option value="percentage"
															disabled="true"><?php 
esc_html_e( 'Percentage', 'bsd-split-pay-stripe-connect-woo' );
?></option>
													<option value="amount"
															disabled="true"><?php 
esc_html_e( 'Fixed Amount', 'bsd-split-pay-stripe-connect-woo' );
?></option>
												</select>
											</div>
											<div class="table-field-col-6">
												<input type="number" min="0" max="100" step="1" value=""
														placeholder="e.g. 10" class="bsd_spscwtp_input">
											</div>
										</div>
									</div>
									<div class="table-field-row table-field-row-3">
										<div class="table-field-label">
											<span class="table-field-label-th"><?php 
esc_html_e( 'Shipping-Specific Transfer Amount', 'bsd-split-pay-stripe-connect-woo' );
?></span>
											<p class="bsd-scsp-helper-text"><?php 
esc_html_e( 'Overrides the', 'bsd-split-pay-stripe-connect-woo' );
?>
												<a href="<?php 
echo esc_url( admin_url( 'admin.php?page=bsd-split-pay-stripe-connect-woo-account' ) );
?>"
													target="_blank" rel="noopener noreferrer"><?php 
esc_html_e( 'Global Transfer Value', 'bsd-split-pay-stripe-connect-woo' );
?></a> <?php 
esc_html_e( 'settings.', 'bsd-split-pay-stripe-connect-woo' );
?>
											</p>
										</div>

										<div class="table-field-row-inner">
											<div class="table-field-col-6">
												<select class="bsd_spscwt_shipping_type"
														id="bsd_spscwt_shipping_type_0">
													<option value="percentage"
															disabled="true"><?php 
esc_html_e( 'Percentage', 'bsd-split-pay-stripe-connect-woo' );
?></option>
													<option value="amount"
															disabled="true"><?php 
esc_html_e( 'Fixed Amount', 'bsd-split-pay-stripe-connect-woo' );
?></option>
												</select>
											</div>
											<div class="table-field-col-6">
												<input type="number" min="0" max="100" step="1" value=""
														placeholder="e.g. 10" class="bsd_spscwtp_shipping_input">
											</div>
										</div>
										<div class="table-icon">
											<button class="remove_account round-icon-del-copy round-icon-del"
													data-select_row_id="0" type="button">
												<img src="<?php 
echo esc_url( SPP_PLUGIN_URI );
?>assets/remove-account-id.png"
													alt="remove-account-id"
													title="<?php 
esc_attr_e( 'Remove account', 'bsd-split-pay-stripe-connect-woo' );
?>">
											</button>
										</div>
									</div>
								</div>
							</td>
						</tr>

						<tr class="multi-account-upgrade-row text-center">
							<td colspan="3">
								<p class="bsd-scsp-helper-text bsd-ps-helper-text"><?php 
esc_html_e( 'To split product-specific payments between multiple Stripe accounts, please', 'bsd-split-pay-stripe-connect-woo' );
?>
									<a href="<?php 
echo esc_url( $upgrade_url );
?>"><?php 
esc_html_e( 'Upgrade >', 'bsd-split-pay-stripe-connect-woo' );
?></a>.
								</p>
							</td>
						</tr>

						</tbody>
					</table>


					<?php 
?>


			</table>
		</div>
		<div class="table-field-btn-row">
			<?php 
?>
				<button class="add_new_account_for_prod table-field-btn-addnew disabled" type="button">
					<img src="<?php 
echo esc_url( SPP_PLUGIN_URI );
?>assets/add-new-account-id.png" alt="add-new-account-id"
						title="<?php 
esc_attr_e( 'Add new account', 'bsd-split-pay-stripe-connect-woo' );
?>">
					<?php 
esc_html_e( 'Add new account', 'bsd-split-pay-stripe-connect-woo' );
?>
				</button>
				<?php 
?>

		</div>
	</div>
</div>
