<?php

namespace App\Http\Controllers;



use App\Models\PaymentMethod;
use App\Models\UserAccount;
use App\Repositories\UserRepository;
use App\Services\HTMLHelper;
use App\Services\SysLog;
use App\User;
use Auth;
use Illuminate\Support\Facades\Mail;
use 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.user.index', $data);
    }

    public function edit($user_id)
    {
        $user = User::findOrFail($user_id);
        if(!$user->account){
            $user->account = new UserAccount();
        }

        $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 = Request::all();
        $user = User::findOrFail($data['id']);

        if(isset($data['save-admin'])){
            $user->admin = $data['admin'];
            SysLog::action('save-admin', 'admin_user', 3)
                ->setUserId(Auth::user()->id)
                ->setModel($user->id, User::class)
                ->setMessage('Set user admin value: '.HTMLHelper::getRoleLabel($user->admin))
                ->save();
        }

        if(isset($data['save-confirmed'])){
            $data['confirmed'] = isset($data['confirmed']) ? true : false;
            $user->confirmed = $data['confirmed'];
            if($data['confirmed']){
                if(!isset($data['confirmation_date']) || $data['confirmation_date'] == ""){
                    $user->confirmation_date = now();
                }else{
                    $user->confirmation_date = \Carbon::parse(str_replace("- ", "", $data['confirmation_date']));
                }
            }else{
                $user->confirmation_date = null;
            }
            SysLog::action('save-confirmed', 'admin_user', 3)
                ->setUserId(Auth::user()->id)
                ->setModel($user->id, User::class)
                ->setMessage('Set user confirmed value: '.$user->confirmed." to date: ".$data['confirmation_date'])
                ->save();
        }

        if(isset($data['save-active'])){
            $data['active'] = isset($data['active']) ? true : false;
            $user->active = $data['active'];
            if($data['active']){
                if(!isset($data['active_date']) || $data['active_date'] == ""){
                    $user->active_date = now();
                }else{
                    $user->active_date = \Carbon::parse(str_replace("- ", "", $data['active_date']));
                }
            }else{
                $user->active_date = null;
            }
            SysLog::action('save-active', 'admin_user', 3)
                ->setUserId(Auth::user()->id)
                ->setModel($user->id, User::class)
                ->setMessage('Set user active value: '.$user->active." to date: ".$data['active_date'])
                ->save();
        }

        if(isset($data['save-account'])){
            $old = $user->getPaymentAccountDateFormat(true);
            if(!isset($data['payment_account']) || $data['payment_account'] == ""){
                $user->payment_account = null;
            }else{
                $user->wizard = 100;
                $user->payment_account = \Carbon::parse(str_replace("- ", "", $data['payment_account']));
            }
            SysLog::action('save-account', 'admin_user', 3)
                ->setUserId(Auth::user()->id)
                ->setModel($user->id, User::class)
                ->setMessage('Set user payment_account from date: '.$old." to date: ".$data['payment_account'])
                ->save();
        }

        if(isset($data['save-shop'])){
            $old = $user->getPaymentShopDateFormat(true);
            if(!isset($data['payment_shop']) || $data['payment_shop'] == ""){
                $user->payment_shop = null;
            }else{
                $user->wizard = 100;
                $user->payment_shop =  \Carbon::parse(str_replace("- ", "", $data['payment_shop']));
            }
            SysLog::action('save-shop', 'admin_user', 3)
                ->setUserId(Auth::user()->id)
                ->setModel($user->id, User::class)
                ->setMessage('Set user payment_shop from date: '.$old." to date: ".$data['payment_shop'])
                ->save();
        }

        if(isset($data['save-test_mode'])){
            $user->test_mode = isset($data['test_mode']) ? true : false;
            SysLog::action('save-test_mode', 'admin_user', 3)
                ->setUserId(Auth::user()->id)
                ->setModel($user->id, User::class)
                ->setMessage('Set user test_mode value: '.$user->test_mode)
                ->save();
        }

        if(isset($data['save-payment_methods'])){
            $user->payment_methods = isset($data['payment_methods']) ? array_map('intval', $data['payment_methods']) : null;
            SysLog::action('save-payment_methods', 'admin_user', 3)
                ->setUserId(Auth::user()->id)
                ->setModel($user->id, User::class)
                ->setMessage('Set user payment_methods value: '.$user->getPaymentMethodsShort())
                ->save();
        }

        $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');

    }


    public function userLoginAs($userId){
        if(Auth::user()->isSuperAdmin()){
            $user = User::find($userId);
            Auth::login($user);
            return redirect('/home');
        }
    }

}