<?php

namespace App\Http\Controllers;

use App\User;
use App\admin_record;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use phpDocumentor\Reflection\DocBlock\Tags\Uses;

class adminController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth', 'admin',]);
//        ->except(['create', 'store'])
    }

    public function index(){
        $user = User::get()->sortByDesc('id');
        return view('auth.index')->with('user', $user);
    }

    public function create(){
        return view('auth.register');
    }

    public function save(Request $request){
        $request->validate([
            'full_name' => 'required|string|min:6',
            'email' => 'required|email|unique:users',
            'username' => 'required|min:4|unique:users',
            'Gender' => 'required',
            'role' => 'required',
        ]);

        $user = new User;
        $user->uid = str::random(20);
        $user->name = $request['full_name'];
        $user->email = $request['email'];
        $user->username = $request['username'];
        $user->sex = $request['Gender'];
        $user->role = $request['role'];
        $user->password = Hash::make($request['password']);
        $user->save();

        return redirect()->route('register-home');
    }

    public function edit($id){
        $data = User::where('uid', $id)->first();
        return view('auth.edit')->with('user', $data);
    }

    public function update(Request $request, $id){
        $request->validate([
            'full_name' => 'required|string|min:6',
            // 'email' => 'required|email|unique:users',
            // 'username' => 'required|min:4|unique:users',
            'Gender' => 'required',
            'role' => 'required',
        ]);

        $user = User::find($id);

        $user->name = $request['full_name'];
        $user->email = $request['email'];
        $user->username = $request['username'];
        $user->sex = $request['Gender'];
        $user->role = $request['role'];
        // $user->email = $request['email'];
        $user->save();

        return redirect()->route('register-home');

    }

    public function delete($id){
        User::find($id)->delete();
        return redirect()->back()->with('success','Successfully Staff deleted');
    }
}
