<?php

namespace App\Http\Controllers\Webprofile;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserRequest;
use App\Repositories\UserRepository;
use Crypt;

class UserController extends Controller
{
    public function __construct(UserRepository $userRepo)
    {
        $this->userRepo = $userRepo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = $this->userRepo->get();

        $data = [
            'users' => $users,
        ];

        return view('webprofile.backend.users.index', $data)->withTitle(trans('feature.user'));
    }

    public function status($id, Request $request)
    {
        $data = $request->except('_token');

        $user = $this->userRepo->findId($id);
        $this->userRepo->update($data, $user);

        return redirect()->route('user.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('webprofile.backend.users.create')->withTitle(trans('feature.create_user'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(UserRequest $request)
    {
        $request->validate([
            'password' => 'required|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/|min:8|confirmed',
        ], [
            'password.required' => 'Password wajib diisi',
            'password.regex' => 'Password tidak aman! Harus kombinasi angka, huruf kapital, huruf kecil, dan karakter',
            'password.min' => 'Password minimal 8 karakter',
        ]);
        $user = auth()->user();
        $data = $request->except('_token');

        $data['password'] = bcrypt($request->input('password'));
        $data['is_active'] = 1;
        $data['userid_created'] = $user->id;
        $data['userid_updated'] = $user->id;

        $this->userRepo->store($data);

        return redirect()->route('user.index');
    }

    /**
     * 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)
    {
        $id = Crypt::decrypt($id);
        $user = $this->userRepo->findId($id);

        $data = [
            'user' => $user,
        ];

        return view('webprofile.backend.users.edit', $data)->withTitle(trans('feature.edit_user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $user = $this->userRepo->findId($id);

        if ($request->input('password') != null || $request->input('password') != '') {
            $data = $request->except('_token');
            $data['password'] = bcrypt($request->password);
            $this->userRepo->update($data, $user);
        } else {
            $data = $request->except('_token', 'password', 'password_confirmation');
            $this->userRepo->update($data, $user);
        }

        return redirect()->route('user.index');
    }

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