<?php
/**
 * Form Templates List View
 *
 * @package Freelance_Studio
 */

if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="wrap fs-wrap">
    <div class="fs-page-header">
        <h1><?php esc_html_e('Form Templates', 'freelance-studio'); ?></h1>
        <div class="fs-header-actions">
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms') ); ?>" class="button">
                <?php esc_html_e('← Back to Forms', 'freelance-studio'); ?>
            </a>
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=add-template') ); ?>" class="button button-primary">
                <?php esc_html_e('+ New Template', 'freelance-studio'); ?>
            </a>
        </div>
    </div>

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

    <p class="fs-page-description">
        <?php esc_html_e('Templates help you quickly create forms with pre-defined fields. Use a template as a starting point and customize it for your needs.', 'freelance-studio'); ?>
    </p>

    <?php if (empty($templates)): ?>
        <div class="fs-empty-state">
            <span class="dashicons dashicons-layout"></span>
            <h3><?php esc_html_e('No templates yet', 'freelance-studio'); ?></h3>
            <p><?php esc_html_e('Create your first template or save an existing form as a template.', 'freelance-studio'); ?></p>
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=add-template') ); ?>" class="button button-primary">
                <?php esc_html_e('Create Template', 'freelance-studio'); ?>
            </a>
        </div>
    <?php else: ?>
        <?php foreach ($templates as $category => $category_templates): ?>
            <div class="fs-template-category">
                <h2><?php echo esc_html($categories[$category] ?? ucfirst($category)); ?></h2>
                <div class="fs-templates-grid">
                    <?php foreach ($category_templates as $template): ?>
                        <div class="fs-template-card">
                            <div class="fs-template-card-header">
                                <h3><?php echo esc_html($template->name); ?></h3>
                                <?php if ($template->is_default): ?>
                                    <span class="fs-badge fs-badge-info"><?php esc_html_e('Default', 'freelance-studio'); ?></span>
                                <?php endif; ?>
                            </div>
                            <?php if ($template->description): ?>
                                <p class="fs-template-description"><?php echo esc_html($template->description); ?></p>
                            <?php endif; ?>
                            <div class="fs-template-meta">
                                <?php /* translators: 1. count/number */ ?>
                                <span><?php echo wp_kses_post( sprintf( __('%d fields', 'freelance-studio'), count($template->fields))); ?></span>
                            </div>
                            <div class="fs-template-actions">
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=use-template&id=' . $template->id) ); ?>" class="button button-primary">
                                    <?php esc_html_e('Use Template', 'freelance-studio'); ?>
                                </a>
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-forms&action=edit-template&id=' . $template->id) ); ?>" class="button">
                                    <?php esc_html_e('Edit', 'freelance-studio'); ?>
                                </a>
                                <?php if (!$template->is_default): ?>
                                    <form method="post" style="display:inline;" onsubmit="return confirm('<?php esc_attr_e('Delete this template?', 'freelance-studio'); ?>');">
                                        <?php wp_nonce_field('fs_save_template', 'fs_template_nonce'); ?>
                                        <input type="hidden" name="template_action" value="delete_template">
                                        <input type="hidden" name="template_id" value="<?php echo esc_attr($template->id); ?>">
                                        <button type="submit" class="button fs-button-link-danger">
                                            <?php esc_html_e('Delete', 'freelance-studio'); ?>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>
