<?php

namespace App\Http\Controllers;

use App\Area;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class AreasController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function ReadAreas()
    {
        $areas = Area::orderBy('nombre', 'asc')->get();
        return (new Response($areas, 200));
    }

    public function CreateAreas(Request $request)
    {

        $datos = json_decode($request->datos);

        $area = new Area();
        $area->nombre = $datos[0]->nombre;
        $area->disponible_portal = $datos[0]->disponible_portal;
        $area->estado = $datos[0]->estado;
        $area->save();

        return (new Response($area, 200));
    }

    public function UpdateArea(Request $request)
    {
        $datos = json_decode($request->datos);

        $area = Area::find($datos[0]->id);
        $area->nombre = $datos[0]->nombre;
        $area->disponible_portal = $datos[0]->disponible_portal;
        $area->estado = $datos[0]->estado;

        $area->save();

        return (new Response($area, 200));

    }

    public function ReadAreasActivas()
    {
        if (\Auth::user()->id_perfil == 1 || \Auth::user()->id_perfil == 3) {
            $areas = Area::where('estado', 1)
                ->orderBy('nombre', 'asc')->get();
            return (new Response($areas, 200));
        } else {

            return (new Response(\Auth::user()->Areas, 200));
        }

    }

    public function ReadAreasActivasTodas()
    {

        $areas = Area::where('estado', 1)
            ->orderBy('nombre', 'asc')->get();
        return (new Response($areas, 200));

    }

    

}
