<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use Session;

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

class IndicatorCategoryController extends Controller
{

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

        $query = IndicatorCategoryModel::getIndicatorCategoryQuery($request);
        $grid = GridBuilder::builder()
            ->htmlid('#divIndicatorCategory')
            //        ->enableAjaxGrid()
            ->recordsPerPage(10)
            //->parameters()
            ->data($query)
            ->addColumn([
                'name' => 'indicator_category_id',
                'title' => 'Indicator category code',
                'filter' => 'indicator_category_id',
                'filter_name' => 'indicator_category_id',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'name',
                'title' => 'name',
                'filter' => 'name',
                'filter_name' => 'name',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'description',
                'title' => 'description',
                'filter' => 'description',
                'filter_name' => 'description',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'actions',
                'title' => 'Acciones',
                'html_decorator' => function ($indicatorCategory) use ($request) {

                    $btnEditUrl = route('indicatorCategoryForm', $indicatorCategory->indicator_category_id);
                    $btnDeleteUrl = route('indicatorCategoryDelete', $indicatorCategory->indicator_category_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 Indicator Category?\");' > Delete </a> &nbsp;&nbsp; ";

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


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


    public function form(Request $request)
    {
        $indicatorCategory = IndicatorCategoryModel::getIndicatorCategory($request->indicator_category_id);
        return view('indicatorCategories.form', compact('indicatorCategory'));
    }

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

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

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

    public function delete(Request $request)
    {
        $indicatorCategoryDelete = IndicatorCategoryModel::delete($request->indicator_category_id);
        if ($indicatorCategoryDelete) {
            Session::flash('message', 'Categoria eliminada');
        } else {
            Session::flash('errorMessage', 'Categoria invalida ');
        }
        return redirect()->back();
    }
}
