<?php include APP_PATH . '/views/layouts/header.php'; ?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <h1>Edit Template</h1>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo URL_PATH; ?>/admin/dashboard">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="<?php echo URL_PATH; ?>/admin/templates">Templates</a></li>
                <li class="breadcrumb-item active">Edit</li>
            </ol>
        </nav>
    </div>
    
    <div class="row">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-pencil"></i> Edit Template Information
                </div>
                <div class="card-body">
                    <?php if (isset($errorMessage)): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="bi bi-exclamation-triangle"></i>
                            <?php echo $errorMessage; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <form method="POST" action="<?php echo URL_PATH; ?>/admin/templates/edit/<?php echo $template->id; ?>" class="needs-validation" novalidate>
                        <div class="mb-3">
                            <label for="name" class="form-label">Template Name *</label>
                            <input type="text" class="form-control" id="name" name="name" 
                                   value="<?php echo htmlspecialchars($_POST['name'] ?? $template->name); ?>" 
                                   placeholder="e.g., Birthday Wish 1" required>
                            <div class="invalid-feedback">Please enter a template name</div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="description" class="form-label">Description</label>
                            <input type="text" class="form-control" id="description" name="description" 
                                   value="<?php echo htmlspecialchars($_POST['description'] ?? $template->description); ?>" 
                                   placeholder="Brief description of this template">
                            <small class="text-muted">Optional - helps identify the template</small>
                        </div>
                        
                        <div class="mb-3">
                            <label for="content" class="form-label">Template Content *</label>
                            <textarea class="form-control" id="content" name="content" rows="6" 
                                      placeholder="Happy Birthday! 🎉 Wishing you a wonderful day..." required><?php echo htmlspecialchars($_POST['content'] ?? $template->content); ?></textarea>
                            <small class="text-muted">This message will be used for WhatsApp and SMS</small>
                            <div class="invalid-feedback">Please enter template content</div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="status" class="form-label">Status *</label>
                            <select class="form-select" id="status" name="status" required>
                                <option value="active" <?php echo (isset($_POST['status']) ? $_POST['status'] : $template->status) === 'active' ? 'selected' : ''; ?>>Active</option>
                                <option value="inactive" <?php echo (isset($_POST['status']) ? $_POST['status'] : $template->status) === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                            </select>
                            <small class="text-muted">Only active templates are visible to users</small>
                        </div>
                        
                        <hr>
                        
                        <div class="d-flex justify-content-between">
                            <a href="<?php echo URL_PATH; ?>/admin/templates" class="btn btn-secondary">
                                <i class="bi bi-arrow-left"></i> Cancel
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-save"></i> Update Template
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <i class="bi bi-info-circle"></i> Template Details
                </div>
                <div class="card-body">
                    <p><strong>Template ID:</strong> <?php echo $template->id; ?></p>
                    <p><strong>Created:</strong> <?php echo formatDateTime($template->created_at); ?></p>
                    <p><strong>Last Updated:</strong> <?php echo formatDateTime($template->updated_at); ?></p>
                    <p><strong>Current Status:</strong> 
                        <?php if ($template->status === 'active'): ?>
                            <span class="badge bg-success">Active</span>
                        <?php else: ?>
                            <span class="badge bg-secondary">Inactive</span>
                        <?php endif; ?>
                    </p>
                </div>
            </div>
            
            <div class="card mt-3">
                <div class="card-header">
                    <i class="bi bi-lightbulb"></i> Tips
                </div>
                <div class="card-body small">
                    <ul>
                        <li>Keep messages concise and meaningful</li>
                        <li>Use emojis to add personality</li>
                        <li>Test your template before making it active</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include APP_PATH . '/views/layouts/footer.php'; ?>
