<div class="wrap pcfb-wrap">
    <h1 class="wp-heading-inline">
        <span class="pcfb-heading-icon dashicons dashicons-forms"></span>
        <?php esc_html_e( 'All Forms', 'pc-form-builder-xyz123' ); ?>
    </h1>
    <a href="<?php echo admin_url( 'admin.php?page=pcfb-form-builder' ); ?>" class="page-title-action">
        <span class="dashicons dashicons-plus"></span>
        <?php esc_html_e( 'Add New Form', 'pc-form-builder-xyz123' ); ?>
    </a>

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

    <?php
    $forms = PC_Form_Handler::get_forms( array( 'limit' => 50, 'status' => '' ) );
    $active_count = PC_Form_Handler::get_form_count( 'active' );
    $inactive_count = PC_Form_Handler::get_form_count( 'inactive' );
    $total_responses = 0;
    foreach ( $forms as $form ) {
        $total_responses += PC_Form_Handler::get_form_response_count( $form->id );
    }
    ?>

    <div class="pcfb-all-forms-page">
        <div class="pcfb-stats-row">
            <div class="pcfb-stat-card">
                <div class="pcfb-stat-icon pcfb-stat-icon-blue">
                    <span class="dashicons dashicons-forms"></span>
                </div>
                <div class="pcfb-stat-content">
                    <span class="pcfb-stat-number"><?php echo esc_html( count( $forms ) ); ?></span>
                    <span class="pcfb-stat-label"><?php esc_html_e( 'Total Forms', 'pc-form-builder-xyz123' ); ?></span>
                </div>
            </div>

            <div class="pcfb-stat-card">
                <div class="pcfb-stat-icon pcfb-stat-icon-green">
                    <span class="dashicons dashicons-yes-alt"></span>
                </div>
                <div class="pcfb-stat-content">
                    <span class="pcfb-stat-number"><?php echo esc_html( $active_count ); ?></span>
                    <span class="pcfb-stat-label"><?php esc_html_e( 'Active Forms', 'pc-form-builder-xyz123' ); ?></span>
                </div>
            </div>

            <div class="pcfb-stat-card">
                <div class="pcfb-stat-icon pcfb-stat-icon-orange">
                    <span class="dashicons dashicons-hidden"></span>
                </div>
                <div class="pcfb-stat-content">
                    <span class="pcfb-stat-number"><?php echo esc_html( $inactive_count ); ?></span>
                    <span class="pcfb-stat-label"><?php esc_html_e( 'Inactive Forms', 'pc-form-builder-xyz123' ); ?></span>
                </div>
            </div>

            <div class="pcfb-stat-card">
                <div class="pcfb-stat-icon pcfb-stat-icon-purple">
                    <span class="dashicons dashicons-pressthis"></span>
                </div>
                <div class="pcfb-stat-content">
                    <span class="pcfb-stat-number"><?php echo esc_html( $total_responses ); ?></span>
                    <span class="pcfb-stat-label"><?php esc_html_e( 'Total Signups', 'pc-form-builder-xyz123' ); ?></span>
                </div>
            </div>
        </div>

        <?php if ( empty( $forms ) ) : ?>
            <div class="pcfb-empty-state">
                <span class="dashicons dashicons-forms pcfb-empty-icon"></span>
                <h3><?php esc_html_e( 'No forms created yet', 'pc-form-builder-xyz123' ); ?></h3>
                <p><?php esc_html_e( 'Create your first form to start collecting responses from your visitors.', 'pc-form-builder-xyz123' ); ?></p>
                <a href="<?php echo admin_url( 'admin.php?page=pcfb-form-builder' ); ?>" class="button button-primary">
                    <?php esc_html_e( 'Create Your First Form', 'pc-form-builder-xyz123' ); ?>
                </a>
            </div>
        <?php else : ?>
            <div class="pcfb-forms-table-card">
                <div class="pcfb-card-header pcfb-table-header">
                    <h2><?php esc_html_e( 'Your Forms', 'pc-form-builder-xyz123' ); ?></h2>
                </div>

                <table class="widefat pcfb-table pcfb-forms-table">
                    <thead>
                        <tr>
                            <th class="column-primary"><?php esc_html_e( 'Form Name', 'pc-form-builder-xyz123' ); ?></th>
                            <th><?php esc_html_e( 'Shortcode', 'pc-form-builder-xyz123' ); ?></th>
                            <th><?php esc_html_e( 'Fields', 'pc-form-builder-xyz123' ); ?></th>
                            <th><?php esc_html_e( 'Signups', 'pc-form-builder-xyz123' ); ?></th>
                            <th><?php esc_html_e( 'Status', 'pc-form-builder-xyz123' ); ?></th>
                            <th><?php esc_html_e( 'Created', 'pc-form-builder-xyz123' ); ?></th>
                            <th class="column-actions"><?php esc_html_e( 'Actions', 'pc-form-builder-xyz123' ); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $forms as $form ) : ?>
                            <?php
                            $field_count = pcfb_get_field_count( $form->id );
                            $response_count = PC_Form_Handler::get_form_response_count( $form->id );
                            $edit_url = admin_url( 'admin.php?page=pcfb-form-builder&form_id=' . $form->id );
                            $responses_url = admin_url( 'admin.php?page=pcfb-responses&form_id=' . $form->id );
                            $status_class = 'active' === $form->status ? 'status-active' : 'status-inactive';
                            $status_text = 'active' === $form->status ? __( 'Active', 'pc-form-builder-xyz123' ) : __( 'Inactive', 'pc-form-builder-xyz123' );
                            ?>
                            <tr>
                                <td class="column-primary">
                                    <strong>
                                        <a href="<?php echo esc_url( $edit_url ); ?>">
                                            <?php echo esc_html( $form->name ); ?>
                                        </a>
                                    </strong>
                                    <div class="row-actions">
                                        <span class="edit"><a href="<?php echo esc_url( $edit_url ); ?>"><?php esc_html_e( 'Edit', 'pc-form-builder-xyz123' ); ?></a> | </span>
                                        <span class="responses"><a href="<?php echo esc_url( $responses_url ); ?>"><?php esc_html_e( 'Responses', 'pc-form-builder-xyz123' ); ?></a> | </span>
                                        <span class="delete"><button type="button" class="pcfb-delete-form-link" data-form-id="<?php echo esc_attr( $form->id ); ?>" data-form-name="<?php echo esc_attr( $form->name ); ?>"><?php esc_html_e( 'Delete', 'pc-form-builder-xyz123' ); ?></button></span>
                                    </div>
                                    <?php if ( $form->description ) : ?>
                                    <br>
                                    <small class="description"><?php echo esc_html( $form->description ); ?></small>
                                    <?php endif; ?>
                                </td>
                                <td data-colname="<?php esc_attr_e( 'Shortcode', 'pc-form-builder-xyz123' ); ?>">
                                    <code class="pcfb-shortcode" onclick="this.select(); document.execCommand('copy');" title="<?php esc_attr_e( 'Click to copy', 'pc-form-builder-xyz123' ); ?>">
                                        [pcfb-form id="<?php echo esc_attr( $form->id ); ?>"]
                                    </code>
                                </td>
                                <td data-colname="<?php esc_attr_e( 'Fields', 'pc-form-builder-xyz123' ); ?>">
                                    <span class="pcfb-field-badge"><?php echo esc_html( $field_count ); ?></span>
                                </td>
                                <td data-colname="<?php esc_attr_e( 'Signups', 'pc-form-builder-xyz123' ); ?>">
                                    <?php if ( $response_count > 0 ) : ?>
                                        <a href="<?php echo esc_url( $responses_url ); ?>" class="pcfb-signups-link">
                                            <span class="pcfb-signup-count"><?php echo esc_html( $response_count ); ?></span>
                                        </a>
                                    <?php else : ?>
                                        <span class="pcfb-signup-count zero"><?php echo esc_html( $response_count ); ?></span>
                                    <?php endif; ?>
                                </td>
                                <td data-colname="<?php esc_attr_e( 'Status', 'pc-form-builder-xyz123' ); ?>">
                                    <span class="pcfb-status <?php echo esc_attr( $status_class ); ?>">
                                        <?php echo esc_html( $status_text ); ?>
                                    </span>
                                </td>
                                <td data-colname="<?php esc_attr_e( 'Created', 'pc-form-builder-xyz123' ); ?>">
                                    <?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $form->created_at ) ) ); ?>
                                </td>
                                <td class="column-actions" data-colname="<?php esc_attr_e( 'Actions', 'pc-form-builder-xyz123' ); ?>">
                                    <div class="pcfb-actions">
                                        <a href="<?php echo esc_url( $edit_url ); ?>" class="button button-small" title="<?php esc_attr_e( 'Edit Form', 'pc-form-builder-xyz123' ); ?>">
                                            <span class="dashicons dashicons-edit"></span>
                                        </a>
                                        <a href="<?php echo esc_url( $responses_url ); ?>" class="button button-small" title="<?php esc_attr_e( 'View Responses', 'pc-form-builder-xyz123' ); ?>">
                                            <span class="dashicons dashicons-pressthis"></span>
                                        </a>
                                        <button type="button" class="button button-small pcfb-delete-form" data-form-id="<?php echo esc_attr( $form->id ); ?>" data-form-name="<?php echo esc_attr( $form->name ); ?>" title="<?php esc_attr_e( 'Delete Form', 'pc-form-builder-xyz123' ); ?>">
                                            <span class="dashicons dashicons-trash"></span>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>

    <div id="pcfb-delete-modal" class="pcfb-modal" style="display: none;">
        <div class="pcfb-modal-content">
            <div class="pcfb-modal-header">
                <h3><?php esc_html_e( 'Delete Form', 'pc-form-builder-xyz123' ); ?></h3>
                <button type="button" class="pcfb-modal-close">&times;</button>
            </div>
            <div class="pcfb-modal-body">
                <p><?php esc_html_e( 'Are you sure you want to delete this form? All responses will also be deleted. This action cannot be undone.', 'pc-form-builder-xyz123' ); ?></p>
                <p><strong id="pcfb-delete-form-name"></strong></p>
            </div>
            <div class="pcfb-modal-footer">
                <form method="post" id="pcfb-delete-form">
                    <?php wp_nonce_field( 'pcfb_admin_action', '_wpnonce' ); ?>
                    <input type="hidden" name="pcfb_action" value="delete_form">
                    <input type="hidden" name="form_id" id="pcfb-delete-form-id" value="">
                    <button type="button" class="button pcfb-modal-cancel"><?php esc_html_e( 'Cancel', 'pc-form-builder-xyz123' ); ?></button>
                    <button type="submit" class="button button-primary"><?php esc_html_e( 'Delete Form', 'pc-form-builder-xyz123' ); ?></button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php
function pcfb_get_field_count( $form_id ) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'pcfb_fields';
    return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table_name} WHERE form_id = %d", $form_id ) );
}
?>
