<?php

namespace App\Http\Controllers\Admins;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Spatie\Permission\Models\Role;

class RespondenController extends Controller
{

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        // call table user with pagination
        $users = User::role('Responden')->latest()->paginate(10);

        return view('pages.admins.respondens.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        // call view users.create
        return view('pages.admins.respondens.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // validation
        $rules = array(
            'nik'  => 'required|unique:users|min:16|max:16',
            'name'  => 'required|min:5',
            'email' => 'required|email|unique:users',
            'password'  => 'required|min:8',
            'gender'  => 'required',
            'place_of_birth'  => 'required',
            'date_of_birth'  => 'required',
        );
        $validator = Validator::make($request->all(), $rules);

        // check validation
        if ($validator->fails()) {
            return Redirect::to('admin/respondens/create')
                ->withErrors($validator)
                ->withInput($request->except('password'));
        } else {
            // create new account
            $user = User::create([
                'nik' => $request->nik,
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                'gender' => $request->gender,
                'place_of_birth' => $request->place_of_birth,
                'date_of_birth' => $request->date_of_birth,
            ]);

            // Create role for user
            $role = Role::findOrFail(3); // 3 = Responden
            $user->assignRole($role);

            // Create Session message
            Session::flash('users-message', [
                'type' => 'success',
                'msg' => 'Anda berhasil menambahkan data!'
            ]);

            return Redirect::to('admin/respondens');
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        // get data from user
        $user = User::findOrFail($id);

        // call view pages
        return view('pages.admins.respondens.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        // validation
        $rules = array(
            // 'nik'  => 'required|unique:users|min:16|max:16',
            'name'  => 'required|min:5',
            'password'   => 'required|min:8',
            'gender'  => 'required',
            'place_of_birth'  => 'required',
            'date_of_birth'  => 'required',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('admin/respondens/edit/' . $id)
                ->withErrors($validator)
                ->withInput($request->except('password'));
        } else {
            // create new account
            $user = User::findOrFail($id);
            if (!$user) {
                // Create Session message
                Session::flash('users-message', [
                    'type' => 'warning',
                    'msg' => 'Data pengguna tidak ditemukan!'
                ]);

                return Redirect::to('admin/respondens');
            }
            $user->update([
                'nik' => $request->nik,
                'name' => $request->name,
                'password' => bcrypt($request->password),
                'gender' => $request->gender,
                'place_of_birth' => $request->place_of_birth,
                'date_of_birth' => $request->date_of_birth,
            ]);

            // Create Session message
            Session::flash('users-message', [
                'type' => 'success',
                'msg' => 'Anda berhasil mengubah data!'
            ]);

            return Redirect::to('admin/respondens');
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    // public function destroy(string $id)
    // {
    //     // get data from role
    //     $role = Role::findOrFail($id);

    //     // check role
    //     if (!$role) {
    //         // Create Session message
    //         Session::flash('roles-message', [
    //             'type' => 'warning',
    //             'msg' => 'Data pengguna tidak ditemukan!'
    //         ]);

    //         return Redirect::to('admin/respondens');
    //     }

    //     // delete role
    //     $role->delete();

    //     // Create Session message
    //     Session::flash('roles-message', [
    //         'type' => 'success',
    //         'msg' => 'Anda berhasil menghapus data!'
    //     ]);

    //     return Redirect::to('admin/respondens');
    // }
}
