@extends('layouts.admin')
@section('content')

<div class="card">
    <div class="card-header">
        {{ trans('global.create') }} {{ trans('cruds.product.title_singular') }}
    </div>

    <div class="card-body">
        <form method="POST" action="{{ route("admin.products.store") }}" enctype="multipart/form-data">
            @csrf
            <div class="form-group">
                <label class="required" for="machine_type_id">{{ trans('cruds.product.fields.machine_type') }}</label>
                <select class="form-control select2 {{ $errors->has('machine_type') ? 'is-invalid' : '' }}" name="machine_type_id" id="machine_type_id" required>
                    @foreach($machine_types as $id => $entry)
                        <option value="{{ $id }}" {{ old('machine_type_id') == $id ? 'selected' : '' }}>{{ $entry }}</option>
                    @endforeach
                </select>
                @if($errors->has('machine_type'))
                    <div class="invalid-feedback">
                        {{ $errors->first('machine_type') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.machine_type_helper') }}</span>
            </div>
            <div class="form-group">
                <label class="required" for="name">{{ trans('cruds.product.fields.name') }}</label>
                <input class="form-control {{ $errors->has('name') ? 'is-invalid' : '' }}" type="text" name="name" id="name" value="{{ old('name', '') }}" required>
                @if($errors->has('name'))
                    <div class="invalid-feedback">
                        {{ $errors->first('name') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.name_helper') }}</span>
            </div>
            <div class="form-group">
                <label class="required" for="model">{{ trans('cruds.product.fields.model') }}</label>
                <input class="form-control {{ $errors->has('model') ? 'is-invalid' : '' }}" type="text" name="model" id="model" value="{{ old('model', '') }}" required>
                @if($errors->has('model'))
                    <div class="invalid-feedback">
                        {{ $errors->first('model') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.model_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="product_version">{{ trans('cruds.product.fields.product_version') }}</label>
                <input class="form-control {{ $errors->has('product_version') ? 'is-invalid' : '' }}" type="text" name="product_version" id="product_version" value="{{ old('product_version', '') }}">
                @if($errors->has('product_version'))
                    <div class="invalid-feedback">
                        {{ $errors->first('product_version') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.product_version_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="part_no">{{ trans('cruds.product.fields.part_no') }}</label>
                <input class="form-control {{ $errors->has('part_no') ? 'is-invalid' : '' }}" type="text" name="part_no" id="part_no" value="{{ old('part_no', '') }}">
                @if($errors->has('part_no'))
                    <div class="invalid-feedback">
                        {{ $errors->first('part_no') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.part_no_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="mfg_year">{{ trans('cruds.product.fields.mfg_year') }}</label>
                <input class="form-control {{ $errors->has('mfg_year') ? 'is-invalid' : '' }}" type="text" name="mfg_year" id="mfg_year" value="{{ old('mfg_year', '') }}">
                @if($errors->has('mfg_year'))
                    <div class="invalid-feedback">
                        {{ $errors->first('mfg_year') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.mfg_year_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="manufacturer">{{ trans('cruds.product.fields.manufacturer') }}</label>
                <input class="form-control {{ $errors->has('manufacturer') ? 'is-invalid' : '' }}" type="text" name="manufacturer" id="manufacturer" value="{{ old('manufacturer', '') }}">
                @if($errors->has('manufacturer'))
                    <div class="invalid-feedback">
                        {{ $errors->first('manufacturer') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.manufacturer_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="description">{{ trans('cruds.product.fields.description') }}</label>
                <textarea class="form-control {{ $errors->has('description') ? 'is-invalid' : '' }}" name="description" id="description">{{ old('description') }}</textarea>
                @if($errors->has('description'))
                    <div class="invalid-feedback">
                        {{ $errors->first('description') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.description_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="product_catalog">{{ trans('cruds.product.fields.product_catalog') }}</label>
                <div class="needsclick dropzone {{ $errors->has('product_catalog') ? 'is-invalid' : '' }}" id="product_catalog-dropzone">
                </div>
                @if($errors->has('product_catalog'))
                    <div class="invalid-feedback">
                        {{ $errors->first('product_catalog') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.product.fields.product_catalog_helper') }}</span>
            </div>
            <div class="form-group">
                <button class="btn btn-danger" type="submit">
                    {{ trans('global.save') }}
                </button>
            </div>
        </form>
    </div>
</div>



@endsection

@section('scripts')
<script>
    var uploadedProductCatalogMap = {}
Dropzone.options.productCatalogDropzone = {
    url: '{{ route('admin.products.storeMedia') }}',
    maxFilesize: 250, // MB
    addRemoveLinks: true,
    headers: {
      'X-CSRF-TOKEN': "{{ csrf_token() }}"
    },
    params: {
      size: 250
    },
    success: function (file, response) {
      $('form').append('<input type="hidden" name="product_catalog[]" value="' + response.name + '">')
      uploadedProductCatalogMap[file.name] = response.name
    },
    removedfile: function (file) {
      file.previewElement.remove()
      var name = ''
      if (typeof file.file_name !== 'undefined') {
        name = file.file_name
      } else {
        name = uploadedProductCatalogMap[file.name]
      }
      $('form').find('input[name="product_catalog[]"][value="' + name + '"]').remove()
    },
    init: function () {
@if(isset($product) && $product->product_catalog)
          var files =
            {!! json_encode($product->product_catalog) !!}
              for (var i in files) {
              var file = files[i]
              this.options.addedfile.call(this, file)
              file.previewElement.classList.add('dz-complete')
              $('form').append('<input type="hidden" name="product_catalog[]" value="' + file.file_name + '">')
            }
@endif
    },
     error: function (file, response) {
         if ($.type(response) === 'string') {
             var message = response //dropzone sends it's own error messages in string
         } else {
             var message = response.errors.file
         }
         file.previewElement.classList.add('dz-error')
         _ref = file.previewElement.querySelectorAll('[data-dz-errormessage]')
         _results = []
         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
             node = _ref[_i]
             _results.push(node.textContent = message)
         }

         return _results
     }
}
</script>
@endsection