<?php
/**
 * Forms List View
 *
 * @package Freelance_Studio
 */

if (!defined('ABSPATH')) {
    exit;
}
?>
<div class="wrap fs-wrap">
    <div class="fs-page-header">
        <h1 class="fs-page-title"><?php esc_html_e('Forms', 'freelance-studio'); ?></h1>
        <div style="flex:1;"></div>
        <div class="fs-header-actions">
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=templates') ); ?>" class="fs-btn fs-btn-secondary">
                <span class="dashicons dashicons-layout"></span>
                <?php esc_html_e('Templates', 'freelance-studio'); ?>
            </a>
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=add') ); ?>" class="fs-btn fs-btn-primary">
                <?php esc_html_e('New form', 'freelance-studio'); ?>
            </a>
        </div>
    </div>

    <?php if ($message && isset($messages[$message])): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo esc_html($messages[$message]); ?></p>
        </div>
    <?php endif; ?>

    <div class="fs-filters">
        <form method="get" class="fs-filter-form">
            <input type="hidden" name="page" value="fs-forms">
            
            <select name="client_id">
                <option value=""><?php esc_html_e('All Clients', 'freelance-studio'); ?></option>
                <?php foreach ($clients as $client): ?>
                    <option value="<?php echo esc_attr($client->id); ?>" <?php selected($client_filter, $client->id); ?>>
                        <?php echo esc_html($client->company_name ?: $client->name); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            
            <button type="submit" class="button"><?php esc_html_e('Filter', 'freelance-studio'); ?></button>
            
            <?php if ($client_filter): ?>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms') ); ?>" class="button">
                    <?php esc_html_e('Clear', 'freelance-studio'); ?>
                </a>
            <?php endif; ?>
        </form>
    </div>

    <?php if (empty($forms)): ?>
        <div class="fs-empty-state">
            <div class="fs-empty-state-icon">
                <span class="dashicons dashicons-feedback"></span>
            </div>
            <h2><?php esc_html_e('No Forms Yet', 'freelance-studio'); ?></h2>
            <p><?php esc_html_e('Create your first form to collect information from clients.', 'freelance-studio'); ?></p>
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=add') ); ?>" class="button button-primary button-hero">
                <?php esc_html_e('Create Your First Form', 'freelance-studio'); ?>
            </a>
        </div>
    <?php else: ?>
        <table class="wp-list-table widefat fixed striped fs-table">
            <thead>
                <tr>
                    <th class="column-name"><?php esc_html_e('Form Name', 'freelance-studio'); ?></th>
                    <th class="column-client"><?php esc_html_e('Client / Project', 'freelance-studio'); ?></th>
                    <th class="column-responses"><?php esc_html_e('Responses', 'freelance-studio'); ?></th>
                    <th class="column-actions"></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($forms as $form): ?>
                    <tr>
                        <td class="column-name">
                            <strong>
                                <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>
                            </strong>
                            <?php if ($form->description): ?>
                                <div class="fs-meta"><?php echo esc_html(wp_trim_words($form->description, 10)); ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="column-client">
                            <?php if ($form->client_name): ?>
                                <span class="fs-client-name"><?php echo esc_html($form->client_company ?: $form->client_name); ?></span>
                            <?php endif; ?>
                            <?php if ($form->project_name): ?>
                                <div class="fs-meta">
                                    <span class="dashicons dashicons-portfolio"></span>
                                    <?php echo esc_html($form->project_name); ?>
                                </div>
                            <?php elseif (!$form->client_name): ?>
                                <span class="fs-meta"><?php esc_html_e('Generic Form', 'freelance-studio'); ?></span>
                            <?php endif; ?>
                        </td>
                        <td class="column-responses">
                            <?php if ($form->response_count > 0): ?>
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=responses&id=' . $form->id) ); ?>" class="fs-response-count">
                                    <?php echo number_format($form->response_count); ?>
                                </a>
                            <?php else: ?>
                                <span class="fs-meta">0</span>
                            <?php endif; ?>
                        </td>
                        <td class="column-actions">
                            <div class="fs-row-menu">
                                <button type="button" class="fs-row-menu-trigger">⋯</button>
                                <div class="fs-row-menu-dropdown">
                                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=edit&id=' . $form->id) ); ?>"><?php esc_html_e('Edit', 'freelance-studio'); ?></a>
                                    <a href="<?php echo esc_url(FS_Form::get_public_url($form)); ?>" target="_blank"><?php esc_html_e('View', 'freelance-studio'); ?></a>
                                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=responses&id=' . $form->id) ); ?>"><?php esc_html_e('Responses', 'freelance-studio'); ?></a>
                                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=duplicate&id=' . $form->id) ); ?>"><?php esc_html_e('Duplicate', 'freelance-studio'); ?></a>
                                    <hr>
                                    <a href="#" class="fs-delete-btn text-danger" data-id="<?php echo esc_attr($form->id); ?>" data-type="form"><?php esc_html_e('Delete', 'freelance-studio'); ?></a>
                                </div>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
