<?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 App\Http\Controllers\Utils\PaginatorController;

use Illuminate\Http\Request;
use App\Models\User;
use stdClass;

class UserController extends Controller
{
    protected $paginate;

    public function  __construct()
    {
        $this->paginate = config('masterfile.defaultPagination');
    }

    public function index()
    {

        // $columns = [
        //     [
        //         'id' => 'name',
        //         'name' => 'Name'
        //     ],
        //     [
        //         'id' => 'email',
        //         'name' => 'Email',
        //     ],
        // ];

        // $json_columns = json_encode($columns);
        // $paginate =  $this->paginate;

        $exampledata = [
            [
                'id' => 1,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin1',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 2,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin2',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 3,
                'username' => 'nbdev',
                'firstname' => 'Netbay2',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin3',
                'profile' => 'default',
                'approved' => 'N'
            ],
            [
                'id' => 4,
                'username' => 'nbdev',
                'firstname' => 'Netbay3',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin4',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 1,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin1',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 2,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin2',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 3,
                'username' => 'nbdev',
                'firstname' => 'Netbay2',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin3',
                'profile' => 'default',
                'approved' => 'N'
            ],
            [
                'id' => 4,
                'username' => 'nbdev',
                'firstname' => 'Netbay3',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin4',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 1,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin1',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 2,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin2',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 3,
                'username' => 'nbdev',
                'firstname' => 'Netbay2',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin3',
                'profile' => 'default',
                'approved' => 'N'
            ],
            [
                'id' => 4,
                'username' => 'nbdev',
                'firstname' => 'Netbay3',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin4',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 1,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin1',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 2,
                'username' => 'nbdev',
                'firstname' => 'Netbay',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin2',
                'profile' => 'default',
                'approved' => 'Y'
            ],
            [
                'id' => 3,
                'username' => 'nbdev',
                'firstname' => 'Netbay2',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin3',
                'profile' => 'default',
                'approved' => 'N'
            ],
            [
                'id' => 4,
                'username' => 'nbdev',
                'firstname' => 'Netbay3',
                'email' => 'admin@mail.com',
                'lastname' => 'testnaja',
                'group' => 'admin4',
                'profile' => 'default',
                'approved' => 'Y'
            ]
        ];

        // $users = User::paginate(1);
        // $permission = ["EDIT", "DELETE"];
        // return view('configurations.users.index', compact('users', 'json_columns', 'paginate', 'url', 'permission'));


        $users = PaginatorController::paginate($exampledata, 4);
        $users->withPath('/configurations/user');
        $url = '/configurations/user';

        $searchBy = [ 
            "username" => "Username", 
            "firstname" => "Firstname", 
            "lastname" => "Lastname",
            "phone" => "Phone",
            "approved" =>  "Approve"
        ];

        return view('configurations.users.index', compact('users', 'url', 'searchBy'));
    }

    public function create()
    {
        return view('configurations.users.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($id)
    {
        //Mock data
        $user = User::find(1);
        return view('configurations.users.edit', [
            'user' => $user
        ]);
    }

    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')
                ->withSuccess(__('User updated successfully.'));
        }
    }
    public function destroy(User $user)
    {
        $user->delete();

        return response()->json([
            'status' => 'User deleted successfully'
        ], 200);
    }
}
