<?php
if (! defined('ABSPATH')) {
    exit;
} $attrs = $attributes ?? array();
$title = isset($attrs['title']) ? $attrs['title'] : 'Contact Us';
$subtitle = isset($attrs['subtitle']) ? $attrs['subtitle'] : '';
$fields = isset($attrs['fields']) && is_array($attrs['fields']) ? $attrs['fields'] : array();
$button_text = ! empty($attrs['buttonText']) ? $attrs['buttonText'] : 'Submit';
$success_msg = isset($attrs['successMessage']) ? $attrs['successMessage'] : '';
$success_url = isset($attrs['successUrl']) ? $attrs['successUrl'] : '/thank-you';

if (empty($fields)) {
    $fields = array(array('name' => 'name', 'label' => 'Your Name', 'required' => true), array('name' => 'email', 'label' => 'Email', 'required' => true), array('name' => 'phone', 'label' => 'Phone', 'required' => false), array('name' => 'message', 'label' => 'Message', 'required' => true), );
} $form_attrs = 'data-pg-submit="contact"';

if ($success_msg) {
    $form_attrs .= ' data-pg-success-text="' . esc_attr($success_msg) . '"';
}

if ($success_url) {
    $form_attrs .= ' data-pg-success-url="' . esc_attr($success_url) . '"';
} $wrapper_attributes = get_block_wrapper_attributes(); ?>
<div <?php echo $wrapper_attributes; ?>>
	<div class="pg-cf">
		<?php if ($title) : ?>
		<div class="pg-cf-header">
			<h2 class="pg-cf-title"><?php echo esc_html($title); ?></h2>
			<?php if ($subtitle) : ?>
			<p class="pg-cf-subtitle"><?php echo esc_html($subtitle); ?></p>
			<?php endif; ?>
		</div>
		<?php endif; ?>

		<form <?php echo $form_attrs; ?> class="pg-cf-form">
			<div class="pg-cf-grid">
			<?php foreach ($fields as $field) : $name = isset($field['name']) ? sanitize_key($field['name']) : '';
			    $label = isset($field['label']) ? $field['label'] : $name;
			    $required = ! empty($field['required']);
			    $is_message = ('message' === $name);
			    $is_email = ('email' === $name); ?>
				<div class="pg-cf-field<?php echo $is_message ? ' pg-cf-field--full' : ''; ?>">
					<label for="pg-cf-<?php echo esc_attr($name); ?>" class="pg-cf-label">
						<?php echo esc_html($label); ?>
						<?php if ($required) : ?><span class="pg-cf-required"> *</span><?php endif; ?>
					</label>
					<?php if ($is_message) : ?>
					<textarea name="<?php echo esc_attr($name); ?>" id="pg-cf-<?php echo esc_attr($name); ?>" rows="5" <?php echo $required ? 'required' : ''; ?> class="pg-cf-textarea" placeholder="<?php echo esc_attr($label); ?>"></textarea>
					<?php else : ?>
					<input type="<?php echo $is_email ? 'email' : 'text'; ?>" name="<?php echo esc_attr($name); ?>" id="pg-cf-<?php echo esc_attr($name); ?>" <?php echo $required ? 'required' : ''; ?> class="pg-cf-input" placeholder="<?php echo esc_attr($label); ?>">
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
			</div>

			<button type="submit" class="pg-cf-button">
				<span><?php echo esc_html($button_text); ?></span>
				<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
					<path stroke-linecap="round" stroke-linejoin="round" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
				</svg>
			</button>
			<div data-pg-form-status class="pg-cf-status"></div>
		</form>
	</div>
</div>
