<?php

// phpcs:ignoreFile
defined( 'ABSPATH' ) || exit;
global $bsd_sca, $wpdb;
$table_prefix = $wpdb->prefix;
$table_name = $table_prefix . SPP_CONNECTED_ID_TABLE;
$connected_account_query = "select * from " . $table_name . " ";
$connected_account_results = $wpdb->get_results( $connected_account_query, ARRAY_A );
$woocommerce_stripe_settings = get_option( "woocommerce_stripe_settings" );
$bsd_split_pay_stripe_connect_woo_stripe_connected_account = get_option( 'bsd_split_pay_stripe_connect_woo_stripe_connected_account' );
// 3.7.5: the store's active Stripe mode — used to filter the account
// dropdowns so only same-mode (plus legacy/undetected) vendor accounts show.
$spp_active_mode = ( !empty( $bsd_sca ) ? $bsd_sca->active_stripe_mode() : '' );
if ( empty( $bsd_sca ) ) {
    $get_accounts = [];
} else {
    $get_accounts = $bsd_sca->get_stored_accounts( $spp_active_mode );
}
$level = 'global';
$get_accounts = apply_filters( "spp_get_stored_accounts", $get_accounts, $level );
$need_to_gray_tabs = ( !empty( $bsd_sca ) && $bsd_sca->is_stripe_enabled_and_configured() ? '' : 'grayed-tab' );
$bsd_spscwt_type = get_option( 'bsd_spscwt_type' );
$bsd_split_pay_stripe_connect_woo_transfer_percentage = get_option( 'bsd_split_pay_stripe_connect_woo_transfer_percentage' );
$bsd_spscwt_amount = get_option( 'bsd_spscwt_amount' );
$upgrade_url = SPP_PLUGIN_UPGRADE_URL;
if ( function_exists( 'bsdwcscsp_fs' ) ) {
    $upgrade_url = bsdwcscsp_fs()->get_upgrade_url();
}
?>
<div class='bsd-scsp-settings <?php 
echo $need_to_gray_tabs;
?>'>
    <input type='hidden' name='tab_name' value='main'>
    <?php 
settings_fields( 'bsd_scsp_options_main' );
$text = sprintf( esc_html__( 'The Connected Stripe Account ID should be the Account ID for one of your connected accounts. For questions about how to configure the settings below, please refer to our %sDocumentation%s.', 'bsd-split-pay-stripe-connect-woo' ), '<a href="https://docs.splitpayplugin.com/" target="_blank" rel="noopener noreferrer">', '</a>' );
echo '<p>' . wp_kses_post( $text ) . ' </p>';
// 3.7.5: prominent active-mode badge. The account dropdowns below are
// filtered to this mode, so make it unmistakable which one is live.
if ( !empty( $bsd_sca ) ) {
    echo '<p style="margin:10px 0 4px;">' . $bsd_sca->mode_badge( $spp_active_mode ) . ' <span style="color:#555;font-size:12px;">' . esc_html__( 'Only connected accounts onboarded in this mode appear in the dropdowns below.', 'bsd-split-pay-stripe-connect-woo' ) . '</span></p>';
}
/** table started */
?>
    
    <table class="form-table bsd-scsp-settings-table" role="presentation">
        <thead>
        <tr>
            <td scope="row">
                <strong><?php 
esc_html_e( 'Connected Stripe Account ID', 'bsd-split-pay-stripe-connect-woo' );
?></strong>
                <p class="bsd-scsp-helper-text">
					<?php 
printf( esc_html__( 'Manage %1$sIntegrations & Connected Accounts%2$s.', 'bsd-split-pay-stripe-connect-woo' ), '<a href="' . esc_url( admin_url( 'admin.php?page=bsd-split-pay-stripe-connect-woo-settings&tab=integrations' ) ) . '">', '</a>' );
?>
                </p>

               
            </td>
            <td scope="row">
                <strong><?php 
esc_html_e( 'Global Transfer Value', 'bsd-split-pay-stripe-connect-woo' );
?></strong>
                <p class="bsd-scsp-helper-text"><?php 
echo __( "Used if a <a href='https://docs.splitpayplugin.com/features/how-to-transfer-payments/product-level-transfers/' target='_blank' rel='noopener noreferrer'>product-level transfer value</a> is not set.", 'bsd-split-pay-stripe-connect-woo' );
?></p>
            </td>
            <td scope="row">
                <strong><?php 
esc_html_e( 'Global Shipping Transfer Value', 'bsd-split-pay-stripe-connect-woo' );
?></strong>
                <p class="bsd-scsp-helper-text"><?php 
echo __( "Used if a <a href='https://docs.splitpayplugin.com/features/how-to-transfer-shipping-fees/product-level-shipping-transfers' target='_blank' rel='noopener noreferrer'>product-level shipping transfer value</a> is not set.", "bsd-split-pay-stripe-connect-woo" );
?></p>
            </td>
            <td scope="row"></td>
        </tr>
        </thead>
        <tbody>
		<?php 
if ( empty( $connected_account_results ) ) {
    ?>
            <tr class="bsd_connect_acc_first_row bsd_connect_acc_id_row" data-crow_index="0">
                <td>
                    <div class="bsd_stripe_select_wrapper" style="max-width: 470px;min-width: 470px;">
                        <select name="bsd_split_pay_stripe_connect_woo_stripe_connected_account[0]"
                                id="sspscwsca_select_0" class="sspscwsca_select" style="width: 420px;" aria-label="<?php 
    esc_attr_e( 'Connected Stripe account', 'bsd-split-pay-stripe-connect-woo' );
    ?>">
							<?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 ) {
            $display_option = "";
            $has_name = !empty( $gak["bsd_account_name"] ) && $gak["bsd_account_name"] !== $gak["bsd_account_id"];
            $has_email = isset( $gak["bsd_account_email"] ) && !empty( $gak["bsd_account_email"] );
            if ( $has_name && $has_email ) {
                $display_option .= $gak["bsd_account_name"] . " - " . $gak["bsd_account_email"];
            } elseif ( $has_name ) {
                $display_option .= $gak["bsd_account_name"];
            } elseif ( $has_email ) {
                $display_option .= $gak["bsd_account_email"];
            } else {
                $display_option .= trim( $gak["bsd_account_id"] );
            }
            ?>
                                        <option value="<?php 
            echo esc_attr( trim( $gak['bsd_account_id'] ) );
            ?>"><?php 
            echo esc_html( $display_option );
            ?></option><?php 
            // PT-fix-3.7.5 (security #8A): Stripe-controlled account name/email could contain HTML (Stripe permits </option><script>...); esc_html() neutralizes XSS in the dropdown text content.
            ?>
									<?php 
        }
    }
    ?>

                        </select>
                        <button class="copy_account_id" aria-label="<?php 
    esc_attr_e( 'Copy account ID', 'bsd-split-pay-stripe-connect-woo' );
    ?>" data-select_id="sspscwsca_select_0" type="button">
                            <img src="<?php 
    echo SPP_PLUGIN_URI;
    ?>assets/copy-account-id.svg"
                                 alt="copy-account-id"
                                 title="<?php 
    esc_html_e( "Copy Account ID", "bsd-split-pay-stripe-connect-woo" );
    ?>"/>
                        </button>
                    </div>
                </td>

                <td>

                    <select name="bsd_spscwt_type[0]" class="bsd_spscwt_type" id="bsd_spscwt_type_0" aria-label="<?php 
    esc_attr_e( 'Transfer type', 'bsd-split-pay-stripe-connect-woo' );
    ?>">
                        <option value="percentage"><?php 
    esc_html_e( "Percentage", "bsd-split-pay-stripe-connect-woo" );
    ?></option>
						<?php 
    ?>
                            <option value="amount"
                                    disabled="true"><?php 
    esc_html_e( "Fixed Amount", "bsd-split-pay-stripe-connect-woo" );
    ?></option>
							<?php 
    ?>
                    </select>

					<?php 
    ?>
                        <div class="percentage_wrapper">
                            <input type='number' min='0' max="100" step="1"
                                   id='bsd_split_pay_stripe_connect_woo_transfer_percentage_0'
                                   name='bsd_split_pay_stripe_connect_woo_transfer_percentage[0]' value=""
                                   placeholder="e.g. 10" class="bsd_spscwtp_input "/>

                        </div>


						<?php 
    ?>


					<?php 
    ?>
                        <p class="bsd-scsp-helper-text"><?php 
    esc_html_e( "For Fixed amount transfers, 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>
					<?php 
    ?>

                </td>

                <td>
					<?php 
    ?>
                        <select class="bsd_spscwt_type bsd_shipping_type" disabled="true">
                            <option value="percentage"><?php 
    esc_html_e( "Percentage", "bsd-split-pay-stripe-connect-woo" );
    ?></option>
                            <option value="amount"><?php 
    esc_html_e( "Fixed Amount", "bsd-split-pay-stripe-connect-woo" );
    ?></option>
                        </select>
						<?php 
    ?>


					<?php 
    ?>
                        <div class="percentage_wrapper">
                            <input type='number' min='0' max="100" step="1" placeholder="e.g. 10"
                                   class="bsd_spscwtp_input" disabled="true"/>

                        </div>


						<?php 
    ?>


					<?php 
    ?>
                        <p class="bsd-scsp-helper-text"><?php 
    esc_html_e( "For Shipping transfers, 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>
					<?php 
    ?>

                </td>

                <td>

                    <input type="hidden" name="bsd_connected_acc_action" value="add">


                </td>
            </tr>
		<?php 
} else {
    foreach ( $connected_account_results as $cark => $carv ) {
        if ( !\BSD_Split_Pay_Stripe_Connect_Woo\bsdwcscsp_fs()->can_use_premium_code() ) {
            if ( $cark > 0 ) {
                continue;
            }
        }
        ?>
                <tr class="<?php 
        echo ( $cark == 0 ? "bsd_connect_acc_first_row" : "" );
        ?> bsd_connect_acc_id_row"
                    data-crow_index="<?php 
        echo $cark;
        ?>">
                    <td>
                        <div class="bsd_stripe_select_wrapper" style="max-width: 470px; min-width: 470px;">
                            <select name="bsd_split_pay_stripe_connect_woo_stripe_connected_account[<?php 
        echo $cark;
        ?>]"
                                    id="sspscwsca_select_<?php 
        echo $cark;
        ?>" class="sspscwsca_select" style="width: 420px;" aria-label="<?php 
        esc_attr_e( 'Connected Stripe account', 'bsd-split-pay-stripe-connect-woo' );
        ?>">
									<?php 
        /*
         * 3.7.5: if this row's saved account is the wrong
         * Stripe mode (filtered out of $get_accounts) or was
         * never synced, render it as a labelled selected
         * option so it stays visible + savable. Without this,
         * a mode switch would silently drop the selection and
         * the next save would wipe the mapping (sc-7201 class).
         */
        $spp_g_saved = ( isset( $carv['bsd_connected_account_id'] ) ? trim( (string) $carv['bsd_connected_account_id'] ) : '' );
        $spp_g_in_list = false;
        if ( $spp_g_saved !== '' && !empty( $get_accounts ) ) {
            foreach ( $get_accounts as $spp_g_acc ) {
                if ( isset( $spp_g_acc['bsd_account_id'] ) && trim( (string) $spp_g_acc['bsd_account_id'] ) === $spp_g_saved ) {
                    $spp_g_in_list = true;
                    break;
                }
            }
        }
        if ( !$spp_g_in_list && $spp_g_saved !== '' && !empty( $bsd_sca ) ) {
            printf( "<option value=\"%s\" selected='selected'>%s</option>", esc_attr( $spp_g_saved ), esc_html( $bsd_sca->unavailable_account_label( $spp_g_saved, $spp_active_mode ) ) );
        }
        ?>
								<?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 ) {
                $display_option = "";
                $has_name = !empty( $gak["bsd_account_name"] ) && $gak["bsd_account_name"] !== $gak["bsd_account_id"];
                $has_email = isset( $gak["bsd_account_email"] ) && !empty( $gak["bsd_account_email"] );
                if ( $has_name && $has_email ) {
                    $display_option .= $gak["bsd_account_name"] . " - " . $gak["bsd_account_email"];
                } elseif ( $has_name ) {
                    $display_option .= $gak["bsd_account_name"];
                } elseif ( $has_email ) {
                    $display_option .= $gak["bsd_account_email"];
                } else {
                    $display_option .= trim( $gak["bsd_account_id"] );
                }
                ?>
                                        <option value="<?php 
                echo esc_attr( trim( $gak['bsd_account_id'] ) );
                ?>" <?php 
                echo ( trim( $gak['bsd_account_id'] ) == $carv['bsd_connected_account_id'] ? "selected='selected'" : '' );
                ?>><?php 
                echo esc_html( $display_option );
                ?></option><?php 
                // PT-fix-3.7.5 (security #8A): see comment above; same XSS vector for the edit row.
                ?>
										<?php 
            }
        }
        ?>

                            </select>
                            <button class="copy_account_id" aria-label="<?php 
        esc_attr_e( 'Copy account ID', 'bsd-split-pay-stripe-connect-woo' );
        ?>" data-select_id="sspscwsca_select_<?php 
        echo $cark;
        ?>"
                                    type="button">
                                <img src="<?php 
        echo SPP_PLUGIN_URI;
        ?>assets/copy-account-id.svg"
                                     alt="copy-account-id"
                                     title="<?php 
        esc_html_e( "Copy Account ID", "bsd-split-pay-stripe-connect-woo" );
        ?>"/>
                            </button>
                        </div>
                    </td>

                    <td>
                        <select name="bsd_spscwt_type[<?php 
        echo $cark;
        ?>]" class="bsd_spscwt_type"
                                id="bsd_spscwt_type_<?php 
        echo $cark;
        ?>" aria-label="<?php 
        esc_attr_e( 'Transfer type', 'bsd-split-pay-stripe-connect-woo' );
        ?>">
                            <option value="percentage" <?php 
        echo ( "percentage" == $carv["bsd_spscwt_type"] ? "selected='selected'" : "" );
        ?>><?php 
        esc_html_e( "Percentage", "bsd-split-pay-stripe-connect-woo" );
        ?></option>
							<?php 
        ?>
                                <option value="amount"
                                        disabled="true"><?php 
        esc_html_e( "Fixed Amount", "bsd-split-pay-stripe-connect-woo" );
        ?></option>
								<?php 
        ?>
                        </select>

						<?php 
        ?>
                            <div class="percentage_wrapper">
                                <input type='number' min='0' max="100" step="1"
                                       id='bsd_split_pay_stripe_connect_woo_transfer_percentage_<?php 
        echo $cark;
        ?>'
                                       name='bsd_split_pay_stripe_connect_woo_transfer_percentage[<?php 
        echo $cark;
        ?>]'
                                       value="<?php 
        echo $carv["bsd_spscwt_percentage_amount"];
        ?>"
                                       placeholder="e.g. 10" class="bsd_spscwtp_input"/>

                            </div>


							<?php 
        ?>



						<?php 
        ?>
                            <p class="bsd-scsp-helper-text"><?php 
        esc_html_e( "For Fixed amount transfers, 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>
						<?php 
        ?>

                    </td>

                    <td>
						<?php 
        ?>
                            <select class="bsd_spscwt_type bsd_shipping_type" disabled="true">
                                <option value="percentage"><?php 
        echo __( "Percentage", 'bsd-split-pay-stripe-connect-woo' );
        ?></option>
                                <option value="amount"><?php 
        echo __( "Fixed Amount", 'bsd-split-pay-stripe-connect-woo' );
        ?></option>
                            </select>
							<?php 
        ?>
						<?php 
        ?>
                            <div class="percentage_wrapper">
                                <input type='number' min='0' max="100" step="1" disabled="true" placeholder="e.g. 10"
                                       class="bsd_spscwtp_input"/>
                            </div>
							<?php 
        ?>



						<?php 
        ?>
                            <p class="bsd-scsp-helper-text"><?php 
        esc_html_e( "For Shipping transfers, 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>
						<?php 
        ?>

                    </td>

                    <td>
                        <input type="hidden" name="bsd_connected_acc_primary_id[<?php 
        echo $cark;
        ?>]"
                               value="<?php 
        echo $carv["bsd_connected_id"];
        ?>" class="bsd_connected_acc_primary_id">


						<?php 
        ?>


                    </td>
                </tr>
				<?php 
    }
}
?>

		<?php 
?>
            <tr class=" multi-account-upgrade-row disabled">
                <td>
                    <div class="bsd_stripe_select_wrapper" style="min-width: 470px;">
                                    
                                    <span class="select2 select2-container select2-container--default" dir="ltr"
                                          data-select2-id="4" style="width: 205.6px;">
                                    <span class="selection">
                                        <span class="select2-selection select2-selection--single" role="combobox"
                                              aria-haspopup="true" aria-expanded="false" tabindex="0"
                                              aria-disabled="false"
                                              aria-labelledby="select2-sspscwsca_select_1-container">
                                        <span class="select2-selection__rendered"
                                              id="select2-sspscwsca_select_1-container" role="textbox"
                                              aria-readonly="true" title="">
                                        <span class="select2-selection__clear" title="Remove all items"
                                              data-select2-id="6">x</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>
                        <button class="copy_account_id" aria-label="<?php 
esc_attr_e( 'Copy account ID', 'bsd-split-pay-stripe-connect-woo' );
?>" type="button">
                            <img src="<?php 
echo SPP_PLUGIN_URI;
?>assets/copy-account-id.svg"
                                 alt="copy-account-id" title="<?php 
esc_html_e( "Copy Account ID", 'bsd-split-pay-stripe-connect-woo' );
?>"/>
                        </button>
                    </div>
                </td>
                <td>
                    <select class="bsd_spscwt_type">
                        <option value="percentage">Percentage</option>
                        <option value="amount" selected="selected">Fixed Amount</option>
                    </select>

                    <input type="number" placeholder="e.g. 20" class="bsd_spscwt_amount ">
                </td>
                <td>
                    <select class="bsd_spscwt_type">
                        <option value="percentage">Percentage</option>
                        <option value="amount" selected="selected">Fixed Amount</option>
                    </select>

                    <input type="number" placeholder="e.g. 20" class="bsd_spscwt_amount ">


                </td>
                <td>


                    <button class="remove_account" aria-label="<?php 
esc_attr_e( 'Remove account', 'bsd-split-pay-stripe-connect-woo' );
?>" type="button">
                        <img src="<?php 
echo SPP_PLUGIN_URI;
?>assets/remove-account-id.png"
                             alt="remove-account-id"
                             title="<?php 
esc_html_e( "Remove account", 'bsd-split-pay-stripe-connect-woo' );
?>"/>
                    </button>
                </td>
            </tr>
            <tr class="multi-account-upgrade-row text-center">
                <td colspan="4">
                    <p class="bsd-scsp-helper-text"><?php 
esc_html_e( "To split 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>
			<?php 
?>


        </tbody>
    </table>

    <input type="hidden" name="bsd_connected_acc_primary_remove_ids" id="bsd_connected_acc_primary_remove_ids"
           value=""/>

	<?php 
$can_use_premium_code = false;
$documentation_link = '';
$upgrade_text = sprintf( __( '<a href="%s" target="_blank" rel="noopener noreferrer">Upgrade ></a>', 'bsd-split-pay-stripe-connect-woo' ), $upgrade_url );
$sending_meta = get_option( "sending_meta", false );
$transfer_email = get_option( "transfer_email", false );
$transfer_taxes = get_option( "transfer_taxes", false );
$tax_transfer_type = get_option( "tax_transfer_type", false );
$coupon_adjust_transfers = get_option( "coupon_adjust_transfers", false );
$spp_delayed_transfers_enabled = get_option( "spp_delayed_transfers_enabled", '0' );
$is_disabled = "";
if ( !$can_use_premium_code ) {
    $is_disabled = "disabled";
}
$tax_transfer_type_dropdown = '<select name="tax_transfer_type" id="tax_transfer_type" aria-label="' . esc_attr__( 'Tax transfer type', 'bsd-split-pay-stripe-connect-woo' ) . '" ' . $is_disabled . '>';
$selected = "";
if ( $tax_transfer_type == "all" ) {
    $selected = "selected='selected'";
}
$tax_transfer_type_dropdown .= '<option value="all" ' . $selected . '>' . esc_html__( "All (100%)", 'bsd-split-pay-stripe-connect-woo' ) . '</option>';
$selected = "";
if ( $tax_transfer_type == "partial" ) {
    $selected = "selected='selected'";
}
$tax_transfer_type_dropdown .= '<option value="partial" ' . $selected . '>' . esc_html__( "Partial", 'bsd-split-pay-stripe-connect-woo' ) . '</option>';
$tax_transfer_type_dropdown .= ' </select>';
$settings = [
    'transfer_taxes'                => [
        'label'              => sprintf( __( 'Transfer %s Tax for each product to Connected Accounts.', 'bsd-split-pay-stripe-connect-woo' ), $tax_transfer_type_dropdown ),
        'description'        => sprintf( __( 'Please read our <a href="%s" target="_blank" rel="noopener noreferrer">documentation</a> for details.', 'bsd-split-pay-stripe-connect-woo' ), esc_url( 'https://docs.splitpayplugin.com/features/tax-handling/' ) ),
        'checked'            => ( $can_use_premium_code && $transfer_taxes ? "checked = checked" : '' ),
        'disabled'           => ( !$can_use_premium_code ? "disabled" : '' ),
        'upgrade_text'       => ( !$can_use_premium_code ? $upgrade_text : '' ),
        'is_visible_in_free' => true,
        'class'              => '',
        'type'               => 'checkbox',
    ],
    'transfer_email'                => [
        'label'              => __( 'Enable a Transfer Confirmation email after transactions occur.', 'bsd-split-pay-stripe-connect-woo' ),
        'description'        => sprintf( __( 'Please read our <a href="%s" target="_blank" rel="noopener noreferrer">documentation</a> for details.', 'bsd-split-pay-stripe-connect-woo' ), esc_url( 'https://docs.splitpayplugin.com/features/transfer-confirmation-email/' ) ),
        'checked'            => ( $can_use_premium_code && $transfer_email ? "checked = checked" : '' ),
        'disabled'           => ( !$can_use_premium_code ? "disabled" : '' ),
        'upgrade_text'       => ( !$can_use_premium_code ? $upgrade_text : '' ),
        'is_visible_in_free' => true,
        'class'              => '',
        'type'               => 'checkbox',
    ],
    'sending_meta'                  => [
        'label'              => __( 'Include Order Details in Stripe Transfer Metadata.', 'bsd-split-pay-stripe-connect-woo' ),
        'description'        => sprintf( __( 'Please read our <a href="%s" target="_blank" rel="noopener noreferrer">documentation</a> for details.', 'bsd-split-pay-stripe-connect-woo' ), 'https://docs.splitpayplugin.com/features/stripe-metadata' ),
        'checked'            => ( $can_use_premium_code && $sending_meta ? "checked = checked" : '' ),
        'disabled'           => ( !$can_use_premium_code ? "disabled" : '' ),
        'upgrade_text'       => ( !$can_use_premium_code ? $upgrade_text : '' ),
        'is_visible_in_free' => true,
        'class'              => '',
        'type'               => 'checkbox',
    ],
    'coupon_adjust_transfers'       => [
        'label'              => __( 'Adjust transfer amounts based on coupon discounts.', 'bsd-split-pay-stripe-connect-woo' ),
        'description'        => sprintf( __( 'Please read our <a href="%s" target="_blank" rel="noopener noreferrer">documentation</a> for details.', 'bsd-split-pay-stripe-connect-woo' ), esc_url( 'https://docs.splitpayplugin.com/features/coupon-handling/' ) ),
        'checked'            => ( $can_use_premium_code && $coupon_adjust_transfers ? "checked = checked" : '' ),
        'disabled'           => ( !$can_use_premium_code ? "disabled" : '' ),
        'upgrade_text'       => ( !$can_use_premium_code ? $upgrade_text : '' ),
        'is_visible_in_free' => true,
        'class'              => '',
        'type'               => 'checkbox',
    ],
    'spp_delayed_transfers_enabled' => [
        'label'              => __( 'Delay transfers until order is marked Completed (WooCommerce).', 'bsd-split-pay-stripe-connect-woo' ),
        'description'        => __( 'When enabled, Stripe transfers are not created on payment. Instead they are released the moment the order status moves to Completed. Useful for fulfilment-gated payouts.', 'bsd-split-pay-stripe-connect-woo' ),
        'checked'            => ( $can_use_premium_code && $spp_delayed_transfers_enabled === '1' ? "checked = checked" : '' ),
        'disabled'           => ( !$can_use_premium_code ? "disabled" : '' ),
        'upgrade_text'       => ( !$can_use_premium_code ? $upgrade_text : '' ),
        'is_visible_in_free' => true,
        'class'              => '',
        'type'               => 'checkbox',
    ],
];
?>
        <div class="form-table sps_connect_accounts">
			<?php 
// 3.7.5 static-analysis: every entry in $settings sets is_visible_in_free=true
// (see literal above), so the per-item if was dead.
foreach ( $settings as $key => $setting ) {
    ?>
                <div class="form-block-row form-block-checkbox-row <?php 
    echo $setting['class'];
    ?>">
                    <div class="form-block-checkbox">
                        <div class="form-block-checkbox">
                            <input type="checkbox" name="<?php 
    echo $key;
    ?>" id="<?php 
    echo $key;
    ?>"
                                   value="1" <?php 
    echo $setting['checked'];
    ?>  <?php 
    echo $setting['disabled'];
    ?>/>
                            <label for="<?php 
    echo esc_attr( $key );
    ?>"><?php 
    echo "{$setting['label']} <i>{$setting['upgrade_text']}</i>";
    ?> </label>
                        </div>
                    </div>
                    <div class="form-block-nots">
						<?php 
    echo $setting['description'];
    ?>
                    </div>
                </div>
			<?php 
}
?>
        </div>
        <?php 
?>
    <div class="form-block-submit">
		<?php 
submit_button();
?>
    </div>

	<?php 
/*
 * 3.7.5 (PT-9): the "Integration Setup Required" notice text talks
 * about a supported e-commerce plugin (WooCommerce or FluentCart)
 * being installed — but the previous gate counted stored
 * connected accounts (`count( $bsd_sca->get_stored_accounts() )`).
 * That meant the notice fired any time there were 0 synced accounts,
 * even on sites where WC was clearly active. The Connected Accounts
 * state already lives on the Integrations tab, so this notice's only
 * job is to surface a TRULY missing supported integration. Gate it
 * on that, not on the accounts count.
 */
$_spp_has_supported_integration = class_exists( 'WooCommerce' ) || defined( 'FLUENTCART_VERSION' );
if ( !$_spp_has_supported_integration ) {
    ?>
	<div class="notice notice-info" style="max-width:700px;margin:20px 0;padding:12px 16px;">
		<p><strong><?php 
    esc_html_e( 'Integration Setup Required', 'bsd-split-pay-stripe-connect-woo' );
    ?></strong></p>
		<p>
			<?php 
    printf( esc_html__( 'To use Split Pay, you must have a supported e-commerce plugin (WooCommerce or FluentCart) installed with a Stripe payment gateway configured. Visit the %1$sIntegrations tab%2$s to verify your setup, sync connected accounts, and configure gateway-specific settings.', 'bsd-split-pay-stripe-connect-woo' ), '<a href="' . esc_url( admin_url( 'admin.php?page=bsd-split-pay-stripe-connect-woo-settings&tab=integrations' ) ) . '">', '</a>' );
    ?>
		</p>
	</div>
	<?php 
}
?>

	<?php 
$_spp_is_premium = function_exists( 'BSD_Split_Pay_Stripe_Connect_Woo\\bsdwcscsp_fs' ) && \BSD_Split_Pay_Stripe_Connect_Woo\bsdwcscsp_fs()->is__premium_only() && \BSD_Split_Pay_Stripe_Connect_Woo\bsdwcscsp_fs()->can_use_premium_code();
if ( !$_spp_is_premium ) {
    ?>
		<div style="max-width:700px;margin:20px 0;padding:16px 24px;background:linear-gradient(135deg,#f0f0f1 0%,#e8edf3 100%);border:1px solid #c3c4c7;border-left:4px solid #2271b1;border-radius:4px;display:flex;align-items:center;justify-content:space-between;gap:16px;">
			<p style="margin:0;color:#1d2327;font-size:13px;"><?php 
    esc_html_e( 'Product-level split pay configuration and other useful features below require PRO.', 'bsd-split-pay-stripe-connect-woo' );
    ?></p>
			<a href="<?php 
    echo esc_url( admin_url( 'admin.php?page=bsd-split-pay-stripe-connect-woo-settings-pricing' ) );
    ?>" class="button button-primary" style="white-space:nowrap;">
				<?php 
    esc_html_e( 'Upgrade to PRO', 'bsd-split-pay-stripe-connect-woo' );
    ?>
			</a>
		</div>
	<?php 
}
?>

	<?php 
/*
 * 3.7.5 — Uninstall data-retention toggle.
 *
 * Defaults to UNCHECKED — safe. Customer-initiated uninstall deletes
 * the 23 wp_options the plugin owns (settings, schema-version
 * sentinels) but PRESERVES the three custom tables
 * (bsd_scsp_transfer_log, bsd_strp_accnt, bsd_strp_connected_id).
 * Connected accounts, transfer history, and configured vendor
 * splits survive a remove → reinstall cycle.
 *
 * Customers who want a full wipe can opt in BEFORE clicking "Delete
 * plugin" in WP Plugins. Once the checkbox is saved + the plugin is
 * uninstalled, the data is gone — no undo. The red warning callout
 * below is shown via JS only when the box is checked; the static
 * label + helper text are always visible.
 */
$_spp_wipe_on_uninstall = get_option( 'spp_uninstall_remove_all_data', '0' );
$_spp_wipe_checked = $_spp_wipe_on_uninstall === '1' || $_spp_wipe_on_uninstall === 1 || $_spp_wipe_on_uninstall === true;
?>
	<div id="spp-uninstall-danger-zone" style="max-width:700px;margin:40px 0 20px;padding:0;">
		<h3 style="margin:0 0 8px;color:#1d2327;font-size:15px;font-weight:600;">
			<?php 
esc_html_e( 'Uninstall behavior', 'bsd-split-pay-stripe-connect-woo' );
?>
		</h3>
		<p style="margin:0 0 12px;color:#50575e;font-size:13px;">
			<?php 
esc_html_e( 'By default, removing this plugin keeps your transfer history, connected vendor accounts, and product-level vendor splits intact — so reinstalling restores your data automatically.', 'bsd-split-pay-stripe-connect-woo' );
?>
		</p>

		<label style="display:flex;align-items:flex-start;gap:8px;cursor:pointer;font-size:13px;color:#1d2327;">
			<input
				type="checkbox"
				id="spp_uninstall_remove_all_data"
				name="spp_uninstall_remove_all_data"
				value="1"
				<?php 
checked( $_spp_wipe_checked, true );
?>
				style="margin-top:2px;"
			/>
			<span>
				<strong><?php 
esc_html_e( 'Delete all plugin data on uninstall', 'bsd-split-pay-stripe-connect-woo' );
?></strong><br/>
				<span style="color:#50575e;">
					<?php 
esc_html_e( 'When checked, uninstalling Split Pay will permanently remove every transfer log row, every connected vendor account, and every per-product vendor split this plugin has stored.', 'bsd-split-pay-stripe-connect-woo' );
?>
				</span>
			</span>
		</label>

		<div
			id="spp-uninstall-wipe-warning"
			role="alert"
			style="<?php 
echo ( $_spp_wipe_checked ? '' : 'display:none;' );
?>margin-top:12px;padding:14px 16px;background:#fef2f2;border:1px solid #fecaca;border-left:4px solid #d63638;border-radius:4px;color:#7a1a1d;font-size:13px;line-height:1.5;">
			<strong style="display:block;margin-bottom:4px;font-size:13px;color:#7a1a1d;">
				<?php 
esc_html_e( '⚠ Warning — irreversible', 'bsd-split-pay-stripe-connect-woo' );
?>
			</strong>
			<?php 
esc_html_e( 'Once the plugin is uninstalled with this option enabled, your Split Pay transfer history, connected accounts, and configured transfer values may NOT be recoverable. Stripe-side transfer records will remain in your Stripe Dashboard, but the in-WordPress mapping between orders, vendors, and split-pay configuration will be lost.', 'bsd-split-pay-stripe-connect-woo' );
?>
		</div>
	</div>

	<script type="text/javascript">
		(function() {
			var cb = document.getElementById('spp_uninstall_remove_all_data');
			var warn = document.getElementById('spp-uninstall-wipe-warning');
			if (cb && warn) {
				cb.addEventListener('change', function() {
					warn.style.display = cb.checked ? '' : 'none';
				});
			}
		})();
	</script>

</div>
