<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class PermissionController extends Controller
{
   

    public function modif($id)
    {
		if(session('user')== null) {
            return redirect('/');
        }

        $typepermission = DB::table('type_permission')->get();

        $vue =  DB::table('vue')->orderBy('lib')->get();

            return view('parametre/modifier_permission',[

                'idpermission' => $id,
    
                'typepermission' => $typepermission,
    
                'vue' => $vue,
    
            ]);


    }


    public function perm($id)
    {
		if(session('user')== null) {
            return redirect('/');
        }

		$idrole = $id;

        $typepermission = DB::table('type_permission')->get();

        $vue =  DB::table('vue')->orderBy('lib')->get();

            return view('parametre/permission',[

                'idrole' => $idrole,
    
                'typepermission' => $typepermission,
    
                'vue' => $vue,
    
            ]);

    }



    function enre (Request $request, $id){

        $role = request("role");

        $vu = request("vu");

        $typerm = request("typerm");


       $veri = DB::table('permission')->where('role_id',$id)->first();

        if ($veri) {

            return redirect()->back()->with('error', 'Echec ! Permissions déjà attribué à ce rôle');  

        }

        else{
			
			if( $vu == null && $typerm == null ){
				
				 return redirect()->back()->with('error', 'Echec ! Veuillez activer au moins une permission');  
				
			}else{
				
			
			DB::table('permission')->insert(
                [
                    'role_id' => $role,

    
				]);


            $idLastperm = DB::table('permission')->get()->last()->id;

            foreach( $vu as $k => $vus){ 

                if (isset($typerm[$k])) {

                    foreach( $typerm[$k] as  $typerms ){


                        DB::table('ligne_permission')->insert(
                        [
                            'permission_id' => $idLastperm,
                    
                            'role_id' => $role,
                    
                            'vue_id' => $vus,
                    
                            'typepermission_id' => $typerms,
                    
                        ]);
                    
                    }
                }
                
            }

            return redirect()->route('nouveau-role')->with('status', 'Permission attribuée avec succes');  	
				
				
			}


            
        }
		
    }



    function mod (Request $request, $id){

        $role = request("role");

        $vu = request("vu");

        $typerm = request("typerm");
		
		if( $vu == null && $typerm == null ){
			
			 return redirect()->back()->with('error', 'Echec ! Veuillez activer au moins une permission');  
			
		}else{
			
			
			
			DB::table('permission')->where('id',$id)->update(
                [
                    'role_id' => $role,
    
				]);


            DB::table('ligne_permission')->where('permission_id', $id)->delete();

            foreach( $vu as $k => $vus){ 

                if (isset($typerm[$k])) {
                    
                    foreach( $typerm[$k] as  $typerms ){


                        DB::table('ligne_permission')->insert(
                        [
                            'permission_id' => $id,
                    
                            'role_id' => $role,
                    
                            'vue_id' => $vus,
                    
                            'typepermission_id' => $typerms,
                    
                        ]);
                    
                    }
                }
                
            }
       

            return redirect()->route('permissions')->with('status', 'Permissions modifiées avec succes'); 
			
		}
 
		
    }



    public function permis()
    {
		if(session('user')== null) {
            return redirect('/');
        }

            $permission = DB::table('permission')->orderBydesc('id')->get();

		return view('parametre/permissions',[

			'permission' => $permission,
  
		]);
		
    }


    function VoirSuppressionper ($id) {

        $pe= DB::table('permission')->where('id',$id)->first();
        return [
            'pe' => $pe,
        ];
    }



    function suppper () {

        $id=request('id');

        DB::table('permission')->where('id', $id)->delete();

        DB::table('ligne_permission')->where('permission_id', $id)->delete();

        return redirect()->route('permissions')->with('status', 'Suppression effectuée avec succes');


    }











}
