<?php
use App\Models\Config;
use App\Models\Issue;


// dd(stream_get_contents($user->firma));

?>
<x-app-layout>
  <x-slot name="header">
    <h2 class="font-semibold text-xl text-gray-800 leading-tight">
      <i class="fas fa-user"></i> Profilo utente
    </h2>
  </x-slot>


  <style>
  thead input{
    width: 100%;
  }
  thead select{
    width: 100%;
  }

</style>
<div class="container">

  <div class="row justify-content-center">
    <div class="col-xxl-6 col-xl-8 col-md-10 col">
      <div class="card">
        <div class="card-body">
          @if(Session::has('flash_message'))
          <div class="alert alert-success">
            {{ Session::get('flash_message') }}
          </div>
          @endif

          {!! Form::open(['method' => 'POST', 'route' => ['user.profilo'], 'files' => true]) !!}
          {!! Form::hidden('user_id', $user->id) !!}

          <div class="form-row">
            <div class="form-group col">
              @if($user->firma == null)
              {!! Form::label('', 'Nuova firma') !!}
              <canvas id="signature-pad_tecnico" class="signature-pad"></canvas>
              {!! Form::hidden('signature_image_tecnico', null, ['id' => 'signature_image_tecnico']) !!}
              @else
              {!! Form::label('', 'Firma salvata', ['style' => 'display: inline']) !!}
              <button type="submit" class="btn btn-sm btn-danger" name='elimina_firma'><i class="fas fa-trash"></i>Elimina</button>

              @if(config('database.default') == 'mysql')
              <img src="data:image/png;base64,{!! base64_decode($user->firma) !!}" />
              @else
              <img src="data:image/png;base64,{!! stream_get_contents($user->firma) !!}" />
              @endif

              @endif
            </div>
          </div>

          <div class="form-row">
            <div class="form-group col" style="padding-top: 28px;">
              <button type="submit" class="btn btn-primary"><i class="far fa-save"></i>Salva</button>
            </div>
          </div>

          {!! Form::close() !!}



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

  </div>
</div>


</x-app-layout>

<script>


$(document).ready(function(){
  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': '{{csrf_token()}}'
    }
  });

  setTimeout(init_canvas(), 2000);

});

function init_canvas(){
  $.each(['tecnico'], function(index, value){
    var canvas = document.getElementById("signature-pad_"+value);
    window["signaturePad_"+value] = new SignaturePad(canvas, {
      onEnd: function () {
        $("#signature_image_"+value).val(this.toDataURL());
      }
    });
    window.addEventListener("resize", resizeCanvas(canvas));
    resizeCanvas(canvas);
  });
}

function resizeCanvas(canvas){
  var parentWidth = $(canvas).parent().outerWidth();
  canvas.setAttribute("width", parentWidth);
}



</script>
