<?php
/**
 * Single Form Response 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&action=responses&id=' . $form->id) ); ?>" class="fs-back-link">
                <span class="dashicons dashicons-arrow-left-alt2"></span>
                <?php echo esc_html($form->name); ?> — <?php esc_html_e('Responses', 'freelance-studio'); ?>
            </a>
            <?php /* translators: 1. response number */ ?>
            <h1><?php echo wp_kses_post( sprintf( __('Response #%d', 'freelance-studio'), $response->id)); ?></h1>
        </div>
        <div class="fs-page-header-actions">
            <a href="#" class="button fs-action-delete" data-id="<?php echo esc_attr($response->id); ?>" data-type="form_response" data-redirect="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=responses&id=' . $form->id . '&message=response_deleted') ); ?>">
                <span class="dashicons dashicons-trash"></span>
                <?php esc_html_e('Delete Response', 'freelance-studio'); ?>
            </a>
        </div>
    </div>

    <div class="fs-response-detail-layout">
        <div class="fs-response-main">
            <div class="fs-card">
                <div class="fs-card-header">
                    <h2><?php esc_html_e('Response Details', 'freelance-studio'); ?></h2>
                </div>
                <div class="fs-card-body">
                    <?php foreach ($form->fields as $field): ?>
                        <?php
                        // Skip dividers in the response view
                        if ($field['type'] === 'divider') continue;
                        
                        // Handle content types differently
                        if (in_array($field['type'], array('heading', 'paragraph'))) {
                            if ($field['type'] === 'heading') {
                                echo '<h3 class="fs-response-section-heading">' . esc_html($field['label']) . '</h3>';
                            } else {
                                echo '<p class="fs-response-paragraph">' . esc_html($field['content'] ?? '') . '</p>';
                            }
                            continue;
                        }
                        
                        // Skip file downloads (not a question)
                        if ($field['type'] === 'file_download') continue;
                        
                        $value = isset($response->response_data[$field['id']]) ? $response->response_data[$field['id']] : '';
                        ?>
                        <div class="fs-response-field">
                            <div class="fs-response-field-label">
                                <?php echo esc_html($field['label']); ?>
                                <?php if (!empty($field['required'])): ?>
                                    <span class="required">*</span>
                                <?php endif; ?>
                            </div>
                            <div class="fs-response-field-value">
                                <?php if (empty($value)): ?>
                                    <span class="fs-no-response"><?php esc_html_e('No response', 'freelance-studio'); ?></span>
                                <?php elseif ($field['type'] === 'file_upload' && is_array($value)): ?>
                                    <a href="<?php echo esc_url($value['url']); ?>" target="_blank" class="fs-file-link">
                                        <span class="dashicons dashicons-media-default"></span>
                                        <?php echo esc_html($value['filename'] ?? basename($value['url'])); ?>
                                    </a>
                                <?php elseif ($field['type'] === 'signature' && !empty($value)): ?>
                                    <div class="fs-signature-display">
                                        <img src="<?php echo esc_attr($value); ?>" alt="<?php esc_attr_e('Signature', 'freelance-studio'); ?>">
                                    </div>
                                <?php elseif ($field['type'] === 'yes_no'): ?>
                                    <span class="fs-yes-no-badge fs-yes-no-<?php echo $value ? 'yes' : 'no'; ?>">
                                        <?php echo  esc_html( $value ? __('Yes', 'freelance-studio') : __('No', 'freelance-studio') ); ?>
                                    </span>
                                <?php elseif (is_array($value)): ?>
                                    <ul class="fs-response-list">
                                        <?php foreach ($value as $item): ?>
                                            <li><?php echo esc_html($item); ?></li>
                                        <?php endforeach; ?>
                                    </ul>
                                <?php elseif ($field['type'] === 'long_text'): ?>
                                    <div class="fs-response-long-text"><?php echo nl2br(esc_html($value)); ?></div>
                                <?php elseif ($field['type'] === 'url'): ?>
                                    <a href="<?php echo esc_url($value); ?>" target="_blank"><?php echo esc_html($value); ?></a>
                                <?php elseif ($field['type'] === 'email'): ?>
                                    <a href="mailto:<?php echo esc_attr($value); ?>"><?php echo esc_html($value); ?></a>
                                <?php elseif ($field['type'] === 'phone'): ?>
                                    <a href="tel:<?php echo esc_attr($value); ?>"><?php echo esc_html($value); ?></a>
                                <?php elseif ($field['type'] === 'scale'): ?>
                                    <div class="fs-scale-display">
                                        <span class="fs-scale-value"><?php echo esc_html($value); ?></span>
                                        <?php if (!empty($field['min_label']) || !empty($field['max_label'])): ?>
                                            <span class="fs-scale-labels">
                                                (<?php echo esc_html($field['min'] ?? 1); ?> = <?php echo esc_html($field['min_label'] ?? __('Min', 'freelance-studio')); ?>,
                                                <?php echo esc_html($field['max'] ?? 10); ?> = <?php echo esc_html($field['max_label'] ?? __('Max', 'freelance-studio')); ?>)
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                <?php else: ?>
                                    <?php echo esc_html($value); ?>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>

        <div class="fs-response-sidebar">
            <div class="fs-card">
                <div class="fs-card-header">
                    <h2><?php esc_html_e('Submission Info', 'freelance-studio'); ?></h2>
                </div>
                <div class="fs-card-body">
                    <table class="fs-info-table">
                        <tr>
                            <th><?php esc_html_e('Submitted', 'freelance-studio'); ?></th>
                            <td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($response->submitted_at))); ?></td>
                        </tr>
                        <?php if ($response->respondent_name): ?>
                        <tr>
                            <th><?php esc_html_e('Name', 'freelance-studio'); ?></th>
                            <td><?php echo esc_html($response->respondent_name); ?></td>
                        </tr>
                        <?php endif; ?>
                        <?php if ($response->respondent_email): ?>
                        <tr>
                            <th><?php esc_html_e('Email', 'freelance-studio'); ?></th>
                            <td><a href="mailto:<?php echo esc_attr($response->respondent_email); ?>"><?php echo esc_html($response->respondent_email); ?></a></td>
                        </tr>
                        <?php endif; ?>
                        <?php if ($response->client_id): ?>
                        <tr>
                            <th><?php esc_html_e('Client', 'freelance-studio'); ?></th>
                            <td>
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-clients&action=view&id=' . $response->client_id) ); ?>">
                                    <?php echo esc_html($response->client_company ?: $response->client_name); ?>
                                </a>
                            </td>
                        </tr>
                        <?php endif; ?>
                        <?php if ($response->ip_address): ?>
                        <tr>
                            <th><?php esc_html_e('IP Address', 'freelance-studio'); ?></th>
                            <td><code><?php echo esc_html($response->ip_address); ?></code></td>
                        </tr>
                        <?php endif; ?>
                    </table>
                </div>
            </div>
            
            <div class="fs-card">
                <div class="fs-card-header">
                    <h2><?php esc_html_e('Form Info', 'freelance-studio'); ?></h2>
                </div>
                <div class="fs-card-body">
                    <table class="fs-info-table">
                        <tr>
                            <th><?php esc_html_e('Form', 'freelance-studio'); ?></th>
                            <td>
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=edit&id=' . $form->id) ); ?>">
                                    <?php echo esc_html($form->name); ?>
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <th><?php esc_html_e('Total Responses', 'freelance-studio'); ?></th>
                            <td>
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=responses&id=' . $form->id) ); ?>">
                                    <?php echo number_format(FS_Form_Response::count(array('form_id' => $form->id))); ?>
                                </a>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
