<?php

namespace App\Http\Controllers;



use App\Models\Account;
use App\Repositories\UserRepository;
use App\User;
use Illuminate\Support\Facades\Mail;
use Input;
use Illuminate\Http\Request;
use Validator;



class AdminUserController extends Controller
{
    protected $userRepo;

    public function __construct(UserRepository $userRepo)
    {
        $this->middleware('superadmin');
        $this->userRepo = $userRepo;

    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $data = [
            //'values' => User::where('admin', 0)->get(),
            'values' =>  User::where('confirmation_code_remider', '!=', 2)->get(),
        ];
        return view('admin.users', $data);
    }

    public function edit($user_id)
    {
        $user = User::findOrFail($user_id);
        /*if(!$user->account){
            $user->account = new Account();
        }
        */
        $data = [
            'user' => $user,
        ];
        return view('admin.user_edit', $data);


    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function store(Request $request)
    {
        $data = Input::all();
        $user = User::findOrFail($data['id']);

        $user->admin = $data['admin'];
        $user->confirmed = isset($data['confirmed']) ? true : false;
        $user->active = isset($data['active']) ? true : false;
        $user->save();


        \Session()->flash('alert-save', true);
        return redirect('/admin/users');

    }

    public function deleteUser($user_id)
    {
        $user = User::findOrFail($user_id);
        $this->userRepo->deleteUser($user);

        \Session()->flash('alert-success', "Kontakt gelöscht");
        return redirect('/admin/users');

    }

}