<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    private $indexRoute = null;

    public function __construct(){
        $this->indexRoute = route('user.index');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data['name'] = $request->name;
        $data['role'] = !is_null($request->role) ? decrypt($request->role) : null;

        $data['user'] = User::query()
        ->with('roles')
        ->orderBy('created_at')
        ->when($data['role'], function($query,  $role){
            $query->role($role);
        })
        ->when($data['name'], function($query,  $name){
            $query->where('name', 'ilike', '%'.$name.'%');
        })
        ->paginate(10)
        ->withQueryString();
        $data['roles'] = Role::query()->get();

        return view('content.backend.user.index', $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)
    {
        $data = $request->except('_token', 'role_id');
        $data['password'] = bcrypt($data['password']);
        $data['email_verified_at'] = Carbon::now();
        $user = User::query()->create($data);
        $role = Role::query()->find(decrypt($request->role_id));
        $user->assignRole($role);

        return redirect()->intended($this->indexRoute);
    }

    /**
     * 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)
    {
        $data['user'] = User::query()->find(decrypt($id));
        $data['roles'] = Role::query()->get();
        return view('content.backend.user.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token', 'role_id', 'password');
        $data['email_verified_at'] = Carbon::now();
        if(!is_null($request->password)){
            $data['password'] = bcrypt($request->password);
        }

        $user = User::query()->find(decrypt($id));
        $user->update($data);
        $role = Role::query()->find(decrypt($request->role_id));

        $user->syncRoles([$role]);

        return redirect()->intended($this->indexRoute);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::beginTransaction();
        try{
            $user = User::query()->find(decrypt($id));
            $user->delete();
            DB::commit();
            return response('Berhasil menghapus data!', 200);
        }
        catch(Exception $ex){
            DB::rollBack();
            return response('Gagal menghapus data!', 500);
        }

    }
}
