<?php

namespace App\Http\Controllers;

use App\Http\Requests\RightRequest;
use App\Right;
use App\User;
use Request;
use EF;

class RightController extends Controller
{
    function index()
    { 
       exit;
       
    	$right = new Right;

    	return view('rights.manage_rights',['right_list' => $right->getRightList()]);
    }

    function updateRight(RightRequest $request)
    {
    	$right = new Right;

    	try 
        {
        	$form_collect = $request->input();

        	$form_collect['right'] = decrypt($form_collect['right']);

            if(isset($right->updateRight($form_collect)->id))
	    	{
	        	parent::successMessage('Rights has been updated.');
	    	}
        } 
        catch (DecryptException $e) 
        {
            
        }

 
    	return redirect()->back();
    }

    function storeAssignRight(RightRequest $request)
    {

        $right = new Right;
        $user  = new User;
        $right_list = array();

        try 
        {
            $form_collect = $request->input();

            $id = decrypt($form_collect['user']);

            if(isset($form_collect['right_list']))
            {
                foreach ($form_collect['right_list'] as $key => $rows) 
                {
                    $curr_right = $right->getRightById(decrypt($rows));

                    if(isset($curr_right->id))
                    {
                        $right_list[] = array(

                            'id'        => $curr_right->id,
                            'module'    => $curr_right->module,
                            'feature'   => $curr_right->feature,
                            'type'      => $curr_right->type,
                            'uri'       => $curr_right->uri,
                            'priority'  => $curr_right->priority,
                            'icon'      => $curr_right->icons,
                        );
                    }

                    
                }

                $user->updateUserRight($id,$right_list);

                if(@count($right_list) > 0)
                {
                    parent::successMessage('Rights has been assigned.');
                }
            }


            
        } 
        catch (DecryptException $e) 
        {
            
        }

 
        return redirect()->back();
    }

    public function getViewModuleWise($module)
    {
    	$right = new Right;

    	return view('rights.modulewise_view',['right_list' => $right->getRightByModule($module),'module' => $module]);
    }

    function showAssignRight($id)
    {
    	if(Request::ajax())
    	{

            try 
            {
                $view = '<input type="hidden" name="user" value="'.$id.'">';

                $id = decrypt($id);

                foreach (EF::getModuleList() as $rows) 
                {
                    $view.= $this->getViewModuleWise($rows);
                }

                return $view;
            } 
            catch (DecryptException $e) 
            {
                
            }

			
    	}

    	return redirect()->back();
    }
}
