<?php

/**
 * Field Element Template - renders a single field/element in the builder.
 *
 * @since   4.4.0
 * @package Extra Product Options for WooCommerce
 *
 * Available Variables (from addon-builder.php foreach loop):
 * $epofw_field_id - The field ID.
 * $epofw_field_data - The field data array.
 * $epofw_section_id - The section ID.
 * $epofw_group_id - The row/group ID.
 * $epofw_row_id - The column/row ID.
 * $epofw_get_action - The current action (add/edit).
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Ensure required variables are set with defaults.
$epofw_get_action = ( isset( $epofw_get_action ) ? $epofw_get_action : 'edit' );
$epofw_field_data = ( isset( $epofw_field_data ) ? $epofw_field_data : array() );
$epofw_field_id = ( isset( $epofw_field_id ) ? $epofw_field_id : '' );
$epofw_section_id = ( isset( $epofw_section_id ) ? $epofw_section_id : '' );
$epofw_group_id = ( isset( $epofw_group_id ) ? $epofw_group_id : '' );
$epofw_row_id = ( isset( $epofw_row_id ) ? $epofw_row_id : '' );
$epofw_field_type_arr = epofw_check_array_key_exists( 'field', $epofw_field_data );
$epofw_field_type_str = ( is_array( $epofw_field_type_arr ) && isset( $epofw_field_type_arr['type'] ) ? $epofw_field_type_arr['type'] : 'text' );
$epofw_label_data = epofw_check_array_key_exists( 'label', $epofw_field_data );
// Safety check: Ensure epofw_label_data is an array with title key.
if ( !is_array( $epofw_label_data ) || !isset( $epofw_label_data['title'] ) ) {
    // For separator, use field type as title.
    $epofw_default_title = ( 'separator' === $epofw_field_type_str ? __( 'Separator', 'extra-product-options-for-woocommerce' ) : __( 'Untitled Field', 'extra-product-options-for-woocommerce' ) );
    $epofw_label_data = ( is_array( $epofw_label_data ) ? $epofw_label_data : array() );
    $epofw_label_data['title'] = $epofw_default_title;
}
// Get field status for toggle.
// Check if field_status key exists - if not set at all, default to 'on' for new fields.
// If set but empty string, it means disabled.
if ( isset( $epofw_field_data['field_status'] ) ) {
    $epofw_field_status = $epofw_field_data['field_status'];
} else {
    $epofw_field_status = 'on';
    // Default to enabled for new fields.
}
?>
<div class="epofw-element" data-element-id="<?php 
echo esc_attr( $epofw_field_id );
?>" data-element-type="<?php 
echo esc_attr( $epofw_field_type_str );
?>" data-action="<?php 
echo esc_attr( $epofw_get_action );
?>">
	<?php 
// Hidden inputs for field association must be outside the modal.
// Because modals are appended to body by JS (outside the form).
$epofw_field_row_association = epofw_check_array_key_exists( 'row_association', $epofw_field_data );
$epofw_field_section_id_val = '';
$epofw_field_row_id_val = '';
$epofw_field_group_id_val = '';
$epofw_field_layout_val = 'single';
$epofw_field_columns_val = 1;
// v2.3: Also get columns_id and columns_order for proper columns_group association.
$epofw_field_columns_id_val = '';
$epofw_field_columns_order_val = 0;
// Get values from row_association or field_data.
if ( !empty( $epofw_field_row_association ) && is_array( $epofw_field_row_association ) ) {
    $epofw_field_section_id_val = ( isset( $epofw_field_row_association['section_id'] ) ? $epofw_field_row_association['section_id'] : '' );
    $epofw_field_row_id_val = ( isset( $epofw_field_row_association['row_id'] ) ? $epofw_field_row_association['row_id'] : $epofw_row_id );
    $epofw_field_group_id_val = ( isset( $epofw_field_row_association['group_id'] ) ? $epofw_field_row_association['group_id'] : $epofw_group_id );
    $epofw_field_columns_id_val = ( isset( $epofw_field_row_association['columns_id'] ) ? $epofw_field_row_association['columns_id'] : '' );
    $epofw_field_columns_order_val = ( isset( $epofw_field_row_association['columns_order'] ) ? $epofw_field_row_association['columns_order'] : 0 );
    $epofw_field_layout_val = ( isset( $epofw_field_row_association['layout'] ) ? $epofw_field_row_association['layout'] : 'single' );
    $epofw_field_columns_val = ( isset( $epofw_field_row_association['columns'] ) ? $epofw_field_row_association['columns'] : 1 );
}
// Fallback to outer loop variables if row_association is empty.
if ( empty( $epofw_field_section_id_val ) ) {
    $epofw_field_section_id_val = $epofw_section_id;
}
if ( empty( $epofw_field_row_id_val ) ) {
    $epofw_field_row_id_val = $epofw_row_id;
}
if ( empty( $epofw_field_group_id_val ) ) {
    $epofw_field_group_id_val = $epofw_group_id;
}
// v2.3: Get columns_id from the column container if not in row_association.
if ( empty( $epofw_field_columns_id_val ) && isset( $epofw_columns_id ) ) {
    $epofw_field_columns_id_val = $epofw_columns_id;
}
if ( empty( $epofw_field_columns_order_val ) && isset( $epofw_columns_order ) ) {
    $epofw_field_columns_order_val = $epofw_columns_order;
}
?>
	<!-- Field Association Hidden Inputs - Outside modal for form submission -->
	<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][section_id]" value="<?php 
echo esc_attr( $epofw_field_section_id_val );
?>" class="epofw-field-section-id">
	<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][row_id]" value="<?php 
echo esc_attr( $epofw_field_row_id_val );
?>" class="epofw-field-row-id">
	<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][group_id]" value="<?php 
echo esc_attr( $epofw_field_group_id_val );
?>" class="epofw-field-group-id">
	<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][columns_id]" value="<?php 
echo esc_attr( $epofw_field_columns_id_val );
?>" class="epofw-field-columns-id">
	<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][columns_order]" value="<?php 
echo esc_attr( $epofw_field_columns_order_val );
?>" class="epofw-field-columns-order">
	<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][layout]" value="<?php 
echo esc_attr( $epofw_field_layout_val );
?>" class="epofw-field-layout">
	<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][columns]" value="<?php 
echo esc_attr( $epofw_field_columns_val );
?>" class="epofw-field-columns">

	<div class="epofw-element-drag">
		<i class="dashicons dashicons-menu"></i>
	</div>
	<div class="epofw-element-content">
		<div class="epofw-field-status-toggle" title="<?php 
echo ( 'on' === $epofw_field_status ? esc_attr__( 'Click to disable', 'extra-product-options-for-woocommerce' ) : esc_attr__( 'Click to enable', 'extra-product-options-for-woocommerce' ) );
?>">
			<input type="hidden" name="epofw_field[<?php 
echo esc_attr( $epofw_field_id );
?>][field_status]" value="<?php 
echo esc_attr( $epofw_field_status );
?>" class="epofw-field-status-value">
			<input type="checkbox" class="epofw-field-status-checkbox" data-field-id="<?php 
echo esc_attr( $epofw_field_id );
?>" <?php 
checked( $epofw_field_status, 'on' );
?>>
			<label class="epofw-toggle-switch"></label>
		</div>
		<div class="epofw-element-icon">
			<?php 
$epofw_icon_class = 'dashicons-admin-generic';
switch ( $epofw_field_type_str ) {
    case 'text':
        $epofw_icon_class = 'dashicons-editor-textcolor';
        break;
    case 'multiple_upload':
        $epofw_icon_class = 'dashicons-upload';
        break;
    case 'select':
        $epofw_icon_class = 'dashicons-list-view';
        break;
    case 'textarea':
        $epofw_icon_class = 'dashicons-editor-paragraph';
        break;
    case 'number':
        $epofw_icon_class = 'dashicons-calculator';
        break;
    case 'date':
        $epofw_icon_class = 'dashicons-calendar-alt';
        break;
    case 'checkbox':
        $epofw_icon_class = 'dashicons-yes';
        break;
    case 'radio':
        $epofw_icon_class = 'dashicons-marker';
        break;
}
?>
			<i class="dashicons <?php 
echo esc_attr( $epofw_icon_class );
?>"></i>
		</div>
		<div class="epofw-element-info">
			<div class="epofw-element-title"><?php 
echo esc_html( $epofw_label_data['title'] );
?></div>
			<div class="epofw-element-subtitle"><?php 
echo esc_html( ucfirst( $epofw_field_type_str ) );
?></div>
		</div>
		<div class="epofw-element-actions">
			<button type="button" class="epofw-element-edit" data-element-id="<?php 
echo esc_attr( $epofw_field_id );
?>">
				<i class="dashicons dashicons-edit"></i>
			</button>
			<button type="button" class="epofw-element-delete">
				<i class="dashicons dashicons-no-alt"></i>
			</button>
		</div>
	</div>
	<div class="epofw-element-modal" id="element-settings-modal-<?php 
echo esc_attr( $epofw_field_id );
?>" style="display: none;">
		<div class="epofw-modal-overlay"></div>
		<div class="epofw-modal-content">
			<?php 
// Check if this is a premium field type and user is not premium.
$epofw_is_premium_field_type_header = epofw_is_premium_field_type( $epofw_field_type_str );
$epofw_is_premium_user_header = false;
$epofw_show_premium_header = $epofw_is_premium_field_type_header && !$epofw_is_premium_user_header;
$epofw_premium_header_class = ( $epofw_show_premium_header ? ' epofw-premium-field-header' : '' );
?>
			<div class="epofw-modal-header<?php 
echo esc_attr( $epofw_premium_header_class );
?>">
				<h3>
					<?php 
printf( 
    /* translators: %s: Field type */
    esc_html__( '%s Settings', 'extra-product-options-for-woocommerce' ),
    esc_html( ucfirst( $epofw_field_type_str ) )
 );
?>
				</h3>
				<?php 
if ( $epofw_show_premium_header ) {
    ?>
				<span class="epofw-modal-pro-badge">
					<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="12" height="12"><path fill="currentColor" d="M12 2L15.09 8.26L22 9.27L17 14.14L18.18 21.02L12 17.77L5.82 21.02L7 14.14L2 9.27L8.91 8.26L12 2Z"/></svg>
					<?php 
    esc_html_e( 'PRO', 'extra-product-options-for-woocommerce' );
    ?>
				</span>
				<a href="<?php 
    echo esc_url( epofw_fs()->get_upgrade_url() );
    ?>" target="_blank" class="epofw-modal-upgrade-btn">
					<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="14" height="14"><path fill="currentColor" d="M13 5v6h6l-8 8-8-8h6V5h4z" transform="rotate(180 12 12)"/></svg>
					<?php 
    esc_html_e( 'Upgrade to Pro', 'extra-product-options-for-woocommerce' );
    ?>
				</a>
				<?php 
}
?>
				<button type="button" class="epofw-modal-close">
					&times;
				</button>
			</div>
			<div class="epofw-modal-body">
				<div class="epofw-modal-content-wrapper">
					<?php 
ob_start();
if ( file_exists( EPOFW_PLUGIN_DIR . '/builder/element-form.php' ) ) {
    // Ensure $epofw_get_post_id is available for the form file.
    // It should come from addon-builder.php's scope.
    if ( !isset( $epofw_get_post_id ) ) {
        // Fallback: get from URL if not in scope.
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for read-only display purposes in admin page, not form processing.
        $epofw_get_post_id = ( isset( $_GET['post'] ) ? intval( sanitize_text_field( wp_unslash( $_GET['post'] ) ) ) : 0 );
    }
    // The form file will have access to the current loop variables.
    include EPOFW_PLUGIN_DIR . '/builder/element-form.php';
}
$epofw_content = ob_get_clean();
echo $epofw_content;
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
				</div>
			</div>
			<div class="epofw-modal-footer">
				<button type="button" class="epofw-btn epofw-btn-secondary epofw-modal-cancel"><?php 
esc_html_e( 'Cancel', 'extra-product-options-for-woocommerce' );
?></button>
				<button type="button" class="epofw-btn epofw-btn-primary epofw-save-element"><?php 
esc_html_e( 'Done', 'extra-product-options-for-woocommerce' );
?></button>
			</div>
		</div>
	</div>
</div>
