<x-app-layout>
    @section('title')
        Blog Update
    @endsection
    <!-- Page Heading -->
    <h1 class="h3 mb-2 text-gray-800">
        @can('Blog access')
            <a href="{{ route('admin.blogs.index') }}" class="btn btn-primary"><i class="fas fa-plus" aria-hidden="true"></i>
                Blog Listing
            </a>
        @endcan
    </h1>

    <!-- DataTales Example -->
    <div class="row">
        <div class="col-md-12">
            <form action="{{ route('admin.blogs.update',$data->id) }}" method="POST" enctype="multipart/form-data" class="needs-validation" role="form" novalidate>
                @csrf
                @method('PUT')
                <div class="card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary">Blog Form</h6>
                    </div>
                    <div class="card-body">

                        <div class="form-group">
                            <label class="mb-0" for="title">Title <span class="text-danger">*</span></label>
                            <input type="text" value="{{ $data->title }}" name="title" class="form-control @error('title') is-invalid @enderror" id="title" />
                            @error('title')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label class="mb-0" for="category_id">Category <span class="text-danger">*</span></label>
                            <select name="category_id" id="category_id" class="form-control @error('category_id') is-invalid @enderror">
                                @foreach ($categories as $row)
                                    <option value="{{ $row->id }}" {{ $row->id == $data->category_id ? 'selected' : '' }}>{{ $row->name }}</option>
                                @endforeach
                            </select>
                            @error('category_id')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label class="mb-0" for="image">Image </label>
                            <input type="file" name="image" value="{{$data->image}}" id="image" class="form-control mb-2 @error('image') is-invalid @enderror" mb-3" />
                            @if ($data->image==NULL)
                            <img class="img-fuild" width="150px" height="120px" id="prevImg" src="{{ asset('backend/assets/images/prev.jpg') }}" alt="" style="border: 1px dotted #000">
                            @else
                                <img class="img-fuild" width="150px" height="120px" id="prevImg" src="{{ asset($data->image) }}" alt="" style="border: 1px dotted #000">
                            @endif
                            @error('image')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label class="mb-0" for="short_description">Short Description <span class="text-danger">*</span></label>
                            <textarea class="form-control @error('image') is-invalid @enderror" cols="30" rows="5" id="short_description" name="short_description">
                                {!! $data->short_description !!}
                            </textarea>
                            @error('image')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label class="mb-0" for="description">Description <span class="text-danger">*</span></label>
                            <textarea class="summernote @error('description') is-invalid @enderror" id="description" name="description">
                                {!! $data->description !!}
                            </textarea>
                            @error('description')
                                <span class="text-danger">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="mb-0" for="status">Type and press Enter to add tags <span class="text-danger">*</span></label>
                                    <input name="tags" value="{{ $data->tags }}" class="form-control @error('tags') is-invalid @enderror" type="text" id="tags" placeholder="tags">
                                    @error('tags')
                                        <span class="text-danger">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="mb-0" for="status">Status <span class="text-danger">*</span></label>
                                    <select name="status" id="status" class="form-control @error('status') is-invalid @enderror">
                                        <option value="1" {{ $data->status == 1 ? 'selected' : '' }}>Active</option>
                                        <option value="0" {{ $data->status == 0 ? 'selected' : '' }}>Inactive</option>
                                    </select>
                                    @error('status')
                                        <span class="text-danger">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>

                        </div>
                    </div>

                    <div class="card-footer text-right">
                        <button type="submit" class="btn btn-success">Update</button>
                    </div>

                </div>
            </form>
        </div>
    </div>
    @section('script')

    <script>
        $(document).ready(() => {
            // Banner Image
            $('#image').change(function() {
                const file = this.files[0];
                console.log(file);
                if (file) {
                    let reader = new FileReader();
                    reader.onload = function(event) {
                        console.log(event.target.result);
                        $('#prevImg').attr('src', event.target.result);
                        $("#showImage").css('display','block')
                    }
                    reader.readAsDataURL(file);
                }
            });

        });
    </script>

    <script>
    $('input[name="tags"]').amsifySuggestags({
        type : 'amsify',
        suggestions: ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupitor', 'Uranus', 'Neptune', 'Pluto'],
    });
    </script>


    @endsection
</x-app-layout>
