<?php
/**
 * Form Display Template
 *
 * Frontend template for displaying forms.
 *
 * @package PCFormBuilder
 */

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

$form_id = ! empty( $form->id ) ? $form->id : 0;
$form_name = ! empty( $form->name ) ? $form->name : '';
$form_description = ! empty( $form->description ) ? $form->description : '';
$submit_text = ! empty( $settings['submit_text'] ) ? $settings['submit_text'] : __( 'Submit', 'pc-form-builder-xyz123' );
$success_message = ! empty( $settings['success_message'] ) ? $settings['success_message'] : __( 'Thank you! Your response has been submitted.', 'pc-form-builder-xyz123' );
?>

<div class="pcfb-form-wrapper" id="pcfb-form-wrapper-<?php echo esc_attr( $form_id ); ?>">
    <div class="pcfb-form-container">
        <?php if ( ! empty( $form_name ) ) : ?>
            <div class="pcfb-form-header">
                <h2 class="pcfb-form-title"><?php echo esc_html( $form_name ); ?></h2>
                <?php if ( ! empty( $form_description ) ) : ?>
                    <p class="pcfb-form-description"><?php echo esc_html( $form_description ); ?></p>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <form class="pcfb-form" id="pcfb-form-<?php echo esc_attr( $form_id ); ?>" data-form-id="<?php echo esc_attr( $form_id ); ?>">
            <?php wp_nonce_field( 'pcfb_public_nonce', 'pcfb_nonce' ); ?>
            <input type="hidden" name="action" value="pcfb_submit_form">
            <input type="hidden" name="form_id" value="<?php echo esc_attr( $form_id ); ?>">

            <div class="pcfb-form-fields">
                <?php if ( ! empty( $fields ) && is_array( $fields ) ) : ?>
                    <?php foreach ( $fields as $field ) : ?>
                        <?php
                        $field_id = ! empty( $field->id ) ? $field->id : 0;
                        $field_name = ! empty( $field->field_name ) ? $field->field_name : '';
                        $field_label = ! empty( $field->field_label ) ? $field->field_label : '';
                        $field_type = ! empty( $field->field_type ) ? $field->field_type : 'text';
                        $placeholder = ! empty( $field->placeholder ) ? $field->placeholder : '';
                        $options = ! empty( $field->options ) ? maybe_unserialize( $field->options ) : array();
                        $validation_rules = ! empty( $field->validation_rules ) ? maybe_unserialize( $field->validation_rules ) : array();
                        $is_required = ! empty( $validation_rules['required'] );
                        ?>

                        <div class="pcfb-form-group pcfb-field-<?php echo esc_attr( $field_type ); ?>">
                            <label class="pcfb-label" for="<?php echo esc_attr( 'pcfb-' . $form_id . '-' . $field_name ); ?>">
                                <?php echo esc_html( $field_label ); ?>
                                <?php if ( $is_required ) : ?>
                                    <span class="pcfb-required">*</span>
                                <?php endif; ?>
                            </label>

                            <?php switch ( $field_type ) :
                                case 'text':
                                case 'email':
                                case 'name':
                                    ?>
                                    <input
                                        type="<?php echo esc_attr( 'email' === $field_type ? 'email' : 'text' ); ?>"
                                        id="<?php echo esc_attr( 'pcfb-' . $form_id . '-' . $field_name ); ?>"
                                        name="<?php echo esc_attr( $field_name ); ?>"
                                        class="pcfb-input"
                                        placeholder="<?php echo esc_attr( $placeholder ); ?>"
                                        <?php if ( $is_required ) : ?>required<?php endif; ?>
                                    >
                                    <?php
                                    break;

                                case 'response':
                                    ?>
                                    <textarea
                                        id="<?php echo esc_attr( 'pcfb-' . $form_id . '-' . $field_name ); ?>"
                                        name="<?php echo esc_attr( $field_name ); ?>"
                                        class="pcfb-textarea"
                                        rows="5"
                                        placeholder="<?php echo esc_attr( $placeholder ); ?>"
                                        <?php if ( $is_required ) : ?>required<?php endif; ?>
                                    ></textarea>
                                    <?php
                                    break;

                                case 'dropdown':
                                    ?>
                                    <select
                                        id="<?php echo esc_attr( 'pcfb-' . $form_id . '-' . $field_name ); ?>"
                                        name="<?php echo esc_attr( $field_name ); ?>"
                                        class="pcfb-select"
                                        <?php if ( $is_required ) : ?>required<?php endif; ?>
                                    >
                                        <option value=""><?php echo esc_html( $placeholder ? $placeholder : __( '-- Select --', 'pc-form-builder-xyz123' ) ); ?></option>
                                        <?php if ( ! empty( $options ) && is_array( $options ) ) : ?>
                                            <?php foreach ( $options as $option ) : ?>
                                                <?php if ( ! empty( trim( $option ) ) ) : ?>
                                                    <option value="<?php echo esc_attr( trim( $option ) ); ?>"><?php echo esc_html( trim( $option ) ); ?></option>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </select>
                                    <?php
                                    break;

                                case 'checkbox':
                                    if ( ! empty( $options ) && is_array( $options ) ) :
                                        ?>
                                        <div class="pcfb-checkbox-group">
                                            <?php foreach ( $options as $index => $option ) : ?>
                                                <?php if ( ! empty( trim( $option ) ) ) : ?>
                                                    <?php $checkbox_id = 'pcfb-' . $form_id . '-' . $field_name . '-' . $index; ?>
                                                    <label class="pcfb-checkbox-label" for="<?php echo esc_attr( $checkbox_id ); ?>">
                                                        <input
                                                            type="checkbox"
                                                            id="<?php echo esc_attr( $checkbox_id ); ?>"
                                                            name="<?php echo esc_attr( $field_name ); ?>[]"
                                                            value="<?php echo esc_attr( trim( $option ) ); ?>"
                                                            class="pcfb-checkbox"
                                                        >
                                                        <span class="pcfb-checkbox-text"><?php echo esc_html( trim( $option ) ); ?></span>
                                                    </label>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </div>
                                    <?php endif; ?>
                                    <?php
                                    break;
                            endswitch; ?>

                            <span class="pcfb-field-error" id="<?php echo esc_attr( 'pcfb-error-' . $form_id . '-' . $field_name ); ?>"></span>
                        </div>
                    <?php endforeach; ?>
                <?php else : ?>
                    <p class="pcfb-no-fields"><?php esc_html_e( 'This form has no fields yet.', 'pc-form-builder-xyz123' ); ?></p>
                <?php endif; ?>
            </div>

            <div class="pcfb-form-footer">
                <button type="submit" class="pcfb-submit-button">
                    <span class="pcfb-submit-text"><?php echo esc_html( $submit_text ); ?></span>
                    <span class="pcfb-submit-loader" style="display: none;">
                        <span class="pcfb-spinner"></span>
                        <?php esc_html_e( 'Submitting...', 'pc-form-builder-xyz123' ); ?>
                    </span>
                </button>
            </div>
        </form>

        <div class="pcfb-form-message pcfb-success-message" style="display: none;">
            <div class="pcfb-message-content">
                <span class="pcfb-message-icon dashicons dashicons-yes-alt"></span>
                <p class="pcfb-message-text"><?php echo esc_html( $success_message ); ?></p>
            </div>
        </div>

        <div class="pcfb-form-message pcfb-error-message" style="display: none;">
            <div class="pcfb-message-content">
                <span class="pcfb-message-icon dashicons dashicons-warning"></span>
                <p class="pcfb-message-text"></p>
            </div>
        </div>
    </div>
</div>
