<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Models\Group;

class UserGroupController extends Controller
{
    public function index(){

    	$data['title'] = "User group";
    	$data['all'] = Group::where('depend_on',0)->orderBy('depend_on')->get();
    	$data['all_user'] = User::where('active',1)->get();
    	$data['all_group'] = Group::where('user_id',0)->get();

    	return view('admin.user_group',$data);

        /*
        1=Created;
        2=in process;
        3=Completed;
        4=checking;
        5=bugfixing;
        5=done;
        */
    }

    public function add_group_user(Request $req){
    	
    	$type = $req->type;
    	$group_name = $req->group_name;
    	$group_id = $req->group_id;
    	$user_id = $req->user_id;
    	$designation = $req->designation;

    	if($type == 1){
    		$sav = new Group;
    		$sav->group_name = $group_name;
    		$sav->save();
    	}
    	elseif($type == 2){
    		$sav = new Group;
    		$sav->user_id = $user_id;
    		$sav->depend_on = $group_id;
    		$sav->designation = $designation;
            $sav->active = 1;
    		$sav->save();
    	}
    	else{

    	}

    	return redirect()->back()->with('msg','Saved successfully !');

    }

    public function remove_group_user($id){
    	
    	$del = Group::find($id)->delete();
    	return redirect()->back()->with('msg','Removed successfully !');
    }
}
