<?php

namespace App\Http\Controllers;

// Controladores
use App\Http\Controllers\Controller;
use App\Http\Controllers\AreaController;
use App\Http\Controllers\CorridaAreaController;
use App\Http\Controllers\ProyectoEP15Controller;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

// Modelos
use App\Analito;

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

// Request
use Illuminate\Http\Request;
use App\Http\Requests\AnalitoRequest;

class AnalitoController extends Controller{

    protected $tableAnalito;
    protected $historico;

    public function __construct(){
        $this->tableAnalito = new Analito();
    }


    public function section(){
        $analitos = $this->tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps",
                "analito.estado as estado"
            )
            ->join("area", "area.id_area", "=", "analito.area_id_area")
            ->orderBy("analito.estado","desc")
            ->orderBy("area.nom_area","asc")
            ->orderBy("analito.nom_analito","asc")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            /*
            ->orWhere("analito.super_admin_id", 1)*/
            ->get();

        $areas = AreaController::list();

        return view("analito.analitoSection")
            ->with("analitos",$analitos)
            ->with("areas",$areas)
            ->with("nomSection","SeeSectionAnalito");
    }


    public function index(){
        $analitos = $this->tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps",
                "analito.estado as estado"
            )
            ->join("area", "area.id_area", "=", "analito.area_id_area")
            ->orderBy("analito.estado","desc")
            ->orderBy("area.nom_area","asc")
            ->orderBy("analito.nom_analito","asc")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->get();

        return view("analito.analitoIndex")
            ->with("analitos",$analitos);
    }


    public function store(AnalitoRequest $req){
        $this->historico = new analitoHistorico();
        $this->historico->store("store", $req);

        $this->tableAnalito->area_id_area = $req->id_area;
        $this->tableAnalito->nom_analito = $req->nombre;
        $this->tableAnalito->valor_aps = $req->limite;
        $this->tableAnalito->super_admin_id = Auth::user()->super_admin_id;
        $this->tableAnalito->save();
    }


    public static function list(){
        $tableAnalito = new Analito();
        return $tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps"
            )
            ->join("area","area.id_area","=","analito.area_id_area")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->where("analito.estado",1)
            ->orderBy("area.nom_area")
            ->orderBy("analito.nom_analito")
            ->get();
    }

    public static function listByAreaCorrida($id_corrida_area){ // Traer los analitos que son de la misma area de un proyecto

        $id_area = CorridaAreaController::getAreaById($id_corrida_area);
        $tableAnalito = new Analito();
        return $tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps"
            )
            ->join("area","area.id_area","=","analito.area_id_area")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->where([
                ["analito.estado",1],
                ["area.id_area","=", $id_area]
            ])
            ->orderBy("area.nom_area")
            ->orderBy("analito.nom_analito")
            ->get();
    }


    public static function listByProyectoEP15($id_proyecto_ep15){ // Traer los analitos que son de la misma area de un proyecto

        $id_area = ProyectoEP15Controller::getAreaById($id_proyecto_ep15);
        $tableAnalito = new Analito();
        return $tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps"
            )
            ->join("area","area.id_area","=","analito.area_id_area")
            ->where([
                ["analito.estado",1],
                ["area.id_area","=", $id_area]
            ])
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->orderBy("area.nom_area")
            ->orderBy("analito.nom_analito")
            ->get();
    }


    public static function listByAnalitoCorrida($id_analito_corrida){ // Traer los analitos que son de la misma area de un proyecto

        $tableAnalito = new Analito();
        return $tableAnalito
            ->select("*")
            ->join("corrida_analito","analito.id_analito","=","corrida_analito.analito_id_analito")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->where("corrida_analito.id_corrida_analito","=", $id_analito_corrida)
            ->first();
    }


    public function listHTML(Request $req){ // Lista con base a las corridas de área
        $id_area = CorridaAreaController::getAreaById($req->id);

        $analitos = $this->tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps"
            )
            ->join("area","area.id_area", "=","analito.area_id_area")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->where("area.id_area", $id_area)
            ->where("area.estado", 1)
            ->orderBy("area.nom_area")
            ->orderBy("analito.nom_analito")
            ->get();

        return view("analito.analitoList")
            ->with("analitos", $analitos);
    }



    public function listHTMLEP15(Request $req){ // Lista con base a los proyectos EP15
        $id_area = ProyectoEP15Controller::getAreaById($req->id);

        $analitos = $this->tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps"
            )
            ->join("area","area.id_area","=","analito.area_id_area")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
/*            ->orWhere("analito.super_admin_id", 1)*/
            ->where([
                ["area.id_area",$id_area],
                ["analito.estado", 1],
            ])
            ->orderBy("area.nom_area")
            ->orderBy("analito.nom_analito")
            ->get();

        return view("analito.analitoList")
            ->with("analitos",$analitos);
    }


    public static function listItem($id){
        $tableAnalito = new Analito();
        $analito = $tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.id_area as id_area",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps",
                "analito.estado as estado"
                )
            ->join("area","area.id_area","=","analito.area_id_area")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->where("analito.id_analito",$id)
            ->first();
        return $analito;
    }

    public function destroy(Request $req){

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

        $this->tableAnalito
            ->where("id_analito",$req->id)
            ->delete();
    }


    public function status(Request $req){

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

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

        $registroUpdate = $this->tableAnalito
            ->where("id_analito",$req->id);

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


    public function edit(Request $req){
        $analito = $this->tableAnalito
            ->select(
                "id_analito",
                "area_id_area",
                "nom_analito",
                "analito.super_admin_id",
                "valor_aps",
                "estado"
            )
            ->where("id_analito",$req->id)
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->first();

        $areas = AreaController::list();

        return view("analito.analitoEdit")
            ->with("analito",$analito)
            ->with("areas",$areas)
            ->with("campo",$req->campo);
    }


    public function show(Request $req){
        $analito = $this->tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps"
            )
            ->join("area","area.id_area","=","analito.area_id_area")
            // ->whereIn("analito.super_admin_id", [0, Auth::user()->super_admin_id])
            ->where("analito.id_analito",$req->id)
            ->first();

        return view("analito.analitoShow")
            ->with("analito",$analito)
            ->with("campo",$req->campo);
    }


    public function update(AnalitoRequest $req){

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

        $analito = $this->tableAnalito
            ->where("id_analito",$req->id)
            ->first();

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

    public function exportPdf(Request $req){
        $analitos = $this->tableAnalito
            ->select(
                "analito.id_analito as id_analito",
                "analito.super_admin_id",
                "area.nom_area as nom_area",
                "analito.nom_analito as nom_analito",
                "analito.valor_aps as valor_aps"
            )
            ->join("area", "area.id_area", "=", "analito.area_id_area")
            ->where("analito.estado", 1)
            ->orderBy("area.nom_area","asc")
            ->orderBy("analito.nom_analito","asc")
            ->get();

        return view("analito.analitoInforme")
            ->with("analitos",$analitos);
    }
}
