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

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

    <div class="card-body">
        <form action="{{ route("admin.kegiatanpkapks.update", [$kegiatanpkapk->id]) }}" method="POST" enctype="multipart/form-data">
            @csrf
            @method('PUT')
            <div class="form-group {{ $errors->has('nama_id') ? 'has-error' : '' }}">
                <label for="nama">{{ trans('cruds.kegiatanpkapk.fields.nama') }}</label>
                <select name="nama_id" id="nama" class="form-control select2">
                    @foreach($namas as $id => $nama)
                        <option value="{{ $id }}" {{ (isset($kegiatanpkapk) && $kegiatanpkapk->nama ? $kegiatanpkapk->nama->id : old('nama_id')) == $id ? 'selected' : '' }}>{{ $nama }}</option>
                    @endforeach
                </select>
                @if($errors->has('nama_id'))
                    <em class="invalid-feedback">
                        {{ $errors->first('nama_id') }}
                    </em>
                @endif
            </div>
            <div class="form-group {{ $errors->has('kegiatan_id') ? 'has-error' : '' }}">
                <label for="kegiatan">{{ trans('cruds.kegiatanpkapk.fields.kegiatan') }}</label>
                <select name="kegiatan_id" id="kegiatan" class="form-control select2">
                    @foreach($kegiatans as $id => $kegiatan)
                        <option value="{{ $id }}" {{ (isset($kegiatanpkapk) && $kegiatanpkapk->kegiatan ? $kegiatanpkapk->kegiatan->id : old('kegiatan_id')) == $id ? 'selected' : '' }}>{{ $kegiatan }}</option>
                    @endforeach
                </select>
                @if($errors->has('kegiatan_id'))
                    <em class="invalid-feedback">
                        {{ $errors->first('kegiatan_id') }}
                    </em>
                @endif
            </div>
            <div class="form-group {{ $errors->has('nama_klien_id') ? 'has-error' : '' }}">
                <label for="nama_klien">{{ trans('cruds.kegiatanpkapk.fields.nama_klien') }}</label>
                <select name="nama_klien_id" id="nama_klien" class="form-control select2">
                    @foreach($nama_kliens as $id => $nama_klien)
                        <option value="{{ $id }}" {{ (isset($kegiatanpkapk) && $kegiatanpkapk->nama_klien ? $kegiatanpkapk->nama_klien->id : old('nama_klien_id')) == $id ? 'selected' : '' }}>{{ $nama_klien }}</option>
                    @endforeach
                </select>
                @if($errors->has('nama_klien_id'))
                    <em class="invalid-feedback">
                        {{ $errors->first('nama_klien_id') }}
                    </em>
                @endif
            </div>
            <div class="form-group {{ $errors->has('penilaian') ? 'has-error' : '' }}">
                <label>{{ trans('cruds.kegiatanpkapk.fields.penilaian') }}</label>
                @foreach(App\Kegiatanpkapk::PENILAIAN_RADIO as $key => $label)
                    <div>
                        <input id="penilaian_{{ $key }}" name="penilaian" type="radio" value="{{ $key }}" {{ old('penilaian', $kegiatanpkapk->penilaian) === (string)$key ? 'checked' : '' }}>
                        <label for="penilaian_{{ $key }}">{{ $label }}</label>
                    </div>
                @endforeach
                @if($errors->has('penilaian'))
                    <em class="invalid-feedback">
                        {{ $errors->first('penilaian') }}
                    </em>
                @endif
            </div>
            <div class="form-group {{ $errors->has('dokumen') ? 'has-error' : '' }}">
                <label for="dokumen">{{ trans('cruds.kegiatanpkapk.fields.dokumen') }}</label>
                <div class="needsclick dropzone" id="dokumen-dropzone">

                </div>
                @if($errors->has('dokumen'))
                    <em class="invalid-feedback">
                        {{ $errors->first('dokumen') }}
                    </em>
                @endif
                <p class="helper-block">
                    {{ trans('cruds.kegiatanpkapk.fields.dokumen_helper') }}
                </p>
            </div>
            <div>
                <input class="btn btn-danger" type="submit" value="{{ trans('global.save') }}">
            </div>
        </form>
    </div>
</div>
@endsection

@section('scripts')
<script>
    var uploadedDokumenMap = {}
Dropzone.options.dokumenDropzone = {
    url: '{{ route('admin.kegiatanpkapks.storeMedia') }}',
    maxFilesize: 2, // MB
    addRemoveLinks: true,
    headers: {
      'X-CSRF-TOKEN': "{{ csrf_token() }}"
    },
    params: {
      size: 2
    },
    success: function (file, response) {
      $('form').append('<input type="hidden" name="dokumen[]" value="' + response.name + '">')
      uploadedDokumenMap[file.name] = response.name
    },
    removedfile: function (file) {
      file.previewElement.remove()
      var name = ''
      if (typeof file.file_name !== 'undefined') {
        name = file.file_name
      } else {
        name = uploadedDokumenMap[file.name]
      }
      $('form').find('input[name="dokumen[]"][value="' + name + '"]').remove()
    },
    init: function () {
@if(isset($kegiatanpkapk) && $kegiatanpkapk->dokumen)
          var files =
            {!! json_encode($kegiatanpkapk->dokumen) !!}
              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="dokumen[]" 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>
@stop