@extends('layouts.app')

@section('title', __('whatsapp::lang.whatsapp') . ' — ' . __('whatsapp::lang.templates'))

@section('content')
<section class="content-header">
    <h1><i class="fab fa-whatsapp"></i> {{ __('whatsapp::lang.whatsapp') }}
        <small>{{ __('whatsapp::lang.templates') }}</small></h1>
</section>

<section class="content">
    @include('whatsapp::partials._nav')

    <div class="row">
        <div class="col-md-5">
            <div class="box box-primary">
                <div class="box-header with-border"><h3 class="box-title">New template</h3></div>
                <form method="POST" action="{{ route('whatsapp.templates.store') }}">
                    @csrf
                    @include('whatsapp::templates._form', ['template' => null])
                    <div class="box-footer"><button class="btn btn-primary"><i class="fa fa-plus"></i> Save</button></div>
                </form>
            </div>
        </div>
        <div class="col-md-7">
            <div class="box box-default">
                <div class="box-header with-border"><h3 class="box-title">{{ __('whatsapp::lang.templates') }}</h3></div>
                <div class="box-body table-responsive">
                    <table class="table table-striped">
                        <thead><tr><th>Name</th><th>Lang</th><th>Meta status</th><th>Active</th><th></th></tr></thead>
                        <tbody>
                        @forelse($templates as $t)
                            <tr>
                                <td><strong>{{ $t->name }}</strong><br><small class="text-muted">{{ Str::limit($t->body, 60) }}</small></td>
                                <td>{{ $t->language }}</td>
                                <td><span class="label label-default">{{ $t->meta_status }}</span></td>
                                <td>{!! $t->active ? '<i class="fa fa-check text-green"></i>' : '<i class="fa fa-times text-red"></i>' !!}</td>
                                <td class="text-right">
                                    <button class="btn btn-xs btn-default" data-toggle="modal" data-target="#etpl-{{ $t->id }}"><i class="fa fa-edit"></i></button>
                                    <form method="POST" action="{{ route('whatsapp.templates.destroy', $t->id) }}" style="display:inline;" onsubmit="return confirm('Delete?')">
                                        @csrf @method('DELETE')<button class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-muted">No templates yet.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    @foreach($templates as $t)
        <div class="modal fade" id="etpl-{{ $t->id }}" tabindex="-1" role="dialog">
            <div class="modal-dialog"><div class="modal-content">
                <form method="POST" action="{{ route('whatsapp.templates.update', $t->id) }}">
                    @csrf @method('PUT')
                    <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">{{ $t->name }}</h4></div>
                    <div class="modal-body">@include('whatsapp::templates._form', ['template' => $t])</div>
                    <div class="modal-footer"><button class="btn btn-primary">Save</button></div>
                </form>
            </div></div>
        </div>
    @endforeach
</section>
@endsection
