<?php
/**
 * Mount point for the React coupon designer.
 * The bundle reads window.SuiteMkt3kCoupon and writes back via REST POST /coupons.
 *
 * @var array|null $coupon
 * @var string     $nonce
 * @package SuiteMkt3k
 */
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$row = $coupon ?? array(
	'id'             => 0,
	'name'           => '',
	'marketing_text' => '',
	'format_type'    => 'classic',
	'discount_type'  => 'percent',
	'discount_value' => 10,
	'usage_limit'    => '',
	'status'         => 'draft',
	'starts_at'      => '',
	'ends_at'        => '',
	'design'         => \SuiteMkt3k\Modules\Coupons\Coupon_Manager::default_design(),
);

$products = array();
if ( \SuiteMkt3k\Compat\WooCommerce_Check::is_active() ) {
	$wc_products = get_posts(
		array(
			'post_type'      => 'product',
			'post_status'    => array( 'publish', 'private' ),
			'numberposts'    => 200,
			'orderby'        => 'title',
			'order'          => 'ASC',
			'suppress_filters' => false,
		)
	);
	foreach ( (array) $wc_products as $p ) {
		$products[] = array(
			'id'    => (int) $p->ID,
			'title' => (string) $p->post_title,
		);
	}
}
$currencies = \SuiteMkt3k\Modules\Coupons\Coupon_Manager::currency_options();
?>
<div id="suitemkt3k-coupon-designer"
	data-coupon="<?php echo esc_attr( wp_json_encode( $row ) ); ?>"
	data-nonce="<?php echo esc_attr( $nonce ); ?>"
	data-products="<?php echo esc_attr( wp_json_encode( $products ) ); ?>"
	data-currencies="<?php echo esc_attr( wp_json_encode( $currencies ) ); ?>"
	data-list-url="<?php echo esc_url( admin_url( 'admin.php?page=suitemkt3k-coupons' ) ); ?>">
	<div class="wmk-card"><div class="wmk-builder-loading"><?php esc_html_e( 'Loading designer…', 'suitemkt3k' ); ?></div></div>
</div>
<?php
if ( ! file_exists( SUITEMKT3K_PATH . 'assets/dist/coupon-designer.js' ) ) {
	echo '<div class="wmk-flash wmk-flash-warning" style="margin-top:16px">' . esc_html__( 'Run `npm install && npm run build` to compile the coupon designer React app.', 'suitemkt3k' ) . '</div>';
}
