<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Config;
use App\Models\User;
use Session;

class UserController extends Controller{

  function profilo(){
    $redmineUser = session('redmine_user');
    $user = User::where('redmine_id', $redmineUser->id)->first();
    return view('user.profilo')->withUser($user);
  }

  function salva_profilo(Request $request){
    $user = User::find($request->user_id);

    if($request->has('elimina_firma')){
      $user->firma = null;
      $user->save();
      Session::flash('flash_message', 'Firma eliminata');
      return redirect()->route('user.profilo');
    }

    try{
      $sign_path_tecnico = 'sign_tecnico_'.time().'.png';
      $encoded_image = explode(",", $request->signature_image_tecnico)[1];
      if(config('database.default') == 'mysql'){
        $user->firma = base64_encode($encoded_image);
      }else{
        $user->firma = pg_escape_bytea($encoded_image);
      }
      $user->save();
    }catch(\Exception $e){
      
    }


    Session::flash('flash_message', 'Profilo aggiornato');
    return redirect()->route('user.profilo');
  }

}
