<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\menu;
use App\Models\User;
use App\Models\user_group;
use App\Models\menus_user_details;
use App\Models\user_group_details;
use Schema;
//use App\Models\events;
use App\Http\Controllers\library;

class AdminsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
   // public $obj;

   
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */


    public function test1(){

     return "test1 called";

     }

    public function adminHome()

    {

        return view('aview.adminHome');

    }
    public function c_home()

    {
      return view('contrctr_dash2');

    }


    public function user_group($id)
    {
        // $rows = menu::where('menu_id', $id)->get();
         $rows = DB::table('menus')->where('menu_id', $id)->first();
         $menus = DB::table('menus')->select(array('menu_id','menu_name',DB::raw('ifnull(sedp,",,,") AS sedp'),'menu_type'))->orderBy('menu_order', 'ASC')->get();
        return view('aview.user_group',compact('rows','id','menus'));
    }   
    public function set_menu($id)
    {
        // $rows = menu::where('menu_id', $id)->get();
         $rows = DB::table('menus')->where('menu_id', $id)->first();
         $menus =  $menus = DB::table('menus')->select(array('menu_id','menu_name',DB::raw('ifnull(sedp,",,,") AS sedp'),'menu_type')) ->where('to_admin','=','0')->orderBy('menu_order', 'ASC')->get();
           $user = DB::table('users')->where('is_admin','=','0')->get();

        return view('aview.set_menu',compact('rows','id','menus','user'));
    } 
     public function op_set_menu(Request $request)
    {
        $title="User Menu";
       $uid= $request->uid;

     menus_user_details::where('id', $uid)->delete();
       foreach ($request->marr as $key => $value) {
        $value['id']=$uid;
            $dd=menus_user_details::create($value);

       }
       if ($dd) {$flag="1";
        $msg=$title." Inserted Successfully ";
       }else{
         $flag="0";
        $msg=$title." Insertion  Failed ";
       }
        return response()->json(['flag'=> $flag,'msg'=> $msg]);
    }
      public function user($id)
    {
     
         $ug = DB::table('user_groups')->get();
         $br = DB::table('companysettings')->get();
         $rows = DB::table('menus')->where('menu_id', $id)->first();
         $menus = DB::table('menus')->select(array('menu_id','menu_name',DB::raw('ifnull(sedp,",,,") AS sedp'),'menu_type'))->orderBy('menu_order', 'ASC')->get();
        return view('aview.user',compact('rows','id','ug','br','menus'));
    } 
  public function get_menus(Request $request)
    {

             $menus = DB::table('menus')->select(array('menu_id','menu_name',DB::raw('IFNULL(sedp,",,,,")sedp'),'menu_type')) ->where('to_admin','=','0')->orderBy('menu_order', 'ASC')->get();
         
        return json_encode($menus);
    }   
public function get_all_menus(Request $request)
    {

           
             $menus = DB::table('menus')->select(array('menu_id','menu_name',DB::raw('IFNULL(sedp,",,,,")sedp'),'menu_type'))->whereRaw("status in (0) and to_admin=0 ")->orderBy('menu_order', 'ASC')->get();
         
        return json_encode($menus);
    } 
  public function get_user_menus(Request $request)
    {
           $menus = DB::table('menus_user_details')->select(array('menu_id', DB::raw('IFNULL(sedp,",,,,")sedp')))->where("id",$request->uid)->orderBy('menu_id', 'ASC')->get();
      
       
        return json_encode($menus);
    } 
     public function op_user_group(Request $request)
    {
      // dd($request);
        $title="User Group";
      $hidden_status= $request->hidden_status;
      $data = new user_group;
        $data->ug_name= $request->ug_name;
      if($hidden_status==0){
       if($data->save()){
        foreach ($request->marr as $key => $value) {
           $value['ug_id']=$data->id;
            user_group_details::create($value);
        }
        $flag="1";
        $msg=$title." Inserted Successfully ";
       }else{
         $flag="0";
        $msg=$title." Insertion  Failed ";
       }
       }else if($hidden_status==1){


       $upd= DB::table('user_groups')->where('ug_id', $request->row_id)->update(['ug_name' => $request->ug_name]);
    

if($upd==0||$upd==1){

  user_group_details::where('ug_id', $request->row_id)->delete();
  foreach ($request->marr as $key => $value) {
    $value['ug_id']= $request->row_id;
     user_group_details::create($value);
 }
 $flag="1";
        $msg=$title." Updated Successfully ";
}
else{
  $flag="0";
  $msg=$title." Updated Failed ";
}

       }
       else if($hidden_status==2){


        $del=DB::table('user_groups')->where('ug_id', $request->row_id)->update(['status' => 1]);



       if($del==1||$del==0){
        $flag="1";
        $msg=$title." Deleted Successfully ";
       }
       else{
        $flag="0";
        $msg=$title." Deleted Failed ";
       }
       
       
       ;


       }
       


        return response()->json(['flag'=> $flag,'msg'=> $msg]);
      
    }   
    public function op_user(Request $request)
    {
      // dd($request);
        $title="User ";
      $hidden_status= $request->hidden_status;
      if($hidden_status==0)       $data = new User;
      if($hidden_status==1)     $data = User::find($request->row_id);
    
       $data->name=$request->name;
$data->email='';
$data->uname=$request->uname;
$data->email=$request->email;
$data->password=$request->password;
$data->phone=$request->phone;
$data->address=$request->address;
$data->ug_id=$request->ug_id;
$data->branch_id=$request->branch_id;
      if($hidden_status==0){
       if($data->save()){
         DB::statement("insert into menus_user_details (id, menu_id,sedp) select ".$data->id.", menu_id,sedp from user_group_details where ug_id in (".$data->ug_id.")");
        $flag="1";
        $msg=$title."Iserted Successfully ";
       }else{
         $flag="0";
        $msg=$title."Insertion  Failed ";
       }
       }else  if($hidden_status==1){
              if($data->update()){
         //$payment = user_group::create($request->all());
        if ($request->rs_menu==1) {
        menus_user_details::where('id', $request->row_id)->delete();
        DB::statement("insert into menus_user_details (id, menu_id,sedp) select ".$request->row_id.", menu_id,sedp from user_group_details where ug_id in (".$request->ug_id.")");
        }
        $flag="1";
        $msg=$title."Update Successfully ";
       }else{
         $flag="0";
        $msg=$title."Updation  Failed ";
       }
       }


        return response()->json(['flag'=> $flag,'msg'=> $msg]);
      
    }      
    public function edit_user(Request $request)
    {
        // $rows = menu::where('menu_id', $id)->get();
         
        // $edit = DB::table('users')->select(array('id','uname','password','name','phone','address','ug_id','branch_id'))->where('id', $request->id)->get();
         $edit = DB::table('users')->where('id', $request->row_id)->first();
       //  dd($edit);
       return response()->json($edit);
    }





    public function edit_user_group(Request $request)
    {

      
        // $rows = menu::where('menu_id', $id)->get();
         
        // $edit = DB::table('users')->select(array('id','uname','password','name','phone','address','ug_id','branch_id'))->where('id', $request->id)->get();
         $edit = DB::table('user_groups')->where('ug_id', $request->row_id)->first();
       $details=DB::table('user_group_details')->where('ug_id',$request->row_id)->get();

      
       return response()->json(['edit'=> $edit,'details'=> $details]);
    }



}
