"use client";
import { useState, useMemo } from "react";
import { agg, aggOrd, ETAPA_ORDER, EJECUTIVOS_VENTA } from "../../lib/derive";
import { actividadUrl, money } from "../../lib/constants";
import Panel from "../pipeline/Panel";
import DonutChart from "../pipeline/DonutChart";
const HORIZONTES = [
["hoy", "Hoy"], ["semana", "Esta semana"], ["semana_pasada", "Semana pasada"],
["mes", "Este mes"], ["mes_pasado", "Mes pasado"], ["todas", "Todas"],
];
// Puerto de ACT_TIPO_COLOR/ACT_ESTADO_COLOR (App Informe/assets/js/pages/actividades.js).
const ACT_TIPO_COLOR = { Tarea: "var(--marca)", Cita: "var(--alerta)", Llamada: "var(--profundo)" };
const ACT_ESTADO_COLOR = { Completada: "var(--gana)", Vencida: "var(--pierde)", Pendiente: "var(--alerta)", Cancelada: "var(--tinta-suave)" };
const TIPO_LABEL_PLURAL = { Cita: "Citas", Llamada: "Llamadas", Tarea: "Tareas" };
function inicioSemana(d) {
const x = new Date(d);
const dow = (x.getDay() + 6) % 7;
x.setDate(x.getDate() - dow);
x.setHours(0, 0, 0, 0);
return x;
}
function enRango(fecha, horizonte) {
if (horizonte === "todas") return true;
if (!fecha) return false;
const f = new Date(fecha);
f.setHours(0, 0, 0, 0);
const hoy = new Date();
hoy.setHours(0, 0, 0, 0);
if (horizonte === "hoy") return f.getTime() === hoy.getTime();
if (horizonte === "semana" || horizonte === "semana_pasada") {
const lunes = inicioSemana(hoy);
if (horizonte === "semana_pasada") lunes.setDate(lunes.getDate() - 7);
const domingo = new Date(lunes);
domingo.setDate(lunes.getDate() + (horizonte === "semana" ? 6 : 6));
if (horizonte === "semana_pasada") domingo.setDate(lunes.getDate() + 6);
return f >= lunes && f <= domingo;
}
if (horizonte === "mes" || horizonte === "mes_pasado") {
const base = new Date(hoy.getFullYear(), hoy.getMonth() - (horizonte === "mes_pasado" ? 1 : 0), 1);
return f.getFullYear() === base.getFullYear() && f.getMonth() === base.getMonth();
}
return true;
}
// Tabla de detalle agrupada por tipo, colapsable, con conteo + suma de Ingreso Est. en el
// encabezado — puerto de actSección() (App Informe/assets/js/pages/actividades.js). La columna
// "propia" (Ubicación/Prioridad/Dirección) cambia de nombre según el tipo — viene ya resuelta en
// `a.campoPropio` desde queries.js.
function DetalleTipo({ tipo, rows, locked }) {
if (!rows.length) return null;
const totalIngreso = rows.reduce((s, a) => s + (a.ingresoEstimado || 0), 0);
const sorted = [...rows].sort((a, b) => (a.scheduledstart || a.scheduledend || "").localeCompare(b.scheduledstart || b.scheduledend || ""));
const propioLabel = rows[0]?.campoPropio?.label || "";
return (
▼
{TIPO_LABEL_PLURAL[tipo] || tipo}
{rows.length} · {money(totalIngreso)} Ingreso Est. total
{sorted.map((a, i) => (
#
Referente a
Asunto
Objetivo
Fecha Inicio
Fecha Vencimiento
Ingreso Est.
{propioLabel}
Estado
{!locked && Propietario }
))}
{i + 1}
{a.referente?.nombre || "—"}
{a.asunto}
{a.objetivo || "—"}
{a.scheduledstartFmt || "—"}
{a.scheduledendFmt || "—"}
{a.ingresoEstimado ? money(a.ingresoEstimado) : "—"}
{a.campoPropio?.valor || "—"}
● {a.estadoFinal}
{!locked && {a.owner} }
Con Objetivo de Actividad asignado: {conObjetivo} ({filtradas.length ? Math.round((conObjetivo / filtradas.length) * 100) : 0}%) — adopción real del campo. Referidas a Oportunidad: {conOportunidad.length} ({filtradas.length ? Math.round((conOportunidad.length / filtradas.length) * 100) : 0}%).
Completadas ÷ total de actividades (sin canceladas) de cada quien
{porEjecutivo.map((r) => ({oportunidadesTotalN} oportunidades relacionadas en este período · {money(oportunidadesTotalV)} de ingreso estimado
{oportunidadesPorTipo.length ? oportunidadesPorTipo.map((r) => (Ninguna actividad de este período está referida a una Oportunidad.
}