@extends('layouts.app')

@section('content')

    @php
    $disabled = 'disabled';
    if(\Illuminate\Support\Facades\Session::get('user')['role'] == 1) $disabled = ''
    @endphp


    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12 col-md-6">
                <h3>Редактирование продукта <strong>{{ $product->name }}</strong></h3>
                <form action="{{ route('save_product') }}" method="post">
                    @csrf
                    <div class="row mt-3">
                        <div class="col-6">
                            <label for="article" class="form-label">Артикул</label>
                            <input class="form-control" id="article" value="{{ $product->article }}"
                                   disabled>
                            <input type="hidden" name="id" value="{{ $product->id }}" {{ $disabled }}>
                        </div>
                        <div class="col-6">
                            <label for="series" class="form-label">Серия</label>
                            <input class="form-control @error('series') border-danger @enderror"  {{ $disabled }}
                                   name="series" id="series" value="{{ old('series', $product->series) }}">
                            @error('series')
                            <div class="form-text text-danger" id="titleError">
                                {{ $message }}
                            </div>
                            @enderror
                        </div>
                    </div>

                    <div class="row mt-3">
                        <div class="col-6">
                            <label for="name" class="form-label">Наименовение</label>
                            <input class="form-control @error('name') border-danger @enderror"  {{ $disabled }}
                                   name="name" id="name" value="{{ old('name', $product->name) }}">
                            @error('name')
                            <div class="form-text text-danger" id="titleError">
                                {{ $message }}
                            </div>
                            @enderror
                        </div>
                        <div class="col-6">
                            <label for="name_for_form" class="form-label">Наименование под образец формы</label>
                            <input class="form-control @error('name_for_form') border-danger @enderror"  {{ $disabled }}
                                   name="name_for_form" id="name_for_form"
                                   value="{{ old('name_for_form', $product->name_for_form) }}">
                            @error('name_for_form')
                            <div class="form-text text-danger" id="titleError">
                                {{ $message }}
                            </div>
                            @enderror
                        </div>
                    </div>

                    <div class="row mt-3">
                        <div class="col-6">
                            <label for="product_group" class="form-label">Группа</label>
                            <input class="form-control @error('product_group') border-danger @enderror"
                                   name="product_group" id="product_group"  {{ $disabled }}
                                   value="{{ old('product_group', $product->product_group) }}">
                            @error('product_group')
                            <div class="form-text text-danger" id="titleError">
                                {{ $message }}
                            </div>
                            @enderror
                        </div>
                        <div class="col-6">
                            <label for="price" class="form-label">Цена</label>
                            <input type="number" step="0.01" class="form-control @error('price') border-danger @enderror"
                                   name="price" id="price"  {{ $disabled }}
                                   value="{{ old('price', number_format($product->price, 2, '.', '')) }}">
                            @error('price')
                            <div class="form-text text-danger" id="titleError">
                                {{ $message }}
                            </div>
                            @enderror
                        </div>
                    </div>

                    <label for="characteristics" class="form-label mt-3">Характеристики</label>
                    <textarea class="form-control @error('characteristics') border-danger @enderror"
                              name="characteristics" rows="4" {{ $disabled }}
                              id="characteristics">{{ old('characteristics', $product->characteristics) }}</textarea>
                    @error('characteristics')
                    <div class="form-text text-danger" id="titleError">
                        {{ $message }}
                    </div>
                    @enderror

                    <label for="tech_description" class="form-label mt-3">Техническое описание</label>
                    <textarea class="form-control @error('tech_description') border-danger @enderror"
                              name="tech_description" rows="5" {{ $disabled }}
                              id="tech_description">{{ old('tech_description', $product->tech_description) }}</textarea>
                    @error('tech_description')
                    <div class="form-text text-danger" id="titleError">
                        {{ $message }}
                    </div>
                    @enderror

                    <label for="tech_description_short" class="form-label mt-3">Техническое описание сокращенное без
                        артикула</label>
                    <textarea class="form-control  @error('tech_description_short') border-danger @enderror"
                              name="tech_description_short" rows="5" {{ $disabled }}
                              id="tech_description_short">{{
                                old('tech_description_short', $product->tech_description_short) }}</textarea>
                    @error('tech_description_short')
                    <div class="form-text text-danger" id="titleError">
                        {{ $message }}
                    </div>
                    @enderror

                    <div class="col-12 text-center mt-3">
                        <input type="hidden" name="prev_url" value="{{ URL::previous() }}">
                        <button {{ $disabled }} type="submit" class="btn btn-primary">Сохранить</button>
                        <a href="{{ URL::previous() }}" class="btn btn-dark">Назад</a>
                    </div>
                </form>
            </div>

            <div class="col-sm-12 col-md-6">
                <div class="col-12 text-end">
                    <button {{ $disabled }} class="btn btn-primary" onclick="document.getElementById('upload-image-form').click()">
                        Загрузить
                    </button>
                    <br>
                    <span class="notice">Изображение сохраниться в папку с изображениями товаров: stroyprofit.com/{{ env('IMAGES_PATH', 'fill-path') }}/<br>
                    Имя файла будет сформировано уникальное, в начале имени до точки будет артикул товара.</span>


                    <form action="{{ route('upload_image') }}" method="post" enctype="multipart/form-data"
                          class="visually-hidden" onchange="this.submit()">
                        @csrf
                        <input type="hidden" name="article" value="{{ $product->article }}">
                        <input type="file" name="filename" id="upload-image-form" accept=".jpg">
                    </form>
                </div>


                @empty($product->image_path)
                    <div class="text-center my-5 fs-3">Нет изображения</div>
                @else
                    <div class="col-12 text-center" >
                        <img src="{{ '/' . env('IMAGES_PATH', '/fill_images_path_in_env') . '/' . $product->image_path }}"
                             alt="{{ $product->article }}" class="img-fluid" style="max-height: 40vh;">
                    </div>

                @endempty
                @if(count($images) > 1)
                    <div class="col-12 mt-3">
                        <div class="fs-5 mb-4">Найдены изображения с таким артикулом:</div>
                        <div class="row justify-content-center align-items-end">
                            @foreach($images as $image)
                                <div class="h-100 col-md-3 col-sm-2 text-center">
                                    <img class="img-thumbnail"
                                         src="{{ '/' . env('IMAGES_PATH', 'zzz') . '/' . $image }}" alt="{{ $image }}">


                                    <form action="{{ route('update_image', $product->id) }}" method="post">
                                        @csrf
                                        <input type="hidden" name="image_path" value="{{ $image }}">
                                        <button type="submit" class="btn btn-primary my-3">Выбрать</button>
                                    </form>

                                </div>
                            @endforeach
                        </div>
                    </div>
                @endif


            </div>
        </div>
    </div>

@endsection
