<?php

namespace App\Http\Controllers\Admin;

use App\Model\UIT;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mockery\Exception;
use Yajra\Datatables\Datatables;

class UITController extends Controller
{
    public function getIndex()
    {
        return view('pages.admin.uit.index');
    }

    public function postIndex(Request $request)
    {
        try
        {
            $model = new UIT();
            $uit = $model->uit()
                ->get();

            return Datatables::of($uit)->make(true);
        }
        catch (Exception $e)
        {
            return response()->json(null);
        }
    }

    public function postAgregar(Request $request)
    {
        try
        {
            $model = new UIT();
            $uit = $model->create([
                "anio" => $request->input('anio'),
                "valor" => $request->input('valor'),
                "base_legal" => $request->input('base_legal')
            ]);

            return response()->json($uit);
        }
        catch (Exception $e)
        {
            return response()->json(null);
        }
    }

    public function postEditar(Request $request)
    {
        try
        {
            $model = new UIT();
            $uit = $model->find($request->input('id'));
            $uit['valor'] = $request->input('valor');
            $uit['base_legal'] = $request->input('base_legal');
            $uit->save();

            return response()->json($uit);
        }
        catch (Exception $e)
        {
            return response()->json(null);
        }
    }

    public function postEliminar(Request $request)
    {
        try
        {
            $model = new UIT();
            $uit = $model->find($request->input('id'));
            $uit->delete();

            return response()->json($uit);
        }
        catch (Exception $e)
        {
            return response()->json(null);
        }
    }
}
