<?php

namespace App\Http\Controllers;

use App\Models\ProfileModel;
use Illuminate\Http\Request;
use Auth;
use Session;

// app includes
use App\Services\GridBuilder;
use App\Models\UserModel;

class UserController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {

        $query = UserModel::getUsersQuery($request);
        $grid = GridBuilder::builder()
            ->htmlid('#divUsers')
            //        ->enableAjaxGrid()
            ->recordsPerPage(10)
            //->parameters()
            ->data($query)
            ->addColumn([
                'name' => 'user_id',
                'title' => 'User code',
                'filter' => 'users.user_id',
                'filter_name' => 'user_id',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'companyName',
                'title' => 'Company',
                'filter' => 'companies.name',
                'filter_name' => 'companyName',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'profileName',
                'title' => 'Profile',
                'filter' => 'profiles.name',
                'filter_name' => 'profileName',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'first_name',
                'title' => 'First Name',
                'filter' => 'users.first_name',
                'filter_name' => 'first_name',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'last_name',
                'title' => 'Last Name',
                'filter' => 'users.last_name',
                'filter_name' => 'last_name',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'email',
                'title' => 'Email',
                'filter' => 'users.email',
                'filter_name' => 'email',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'actions',
                'title' => 'Acciones',
                'html_decorator' => function ($user) use ($request) {

                    $btnEditUrl = route('userForm', $user->user_id);
                    $btnDeleteUrl = route('userDelete', $user->user_id);
                    $btnEdit = " <a href='$btnEditUrl' class='btn btn-primary cold-color' > Edit </a> &nbsp;&nbsp; ";
                    $btnDelete = " <a href='$btnDeleteUrl' class='btn btn-danger cold-color' onclick='return confirm(\"¿Confirm delete user?\");' > Delete </a> &nbsp;&nbsp; ";

                    return " {$btnEdit} {$btnDelete}";
                },
                'encode' => false,
                'style' => ' class="text-nowrap" '
            ])
            ->build();

        //        $view = $request->filter ? 'helpers.grid' : 'accAccount.index';

        $view = $request->keyword ? 'helpers.grid' : 'users.index';
        return view($view, compact('grid'));
        //        return view('users.index',compact('grid','view'));
    }

    public function form(Request $request)
    {
        $user = UserModel::getUser($request->user_id);
        $profiles = ProfileModel::getProfiles();
        return view('users.form', compact('user', 'profiles'));
    }

    public function save(Request $request)
    {
        $data = $request->all();

        $validator = UserModel::getValidator($data);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput(); //get the data in all input formfields
        }
        $user = UserModel::save($data);
        if ($user) {
            Session::flash('message', 'Usuario guardado');
        } else {
            Session::flash('errorMessage', 'No fue posible guardar usuario');
        }
        return redirect()->route('users');
    }

    public function delete(Request $request)
    {
        $userDelete = UserModel::delete($request->user_id);
        if ($userDelete) {
            Session::flash('message', 'Usuario eliminado');
        } else {
            Session::flash('errorMessage', 'Usuario invalido');
        }
        return redirect()->back();
    }
}
