<?php
defined( 'ABSPATH' ) || exit;

// Get total from template args
$monto_total = isset( $total ) ? (float) $total : 0;

// Configuration
$configuracion = get_option( 'woocommerce_cetelem_cuotas_settings', [] );

$testing   = ( isset( $configuracion['testing'] ) && $configuracion['testing'] === 'no' ) ? 'https://ecommercecalculadora.cetelem.es' : 'https://test.cetelem.es';
$codcentro = isset( $configuracion['codcentro'] ) ? $configuracion['codcentro'] : '';
?>

<div id="eCalculadoraCetelemEnCuotas"></div>

<script type="text/javascript">
/* enCuotas Calculator - Variables must be set BEFORE external script loads */
window.codCentro = '<?php echo esc_js( $codcentro ); ?>';
window.cantidad  = '<?php echo esc_js( $monto_total ); ?>';
window.server    = '<?php echo esc_js( $testing ); ?>';

/* Load external Cetelem Mix calculator script dynamically AFTER variables are set */
(function() {
    var s    = document.createElement('script');
    s.src    = '<?php echo esc_url( $testing . '/eCommerceCalculadora/resources/js/mix/eCalculadoraCetelemMix.js' ); ?>';
    s.async  = false;
    document.head.appendChild(s);
})();
</script>
