<div>
    <div class="row">
        <div class="col-md-6 d-flex justify-content-between mt-2 mb-2">
            <h4 class="fw-bold "><span class="text-muted fw-light"></span> Email Templates</h4>
        </div>
    </div>
    <!-- Striped Rows -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive text-wrap">
                <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Description</th>
                        <th>Action</th>
                    </tr>
                </thead>
                @if (count($templates) > 0)
                <tbody class="table-border-bottom-0">
                    @foreach ($templates as $template)
                        <tr>
                            <td>{{$template->title}}</td>
                            <td>{{$template->description ?? '-'}}</td>
                            <td width="10%">
                                <a href="javascript:;" class="editTemplate" data-obj="{{json_encode($template)}}">
                                    <i class="bx bx-edit btn btn-primary btn-xs"></i>
                                </a>
                                <a href="{{route('admin.viewEmailTemplates', $template->id)}}" target="_blank">
                                    <i class="bx bx-show btn btn-info btn-xs"></i>
                                </a>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
                @else
                    <td colspan="5" class="text-center">No record found</td>
                @endif
                </table>
            </div>
        </div>
    </div>

    <div class="modal fade" id="editModal"  tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <form id="editForm" action="" method="POST" enctype="multipart/form-data">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="modalTitle">Edit Template</h5>
                        <button
                            type="button"
                            class="btn-close"
                            data-bs-dismiss="modal"
                            aria-label="Close"
                        ></button>
                    </div>
                    <div class="modal-body">
                        @csrf
                
                        <div class="row">
                            <div class="col mb-2">
                                <label for="image" class="form-label">Description</label>
                                <textarea name="description" class="descriptionField form-control @error('description') is-invalid @enderror"></textarea>
                                @error('description')
                                    <span  class="invalid-feedback"><strong>{{$message}}</strong></span>
                                @enderror
                            </div>
                        </div>
                        
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

@section('script')
<script type="text/javascript">
    $(document).ready(function(){

        $(".editTemplate").on('click', function(){
            var obj = $(this).data('obj');
            console.log(obj);

            $("#editForm .descriptionField").val(obj.description);

            $("#editForm").attr('action', `{{route('admin.editEmailTemplate', '')}}/${obj.id}`)
            $("#editModal").modal('show')
        })
    });
</script>

@if($errors->any())
    <script type="text/javascript">
        $(document).ready(function(){ 
            $("#addCategoryModal").modal('show'); 
        });
    </script>
@endif
@endsection