<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Role;

use Auth;
use Mail;
use Session;
use DB;

class AdminController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('admin');
    }
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $loggedin = Auth::guard('admin')->user();
        $admins = Admin::get();
        return view('admin.admins.list',compact('admins'));
    }

    public function create()
    {
        $loggedin = Auth::guard('admin')->user();
        $roles = Role::where('guard_name','admin')->orderBy('name','ASC')->get();
        return view('admin.admins.add',compact('roles'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $loggedin = Auth::guard('admin')->user();
        $this->validate($request,[
            'name' => 'required',
            'email' => 'required|email|max:255|unique:admins',
            'password' => 'required|min:6|confirmed'
        ]);

        DB::beginTransaction();
        try {
            $admin = new Admin;
            $admin->name = $request->name;
            $admin->email = $request->email;
            $admin->status = 1;
            $admin->password = bcrypt($request->password);
            $admin->save();

            $admin->assignRole($request->role);

            DB::commit();
            
            return redirect()->action('Admin\AdminController@index')->with('success','Successfully Created!');
        } catch (\Exception $e) { 
            DB::rollBack();
            return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $loggedin = Auth::guard('admin')->user();

        $admin = Admin::find($id);
        return view('admin.admins.view',compact('admin'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $loggedin = Auth::guard('admin')->user();

        $admin = Admin::find($id);
        $roles = Role::where('guard_name','admin')->orderBy('name','ASC')->get();
        return view('admin.admins.edit',compact('admin','roles'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $loggedin = Auth::guard('admin')->user();

        if($id == 1 && $loggedin->id != 1){
            return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
        }

        $this->validate($request,[
            'name' => 'required'
        ]);

        DB::beginTransaction();
        try {
            $admin = Admin::find($id);
            $admin->name = $request->name;
            if($admin->email != $request->email){
                $this->validate($request,[
                    'email' => 'required|email|max:255|unique:admins'
                ]);
                $admin->email = $request->email;
            }

            if($request->password){
                $this->validate($request,[
                    'password' => 'required|min:6|confirmed'
                ]);
                $admin->password = bcrypt($request->password);
            }
            $admin->syncRoles($request->role);
            $admin->update();

            DB::commit();
            return redirect()->action('Admin\AdminController@index')->with('success','Successfully Updated!');
        }catch (\Exception $e) {
            DB::rollBack();
            return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
        }
    }

    public function edit_profile()
    {
        $loggedin = Auth::guard('admin')->user();
        $admin = Admin::find($loggedin->id);
        return view('admin.admins.profile',compact('admin'));
    }

    public function update_profile(Request $request)
    {
        $loggedin = Auth::guard('admin')->user();

        $this->validate($request,[
            'name' => 'required'
        ]);

        DB::beginTransaction();
        try {
            $admin = Admin::find($loggedin->id);
            $admin->name = $request->name;
            /*if($admin->email != $request->email){
                $this->validate($request,[
                    'email' => 'required|email|max:255|unique:admins'
                ]);
                $admin->email = $request->email;
            }*/
            if($request->password){
                $this->validate($request,[
                    'password' => 'required|min:6|confirmed'
                ]);
                $admin->password = bcrypt($request->password);
            }
            $admin->update();

            DB::commit();
            return redirect()->action('Admin\AdminController@edit_profile')->with('success','Successfully Updated!');
        }catch (\Exception $e) { 
            DB::rollBack();
            return redirect()->action('Admin\AdminController@edit_profile')->with('error', "Something Wrong!");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $loggedin = Auth::guard('admin')->user();
        if($id == 1 && $loggedin->id != 1){
            return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
        }
        $admin = Admin::find($id);

        DB::beginTransaction();
        try {
            Admin::whereId($id)->delete();
            DB::commit();
            return redirect()->action('Admin\AdminController@index')->with('success','Successfully Deleted!');
        }catch (\Exception $e) { 
            DB::rollBack();
            return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
        }
    }

    public function statusUpdate($id)
    {
        $loggedin = Auth::guard('admin')->user();
        if($id == 1 && $loggedin->id != 1){
            return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!" );
        }

        $admin = Admin::find($id);

        if($admin->status == 0){
            $admin->status = 1;
            $status = 'published';
        }else{
            $admin->status = 0;
            $status = 'unpublished';
        }
        $admin->update();
    }
}
