@extends('layouts.admin')

@section('title', 'Create SMS Template')

@section('content')
<div class="admin-page-header d-flex justify-content-between align-items-center flex-wrap gap-2">
    <h1>Create SMS Template</h1>
    <a href="{{ route('admin.sms-templates.index') }}" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left"></i> Back to templates
    </a>
</div>

<div class="row">
    <div class="col-12 col-lg-8">
        <div class="admin-card">
            <div class="card-body">
                <form action="{{ route('admin.sms-templates.store') }}" method="POST">
                    @csrf

                    <div class="mb-4">
                        <label for="create_identifier" class="form-label fw-semibold"><span class="text-danger">*</span> Identifier</label>
                        <input type="text" class="form-control" id="create_identifier" name="identifier" required
                               value="{{ old('identifier') }}"
                               placeholder="e.g. order_placement, welcome_sms"
                               pattern="[a-z0-9_]+">
                        <small class="text-muted">Lowercase letters, numbers and underscores only. Used internally.</small>
                        @error('identifier')
                            <div class="text-danger small mt-1">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-4">
                        <label for="create_name" class="form-label fw-semibold"><span class="text-danger">*</span> Name</label>
                        <input type="text" class="form-control" id="create_name" name="name" required
                               value="{{ old('name') }}"
                               placeholder="e.g. Order Placement">
                        <small class="text-muted">Display name shown in the sidebar</small>
                        @error('name')
                            <div class="text-danger small mt-1">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-4">
                        <label for="create_body" class="form-label fw-semibold">SMS Body</label>
                        <textarea class="form-control" id="create_body" name="body" rows="4"
                                  placeholder="Use [[placeholder_name]] for dynamic values">{{ old('body') }}</textarea>
                        @error('body')
                            <div class="text-danger small mt-1">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-4">
                        <label for="create_template_id" class="form-label fw-semibold">Template ID</label>
                        <input type="text" class="form-control" id="create_template_id" name="template_id"
                               value="{{ old('template_id') }}"
                               placeholder="DLT Template ID">
                        @error('template_id')
                            <div class="text-danger small mt-1">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-4">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="is_active" value="1"
                                   id="create_active" {{ old('is_active', true) ? 'checked' : '' }}
                                   style="width: 3rem; height: 1.5rem;">
                            <label class="form-check-label" for="create_active">Active</label>
                        </div>
                    </div>

                    <div class="pt-2">
                        <button type="submit" class="btn text-white" style="background: var(--admin-primary);">
                            <i class="bi bi-plus-lg"></i> Create SMS Template
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    @if($templates->isNotEmpty())
    <div class="col-12 col-lg-4">
        <div class="admin-card">
            <div class="card-body">
                <h6 class="mb-3" style="color: var(--admin-primary);">Existing templates</h6>
                <ul class="list-unstyled mb-0">
                    @foreach($templates as $t)
                        <li class="py-2 border-bottom border-light">
                            <a href="{{ route('admin.sms-templates.show', ['identifier' => $t->identifier]) }}" class="text-decoration-none">
                                {{ $t->name }} <small class="text-muted">({{ $t->identifier }})</small>
                            </a>
                        </li>
                    @endforeach
                </ul>
            </div>
        </div>
    </div>
    @endif
</div>
@endsection
