<?php

namespace App\Http\Controllers\user;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use DB;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;

class RoleController extends Controller
{
    //
    public function __construct(){      
        $this->middleware(function ($request, $next) {
        $url = request()->segment(1);
        $kode_group = Session::get('kode_group');
        $d_menu = DB::table("tb_menu as a")->join("tb_role as b","a.kode_menu","b.kode_menu")->where([['a.link_menu',$url],['b.kode_group',$kode_group],['b.status_role','1'],['a.active','1']])->orderBy('a.sort_menu','asc')->count();
        if(Session::get('decode') <> 1)
         {
            Session::flash('message', 'Silahkan login dengan username dan password anda.!!');
             return redirect()->to('login');
         }
         if($d_menu == 0){
            Session::flush();
            Session::flash('message', 'Anda Tidak Punya Akses ke Halaman ini.!!');
             return redirect()->to('login');
         }
        return $next($request); 
        });
    }
    
    public function index(){
    	return view('user.RoleList');
    }

    function tampil(){
    	DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('tb_group as tg')->select([DB::raw('tg.*')]);

		return Datatables::of($d_data)
				->addColumn('aksi', function($d_data){
					$btnAction = '<a class="btn bg-info-400" type="button" href="'.url('role/settingRole').'/'.$d_data->kode_group.'"><i class="fa fa-cog"></i></a>'.
						'<input type="hidden" id="table_id'.$d_data->kode_group.'" value="'.$d_data->kode_group.'">'.
						'<input type="hidden" id="table_nama'.$d_data->kode_group.'" value="'.$d_data->nama_group.'">';
					return $btnAction;
				})->make(true);
    }

    public function settingRole($id){
    	$d_role = DB::table('tb_group')->where('kode_group', $id);
        $data['group'] = $d_role->first();

        $cm = DB::table('tb_menu')->select(DB::raw('kode_menu'))->get();
        foreach($cm as $d){
        	$kode_menu  = $d->kode_menu;
        	$d_menu = DB::table('tb_role')->where('kode_group',$id)->where('kode_menu',$kode_menu);
        	if($d_menu->count() == 0){
        		$menu['kode_menu'] = $kode_menu;
        		$menu['kode_group'] = $id;
        		DB::table('tb_role')->insert($menu);
        	}
        }

        return view('user.SettingRoleList')->with('data',$data);
    }

    function get_data(Request $request){
        $id = $request->get('id');

        $PDO = DB::connection('mysql')->getPdo();
        $query = $PDO->prepare("SELECT * FROM (
								SELECT kode_role,nama_menu,CASE parent_menu WHEN 0 THEN '#'  ELSE NULL END AS parent,status_role,a.sort_menu sort
								FROM tb_menu a , tb_role b WHERE a.kode_menu=b.kode_menu AND parent_menu=0 AND kode_group='$id' 
								UNION 
								SELECT kode_role,a.nama_menu,b.nama_menu parent,status_role,a.sort_menu
								FROM tb_menu a,(SELECT kode_menu,nama_menu FROM tb_menu) b, tb_role c WHERE a.parent_menu=b.kode_menu AND a.kode_menu=c.kode_menu AND kode_group='$id' ) cb ORDER BY parent asc,cb.sort ASC");
        $query->execute();
        $d_data = $query->fetchAll((\PDO::FETCH_ASSOC));

        if(count($d_data) > 0){
            foreach($d_data as $d){
                $arr[] = array('kode_role' => $d['kode_role'],
                                'parent' => $d['parent'],
                                'status_role' => $d['status_role'],
                				'nama_menu' => $d['nama_menu'],
                                'sort' => $d['sort']);
            }
        }else{
            $arr = array();
        }

        return response()->json($arr);
    }

    function simpan(Request $request){
    	$kode_group = $request->get('kode_group');
    	$kode_role = $request->get('tabel_role');

    	$c_data['status_role'] = '0';
    	$cek = DB::table('tb_role')->where('kode_group',$kode_group)->update($c_data);
		
		for($i=0; $i < count($kode_role); $i++){
			$role = $kode_role[$i];
			$data['status_role'] = '1';
			DB::table('tb_role')->where('kode_role',$role)->update($data);

		}	

		return redirect('role');
    }


}
