<?php
/**
 * Form Responses View
 *
 * @package Freelance_Studio
 */

if (!defined('ABSPATH')) {
    exit;
}
?>
<div class="wrap fs-wrap">
    <div class="fs-page-header">
        <div class="fs-page-header-left">
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms') ); ?>" class="fs-back-link">
                <span class="dashicons dashicons-arrow-left-alt2"></span>
                <?php esc_html_e('Forms', 'freelance-studio'); ?>
            </a>
            <h1><?php echo esc_html($form->name); ?> — <?php esc_html_e('Responses', 'freelance-studio'); ?></h1>
        </div>
        <div class="fs-page-header-actions">
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=edit&id=' . $form->id) ); ?>" class="button">
                <span class="dashicons dashicons-edit"></span>
                <?php esc_html_e('Edit Form', 'freelance-studio'); ?>
            </a>
            <?php if (!empty($responses)): ?>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=export&id=' . $form->id) ); ?>" class="button button-primary">
                    <span class="dashicons dashicons-download"></span>
                    <?php esc_html_e('Export CSV', 'freelance-studio'); ?>
                </a>
            <?php endif; ?>
        </div>
    </div>

    <?php if (!empty($message)): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo esc_html($message); ?></p>
        </div>
    <?php endif; ?>

    <div class="fs-response-stats">
        <div class="fs-stat-card">
            <div class="fs-stat-value"><?php echo number_format($stats['total']); ?></div>
            <div class="fs-stat-label"><?php esc_html_e('Total Responses', 'freelance-studio'); ?></div>
        </div>
        <div class="fs-stat-card">
            <div class="fs-stat-value"><?php echo number_format($stats['today']); ?></div>
            <div class="fs-stat-label"><?php esc_html_e('Today', 'freelance-studio'); ?></div>
        </div>
        <div class="fs-stat-card">
            <div class="fs-stat-value"><?php echo number_format($stats['this_week']); ?></div>
            <div class="fs-stat-label"><?php esc_html_e('This Week', 'freelance-studio'); ?></div>
        </div>
        <div class="fs-stat-card">
            <div class="fs-stat-value"><?php echo number_format($stats['this_month']); ?></div>
            <div class="fs-stat-label"><?php esc_html_e('This Month', 'freelance-studio'); ?></div>
        </div>
    </div>

    <?php if (empty($responses)): ?>
        <div class="fs-empty-state">
            <div class="fs-empty-state-icon">
                <span class="dashicons dashicons-clipboard"></span>
            </div>
            <h2><?php esc_html_e('No Responses Yet', 'freelance-studio'); ?></h2>
            <p><?php esc_html_e('Share your form to start collecting responses.', 'freelance-studio'); ?></p>
            <?php if ($form->status === 'active'): ?>
                <div class="fs-share-link-box">
                    <input type="text" value="<?php echo esc_url(FS_Form::get_public_url($form)); ?>" readonly class="regular-text" id="fs-form-url">
                    <button type="button" class="button button-primary" onclick="navigator.clipboard.writeText(document.getElementById('fs-form-url').value); this.textContent='<?php esc_attr_e('Copied!', 'freelance-studio'); ?>';">
                        <?php esc_html_e('Copy Link', 'freelance-studio'); ?>
                    </button>
                </div>
            <?php else: ?>
                <p class="description"><?php esc_html_e('Form must be Active to accept responses.', 'freelance-studio'); ?></p>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=edit&id=' . $form->id) ); ?>" class="button button-primary">
                    <?php esc_html_e('Activate Form', 'freelance-studio'); ?>
                </a>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <table class="wp-list-table widefat fixed striped fs-table">
            <thead>
                <tr>
                    <th class="column-id" style="width: 60px;">#</th>
                    <th class="column-respondent"><?php esc_html_e('Respondent', 'freelance-studio'); ?></th>
                    <th class="column-preview"><?php esc_html_e('Preview', 'freelance-studio'); ?></th>
                    <th class="column-date"><?php esc_html_e('Submitted', 'freelance-studio'); ?></th>
                    <th class="column-actions" style="width: 100px;"><?php esc_html_e('Actions', 'freelance-studio'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($responses as $response): ?>
                    <tr>
                        <td class="column-id">
                            <strong>#<?php echo esc_html($response->id); ?></strong>
                        </td>
                        <td class="column-respondent">
                            <?php if ($response->respondent_name): ?>
                                <strong><?php echo esc_html($response->respondent_name); ?></strong>
                            <?php elseif ($response->client_name): ?>
                                <strong><?php echo esc_html($response->client_company ?: $response->client_name); ?></strong>
                            <?php else: ?>
                                <span class="fs-meta"><?php esc_html_e('Anonymous', 'freelance-studio'); ?></span>
                            <?php endif; ?>
                            <?php if ($response->respondent_email): ?>
                                <div class="fs-meta"><?php echo esc_html($response->respondent_email); ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="column-preview">
                            <?php
                            // Show first 2-3 field responses as preview
                            $preview_count = 0;
                            foreach ($form->fields as $field) {
                                if (in_array($field['type'], array('heading', 'paragraph', 'divider', 'file_download'))) continue;
                                if ($preview_count >= 2) break;
                                
                                $value = isset($response->response_data[$field['id']]) ? $response->response_data[$field['id']] : '';
                                if (empty($value)) continue;
                                
                                if (is_array($value)) {
                                    if (isset($value['url'])) {
                                        $value = __('File uploaded', 'freelance-studio');
                                    } else {
                                        $value = implode(', ', $value);
                                    }
                                }
                                
                                $value = wp_trim_words($value, 8, '...');
                                echo '<div class="fs-response-preview-item">';
                                echo '<span class="fs-response-field-label">' . esc_html($field['label']) . ':</span> ';
                                echo '<span class="fs-response-field-value">' . esc_html($value) . '</span>';
                                echo '</div>';
                                $preview_count++;
                            }
                            ?>
                        </td>
                        <td class="column-date">
                            <?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($response->submitted_at))); ?>
                        </td>
                        <td class="column-actions">
                            <div class="fs-row-actions">
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=view-response&id=' . $response->id) ); ?>" class="fs-action-btn" title="<?php esc_attr_e('View', 'freelance-studio'); ?>">
                                    <span class="dashicons dashicons-visibility"></span>
                                </a>
                                <a href="#" class="fs-action-btn fs-action-delete" data-id="<?php echo esc_attr($response->id); ?>" data-type="form_response" title="<?php esc_attr_e('Delete', 'freelance-studio'); ?>">
                                    <span class="dashicons dashicons-trash"></span>
                                </a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
