<div class="wrap pcfb-wrap">
    <?php
    $form_id = isset( $_GET['form_id'] ) ? intval( $_GET['form_id'] ) : 0;
    $response_id = isset( $_GET['response_id'] ) ? intval( $_GET['response_id'] ) : 0;

    if ( empty( $form_id ) || empty( $response_id ) ) {
        echo '<div class="notice notice-error"><p>' . esc_html__( 'Invalid request.', 'pc-form-builder-xyz123' ) . '</p></div>';
        return;
    }

    $form = PC_Form_Handler::get_form_by_id( $form_id );

    if ( ! $form ) {
        echo '<div class="notice notice-error"><p>' . esc_html__( 'Form not found.', 'pc-form-builder-xyz123' ) . '</p></div>';
        return;
    }

    $response_data = PC_Form_Handler::get_response_data( $response_id );

    if ( empty( $response_data ) ) {
        echo '<div class="notice notice-error"><p>' . esc_html__( 'Response not found.', 'pc-form-builder-xyz123' ) . '</p></div>';
        return;
    }

    $back_url = admin_url( 'admin.php?page=pcfb-responses&form_id=' . $form_id );
    $response_meta = pcfb_get_response_meta( $response_id );
    ?>

    <h1 class="wp-heading-inline">
        <a href="<?php echo esc_url( $back_url ); ?>" class="pcfb-back-link">
            <span class="dashicons dashicons-arrow-left-alt2"></span>
        </a>
        <span class="pcfb-heading-icon dashicons dashicons-pressthis"></span>
        <?php esc_html_e( 'Response Details', 'pc-form-builder-xyz123' ); ?>
    </h1>

    <span class="title-ext">
        <?php esc_html_e( 'Response #', 'pc-form-builder-xyz123' ); ?><?php echo esc_html( $response_id ); ?>
    </span>

    <hr class="wp-header-end">

    <div class="pcfb-response-detail-page">
        <div class="pcfb-response-meta">
            <div class="pcfb-card">
                <div class="pcfb-card-header">
                    <h2><?php esc_html_e( 'Response Information', 'pc-form-builder-xyz123' ); ?></h2>
                </div>
                <div class="pcfb-card-body">
                    <table class="widefat pcfb-meta-table">
                        <tr>
                            <th><?php esc_html_e( 'Form:', 'pc-form-builder-xyz123' ); ?></th>
                            <td><?php echo esc_html( $form->name ); ?></td>
                        </tr>
                        <tr>
                            <th><?php esc_html_e( 'Submitted:', 'pc-form-builder-xyz123' ); ?></th>
                            <td><?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $response_meta['created_at'] ) ) ); ?></td>
                        </tr>
                        <tr>
                            <th><?php esc_html_e( 'IP Address:', 'pc-form-builder-xyz123' ); ?></th>
                            <td><?php echo esc_html( $response_meta['user_ip'] ); ?></td>
                        </tr>
                        <tr>
                            <th><?php esc_html_e( 'User Agent:', 'pc-form-builder-xyz123' ); ?></th>
                            <td><?php echo esc_html( $response_meta['user_agent'] ); ?></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="pcfb-response-content">
            <div class="pcfb-card">
                <div class="pcfb-card-header">
                    <h2><?php esc_html_e( 'Submitted Data', 'pc-form-builder-xyz123' ); ?></h2>
                </div>
                <div class="pcfb-card-body">
                    <?php foreach ( $response_data as $data ) : ?>
                        <div class="pcfb-response-field">
                            <div class="pcfb-response-field-label">
                                <?php echo esc_html( $data->field_label ); ?>
                            </div>
                            <div class="pcfb-response-field-value">
                                <?php
                                if ( 'checkbox' === $data->field_type ) {
                                    $options = maybe_unserialize( $data->field_value );
                                    if ( is_array( $options ) ) {
                                        echo esc_html( implode( ', ', $options ) );
                                    } else {
                                        echo esc_html( $data->field_value );
                                    }
                                } elseif ( 'response' === $data->field_type ) {
                                    echo wpautop( esc_html( $data->field_value ) );
                                } else {
                                    echo esc_html( $data->field_value );
                                }
                                ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
function pcfb_get_response_meta( $response_id ) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'pcfb_responses';
    $response = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE id = %d", $response_id ), ARRAY_A );
    return $response ? $response : array(
        'user_ip'     => '',
        'user_agent'  => '',
        'created_at'  => '',
    );
}
?>
