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

    if ( empty( $form_id ) ) {
        echo '<div class="notice notice-error"><p>' . esc_html__( 'Form ID is required.', '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;
    }

    $paged = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
    $per_page = 20;
    $offset = ( $paged - 1 ) * $per_page;

    $responses = PC_Form_Handler::get_form_responses( $form_id, array(
        'limit'  => $per_page,
        'offset' => $offset,
    ) );

    $total_responses = PC_Form_Handler::get_form_response_count( $form_id );
    $total_pages = ceil( $total_responses / $per_page );
    ?>

    <h1 class="wp-heading-inline">
        <a href="<?php echo admin_url( 'admin.php?page=pcfb-responses' ); ?>" class="pcfb-back-link">
            <span class="dashicons dashicons-arrow-left-alt2"></span>
        </a>
        <span class="pcfb-heading-icon dashicons dashicons-pressthis"></span>
        <?php echo esc_html( $form->name ); ?> - <?php esc_html_e( 'Responses', 'pc-form-builder-xyz123' ); ?>
    </h1>

    <span class="title-ext">
        <?php echo esc_html( $total_responses . ' ' . _n( 'Response', 'Responses', $total_responses, 'pc-form-builder-xyz123' ) ); ?>
    </span>

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

    <div class="pcfb-form-responses-page">
        <?php if ( empty( $responses ) ) : ?>
            <div class="pcfb-empty-state">
                <span class="dashicons dashicons-pressthis pcfb-empty-icon"></span>
                <h3><?php esc_html_e( 'No responses yet', 'pc-form-builder-xyz123' ); ?></h3>
                <p><?php esc_html_e( 'This form has not received any responses yet.', 'pc-form-builder-xyz123' ); ?></p>
            </div>
        <?php else : ?>
            <div class="pcfb-responses-header">
                <div class="pcfb-response-count-badge">
                    <span class="dashicons dashicons-pressthis"></span>
                    <?php echo esc_html( $total_responses . ' ' . _n( 'Response', 'Responses', $total_responses, 'pc-form-builder-xyz123' ) ); ?>
                </div>
            </div>

            <table class="widefat pcfb-table pcfb-responses-table">
                <thead>
                    <tr>
                        <th><?php esc_html_e( 'ID', 'pc-form-builder-xyz123' ); ?></th>
                        <th><?php esc_html_e( 'Submitted', 'pc-form-builder-xyz123' ); ?></th>
                        <th><?php esc_html_e( 'IP Address', 'pc-form-builder-xyz123' ); ?></th>
                        <th><?php esc_html_e( 'Status', 'pc-form-builder-xyz123' ); ?></th>
                        <th><?php esc_html_e( 'Actions', 'pc-form-builder-xyz123' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $responses as $response ) : ?>
                        <?php
                        $detail_url = admin_url( 'admin.php?page=pcfb-responses&form_id=' . $form_id . '&response_id=' . $response->id );
                        $status_class = 'new' === $response->status ? 'status-new' : 'status-read';
                        $status_label = 'new' === $response->status ? __( 'New', 'pc-form-builder-xyz123' ) : __( 'Read', 'pc-form-builder-xyz123' );
                        ?>
                        <tr>
                            <td><?php echo esc_html( $response->id ); ?></td>
                            <td><?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $response->created_at ) ) ); ?></td>
                            <td><?php echo esc_html( $response->user_ip ); ?></td>
                            <td>
                                <span class="pcfb-status <?php echo esc_attr( $status_class ); ?>">
                                    <?php echo esc_html( $status_label ); ?>
                                </span>
                            </td>
                            <td>
                                <a href="<?php echo esc_url( $detail_url ); ?>" class="button button-small">
                                    <?php esc_html_e( 'View Details', 'pc-form-builder-xyz123' ); ?>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

            <?php if ( $total_pages > 1 ) : ?>
                <div class="pcfb-pagination">
                    <?php
                    echo paginate_links( array(
                        'base'      => admin_url( 'admin.php?page=pcfb-responses&form_id=' . $form_id . '&paged=%#%' ),
                        'format'    => '',
                        'prev_text' => __( '&laquo; Previous', 'pc-form-builder-xyz123' ),
                        'next_text' => __( 'Next &raquo;', 'pc-form-builder-xyz123' ),
                        'total'     => $total_pages,
                        'current'   => $paged,
                    ) );
                    ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
