<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;

class UserController extends Controller
{

    protected $route = 'user';
    protected $url = '/user';
    protected $prefix_folder = 'components.users.';


    public function index(Request $request)
    {

        $serchSelected = '';
        $keyword = '';
        if(isset($request->search) && isset($request->keyword)) {
            $serchSelected = $request->search;
            $keyword = $request->keyword;
            $companies = User::where($serchSelected,'LIKE','%'.$keyword.'%')->paginate(10);
       
        }else{
            $companies = User::paginate(10);
        }
       
        $url =  $this->url;
        $route = $this->route;
        $companies->withPath($url);

        $searchBy = [
            "name" => "Name",
            "email" => "Email",
        ];

        return view($this->prefix_folder . 'index', compact('users',  'url', 'route', 'searchBy', 'serchSelected', 'keyword'));
    }


    public function create()
    {
        $route = $this->route;
        $groups = Group::all();

        return view($this->prefix_folder . 'create', compact('route', 'groups'));
    }
    public function edit(user $user)
    {
        $route = $this->route;
        $groups = Group::all();
        return view($this->prefix_folder . 'edit', compact('user', 'groups', 'route'));
    }
    public function store(Request $request)
    {
        $rules = array(
            'name'       => 'required',
            'tax'       => 'required|unique:companies,tax'
        );
        $validator = Validator::make($request->all(), $rules);


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

            $userData = $request->except('userType');
            $userData['user_type_id'] = $request->input('userType');

            $user = user::create($userData);

            return redirect()->route($this->route . '.list')
                ->withSuccess(__('user created successfully.'));
        }
    }

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

        // process the login
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        } else {
            $userData = $request->except('userType');
            $userData['user_type_id'] = $request->input('userType');

            $user->update($userData);

            return redirect()->route($this->route . '.list')
                ->withSuccess(__('user created successfully.'));
        }
    }

    public function destroy(user $user)
    {
        $user->delete();

        return redirect()->route($this->route . '.list')
            ->withSuccess(__('user deleted successfully.'));
    }

    public function getUsers()
    {
        $users = User::select('id', 'name', 'email')->get();

        $formattedUsers = [];

        foreach ($users as $user) {
            $formattedUsers[] = [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
            ];
        }

        return response()->json(['users' => $formattedUsers]);
    }
}
