const http = require("http"); const fs = require("fs"); const path = require("path"); const XLSX = require("xlsx"); const PORT = process.env.PORT || 3000; const DATA_FILE = path.join(__dirname, "123-1.xlsx"); let cachedData = null; function loadData() { if (cachedData) return cachedData; try { const wb = XLSX.readFile(DATA_FILE); const ws = wb.Sheets["Проблемная B2G июнь"]; const rows = XLSX.utils.sheet_to_json(ws); cachedData = rows.map(r => ({ "Регион": r["Регион"] || "", "Сервер": r["Сервер"] || "", "Район": r["Район CRM"] || "", "Город": r["Город"] || "", "Адрес": r["Адрес CRM"] || "", "Сегмент": r["Сегмент"] || "", "Категория": r["Категория"] || "", "Лицевой счет": r["Лицевой счет"] || "", "Абонент": r["Абонент"] || "", "БИН": r["БИН/ИИН"] || "", "Контракт": r["номер контракта crm"] || "", "Начало": r["начало действия crm"] || "", "Конец": r["Макс дата образования ДЗ"] || "", "ДЗ итого": r["Дебиторская задолженность(Итого)"] || 0, "ПДЗ": r["ПДЗ"] || 0, "ИТОГО ПДЗ": r["ИТОГО ПДЗ на 01.06.2026"] || r["ИТОГО ПДЗ"] || 0, "ЛС CRM": r["ЛС CRM 2.0"] || "", "Менеджер": r["персональный менеджер"] || "", "Период": r["период внесения"] || "", "Статус С/Д": r["С/Д июнь"] || r["С/Д май"] || "", "Статус": r["Статусы июнь"] || r["Статусы май"] || "", "Отработка": r["Текущий статус июнь"] || r["Текущий статус май"] || "", "Причина": r["Причина июнь"] || r["Причина май"] || "", "Ход работы": r["Ход работы июнь"] || "", "Сотрудник": r["Сотрудник"] || "" })).filter(r => r["Абонент"] && r["Лицевой счет"]); return cachedData; } catch (e) { console.error("Ошибка загрузки данных:", e.message); return []; } } function body(req) { return new Promise((resolve) => { let s = ""; req.on("data", (c) => (s += c)); req.on("end", () => { try { resolve(JSON.parse(s || "{}")); } catch (_) { resolve({}); } }); }); } http.createServer(async (req, res) => { if (req.method === "GET" && req.url === "/api/data") { const data = loadData(); res.writeHead(200, { "Content-Type": "application/json; charset=utf-8", "Access-Control-Allow-Origin": "*" }); return res.end(JSON.stringify(data)); } let file = req.url === "/" ? "/index.html" : req.url.split("?")[0]; const full = path.join(__dirname, file); if (full.startsWith(__dirname) && fs.existsSync(full) && fs.statSync(full).isFile()) { const ext = path.extname(full); const type = ext === ".css" ? "text/css" : ext === ".js" ? "application/javascript" : ext === ".json" ? "application/json" : "text/html"; res.writeHead(200, { "Content-Type": type + "; charset=utf-8" }); return res.end(fs.readFileSync(full)); } res.writeHead(404); res.end("Not found"); }).listen(PORT, () => console.log("Сервер запущен на порту " + PORT));