@extends('layouts.dashboard')

@section('title', 'Message Templates')
@section('page-title', 'Message Templates')

@section('content')
<div class="table-container">
    <!-- Filters -->
    <form action="{{ route('dashboard.templates') }}" method="GET" class="row g-3 mb-4">
        <div class="col-md-3">
            <select name="client_id" class="form-select">
                <option value="">All Organizations</option>
                @foreach($clients as $client)
                <option value="{{ $client->id }}" {{ request('client_id') == $client->id ? 'selected' : '' }}>
                    {{ $client->name }}
                </option>
                @endforeach
            </select>
        </div>

        <div class="col-md-2">
            <select name="type" class="form-select">
                <option value="">All Types</option>
                <option value="auto_reply" {{ request('type') == 'auto_reply' ? 'selected' : '' }}>Auto Reply</option>
                <option value="promotion" {{ request('type') == 'promotion' ? 'selected' : '' }}>Promotion</option>
            </select>
        </div>

        <div class="col-md-2">
            <select name="call_type" class="form-select">
                <option value="">All Call Types</option>
                <option value="incoming" {{ request('call_type') == 'incoming' ? 'selected' : '' }}>Incoming</option>
                <option value="outgoing" {{ request('call_type') == 'outgoing' ? 'selected' : '' }}>Outgoing</option>
                <option value="missed" {{ request('call_type') == 'missed' ? 'selected' : '' }}>Missed</option>
                <option value="all" {{ request('call_type') == 'all' ? 'selected' : '' }}>All Calls</option>
            </select>
        </div>

        <div class="col-md-2">
            <select name="channel" class="form-select">
                <option value="">All Channels</option>
                <option value="sms" {{ request('channel') == 'sms' ? 'selected' : '' }}>SMS</option>
                <option value="whatsapp" {{ request('channel') == 'whatsapp' ? 'selected' : '' }}>WhatsApp</option>
            </select>
        </div>

        <div class="col-md-1">
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-filter"></i>
            </button>
        </div>

        <div class="col-md-1 text-end">
            <a href="{{ route('dashboard.templates.create') }}" class="btn btn-success">
                <i class="bi bi-plus-lg"></i>
            </a>
        </div>
    </form>

    <!-- Results Info -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <span class="text-muted">Showing {{ $templates->count() }} of {{ $templates->total() }} templates</span>
    </div>

    <!-- Templates Grid -->
    <div class="row g-4">
        @forelse($templates as $template)
        <div class="col-md-6 col-lg-4">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h6 class="mb-0">{{ $template->name }}</h6>
                    <div>
                        @if($template->is_default)
                        <span class="badge bg-primary">Default</span>
                        @endif
                        @if($template->is_active)
                        <span class="badge bg-success">Active</span>
                        @else
                        <span class="badge bg-secondary">Inactive</span>
                        @endif
                    </div>
                </div>
                <div class="card-body">
                    <div class="mb-2">
                        <span class="badge bg-{{ $template->type == 'promotion' ? 'warning' : 'info' }}">
                            {{ ucfirst(str_replace('_', ' ', $template->type ?? 'auto_reply')) }}
                        </span>
                        @php
                        $channelBadge = match($template->channel ?? 'sms') {
                        'whatsapp' => 'bg-success',
                        default => 'bg-primary',
                        };
                        $channelLabel = match($template->channel ?? 'sms') {
                        'whatsapp' => 'WhatsApp',
                        default => 'SMS',
                        };
                        @endphp
                        <span class="badge {{ $channelBadge }}">
                            @if(($template->channel ?? 'sms') == 'whatsapp')
                            <i class="bi bi-whatsapp"></i>
                            @else
                            <i class="bi bi-chat-text"></i>
                            @endif
                            {{ $channelLabel }}
                        </span>
                        <span class="badge bg-light text-dark">
                            {{ ucfirst($template->call_type ?? 'all') }}
                        </span>
                        @if($template->priority > 0)
                        <span class="badge bg-secondary">Priority: {{ $template->priority }}</span>
                        @endif
                    </div>
                    <p class="card-text small">{{ Str::limit($template->content, 120) }}</p>
                </div>
                <div class="card-footer bg-transparent">
                    <div class="d-flex justify-content-between align-items-center">
                        <small class="text-muted">
                            <i class="bi bi-building me-1"></i>
                            {{ $template->client->name ?? 'Global' }}
                        </small>
                        <div class="btn-group btn-group-sm">
                            <a href="{{ route('dashboard.templates.edit', $template->id) }}"
                                class="btn btn-outline-secondary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <form action="{{ route('dashboard.templates.toggle-status', $template->id) }}" method="POST" class="d-inline">
                                @csrf
                                <button type="submit" class="btn btn-outline-{{ $template->is_active ? 'warning' : 'success' }}"
                                    title="{{ $template->is_active ? 'Deactivate' : 'Activate' }}">
                                    <i class="bi bi-{{ $template->is_active ? 'pause' : 'play' }}"></i>
                                </button>
                            </form>
                            <form action="{{ route('dashboard.templates.delete', $template->id) }}" method="POST" class="d-inline">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-outline-danger" title="Delete"
                                    data-confirm="Are you sure you want to delete this template?"
                                    data-confirm-title="Delete Template"
                                    data-confirm-btn="Delete"
                                    data-confirm-class="btn-danger">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        @empty
        <div class="col-12">
            <div class="text-center text-muted py-5">
                <i class="bi bi-file-text display-4"></i>
                <p class="mt-3">No message templates found</p>
                <a href="{{ route('dashboard.templates.create') }}" class="btn btn-primary">
                    <i class="bi bi-plus-lg"></i> Create Your First Template
                </a>
            </div>
        </div>
        @endforelse
    </div>

    <!-- Pagination -->
    <div class="d-flex justify-content-center mt-4">
        {{ $templates->withQueryString()->links() }}
    </div>
</div>
@endsection