<?php

namespace App\Http\Controllers;

// Controladores
use App\Http\Controllers\Controller;

// Modelos
use App\Area;

// Historicos
use App\Http\Historico\AreaHistorico;

// Requests
use Illuminate\Http\Request;
use App\Http\Requests\AreaRequest;
use Illuminate\Support\Facades\Auth;

class AreaController extends Controller{

    protected $tableArea;
    protected $historico;

    public function __construct(){
        $this->tableArea = new Area();
    }


    public function section(){
        $areas = $this->tableArea
            ->orderBy("estado","desc")
            ->orderBy("nom_area","asc")
            // ->whereIn("area.super_admin_id", [0, Auth::user()->super_admin_id])
            ->get();

       return view("area.areaSection")
            ->with("areas",$areas)
            ->with("nomSection","SeeSectionArea");
    }


    public function index(){
        $areas = $this->tableArea
            ->orderBy("estado","desc")
            ->orderBy("nom_area","asc")
            // ->whereIn("area.super_admin_id", [0, Auth::user()->super_admin_id])
            ->get();

        return view("area.areaIndex")
            ->with("areas",$areas);
    }


    public function store(AreaRequest $req){

        $this->historico = new areaHistorico();
        $this->historico->store("store", $req);

        $this->tableArea->nom_area = $req->nombre;
        $this->tableArea->super_admin_id = Auth::user()->super_admin_id;
        $this->tableArea->save();
    }


    public static function list(){
        $tableArea = new Area();
        return $tableArea
            ->where("estado",1)
            ->orderBy("nom_area","asc")
            // ->whereIn("area.super_admin_id", [0, Auth::user()->super_admin_id])
            ->get();
    }

    public static function listItem($id_area){
        $tableArea = new Area();
        return $tableArea
            ->where("id_area",$id_area)
            ->first();
    }


    public function listHTML(){

        $areas = $this->tableArea
            ->where("estado",1)
            ->orderBy("nom_area","asc")
            // ->whereIn("area.super_admin_id", [0, Auth::user()->super_admin_id])
            ->get();

        return view("area.areaList")
            ->with("areas",$areas);
    }


    public function destroy(Request $req){

        $this->historico = new areaHistorico();
        $this->historico->store("destroy", $req);

        $this->tableArea
            ->where("id_area",$req->id)
            ->delete();
    }


    public function status(Request $req){

        $this->historico = new areaHistorico();
        $this->historico->store("status", $req);

        $statusNow = $this->tableArea
            ->where("id_area",$req->id)
            ->first()
            ->estado;

        $registroUpdate = $this->tableArea
            ->where("id_area",$req->id);

        if($statusNow == 1){
            $registroUpdate->update(["estado" => 0]);
        } else {
            $registroUpdate->update(["estado" => 1]);
        }
    }


    public function edit(Request $req){

        $area = $this->tableArea
            ->where("id_area",$req->id)
            ->first();

        return view("area.areaEdit")
            ->with("area",$area)
            ->with("campo",$req->campo);
    }


    public function show(Request $req){
        $area = $this->tableArea
            ->where("id_area",$req->id)
            ->first();

        return view("area.areaShow")
            ->with("area",$area)
            ->with("campo",$req->campo);
    }


    public function update(AreaRequest $req){

        $this->historico = new areaHistorico();
        $this->historico->store("update", $req);

        $area = $this->tableArea
            ->where("id_area",$req->id)
            ->first();

        switch($req->campo){
            case "nombre":
                $area->update(["nom_area" => $req->nombre]);
                break;
            case "area":
                $area->update(["area_id_area" => $req->id_area]);
                break;
        }
    }
}
