<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Visitor;
use App\Models\Post;
use App\Models\Video;
use Carbon\Carbon;
use App\Models\Admin;
use Spatie\Permission\Models\Role;

use Validator;

class AdminController extends Controller
{
    public function login_create(){
        return view('back-end.accounts.auth.page-login');
    }
    public function login_store(Request $request){
        $credentials = $request->only('email', 'password');
        if(Auth::attempt($credentials)){
            return redirect()->route('admin.dashboard');
        }else{
            return redirect()->route('admin.login_create')->with('msg_d','OPPS Invalid Credentials');
        }
    }
    public function login_destroy(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect()->route('admin.login_create');
    }



    public function index()
    {
       $user=Admin::orderBy('id','desc')->get();
       $roles = Role::where('guard_name','admin')->get();
       return view('back-end.accounts.user.create',compact('user','roles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $user=Admin::orderBy('id','desc')->get();
        $roles = Role::where('guard_name','admin')->get();
        return view('back-end.accounts.user.create',compact('user','roles'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email|unique:admins,email',
            'password' => 'required',
            'c_password' => 'required|same:password',
            'roles' => 'required'
        ]);
        $admin=new Admin();
        $admin->name=$request->name;
        $admin->email=$request->email;
        $admin->password=Hash::make($request['password']);
        $admin->assignRole($request->input('roles'));
        $is_saved= $admin->save();
        if($is_saved){
            session()->flash("msg", "Success!");
            return redirect()->route('system_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)
    {
        $admin = Admin::find($id);
        $roles = Role::where('guard_name','admin')->get();
        $adminRole = $admin->roles->all();
        return view('back-end.accounts.user.edit',compact('admin','roles','adminRole'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email|unique:admins,email,'.$id,
            'roles' => 'required'
        ]);

        $admin = Admin::find($id);
        $admin->name=$request->name;
        $admin->email=$request->email;
        if(!empty($request->password)){
            $admin->password= Hash::make($request->password);
        }
        else
        {
           $admin->password=$admin->password;
        }

        if($admin->save()){
            DB::table('model_has_roles')->where('model_id',$id)->where('model_type','App\Models\Admin')->delete();
            $admin->assignRole($request->input('roles'));
            session()->flash("msg", "Success!");
            return redirect()->route('system_user.index');
        }


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $delete=Admin::find($id)->delete();
        if($delete){
            DB::table('model_has_roles')->where('model_id',$id)->where('model_type','App\Models\Admin')->delete();
            session()->flash("msg", "Success!");
            return redirect()->route('system_admin.index');
        }

    }

    public function profile() {
        return view("back-end.accounts.user.profile");
    }
    public function change_profile(Request $request) {
        $data = Validator::make($request->all(), [
                    'name' => ['required', 'string', 'max:255'],
                    'email' => ['required', 'string', 'email', 'max:191'],
                ])->validate();

        $admin = Admin::find(Auth::user()->id);
        $admin->name = $request->name;
        $admin->email = $request->email;
        $is_saved = $admin->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return back();
        }
    }
    public function change_password(Request $request) {
        $data = Validator::make($request->all(), [
                    'password' => ['required', 'string', 'min:8', 'confirmed']
                ])->validate();
        if (Hash::check($request->current_password, Auth::user()->password)) {
            $admin = Admin::find(Auth::user()->id);
            $admin->password = Hash::make($request->password);
            $is_saved = $admin->save();
            if ($is_saved) {
                session()->flash("msg", "Passowrd Changed successfully!");
                return back();
            }
        } else {
            session()->flash("msg", "Current Password in incorrect!");
            return back();
        }
    }
}
