<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>

<div class="ehx-admin-metabox">
    <table class="form-table ehxme-form-table ehxme-form-register-gdpr ehx-top-label">
        <tbody>
			<?php
				$ehx_form = get_post_meta($post->ID, 'ehx_form', true);
			?>
            <?php foreach($fields->memberships as $key => $field): ?>
				<tr 
					class="ehxme-forms-line<?php echo $key != 0 ? ' ehxme-forms-line-conditioned':''; ?>" 
					style="display: <?php echo ($key == 1 && (isset($ehx_form['member_role']) && $ehx_form['member_role'] != 'paid_member')) || ($key != 0 && !isset($ehx_form['member_role'])) ? 'none':'block'; ?>;"
				>
					<td>
						<label for="<?php echo esc_attr($field->id); ?>">
							<?php echo esc_html($field->label); ?>
							<?php if(isset($field->tooltip)): ?>
								<span class="ehx_tooltip dashicons dashicons-editor-help" title="<?php echo esc_html($field->tooltip); ?>"></span>
							<?php endif ?>
						</label>
						
						<?php if($field->type === 'select'): ?>
							<select id="<?php echo esc_attr($field->id); ?>" name="ehx_form[<?php echo esc_attr($field->id); ?>]" class="ehxme-forms-field ehx-long-field">
								<?php if(gettype($field->options) === 'string'): ?>
									<?php foreach(get_pages() as $page): ?>
										<option value="<?php echo esc_html($page->ID); ?>" <?php echo selected($page->ID, $ehx_form[$field->id] ?? null) ?>><?php echo esc_html($page->post_title); ?></option>
									<?php endforeach ?>
								<?php else: ?>
									<?php foreach($field->options as $key => $value): ?>
										<option value="<?php echo esc_attr($key); ?>" <?php echo selected($key, $ehx_form[$field->id] ?? null) ?>><?php echo esc_html($value); ?></option>
									<?php endforeach ?>
								<?php endif ?>
							</select>
						<?php else: ?>
							<input type="text" id="<?php echo esc_attr($field->id); ?>" name="ehx_form[<?php echo esc_attr($field->id); ?>]" placeholder="<?php echo esc_attr($field->placeholder ?? ''); ?>" class="ehxme-forms-field ehx-long-field" value="<?php echo esc_html($ehx_form[$field->id] ?? '') ?>">
						<?php endif ?>
					</td>
				</tr>
			<?php endforeach ?>
        </tbody>
    </table>

    <div class="clear"></div>
</div>