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

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

    <div class="card-body">
        <form method="POST" action="{{ route("admin.amc-contracts.store") }}" enctype="multipart/form-data">
            @csrf
            <div class="form-group">
                <label class="required" for="client_id">{{ trans('cruds.amcContract.fields.client') }}</label>
                <select class="form-control select2 {{ $errors->has('client') ? 'is-invalid' : '' }}" name="client_id" id="client_id" required>
                    @foreach($clients as $id => $entry)
                        <option value="{{ $id }}" {{ old('client_id') == $id ? 'selected' : '' }}>{{ $entry }}</option>
                    @endforeach
                </select>
                @if($errors->has('client'))
                    <div class="invalid-feedback">
                        {{ $errors->first('client') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.amcContract.fields.client_helper') }}</span>
            </div>
            <div class="form-group">
                <label class="required" for="assets">{{ trans('cruds.amcContract.fields.asset') }}</label>
                <div style="padding-bottom: 4px">
                    <span class="btn btn-info btn-xs select-all" style="border-radius: 0">{{ trans('global.select_all') }}</span>
                    <span class="btn btn-info btn-xs deselect-all" style="border-radius: 0">{{ trans('global.deselect_all') }}</span>
                </div>
                <select class="form-control select2 {{ $errors->has('assets') ? 'is-invalid' : '' }}" name="assets[]" id="assets" multiple required>
                    @foreach($assets as $id => $asset)
                        <option value="{{ $id }}" {{ in_array($id, old('assets', [])) ? 'selected' : '' }}>{{ $asset }}</option>
                    @endforeach
                </select>
                @if($errors->has('assets'))
                    <div class="invalid-feedback">
                        {{ $errors->first('assets') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.amcContract.fields.asset_helper') }}</span>
            </div>
            <div class="form-group">
                <label class="required" for="amc_start_date">{{ trans('cruds.amcContract.fields.amc_start_date') }}</label>
                <input class="form-control date {{ $errors->has('amc_start_date') ? 'is-invalid' : '' }}" type="text" name="amc_start_date" id="amc_start_date" value="{{ old('amc_start_date') }}" required>
                @if($errors->has('amc_start_date'))
                    <div class="invalid-feedback">
                        {{ $errors->first('amc_start_date') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.amcContract.fields.amc_start_date_helper') }}</span>
            </div>
            <div class="form-group">
                <label class="required" for="amc_expiry_date">{{ trans('cruds.amcContract.fields.amc_expiry_date') }}</label>
                <input class="form-control date {{ $errors->has('amc_expiry_date') ? 'is-invalid' : '' }}" type="text" name="amc_expiry_date" id="amc_expiry_date" value="{{ old('amc_expiry_date') }}" required>
                @if($errors->has('amc_expiry_date'))
                    <div class="invalid-feedback">
                        {{ $errors->first('amc_expiry_date') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.amcContract.fields.amc_expiry_date_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="amc_document">{{ trans('cruds.amcContract.fields.amc_document') }}</label>
                <div class="needsclick dropzone {{ $errors->has('amc_document') ? 'is-invalid' : '' }}" id="amc_document-dropzone">
                </div>
                @if($errors->has('amc_document'))
                    <div class="invalid-feedback">
                        {{ $errors->first('amc_document') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.amcContract.fields.amc_document_helper') }}</span>
            </div>
            <div class="form-group">
                <label for="assigned_team_id">{{ trans('cruds.amcContract.fields.assigned_team') }}</label>
                <select class="form-control select2 {{ $errors->has('assigned_team') ? 'is-invalid' : '' }}" name="assigned_team_id" id="assigned_team_id">
                    @foreach($assigned_teams as $id => $entry)
                        <option value="{{ $id }}" {{ old('assigned_team_id') == $id ? 'selected' : '' }}>{{ $entry }}</option>
                    @endforeach
                </select>
                @if($errors->has('assigned_team'))
                    <div class="invalid-feedback">
                        {{ $errors->first('assigned_team') }}
                    </div>
                @endif
                <span class="help-block">{{ trans('cruds.amcContract.fields.assigned_team_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>
    Dropzone.options.amcDocumentDropzone = {
    url: '{{ route('admin.amc-contracts.storeMedia') }}',
    maxFilesize: 100, // MB
    maxFiles: 1,
    addRemoveLinks: true,
    headers: {
      'X-CSRF-TOKEN': "{{ csrf_token() }}"
    },
    params: {
      size: 100
    },
    success: function (file, response) {
      $('form').find('input[name="amc_document"]').remove()
      $('form').append('<input type="hidden" name="amc_document" value="' + response.name + '">')
    },
    removedfile: function (file) {
      file.previewElement.remove()
      if (file.status !== 'error') {
        $('form').find('input[name="amc_document"]').remove()
        this.options.maxFiles = this.options.maxFiles + 1
      }
    },
    init: function () {
@if(isset($amcContract) && $amcContract->amc_document)
      var file = {!! json_encode($amcContract->amc_document) !!}
          this.options.addedfile.call(this, file)
      file.previewElement.classList.add('dz-complete')
      $('form').append('<input type="hidden" name="amc_document" value="' + file.file_name + '">')
      this.options.maxFiles = this.options.maxFiles - 1
@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