<?php

namespace App\Http\Controllers\Configuration;

use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\TabUser as User;
use App\Http\Controllers\Utils\PaginatorController;
use stdClass;

class GroupController extends Controller
{

    public function index()
    {

        $exampledata = [
            [
                'id' => 1,
                'companyname' => 'Netbay',
                'groupname' => 'Admin',
                'admingroup' => 'Y'
            ],
            [
                'id' => 2,
                'companyname' => 'Netbay',
                'groupname' => 'Admin2',
                'admingroup' => 'Y'
            ],
            [
                'id' => 3,
                'companyname' => 'Netbay2',
                'groupname' => 'Admin3',
                'admingroup' => 'Y'
            ],
            [
                'id' => 4,
                'companyname' => 'Netbay3',
                'groupname' => 'Admin4',
                'admingroup' => 'Y'
            ]
        ];



        $userGroups = PaginatorController::paginate($exampledata, 1);

        $url = '/configurations/user-group';
        $userGroups->withPath($url);
        $searchBy = [
            "groupname" => "Group Name",
            "admingroup" => "Admin Group"
        ];

        return view('configurations.user-groups.index', compact('userGroups', 'url', 'searchBy'));
    }


    public function create()
    {
        return view('configurations.user-groups.create');
    }

    public function store(Request $request)
    {
        $rules = array(
            'name'       => 'required',
            'password'       => 'required',
            'email'      => 'required|email',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        } else {

            $user = new User;
            $user->password = Hash::make($request->input('password'));
            $user->name = $request->input('name');
            $user->email = $request->input('email');
            $user->save();

            return redirect()->route('configurations.user')
                ->withSuccess(__('User created successfully.'));
        }
    }

    public function show(User $user)
    {
        return view('configurations.users.show', [
            'user' => $user
        ]);
    }

    public function edit(User $user)
    {
        $example =  [
            'id' => 4,
            'companyname' => 'Netbay3',
            'groupname' => 'Admin4',
            'admingroup' => 'Y'
        ];

        return view('configurations.user-groups.edit', [
            'example' => $example
        ]);
    }

    public function update(Request $request, User $user)
    {
        $rules = array(
            'name'       => 'required',
            'email'      => 'required|email',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        } else {
            $user->name = $request->input('name');
            $user->email = $request->input('email');
            $user->save();

            return redirect()->route('configurations.user-group')
                ->withSuccess(__('User updated successfully.'));
        }
    }
    public function destroy(User $user)
    {
        $user->delete();

        return redirect()->route('configurations.user-group')
            ->withSuccess(__('user deleted successfully.'));
    }
}
