hamed-dashboards/dashboards/ecommerce/v2/app.js

341 lines
20 KiB
JavaScript
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Advanced BI Logic for Iranian Ecommerce Intelligence Dashboard v2
// Built with Plotly.js, Vazirmatn Typography and Executive BI Layouts
const bundle = window.bundle || {};
const allSources = window.allSources || [];
// 1. Render Executives Intelligence Dossier
function renderExecutives() {
const container = document.getElementById('executives-cards-grid');
if (!container) return;
container.innerHTML = (bundle.executives_dossier || []).map(exec => `
<div class="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm space-y-4 hover:border-blue-400 hover:shadow-md transition flex flex-col justify-between">
<div class="space-y-3">
<div class="flex justify-between items-start">
<div>
<span class="text-[10px] font-extrabold px-2.5 py-1 rounded-lg bg-blue-50 text-blue-700 border border-blue-200">${exec.id}</span>
<h3 class="text-base font-black text-slate-900 mt-1.5">${exec.name}</h3>
<div class="text-xs font-semibold text-rose-600">${exec.current_role}</div>
</div>
<span class="text-xs text-slate-400 font-mono font-medium">${exec.tenure_in_ecom}</span>
</div>
<div class="text-xs text-slate-500 bg-slate-50 p-2.5 rounded-xl border border-slate-100">
<strong class="text-slate-700">سوابق کلیدی:</strong> ${exec.past_roles}
</div>
<div class="p-3 bg-blue-50/50 rounded-xl border border-blue-100 text-xs text-slate-700 leading-relaxed">
<strong class="text-blue-900 block mb-1 font-bold">موضع استراتژیک درباره فروش برندها:</strong>
${exec.stance_on_brands}
</div>
<div class="p-3 bg-emerald-50/50 rounded-xl border border-emerald-100 text-xs text-emerald-950 leading-relaxed">
<strong class="text-emerald-900 block mb-1 font-bold">اقدامات عملیاتی در تعامل با برندها:</strong>
${exec.brand_cooperation_initiatives}
</div>
</div>
<div class="space-y-2 pt-3 border-t border-slate-100 text-xs">
<span class="font-bold text-slate-700 block text-xs">مصاحبه‌های تصویری و اسناد فکت‌چک شده:</span>
<div class="space-y-1.5">
${(exec.direct_interviews || []).map(inv => `
<a href="${inv.url}" target="_blank" class="flex items-center justify-between p-2 bg-slate-50 hover:bg-rose-50 text-slate-700 hover:text-rose-900 rounded-xl transition text-xs font-medium border border-slate-100">
<span class="truncate max-w-[210px]" title="${inv.title}">${inv.title}</span>
<span class="text-[10px] text-rose-600 font-bold shrink-0 font-mono">${inv.platform} (${inv.year}) &rarr;</span>
</a>
`).join('')}
</div>
</div>
</div>
`).join('');
}
// 2. Render Brands Matrix Table
function renderBrandsTable() {
const tbody = document.getElementById('brands-matrix-tbody');
if (!tbody) return;
tbody.innerHTML = (bundle.brands_matrix || []).map(b => `
<tr class="hover:bg-slate-50 transition border-b border-slate-100">
<td class="p-3.5 font-black text-slate-900">${b.brand_name}</td>
<td class="p-3.5 text-xs text-slate-600 font-medium">${b.origin}</td>
<td class="p-3.5"><span class="px-2.5 py-1 rounded-lg bg-slate-100 text-slate-700 font-bold text-xs">${b.sector}</span></td>
<td class="p-3.5 text-center">
<span class="px-2.5 py-1 rounded-lg ${b.has_official_brandstore ? 'bg-emerald-50 text-emerald-700 border border-emerald-200' : 'bg-rose-50 text-rose-700'} font-black text-xs">
${b.has_official_brandstore ? 'بله (VIP)' : 'خیر'}
</span>
</td>
<td class="p-3.5 text-center">
<span class="px-2.5 py-1 rounded-lg ${b.reports_sales_officially ? 'bg-blue-50 text-blue-700 border border-blue-200' : 'bg-slate-100 text-slate-600'} font-black text-xs">
${b.reports_sales_officially ? 'گزارش رسمی داده' : 'بدون انتشار'}
</span>
</td>
<td class="p-3.5 text-xs text-slate-700 font-semibold">${b.cooperation_model}</td>
<td class="p-3.5 text-xs text-slate-600 leading-relaxed max-w-xs">${b.official_stance}</td>
<td class="p-3.5 text-xs text-emerald-800 leading-relaxed max-w-xs bg-emerald-50/40 p-2 rounded-xl">${b.conflicts_and_resolutions}</td>
<td class="p-3.5 text-center">
<a href="${b.brand_executive_interview_link}" target="_blank" class="text-rose-600 hover:text-rose-800 font-bold inline-flex items-center gap-1 text-xs">
مستندات <i data-lucide="external-link" class="w-3.5 h-3.5"></i>
</a>
</td>
</tr>
`).join('');
}
// 3. Render Strategic Playbook Pillars
function renderPlaybook() {
const p = bundle.strategic_playbook || {};
const brandContainer = document.getElementById('playbook-brands-list');
if (brandContainer && p.how_digikala_convinced_brands) {
brandContainer.innerHTML = p.how_digikala_convinced_brands.map(item => `
<div class="p-4 bg-slate-50 rounded-2xl border border-slate-100 space-y-2">
<h4 class="text-xs font-bold text-slate-900 flex items-center gap-1.5">
<i data-lucide="check" class="w-3.5 h-3.5 text-blue-600"></i> ${item.title}
</h4>
<p class="text-xs text-slate-600 leading-relaxed">${item.strategy}</p>
<div class="text-xs text-blue-800 font-semibold bg-blue-50 p-2.5 rounded-xl border border-blue-100">
<strong>نتیجه مستند:</strong> ${item.verified_result}
</div>
</div>
`).join('');
}
const userContainer = document.getElementById('playbook-users-list');
if (userContainer && p.how_digikala_convinced_users) {
userContainer.innerHTML = p.how_digikala_convinced_users.map(item => `
<div class="p-4 bg-slate-50 rounded-2xl border border-slate-100 space-y-2">
<h4 class="text-xs font-bold text-slate-900 flex items-center gap-1.5">
<i data-lucide="check" class="w-3.5 h-3.5 text-emerald-600"></i> ${item.title}
</h4>
<p class="text-xs text-slate-600 leading-relaxed">${item.strategy}</p>
<div class="text-xs text-emerald-800 font-semibold bg-emerald-50 p-2.5 rounded-xl border border-emerald-100">
<strong>نتیجه مستند:</strong> ${item.verified_result}
</div>
</div>
`).join('');
}
const cannibalContainer = document.getElementById('playbook-cannibal-list');
if (cannibalContainer && p.how_digikala_resolved_internal_cannibalization) {
cannibalContainer.innerHTML = p.how_digikala_resolved_internal_cannibalization.map(item => `
<div class="p-4 bg-slate-50 rounded-2xl border border-slate-100 space-y-2">
<h4 class="text-xs font-bold text-slate-900">${item.conflict_name}</h4>
<div class="p-2.5 bg-rose-50 rounded-xl border border-rose-100 text-xs text-rose-900 leading-relaxed">
<strong>ریشه تعارض داخلی:</strong> ${item.issue}
</div>
<div class="p-2.5 bg-emerald-50 rounded-xl border border-emerald-100 text-xs text-emerald-950 leading-relaxed">
<strong>راهکار تثبیت‌شده:</strong> ${item.resolution}
</div>
<div class="text-xs text-slate-600"><strong>دستاورد کمی:</strong> ${item.impact}</div>
</div>
`).join('');
}
}
// 4. Render Deep Failures & Pivots
function renderFailures() {
const container = document.getElementById('failures-deep-grid');
if (!container) return;
container.innerHTML = (bundle.failures_pivots || []).map(item => `
<div class="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm space-y-3.5 hover:shadow-md transition">
<div class="flex justify-between items-start pb-3 border-b border-slate-100 gap-2">
<div>
<span class="text-[10px] font-extrabold px-2.5 py-1 rounded-lg bg-rose-50 text-rose-700 border border-rose-200">${item.id} (${item.year})</span>
<h3 class="text-sm font-bold text-slate-900 mt-1.5">${item.title}</h3>
<span class="text-xs text-slate-400 font-medium">دسته‌بندی: ${item.category}</span>
</div>
<a href="${item.verified_url}" target="_blank" class="text-rose-600 hover:text-rose-800 font-bold inline-flex items-center gap-1 text-xs shrink-0">
مستندات <i data-lucide="external-link" class="w-3.5 h-3.5"></i>
</a>
</div>
<div class="space-y-2 text-xs">
<div>
<span class="font-bold text-slate-800">علت ریشه‌ای وقوع:</span>
<p class="text-slate-600 mt-0.5 leading-relaxed">${item.root_cause}</p>
</div>
<div>
<span class="font-bold text-slate-800">نحوه مداخله و اجرا:</span>
<p class="text-slate-600 mt-0.5 leading-relaxed">${item.execution}</p>
</div>
<div>
<span class="font-bold text-rose-700">تبعات و شوک اولیه:</span>
<p class="text-slate-600 mt-0.5 leading-relaxed">${item.consequences}</p>
</div>
<div class="p-3 bg-emerald-50 rounded-xl border border-emerald-100">
<span class="font-bold text-emerald-900">راهکار پایدار و وضعیت فعلی:</span>
<p class="text-emerald-800 mt-0.5 leading-relaxed">${item.resolution}</p>
</div>
</div>
</div>
`).join('');
}
// 5. Render Industry Breakdown Cards
function renderIndustryBreakdown() {
const container = document.getElementById('industry-breakdown-cards');
if (!container) return;
container.innerHTML = (bundle.industry_breakdown || []).map(ind => `
<div class="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm space-y-4 hover:shadow-md transition">
<div class="flex flex-col sm:flex-row sm:items-center justify-between pb-3 border-b border-slate-100 gap-2">
<div>
<h3 class="text-base font-black text-slate-900">${ind.sector}</h3>
<div class="text-xs text-slate-500 mt-0.5">نسبت فروش ایرانی به خارجی: <strong class="text-slate-800 font-bold">${ind.ratio_domestic_to_foreign}</strong></div>
</div>
<div class="flex flex-wrap items-center gap-2">
<span class="text-xs font-bold px-3 py-1 bg-blue-50 text-blue-700 rounded-lg">اندازه بازار: ${ind.total_market_size_irr}</span>
<span class="text-xs font-bold px-3 py-1 bg-emerald-50 text-emerald-700 rounded-lg">نفوذ آنلاین: ${ind.ecommerce_share_percent}٪</span>
<span class="text-xs font-bold px-3 py-1 bg-purple-50 text-purple-700 rounded-lg">سوشال کامرس: ${ind.social_commerce_share_percent}٪</span>
<span class="text-xs font-bold px-3 py-1 bg-rose-50 text-rose-700 rounded-lg">نرخ قاچاق: ${ind.smuggling_rate_percent}٪</span>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-xs">
<div class="space-y-3">
<div class="p-3 bg-slate-50 rounded-xl border border-slate-100">
<span class="font-bold text-slate-800 block mb-1">پرفروش‌ترین و پرسرچ‌ترین برندهای ایرانی:</span>
<div class="text-blue-700 font-bold">${ind.top_searched_brands_domestic.join(' • ')}</div>
</div>
<div class="p-3 bg-slate-50 rounded-xl border border-slate-100">
<span class="font-bold text-slate-800 block mb-1">پرفروش‌ترین و پرسرچ‌ترین برندهای خارجی:</span>
<div class="text-slate-700 font-bold">${ind.top_searched_brands_foreign.join(' • ')}</div>
</div>
</div>
<div class="space-y-3">
<div class="p-3 bg-amber-50/60 rounded-xl border border-amber-100">
<span class="font-bold text-amber-900 block mb-1">گلوگاه‌های رگولاتوری، گمرکی و استاندارد:</span>
<p class="text-amber-800 leading-relaxed">${ind.regulatory_bottlenecks}</p>
</div>
<div class="p-3 bg-rose-50/60 rounded-xl border border-rose-100">
<span class="font-bold text-rose-900 block mb-1">کانال‌ها و شیوه‌های ورود کالای قاچاق:</span>
<p class="text-rose-800 leading-relaxed">${ind.smuggling_channels}</p>
</div>
</div>
</div>
</div>
`).join('');
}
// 6. Real-time Search in 2,622 Sources Vault
function searchIntelVault() {
const qEl = document.getElementById('intel-search-input');
const platEl = document.getElementById('intel-platform-select');
const q = qEl ? qEl.value.toLowerCase().trim() : '';
const p = platEl ? platEl.value : 'all';
const tbody = document.getElementById('intel-sources-tbody');
if (!tbody) return;
tbody.innerHTML = '';
const filtered = allSources.filter(s => {
const matchQ = !q || (s.title && s.title.toLowerCase().includes(q)) ||
(s.summary && s.summary.toLowerCase().includes(q)) ||
(s.brand_or_person && s.brand_or_person.toLowerCase().includes(q));
const matchP = p === 'all' || (s.platform && s.platform.includes(p));
return matchQ && matchP;
});
const counter = document.getElementById('intel-sources-counter');
if (counter) {
counter.innerText = `نمایش ${filtered.length.toLocaleString('fa-IR')} از ${allSources.length.toLocaleString('fa-IR')} منبع مستند و فکت‌چک شده`;
}
filtered.slice(0, 150).forEach(s => {
const tr = document.createElement('tr');
tr.className = "hover:bg-slate-50 transition border-b border-slate-100";
tr.innerHTML = `
<td class="p-3 font-mono text-slate-400 text-xs">${s.id}</td>
<td class="p-3 font-bold text-slate-900 max-w-md truncate text-xs" title="${s.title}">${s.title}</td>
<td class="p-3"><span class="px-2 py-0.5 rounded bg-slate-100 text-slate-700 text-[10px] font-bold">${s.platform || 'وب'}</span></td>
<td class="p-3 text-xs text-slate-600">${s.category || 'عمومی'}</td>
<td class="p-3 text-xs text-slate-500 font-mono">${s.publication_year || '—'}</td>
<td class="p-3 text-xs text-slate-500 truncate max-w-xs" title="${s.brand_or_person}">${s.brand_or_person || '—'}</td>
<td class="p-3 text-center">
<a href="${s.url}" target="_blank" class="text-rose-600 hover:text-rose-800 font-bold inline-flex items-center gap-1 text-xs">
مشاهده <i data-lucide="external-link" class="w-3.5 h-3.5"></i>
</a>
</td>
`;
tbody.appendChild(tr);
});
if (window.lucide) lucide.createIcons();
}
// 7. Initialize Executive BI Plotly Visualizations
function initIntelPlotlyCharts() {
if (typeof Plotly === 'undefined') return;
const defaultFont = { family: 'Vazirmatn, sans-serif', size: 11, color: '#0F172A' };
const ind = bundle.industry_breakdown || [];
const secNames = ind.map(d => d.sector.split('(')[0].trim());
// 1. Quadrant Scatter: Penetration vs Smuggling
const xPen = ind.map(d => d.ecommerce_share_percent);
const ySmug = ind.map(d => d.smuggling_rate_percent);
const sizes = [42, 34, 28, 38, 52, 22]; // Market scale proportional
const traceQuad = {
x: xPen,
y: ySmug,
text: secNames,
mode: 'markers+text',
textposition: 'top center',
textfont: { size: 11, color: '#0F172A', family: 'Vazirmatn' },
marker: {
size: sizes,
color: ['#2563EB', '#10B981', '#F59E0B', '#8B5CF6', '#EC4899', '#06B6D4'],
opacity: 0.85,
line: { width: 2, color: '#0F172A' }
},
type: 'scatter'
};
const layoutQuad = {
font: defaultFont,
paper_bgcolor: '#FFFFFF',
plot_bgcolor: '#F8FAFC',
margin: { l: 50, r: 40, t: 30, b: 50 },
xaxis: { title: 'نفوذ ایکامرس رسمی (درصد)', gridcolor: '#E2E8F0', range: [0, 35] },
yaxis: { title: 'نرخ تخمینی قاچاق در بازار (درصد)', gridcolor: '#E2E8F0', range: [0, 65] },
shapes: [
{ type: 'line', x0: 15, x1: 15, y0: 0, y1: 65, line: { color: '#94A3B8', width: 1.5, dash: 'dash' } },
{ type: 'line', x0: 0, x1: 35, y0: 25, y1: 25, line: { color: '#94A3B8', width: 1.5, dash: 'dash' } }
],
annotations: [
{ x: 25, y: 55, xref: 'x', yref: 'y', text: 'ناحیه ۱: نفوذ بالا / قاچاق بالا (موبایل و الکترونیک)', showarrow: false, font: { size: 10, color: '#DC2626' } },
{ x: 25, y: 10, xref: 'x', yref: 'y', text: 'ناحیه ۲: نفوذ بالا / قاچاق مهارشده (کالای رسمی)', showarrow: false, font: { size: 10, color: '#059669' } },
{ x: 6, y: 55, xref: 'x', yref: 'y', text: 'ناحیه ۳: قاچاق سنتی شدید (پوشاک و لوازم خانگی)', showarrow: false, font: { size: 10, color: '#D97706' } },
{ x: 6, y: 10, xref: 'x', yref: 'y', text: 'ناحیه ۴: ایکامرس نوپا (FMCG و لبنیات)', showarrow: false, font: { size: 10, color: '#2563EB' } }
]
};
Plotly.newPlot('plotly-quadrant-intel', [traceQuad], layoutQuad, { responsive: true, displayModeBar: false });
// 2. Channels Mix Stacked Bar
const traceEcom = { x: secNames, y: ind.map(d => d.ecommerce_share_percent), name: 'ایکامرس رسمی', type: 'bar', marker: { color: '#2563EB' } };
const traceSoc = { x: secNames, y: ind.map(d => d.social_commerce_share_percent), name: 'اینستاگرام و سوشال', type: 'bar', marker: { color: '#8B5CF6' } };
const traceTrad = { x: secNames, y: ind.map(d => d.traditional_offline_share_percent), name: 'بازار سنتی فیزیکی', type: 'bar', marker: { color: '#CBD5E1' } };
const layoutChannels = {
barmode: 'stack',
font: defaultFont,
paper_bgcolor: '#FFFFFF',
plot_bgcolor: '#F8FAFC',
margin: { l: 40, r: 20, t: 30, b: 40 },
legend: { orientation: 'h', y: 1.15, x: 0.5, xanchor: 'center' },
xaxis: { gridcolor: '#E2E8F0' },
yaxis: { range: [0, 100], gridcolor: '#E2E8F0', title: 'درصد از کل بازار صنعت' }
};
Plotly.newPlot('plotly-channels-intel', [traceEcom, traceSoc, traceTrad], layoutChannels, { responsive: true, displayModeBar: false });
}
// Global initialization
document.addEventListener('DOMContentLoaded', () => {
renderExecutives();
renderBrandsTable();
renderPlaybook();
renderFailures();
renderIndustryBreakdown();
searchIntelVault();
initIntelPlotlyCharts();
if (window.lucide) lucide.createIcons();
});