@extends('layouts.app')
@section('title', 'Email Templates')
@section('content')
<div class="page-header"><div class="page-header-left"><div class="page-header-title"><h5 class="m-b-10">Email Templates</h5></div></div></div>
<div class="main-content">
 @if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
 @if($errors->any())<div class="alert alert-danger">{{ $errors->first() }}</div>@endif
 <div class="card stretch stretch-full" id="emailTemplateWorkspace">
  <div class="card-header d-flex justify-content-between"><div><h5 class="card-title mb-1">Email Template Workspace</h5><small class="text-muted">Filter, create, and manage templates from one screen.</small></div><a class="btn btn-sm btn-light" href="{{ route('workflow.index',[$module,'deleted'=>request('deleted')?null:1]) }}">{{ request('deleted')?'Show Current':'Show Deleted' }}</a></div>

  <section class="border-bottom">
   <div class="px-3 pt-3"><h6 class="mb-0">Filters</h6></div>
   <x-server-list-toolbar form-id="emailTemplateFilters" apply-label="Apply Filters" reset-label="Reset Filters"
     actions-class="w-100 justify-content-center" placeholder="Search by template name, subject, or category" :filters="[
      'active'=>['label'=>'Status','options'=>['1'=>'Active','0'=>'Inactive']],
      'category'=>['label'=>'Category','options'=>\App\Models\EmailTemplate::query()->whereNotNull('category')->distinct()->orderBy('category')->pluck('category','category')->all()],
      'date_from'=>['label'=>'Created from','type'=>'date'],'date_to'=>['label'=>'Created to','type'=>'date'],
     ]" />
  </section>

  <section class="card-body border-bottom bg-light-subtle">
   <h6>Create Email Template</h6>
   <form method="POST" action="{{ route('workflow.store',$module) }}" class="row g-3">@csrf
    <div class="col-md-6"><label class="form-label">Template Name</label><input name="name" value="{{ old('name') }}" class="form-control" required maxlength="255"></div>
    <div class="col-md-6"><label class="form-label">Subject</label><input name="subject" value="{{ old('subject') }}" class="form-control" required maxlength="255"></div>
    <div class="col-md-4"><label class="form-label">Category</label><input name="category" value="{{ old('category') }}" class="form-control" maxlength="255"></div>
    <div class="col-md-4"><label class="form-label">Status</label><select name="active" class="form-select" required><option value="1">Active</option><option value="0">Inactive</option></select></div>
    <div class="col-12"><label class="form-label">Email Body</label><textarea name="body" class="form-control" rows="4" required>{{ old('body') }}</textarea></div>
    <div class="col-12 text-center"><button class="btn btn-primary px-4"><i class="feather-save me-2"></i>Save Template</button></div>
   </form>
  </section>

  <section class="card-body">
   <div class="d-flex justify-content-between align-items-center mb-3"><h6 class="mb-0">{{ request('deleted')?'Deleted':'Current' }} Templates</h6><span class="badge bg-soft-primary text-primary">{{ $records->total() }} records</span></div>
   <div class="table-responsive"><table class="table table-hover align-middle" id="emailTemplateList"><thead><tr><th>Name</th><th>Subject</th><th>Category</th><th>Status</th><th>Created</th><th class="text-end">Actions</th></tr></thead><tbody>
    @forelse($records as $record)<tr><td class="fw-semibold">{{ $record->name }}</td><td>{{ $record->subject }}</td><td>{{ $record->category ?: 'General' }}</td><td><span class="badge {{ $record->active?'bg-soft-success text-success':'bg-soft-secondary text-secondary' }}">{{ $record->active?'Active':'Inactive' }}</span></td><td>{{ $record->created_at?->format('d M Y, h:i A') }}</td><td class="text-end">
     @if($record->trashed())<form method="POST" action="{{ route('workflow.restore',[$module,$record->id]) }}">@csrf<button class="btn btn-sm btn-success">Restore</button></form>
     @else<div class="d-inline-flex gap-2"><form method="POST" action="{{ route('workflow.destroy',[$module,$record->id]) }}" onsubmit="return confirm('Delete this email template?')">@csrf @method('DELETE')<button class="avatar-text avatar-md border-0 bg-soft-danger text-danger" title="Delete Template" aria-label="Delete Template"><i class="feather-trash-2"></i></button></form></div>@endif
    </td></tr>@empty<tr><td colspan="6" class="text-center text-muted py-5">No email templates match the filters.</td></tr>@endforelse
   </tbody></table></div>
   <div class="d-flex flex-wrap justify-content-between align-items-center gap-2 pt-3 border-top"><small class="text-muted">Showing {{ $records->firstItem() ?? 0 }}–{{ $records->lastItem() ?? 0 }} of {{ $records->total() }} email templates</small>@if($records->hasPages()){{ $records->links() }}@else<nav aria-label="Email Template pagination"><ul class="pagination pagination-sm mb-0"><li class="page-item disabled"><span class="page-link">‹</span></li><li class="page-item active"><span class="page-link">1</span></li><li class="page-item disabled"><span class="page-link">›</span></li></ul></nav>@endif</div>
  </section>
 </div>
</div>
@endsection
