<?php
/**
 * Content Field Template
 * 
 * This template displays the content for a property.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/archive/sections/content.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/archive/sections/content.php
 * 3. Modify the copied file to customize the content display
 * 
 * @package     Havenlytics
 * @subpackage  Templates/archive/sections
 * @since       2.0.0
 */


if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Prefix all variables with hvnly_ to avoid global namespace conflicts
$hvnly_property_id   = $property_id ?? get_the_ID();
$hvnly_property_data = $property_data ?? hvnly_get_property_data( $hvnly_property_id );
$hvnly_mode          = $mode ?? 'preset';
$hvnly_section       = $section ?? [];
$hvnly_all_sections  = $all_sections ?? [];

// Get configured field types.
$hvnly_configured_fields = hvnly_get_configured_field_types( $hvnly_all_sections );
?>

<div class="hvnly-property-field-content hvnly-property-field-mode-<?php echo esc_attr( $hvnly_mode ); ?>">
    <div class="hvnly-property--grid-list--content">
        <?php
        // In preset mode, render fields in the order specified in the section.
        if ( $hvnly_mode === 'preset' && ! empty( $hvnly_section['fields'] ) ) {
            foreach ( $hvnly_section['fields'] as $hvnly_field ) {
                $hvnly_field_type = $hvnly_field['type'] ?? '';
                hvnly_render_field( $hvnly_field_type, $hvnly_property_id, $hvnly_property_data, $hvnly_mode, $hvnly_field );
            }
        } else {
            // Default mode: show all content fields in fixed order.
            // Display phone if configured.
            if ( in_array( 'phone', $hvnly_configured_fields, true ) ) {
                hvnly_render_field( 'phone', $hvnly_property_id, $hvnly_property_data, $hvnly_mode );
            }
            
            // Display email if configured.
            if ( in_array( 'email', $hvnly_configured_fields, true ) ) {
                hvnly_render_field( 'email', $hvnly_property_id, $hvnly_property_data, $hvnly_mode );
            }
            
            // Display website if configured.
            if ( in_array( 'website', $hvnly_configured_fields, true ) ) {
                hvnly_render_field( 'website', $hvnly_property_id, $hvnly_property_data, $hvnly_mode );
            }
            
            // Display datetime if configured.
            if ( in_array( 'datetime', $hvnly_configured_fields, true ) ) {
                hvnly_render_field( 'datetime', $hvnly_property_id, $hvnly_property_data, $hvnly_mode );
            }
            
            // Display address if configured.
            if ( in_array( 'address', $hvnly_configured_fields, true ) ) {
                hvnly_render_field( 'address', $hvnly_property_id, $hvnly_property_data, $hvnly_mode );
            }
            
            // Display other content fields from the section.
            if ( ! empty( $hvnly_section['fields'] ) && is_array( $hvnly_section['fields'] ) ) {
                foreach ( $hvnly_section['fields'] as $hvnly_field ) {
                    $hvnly_field_type = $hvnly_field['type'] ?? '';
                    
                    // Skip already handled field types.
                    if ( in_array( $hvnly_field_type, array( 'phone', 'email', 'website', 'datetime', 'address' ), true ) ) {
                        continue;
                    }
                    
                    // Render other field types.
                    hvnly_render_field( $hvnly_field_type, $hvnly_property_id, $hvnly_property_data, $hvnly_mode );
                }
            }
        }
        ?>
    </div>
</div>