import { Download as ActionDownload } from '@/components/common/icons/ActionIcons';
import React, { useMemo } from 'react';
import {
Button,
Empty,
Input,
Select,
Table,
Tag,
Typography,
} from '@/components/ui';
import { Search, Table2, UserCog } from 'lucide-react';
import { ChangeText, OrgNameCell, SectionCard } from './shared';
import { formatPercent } from '../../../helpers/safeDisplay';
import {
ALERT_LEVELS,
ORG_STATUS,
changeRate,
daysSince,
downloadCsv,
formatCny,
formatCst,
orgDisplayName,
quotaToCny,
SEGMENTS,
TIERS,
} from './utils';
const { Text } = Typography;
// ─── 跟进人视图 ───────────────────────────────────────────────────
export const SalesRepTable = ({ reps, cny, onSelect, t }) => {
const m = (v) => {formatCny(v, cny)};
const count = (color) => (v) =>
v > 0 ? (
{v}
) : (
0
);
const columns = [
{
title: t('跟进人'),
dataIndex: 'sales_rep',
width: 120,
fixed: 'left',
render: (v) => (
),
},
{ title: t('企业数'), dataIndex: 'orgs', width: 80, align: 'right' },
{ title: t('活跃企业'), dataIndex: 'active', width: 90, align: 'right' },
{
title: t('本期消耗'),
dataIndex: 'quota',
width: 130,
align: 'right',
render: m,
sorter: (a, b) => a.quota - b.quota,
},
{
title: t('变化'),
dataIndex: 'change',
width: 90,
align: 'right',
render: (_, r) => ,
sorter: (a, b) =>
(changeRate(a.quota, a.prev_quota) ?? -Infinity) -
(changeRate(b.quota, b.prev_quota) ?? -Infinity),
},
{
title: t('企业余额'),
dataIndex: 'balance',
width: 130,
align: 'right',
render: m,
},
{
title: t('续费跟进'),
dataIndex: 'renewal',
width: 90,
align: 'center',
render: count('red'),
sorter: (a, b) => a.renewal - b.renewal,
},
{
title: t('流失挽回'),
dataIndex: 'retention',
width: 90,
align: 'center',
render: count('orange'),
sorter: (a, b) => a.retention - b.retention,
},
{
title: t('增长机会'),
dataIndex: 'growth',
width: 90,
align: 'center',
render: count('green'),
sorter: (a, b) => a.growth - b.growth,
},
];
return (
}
extra={
{t('点击跟进人筛选下方企业明细')}
}
>
r.sales_rep || '__none__'}
size='small'
scroll={{ x: 920 }}
pagination={reps.length > 8 ? { pageSize: 10 } : false}
empty={}
/>
);
};
// ─── 企业明细 ─────────────────────────────────────────────────────
export const EMPTY_DETAIL_FILTER = {
keyword: '',
segment: '',
tier: '',
alert: '',
salesRep: '',
};
const filterOrgs = (orgs, f) => {
const kw = f.keyword.trim().toLowerCase();
return orgs.filter((o) => {
if (kw && !`${o.name || ''} ${o.code || ''}`.toLowerCase().includes(kw))
return false;
if (f.segment && o.segment !== f.segment) return false;
if (f.tier && o.tier !== f.tier) return false;
if (
f.alert === '__any__'
? !o.alert_level
: f.alert && o.alert_level !== f.alert
)
return false;
if (f.salesRep && (o.sales_rep || '__none__') !== f.salesRep) return false;
return true;
});
};
export const OrgDetailTable = ({ report, filter, setFilter, cny, t }) => {
const orgs = useMemo(() => report.orgs || [], [report]);
const rows = useMemo(() => filterOrgs(orgs, filter), [orgs, filter]);
const reps = useMemo(
() =>
Array.from(new Set(orgs.map((o) => o.sales_rep || '__none__'))).sort(),
[orgs],
);
const set = (k) => (v) => setFilter({ ...filter, [k]: v || '' });
const filtered = Object.values(filter).some(Boolean);
const m = (v) => {formatCny(v, cny)};
const columns = [
{
title: t('企业'),
dataIndex: 'name',
width: 190,
fixed: 'left',
render: (_, r) => ,
},
{
title: t('跟进人'),
dataIndex: 'sales_rep',
width: 90,
render: (v) =>
v ? (
{v}
) : (
—
),
},
{
title: t('层级'),
dataIndex: 'tier',
width: 64,
render: (v) =>
v ? (
{v}
) : (
—
),
},
{
title: t('健康'),
dataIndex: 'segment',
width: 96,
render: (v) =>
SEGMENTS[v] ? (
{t(SEGMENTS[v].label)}
) : null,
},
{
title: t('本期消耗'),
dataIndex: 'quota',
width: 120,
align: 'right',
render: m,
sorter: (a, b) => a.quota - b.quota,
defaultSortOrder: 'descend',
},
{
title: t('对比期'),
dataIndex: 'prev_quota',
width: 110,
align: 'right',
render: m,
sorter: (a, b) => a.prev_quota - b.prev_quota,
},
{
title: t('变化'),
dataIndex: 'change',
width: 90,
align: 'right',
render: (_, r) => (
),
sorter: (a, b) => a.quota - a.prev_quota - (b.quota - b.prev_quota),
},
{
title: t('占比'),
dataIndex: 'share',
width: 70,
align: 'right',
render: (v) =>
v > 0 ? (
{formatPercent(v, 1)}
) : (
—
),
},
{
title: t('请求数'),
dataIndex: 'requests',
width: 90,
align: 'right',
render: (v) => (v || 0).toLocaleString(),
sorter: (a, b) => a.requests - b.requests,
},
{
title: t('活跃/成员'),
dataIndex: 'active_members',
width: 90,
align: 'right',
render: (v, r) => (
{v}/{r.member_count}
),
sorter: (a, b) => a.active_members - b.active_members,
},
{
title: t('余额'),
dataIndex: 'balance',
width: 120,
align: 'right',
render: m,
sorter: (a, b) => a.balance - b.balance,
},
{
title: t('预计可用'),
dataIndex: 'days_left',
width: 90,
align: 'right',
render: (v) =>
v < 0 ? (
—
) : (
{v < 1 ? t('不足 1 天') : t('{{days}} 天', { days: v.toFixed(0) })}
),
sorter: (a, b) =>
(a.days_left < 0 ? Infinity : a.days_left) -
(b.days_left < 0 ? Infinity : b.days_left),
},
{
title: t('余额预警'),
dataIndex: 'alert_level',
width: 110,
render: (v) =>
ALERT_LEVELS[v] ? (
{t(ALERT_LEVELS[v].label)}
) : null,
},
{
title: t('最近使用'),
dataIndex: 'last_active',
width: 100,
render: (v) => {
const d = daysSince(v);
return d === null ? (
—
) : d === 0 ? (
t('今天')
) : (
t('{{days}} 天前', { days: d })
);
},
sorter: (a, b) => a.last_active - b.last_active,
},
];
const onExport = () => exportOrgDetail(rows, cny, t);
return (
}
extra={
{filtered
? t('筛选 {{count}} / 共 {{total}} 家', {
count: rows.length,
total: orgs.length,
})
: t('共 {{total}} 家', { total: orgs.length })}
}
>
}
placeholder={t('搜索企业名称或代码')}
value={filter.keyword}
onChange={set('keyword')}
showClear
style={{ width: 220 }}
/>
}
/>
);
};
export const exportOrgDetail = (rows, cny, t) => {
const yuan = (v) => quotaToCny(v, cny).toFixed(2);
downloadCsv(
`${t('企业经营明细')}-${formatCst(Date.now() / 1000, false)}.csv`,
[
t('企业ID'),
t('企业'),
t('企业代码'),
t('状态'),
t('跟进人'),
t('层级'),
t('健康分群'),
t('本期消耗'),
t('对比期消耗'),
t('变化率'),
t('占比'),
t('请求数'),
t('活跃成员'),
t('成员数'),
t('余额'),
t('赠送余额'),
t('近7日日均消耗'),
t('预计可用天数'),
t('建议充值'),
t('余额预警'),
t('最近使用'),
t('创建时间'),
t('到期时间'),
t('联系人'),
t('联系电话'),
],
rows.map((o) => {
const rate = changeRate(o.quota, o.prev_quota);
return [
o.org_id,
orgDisplayName(o, t),
o.code,
ORG_STATUS[o.status] ? t(ORG_STATUS[o.status].label) : o.status,
o.sales_rep,
o.tier,
SEGMENTS[o.segment] ? t(SEGMENTS[o.segment].label) : '',
yuan(o.quota),
yuan(o.prev_quota),
rate === null ? '' : formatPercent(rate, 1),
o.share ? formatPercent(o.share, 2) : '',
o.requests,
o.active_members,
o.member_count,
yuan(o.balance),
yuan(o.gift_quota),
yuan(o.daily_avg),
o.days_left < 0 ? '' : o.days_left.toFixed(1),
yuan(o.topup_suggest),
ALERT_LEVELS[o.alert_level] ? t(ALERT_LEVELS[o.alert_level].label) : '',
o.last_active ? formatCst(o.last_active) : '',
formatCst(o.created_at, false),
o.expired_at > 0 ? formatCst(o.expired_at, false) : '',
o.contact_name,
o.contact_phone,
];
}),
);
};