<?php

namespace App\Modules\User\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use App\Modules\Utilitarios\Controllers\Files_contableController;
use App\User;
use Storage;
class ProfileController extends Controller
{
    protected $table;
    function __construct(){
        $this->middleware('auth');
        $this->table = new User();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = User::find(Auth::id());
        return view('User.views.profile',['user'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       if ($request->has('action')) {

           $data = $request->all();
           switch ($data['action']) {

               case 'update':

                   $user = User::find(Auth::id());

                    if (!empty($user)) {
                       # code...
                        if ($this->table::where('ruc', '=', $data['ruc'])->where('id','!=',Auth::id())->count() > 0) {
                            return response()->json(['error'=>array('El ruc '.$data['ruc'].' ya existe')]);
                        }else if ($this->table::where('email', '=', $data['email'])->where('id','!=',Auth::id())->count() > 0) {
                            return response()->json(['error'=>array('El email '.$data['email'].' ya existe')]);
                        }else{
                            if ($request->hasFile('file')) {
                               $allowedfileExtension=['jpg','png'];
                               if(!Storage::disk('public_uploads')->has($data['carpeta_file'])){
                                    Storage::disk('public_uploads')->makeDirectory($data['carpeta_file']);
                                } 
                                if(!Storage::disk('public_uploads')->has($data['carpeta_file'].'/'.Auth::id())){
                                    Storage::disk('public_uploads')->makeDirectory($data['carpeta_file'].'/'.Auth::id());
                                }
                                $exists = Storage::disk('public_uploads')->exists($user->src_file);
                                if ($exists) {
                                    Storage::disk('public_uploads')->delete($user->src_file);
                                }
                                $f = $request->file('file');
                                $extension = $f->getClientOriginalExtension();
                                $check=in_array($extension,$allowedfileExtension);
                                if ($check) {
                                    $name = $f->store($data['carpeta_file'].'/'.Auth::id(),['disk' => 'public_uploads']);
                                    $data['src_file'] = $name;
                                }else{
                                    //$error++;
                                }
                            }
                            $data['password'] = (empty($data['password'])) ? $user->password : Hash::make($data['password']);
                            unset($data['action'],$data['file'],$data['carpeta_file']);
                            $user->fill($data)->save();
                            $request->session()->flash('update_success', 'Registro actualizado con exito!');
                            return response()->json(['success'=>'Registro actualizado con exito','url'=>url("profile")]);
                        }
                        
                    }
                break;
               
               default:
                   # code...
                   break;
           }
       }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
     

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
