<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Session;
use Auth;
use App\User;
use Carbon\Carbon;
use DB;
use App\Helpers\PasswordEncryptHelper;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\AdminController;
class UserController extends AdminController {

    public function user_profile_page() {
        $data["pageHeader"] = false;
        $data["title"] = "Profile";

        $data["record"] = Auth::user();
        return view('admin/user/profile/user-profile-page', $data);
    }

    public function update_profile(Request $request) {

        $validator = Validator::make($request->all(), [
                    'first_name' => 'required',
                    'last_name' => 'required',
                    'email' => 'required',
                        ], [
                    'first_name.required' => 'First Name required',
                    'last_name.required' => 'Last Namre required',
                    'email.required' => ' Email required'
        ]);

        if ($validator->fails()) {

            return redirect()->back()->withInput()->withErrors($validator->errors());
        } else {
            DB::beginTransaction();

            try {
                $id = $request->get('invisible');
                $mytime = Carbon::now();
                $user = User::find($id);
                $user->first_name = $request->get('first_name');
                $user->last_name = $request->get('last_name');
                $user->email = $request->get('email');
             
                // $user->Password=PasswordEncryptHelper::encrypt_decrypt('encrypt',$request->get('password'));
                $user->updated_at = $mytime->toDateTimeString();
                //echo 'hello'.$user;
                $user->save();
                //echo 'hello'.$user;

                DB::commit();
                // all good
            } catch (\Exception $e) {
                DB::rollback();
                // something went wrong
            }
            Auth::logout();

            return redirect('admin/login');
        }
    }

    public function update_user_password_page() {
        $data["pageHeader"] = false;
        $data["title"] = "Change Password";

        $data["record"] = Auth::user();
        return view('admin/user/profile/update-user-password', $data);
    }

    public function update_user_password(Request $request) {
        $validator = Validator::make($request->all(), [
                    'email' => 'required',
                    'password' => 'required',
                    'conf-password' => 'same:password'
                        ], [
                    'email.required' => ' Email required',
                    'password.required' => 'Password required'
        ]);

        if ($validator->fails()) {

            return redirect()->back()->withInput()->withErrors($validator->errors());
        } else {

            DB::beginTransaction();

            try {
                $id = $request->get('id');
                $mytime = Carbon::now();
                $user = User::find($id);
                $user->password = PasswordEncryptHelper::encrypt_decrypt('encrypt', $request->get('password'));
                $user->updated_at = $mytime->toDateTimeString();
                $user->save();
                //  echo 'hello'.$user;

                DB::commit();
                // all good
            } catch (Exception $e) {
                DB::rollback();
                // something went wrong
            }
            Auth::logout();

            return redirect('admin/login');
        }
    }

//    
//    public function check(\App\MenuHelper\Menus $menus){
//          $arr_menu= $menus->charge();
//           // dd($arr_menu);
////          foreach($arr_menu as $men){
////              dd($men->menu);
////          }
//          echo "<pre>".print_r($arr_menu, true)."</pre>";
//       
//    }
}
