<?php

namespace App\Http\Controllers;

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

// Modelos
use Illuminate\Support\Facades\DB;
use App\Laboratorio;

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

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

// Sesion
use Auth;

class LaboratorioController extends Controller
{

    protected $tableLabo; // Table laboratorio
    protected $historico;

    public function __construct()
    {
        $this->tableLabo = new Laboratorio();
    }


    public function section()
    {
        $laboratorios = $this->tableLabo
            ->select(
                "laboratorio.id_laboratorio as id_laboratorio",
                "laboratorio.no_laboratorio as no_laboratorio",
                "laboratorio.nom_laboratorio as nom_laboratorio",
                "laboratorio.estado as estado",
                "super_admin.*"
            )
            ->join("super_admin", "super_admin.id", "=", "laboratorio.super_admin_id")
            ->where("super_admin.id", "=", Auth::user()->super_admin_id)
            ->orderBy("laboratorio.estado", "desc")
            ->orderBy("laboratorio.no_laboratorio", "asc")
            ->get();
        $clients = SuperUsuarioController::list();
        return view("laboratorio.laboratorioSection")
            ->with("laboratorios", $laboratorios)
            ->with("clients", $clients)
            ->with("nomSection", "SeeSectionLaboratorio");
    }


    public function index()
    {
        $laboratorios = $this->tableLabo
            ->select(
                "laboratorio.id_laboratorio as id_laboratorio",
                "laboratorio.no_laboratorio as no_laboratorio",
                "laboratorio.nom_laboratorio as nom_laboratorio",
                "laboratorio.estado as estado",
                "super_admin.*"
            )
            ->join("super_admin", "super_admin.id", "=", "laboratorio.super_admin_id")
            ->where("super_admin.id", "=", Auth::user()->super_admin_id)
            ->orderBy("laboratorio.estado", "desc")
            ->orderBy("laboratorio.no_laboratorio", "asc")
            ->get();

        return view("laboratorio.laboratorioIndex")
            ->with("laboratorios", $laboratorios);
    }

    public function store(LaboratorioRequest $req)
    {

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

        $this->tableLabo->no_laboratorio = $req->no_laboratorio;
        $this->tableLabo->nom_laboratorio = $req->nom_laboratorio;
        $this->tableLabo->super_admin_id = $req->super_admin_id;
        $this->tableLabo->save();
    }

    public function destroy(Request $req)
    {

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

        $this->tableLabo
            ->where("id_laboratorio", $req->id)
            ->delete();
    }

    public function status(Request $req)
    {

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

        $statusNow = $this->tableLabo
            ->where("id_laboratorio", $req->id)
            ->first()
            ->estado; // Seleccionar el estado de la institucion que sea igual a la indicada

        $registroUpdate = $this->tableLabo
            ->where("id_laboratorio", $req->id);

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


    public function edit(Request $req)
    {

        $laboratorio = $this->tableLabo
            ->where("id_laboratorio", $req->id)
            ->first();

        switch ($req->campo) {
            case "no_laboratorio":
                return view("laboratorio.laboratorioEdit")
                    ->with("campo", $req->campo)
                    ->with("laboratorio", $laboratorio);
                break;

            case "nom_laboratorio":
                return view("laboratorio.laboratorioEdit")
                    ->with("campo", $req->campo)
                    ->with("laboratorio", $laboratorio);
                break;

            default:
                abort(500, "Opción de modificación no encontrada");
                break;
        }
    }

    public static function listAll()
    {
        // Listar todos los laboratorios que esten habilitados si es un usuario administrador
        if (Auth::user()->rol_id_rol == 1) { // Administrador total
            $tableLabo = new Laboratorio();
            return $tableLabo
                ->select(
                    "laboratorio.id_laboratorio as id_laboratorio",
                    "laboratorio.no_laboratorio as no_laboratorio",
                    "laboratorio.nom_laboratorio as nom_laboratorio",
                    "laboratorio.estado as estado"
                )
                ->join("super_admin", "super_admin.id", "=", "laboratorio.super_admin_id")
                ->where("super_admin.id", "=", Auth::user()->super_admin_id)
                ->orderBy("laboratorio.no_laboratorio", "asc")
                ->get();

        } else if (Auth::user()->rol_id_rol == 5) {
            $tableLabo = new Laboratorio();
            return $tableLabo->select(
                "laboratorio.id_laboratorio as id_laboratorio",
                "laboratorio.no_laboratorio as no_laboratorio",
                "laboratorio.nom_laboratorio as nom_laboratorio",
                "laboratorio.estado as estado"
            )
                ->orderBy("laboratorio.no_laboratorio", "asc")
                ->get();
        } else { // Cualquier otro tipo de usuario
            $tableLabo = new Laboratorio();
            return $tableLabo
                ->select(
                    "laboratorio.id_laboratorio as id_laboratorio",
                    "laboratorio.no_laboratorio as no_laboratorio",
                    "laboratorio.nom_laboratorio as nom_laboratorio",
                    "laboratorio.estado as estado"
                )
                ->join("usuario_laboratorio", "laboratorio.id_laboratorio", "=", "usuario_laboratorio.laboratorio_id_laboratorio")
                ->where("usuario_laboratorio.usuario_id_usuario", Auth::user()->id_usuario)
                ->orderBy("laboratorio.no_laboratorio", "asc")
                ->get();
        }
    }


    public static function listItem($id_laboratorio)
    {
        $tableLabo = new Laboratorio();
        return $tableLabo
            ->where("id_laboratorio", $id_laboratorio)
            ->first();
    }


    public static function listByAsignacionUsuario($id_conexion)
    {
        $tableLabo = new Laboratorio();
        return $tableLabo
            ->join("usuario_laboratorio", "id_laboratorio", "=", "laboratorio_id_laboratorio")
            ->where("id_usuario_laboratorio", $id_conexion)
            ->first();
    }


    public function listHTML()
    {
        if (Auth::user()->rol_id_rol == 5) {
            $laboratorios = $this->tableLabo->select(
                "laboratorio.id_laboratorio as id_laboratorio",
                "laboratorio.no_laboratorio as no_laboratorio",
                "laboratorio.nom_laboratorio as nom_laboratorio",
                "laboratorio.estado as estado"
            )
                ->orderBy("laboratorio.no_laboratorio", "asc")
                ->get();
        } else if (Auth::user()->rol_id_rol == 1) {
            $laboratorios = $this->tableLabo
                ->select(
                    "laboratorio.id_laboratorio as id_laboratorio",
                    "laboratorio.no_laboratorio as no_laboratorio",
                    "laboratorio.nom_laboratorio as nom_laboratorio",
                    "laboratorio.estado as estado"
                )
                ->join("super_admin", "super_admin.id", "=", "laboratorio.super_admin_id")
                ->where("super_admin.id", "=", Auth::user()->super_admin_id)
                ->orderBy("laboratorio.no_laboratorio", "asc")
                ->get();
        }

        return view("laboratorio.laboratorioList")
            ->with("laboratorios", $laboratorios);
    }

    public function show(Request $req)
    {
        $laboratorio = $this->tableLabo
            ->select(
                "laboratorio.id_laboratorio as id_laboratorio",
                "laboratorio.no_laboratorio as no_laboratorio",
                "laboratorio.nom_laboratorio as nom_laboratorio",
                "laboratorio.estado as estado"
            )
            ->orderBy("laboratorio.estado", "desc")
            ->orderBy("laboratorio.no_laboratorio", "asc")
            ->where("id_laboratorio", $req->id)
            ->first();

        return view("laboratorio.laboratorioShow")
            ->with("laboratorio", $laboratorio)
            ->with("campo", $req->campo);
    }


    public function update(LaboratorioRequest $req)
    {

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

        $laboratorio = $this->tableLabo
            ->where("id_laboratorio", $req->id)
            ->first(); // Se selecciona la institucion a la cual se desea realizar la actualización

        switch ($req->campo) {

            case "no_laboratorio":
                $laboratorio->update(["no_laboratorio" => $req->no_laboratorio]);
                break;

            case "nom_laboratorio":
                $laboratorio->update(["nom_laboratorio" => $req->nom_laboratorio]);
                break;

            default:
                abort(500, "Opción de modificación no encontrada");
                break;
        }
    }
}
