@extends('layouts.admin')

@section('title', 'Email Templates')

@section('admin-content')
<style>
    /* Specific Email Template Styles */
    .templates-page .slug-badge { font-family: 'Consolas', 'Monaco', monospace; background: #f8f9fa; color: #6c757d; padding: 4px 8px; border-radius: 6px; border: 1px solid #e9ecef; font-size: 0.8rem; }
    .templates-page .toggle-switch .form-check-input { width: 3em; height: 1.5em; cursor: pointer; }
</style>

<div class="templates-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Email Templates</h4>
            <p class="text-muted mb-0 small">Manage automated system email notifications</p>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success border-0 shadow-sm rounded-3 mb-4 d-flex align-items-center">
            <i class="bi bi-check-circle-fill me-2 fs-5"></i>
            {{ session('success') }}
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger border-0 shadow-sm rounded-3 mb-4 d-flex align-items-center">
            <i class="bi bi-exclamation-triangle-fill me-2 fs-5"></i>
            {{ session('error') }}
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <!-- Stats -->
    <div class="row g-4 mb-4">
        <div class="col-md-4">
            <div class="stat-card stat-card-primary d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-envelope"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $templates->count() }}</div>
                    <div class="stat-label">Total Templates</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card stat-card-success d-flex align-items-center gap-3">
                <div class="stat-icon success">
                    <i class="bi bi-send-check"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $templates->where('is_active', true)->count() }}</div>
                    <div class="stat-label">Active Sending</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card stat-card-secondary d-flex align-items-center gap-3">
                <div class="stat-icon secondary">
                    <i class="bi bi-envelope-slash"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $templates->where('is_active', false)->count() }}</div>
                    <div class="stat-label">Inactive</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Templates Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Template Name</th>
                        <th>Email Subject</th>
                        <th class="text-center">Status</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($templates as $template)
                        <tr>
                            <td class="ps-4">
                                <div class="d-flex flex-column">
                                    <span class="fw-bold text-dark">{{ $template->name }}</span>
                                    <div class="mt-1">
                                        <span class="slug-badge">{{ $template->slug }}</span>
                                    </div>
                                    @if(isset($logicMap[$template->slug]))
                                        <div class="mt-2 text-primary small d-flex align-items-center">
                                            <i class="bi bi-info-circle me-1"></i>
                                            {{ $logicMap[$template->slug] }}
                                        </div>
                                    @endif
                                </div>
                            </td>
                            <td>
                                <span class="text-muted fw-medium">{{ Str::limit($template->subject, 60) }}</span>
                            </td>
                            <td class="text-center">
                                <form action="{{ route('admin.email_templates.toggle', $template->id) }}" method="POST" class="d-inline-block">
                                    @csrf
                                    <div class="form-check form-switch toggle-switch d-flex justify-content-center">
                                        <input class="form-check-input" type="checkbox" role="switch" onchange="this.form.submit()" {{ $template->is_active ? 'checked' : '' }} title="Toggle Status">
                                    </div>
                                    <div class="small fw-bold mt-1 {{ $template->is_active ? 'text-success' : 'text-muted' }}">
                                        {{ $template->is_active ? 'Active' : 'Disabled' }}
                                    </div>
                                </form>
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-2 justify-content-end">
                                    <button type="button" class="action-btn info" title="Sent Test Email" 
                                            data-bs-toggle="modal" data-bs-target="#testEmailModal{{ $template->id }}">
                                        <i class="bi bi-send"></i>
                                    </button>
                                    
                                    <a href="{{ route('admin.email_templates.edit', $template->id) }}" class="action-btn primary" title="Edit Template">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Test Email Modals -->
@foreach($templates as $template)
<div class="modal fade" id="testEmailModal{{ $template->id }}" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg" style="border-radius: 16px;">
            <div class="modal-header border-0 pb-0">
                <div>
                    <h5 class="modal-title fw-bold">Test Email Template</h5>
                    <p class="text-muted small mb-0">Send a test for: <strong>{{ $template->name }}</strong></p>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('admin.email_templates.test', $template->id) }}" method="POST">
                @csrf
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Recipient Email</label>
                        <div class="input-group input-group-lg">
                            <span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-envelope"></i></span>
                            <input type="email" name="email" class="form-control border-start-0 fs-6" 
                                   placeholder="Enter email address" required value="{{ auth()->user()->email }}">
                        </div>
                        <div class="form-text mt-2"><i class="bi bi-info-circle me-1"></i> A preview email will be sent with placeholder data.</div>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary rounded-3 px-4">
                        <i class="bi bi-send-fill me-2"></i> Send Test
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endforeach

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (el) {
            return new bootstrap.Tooltip(el);
        });
    });
</script>
@endsection
