<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

$wpcb_field_group = $wpcb_field;

$class = '';
if ( isset( $wpcb_field['collapsible'] ) && $wpcb_field['collapsible'] ) {
	$class .= ' wpcb-collapsible';
	
	if ( isset( $wpcb_field['field']['collapsed'] ) && 'no' !== $wpcb_field['field']['collapsed'] ) {
		$class .= ' wpcb-collapsed';
	}
}
?>

<div class="wpcb-configurator-field-group<?php echo esc_attr( $class ); ?>"<?php 
	foreach ( $wpcb_field_group as $prop => $prop_val ) {
		if ( ! $prop_val ) {
			continue;
		}
		
		if ( ! in_array( $prop, array( 'subfields', 'template', 'rules' ) ) ) {
			echo ' data-' . esc_attr( str_replace('_', '-', $prop) ) . '="' . esc_attr( $prop_val ) . '"';
		}
	}

	echo ' data-id="' . esc_attr( wpcb_sanitize_title( $wpcb_field_group['label'] ) ) . '"';
	if ( isset( $wpcb_field['rules'] ) ) {
		echo ' data-display-rules="' . wp_kses_post( urldecode( str_replace( '"', "'", wp_json_encode( $wpcb_field['rules'] ) ) ) ) . '"';
	}
	?>>

	<div class="wpcb-configurator-field-group-column wpcb-configurator-field-group-column-header column-1 column-mobile-1">

		<?php if ( isset( $wpcb_field_group['collapsible'] ) && $wpcb_field_group['collapsible'] ) : ?>

			<?php if ( isset( $wpcb_field_group['description'] ) && '' !== $wpcb_field_group['description'] ) : ?>
				<button class="wpcb-view-field-group-details">
					<?php echo wp_kses( wpcb_get_icon( 'info' ), wpcb_get_icon_kses_args() ); ?>
				</button>
			<?php endif; ?>

			<div class="wpcb-configurator-field-group-title">
				<strong class="wpcb-configurator-field-group-label">
					<?php echo esc_attr( $wpcb_field_group['label'] ); ?>
				</strong>
				<p class="wpcb-configurator-field-group-value"></p>
			</div>

			<a href="#" class="wpcb-configurator-field-collapse-button">
				<?php echo wp_kses( wpcb_get_icon( 'chevron-down' ), wpcb_get_icon_kses_args() ); ?>
			</a>

		<?php else : ?>

			<strong class="wpcb-configurator-field-group-label"><?php echo esc_attr( $wpcb_field_group['label'] ); ?></strong>
			
			<?php 
			$price_visible = 'no' !== get_post_meta( $wpcb_configuration_id, '_show_price', true );
			if ( ! is_user_logged_in() && wpcb_get_option( 'wpcb_show_prices_logged_in_users' ) ) {
				$price_visible = false;
			}
			
			if ( $price_visible ) : ?>
				<p class="wpcb-configurator-field-group-price"><?php echo wp_kses_post( wpcb_get_formatted_price( 0 ) ); ?></p>
			<?php endif; ?>

			<?php if ( isset( $wpcb_field_group['description'] ) && '' !== $wpcb_field_group['description'] ) : ?>
				<button class="wpcb-view-field-group-details">
					<?php echo wp_kses( wpcb_get_icon( 'info' ), wpcb_get_icon_kses_args() ); ?>
				</button>
			<?php endif; ?>

		<?php endif; ?>

	</div>

	<?php
	foreach ( $wpcb_field_group['subfields'] as $wpcb_field ) {
		if ( file_exists( WPCB_BASE . 'public/partials/' . $wpcb_field['template'] . '.php' ) ) {

			$mobile_width = isset( $wpcb_field['width_mobile'] ) ? $wpcb_field['width_mobile'] : 1;
			$desktop_width = isset( $wpcb_field['width'] ) ? $wpcb_field['width'] : 1;

			if ( ! is_numeric( $mobile_width ) ) {
				$mobile_width = 1;
			}
			if ( ! is_numeric( $desktop_width ) ) {
				$desktop_width = 1;
			}

			$properties = '';
			$class = '';
			if ( isset( $wpcb_field['field']['hidden'] ) && $wpcb_field['field']['hidden'] ) {
				$class .= ' wpcb-hidden';
			}

			echo '<div class="wpcb-configurator-field-group-column column-' . esc_attr( $desktop_width ) . ' column-mobile-' . esc_attr( $mobile_width ) . esc_attr( $class ) . '">';

			include WPCB_BASE . 'public/partials/' . $wpcb_field['template'] . '.php';

			echo '</div>';
		}
	}
	?>

	<?php if ( ( isset( $wpcb_field_group['description'] ) && '' !== $wpcb_field_group['description'] ) ) : ?>

		<?php 
		$panel_args = array(
			'description' => wpautop( $wpcb_field_group['description'] ),
			'id' => 'wpcb-configurator-field-group-panel',
		);
		wpcb_display_panel_html_safe( $wpcb_field_group['label'], $panel_args ); 
		?>

	<?php endif; ?>

</div>