<?php

namespace App\Http\Controllers;

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

// Helpers
use App\Helpers\FechaDinamica;
/*
use App\Http\Controllers\CorridaAreaController;
use App\Http\Controllers\AnalitoController;
use App\Http\Controllers\MetodologiaController;
use App\Http\Controllers\ModoController;
use App\Http\Controllers\UnidadController;
*/

// Requests
use Illuminate\Http\Request;

// Modelos
use Illuminate\Support\Facades\DB;
use App\Log;
use Illuminate\Support\Facades\Auth;

class HistoricoController extends Controller{
    
    protected $tableLog;
    
    public function __construct(){
        $this->tableLog = new Log();
    }
    
    public function section(Request $req){
        
        /*
        $corridasArea = CorridaAreaController::listAll();
        $modos = ModoController::list();
        $analitosCorrida = self::listByCorrida(0);
        */

        $tablas = self::listByTablas();
        
        return view("historico.historicoSection")
            ->with("nomSection","SeeSectionHistorico")
            ->with("tablas",$tablas);
            /*
            ->with("corridasArea",$corridasArea)
            ->with("analitosCorrida",$analitosCorrida)
            ->with("modos",$modos)
            ->with("id_reference",$req->id_reference);
            */            
    }

    
    public function index(Request $req){
        $historicos = self::list($req->fecha_inicial, $req->fecha_final, $req->modulo, $req->accion);
        return view("historico.historicoIndex")
            ->with("historicos",$historicos);
    }


    public function listAccionHTML(Request $req){
        $acciones = self::ListByAcciones($req->id);
        return view("historico.historicoAccionList")
            ->with("acciones",$acciones);
    }


    public static function listByTablas(){
        $tableLog = new Log();
        return $tableLog
            ->select("tabla as nombre")
            ->leftJoin("usuario", "usuario.nom_usuario", "=", "log.usuario")
            ->where("usuario.super_admin_id", Auth::user()->super_admin_id)
            ->groupBy("tabla")
            ->get();
    }


    public static function listByAcciones($tabla){
        $tableLog = new Log();
        return $tableLog
        ->select("titulo as nombre")
        ->where("tabla","=",$tabla)
        ->leftJoin("usuario", "usuario.nom_usuario", "=", "log.usuario")
        ->where("usuario.super_admin_id", Auth::user()->super_admin_id)
        ->groupBy("titulo")
        ->get();
    }
    
    
    public static function list($fecha_inicial, $fecha_final, $tabla = "",$accion = ""){
        
        $tableLog = new Log();
        $objFilter = [
            ["log.fecha", ">=", $fecha_inicial],
            ["log.fecha","<=", FechaDinamica::HoraNocturna($fecha_final)]
        ];

        if($tabla != ""){
            array_push($objFilter, ["log.tabla", "=", $tabla]);
        }
        
        if($accion != ""){
            array_push($objFilter, ["log.titulo", "=", $accion]);
        }

        return $tableLog
            ->leftJoin("usuario", "usuario.nom_usuario", "=", "log.usuario")
            ->where("usuario.super_admin_id", Auth::user()->super_admin_id)
            ->where($objFilter)
            ->get();
        /*
        $obj_bd = $tableCorridaAnalito
            ->select(
                "area.*",
                "laboratorio.*",
                "corrida_area.id_corrida_area",
                "corrida_area.fecha_inicio",
                "corrida_area.estado as estado_corrida_area"
            )
            ->join("area", "area.id_area", "=", "corrida_area.area_id_area")
            ->join("laboratorio", "laboratorio.id_laboratorio", "=", "corrida_area.laboratorio_id_laboratorio")
            ->orderBy("corrida_area.estado","desc")
            ->orderBy("laboratorio.no_laboratorio","asc")
            ->orderBy("area.nom_area","asc");

        if($laboratorio != "" && $area != ""){ // Si tanto el area como el laboratorio estan definidos
            $obj_bd->where([
                ["laboratorio.id_laboratorio","=",$laboratorio],
                ["area.id_area","=",$area]
            ]);
        } else if($laboratorio != "") { // Si solo el laboratorio esta definido
            $obj_bd->where("laboratorio.id_laboratorio", $laboratorio);
        } else if($area != ""){ // Si solo el area esta definida
            $obj_bd->where("area.id_area", $area);
        }
        return $obj_bd->get();
        */
    }
}
