<?php
/**
 * Template Nombre: Enviar Grupo
 * 
 * Template para a página de envio de grupos do Telegram
 * 
 * @package Putaria_Telegram_Theme
 */

get_header();
?>

<div class="site-content">
    <div class="content-area">
        
        <?php if (!is_user_logged_in()): ?>
            
            <!-- Usuario NÃO logado -->
            <div class="hot-login-required">
                <h2>🔒 Iniciar Sesión Requerido</h2>
                <p>You need to be logged in to submit groups.</p>
                <div class="login-actions">
                    <a href="<?php echo function_exists('hot_get_login_url') ? hot_get_login_url() : home_url('/login/'); ?>" class="hot-button">
                        Iniciar Sesión
                    </a>
                    <a href="<?php echo home_url('/register/'); ?>" class="hot-button hot-button-secondary">
                        Registrarse
                    </a>
                </div>
            </div>
            
        <?php else: ?>
            
            <!-- Usuario LOGADO - Formulário de Envio -->
            <div class="hot-enviar-form">
                <h2>📤 Enviar Nuevo Grupo</h2>
                
                <?php if (isset($_GET['error'])): ?>
                    <div class="hot-error-message">
                        <?php
                        $errors = explode(',', sanitize_text_field($_GET['error']));
                        $error_messages = array(
                            'title_empty' => 'Por favor completa el título del grupo.',
                            'link_empty' => 'Por favor proporciona el enlace del grupo.',
                            'link_invalid' => 'The link must be from Telegram (t.me or telegram.me).',
                            'description_short' => 'The description must be at least 150 characters.',
                            'spam_detected' => 'Por favor espera 1 minuto entre envíos.',
                            'link_exists' => 'Este grupo ya ha sido enviado anteriormente.'
                        );
                        
                        foreach ($errors as $error) {
                            if (isset($error_messages[$error])) {
                                echo '<p>• ' . $error_messages[$error] . '</p>';
                            }
                        }
                        ?>
                    </div>
                <?php endif; ?>
                
                <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="grupo-form">
                    <input type="hidden" name="action" value="hot_process_grupo_form">
                    <?php wp_nonce_field('hot_grupo_action', 'hot_grupo_nonce'); ?>
                    
                    <p>
                        <label for="grupo_titulo">Group Título *</label>
                        <input type="text" 
                               id="grupo_titulo" 
                               name="grupo_titulo" 
                               required 
                               class="hot-input" 
                               placeholder="Ex: XXX Group +18"
                               value="<?php echo isset($_GET['titulo']) ? esc_attr($_GET['titulo']) : ''; ?>">
                        <small>Enter a descriptive and attractive title</small>
                    </p>
                    
                    <p>
                        <label for="grupo_link">Enlace de Telegram *</label>
                        <input type="url" 
                               id="grupo_link" 
                               name="grupo_link" 
                               required 
                               class="hot-input" 
                               placeholder="https://t.me/yourgroup"
                               value="<?php echo isset($_GET['link']) ? esc_attr($_GET['link']) : ''; ?>">
                        <small>Paste the complete group link (must start with t.me or telegram.me)</small>
                    </p>
                    
                    <p>
                        <label for="grupo_descricao">Group Descripción *</label>
                        <textarea id="grupo_descricao" 
                                  name="grupo_descricao" 
                                  required 
                                  minlength="150" 
                                  class="hot-textarea" 
                                  placeholder="Describe the group content with at least 150 characters..."><?php echo isset($_GET['descricao']) ? esc_textarea($_GET['descricao']) : ''; ?></textarea>
                        <small id="char-count">Minimum: 150 characters</small>
                    </p>
                    
                    <p>
                        <label for="grupo_categoria">Categoría *</label>
                        <select id="grupo_categoria" name="grupo_categoria" class="hot-select" required>
                            <option value="">Selecciona una categoría</option>
                            <?php
                            $categories = get_categories(array(
                                'orderby' => 'name',
                                'order' => 'ASC',
                                'hide_empty' => false,
                                'exclude' => array(1) // Exclui "Sem categoria"
                            ));
                            
                            foreach ($categories as $cat) {
                                echo '<option value="' . $cat->term_id . '">' . esc_html($cat->name) . '</option>';
                            }
                            ?>
                        </select>
                        <small>Choose the most appropriate category for your group</small>
                    </p>
                    
                    <p class="form-info">
                        <strong>ℹ️ Important:</strong><br>
                        • All groups go through moderation before being published<br>
                        • Groups with illegal content will be rejected<br>
                        • Keep the link always updated
                    </p>
                    
                    <p class="form-submit">
                        <button type="submit" class="hot-button hot-button-primary">
                            🚀 Enviar Grupo for Review
                        </button>
                    </p>
                </form>
                
                <div class="form-help">
                    <h4>💡 Tips for Approval:</h4>
                    <ul>
                        <li>Use a clear and specific title</li>
                        <li>Write a detailed description (the more, the better)</li>
                        <li>Choose the correct category</li>
                        <li>Verify that the link works</li>
                        <li>Don't submit duplicate groups</li>
                    </ul>
                </div>
            </div>
            
            <script>
            // Contador de caracteres para descrição
            jQuery(document).ready(function($) {
                var $textarea = $('#grupo_descricao');
                var $counter = $('#char-count');
                
                $textarea.on('input', function() {
                    var length = $(this).val().length;
                    var remaining = 150 - length;
                    
                    if (length < 150) {
                        $counter.text('Missing ' + remaining + ' characters');
                        $counter.css('color', '#ff6b6b');
                    } else {
                        $counter.text('✓ ' + length + ' characters (OK)');
                        $counter.css('color', '#00ff88');
                    }
                });
            });
            </script>
            
        <?php endif; ?>
        
    </div>
</div>

<?php get_footer(); ?>
