Bỏ qua nội dung
(function() {
function initBasSolarCalc() {
const elType = document.getElementById("bas-facility-type");
if (!elType) return;
const elPhase = document.getElementById("bas-phase");
const elMode = document.getElementById("bas-input-mode");
const elLabel = document.getElementById("bas-usage-label");
const elUsage = document.getElementById("bas-usage-value");
const elSelfUse = document.getElementById("bas-self-use");
const elPanelWp = document.getElementById("bas-panel-wp");
const elSunHours = document.getElementById("bas-sun-hours");
const elBackupKw = document.getElementById("bas-backup-kw");
const elBackupHours = document.getElementById("bas-backup-hours");
const elDod = document.getElementById("bas-dod");
const elRte = document.getElementById("bas-rte");
const elBtn = document.getElementById("bas-calc-btn");
const elSendBtn = document.getElementById("bas-send-survey-btn");
const elDailyNote = document.getElementById("bas-daily-kwh-note");
const elOutKwp = document.getElementById("bas-out-kwp");
const elOutKwpSub = document.getElementById("bas-out-kwp-sub");
const elOutPanels = document.getElementById("bas-out-panels");
const elOutPanelsSub = document.getElementById("bas-out-panels-sub");
const elOutInv = document.getElementById("bas-out-inv");
const elOutInvSub = document.getElementById("bas-out-inv-sub");
const elOutBatt = document.getElementById("bas-out-batt");
const elOutBattSub = document.getElementById("bas-out-batt-sub");
const elSurveyNote = document.getElementById("bas-survey-note");
const elTransferStatus = document.getElementById("bas-transfer-status");
const elSurveyForm = document.getElementById("bas-survey-form");
const elSurveyFeedback = document.getElementById("bas-survey-feedback");
const INV_SIZES = [5, 8, 10, 15, 20, 30, 50];
let latestSummaryText = "";
function pickInverterSize(neededKw) {
for (let i = 0; i < INV_SIZES.length; i++) {
if (INV_SIZES[i] >= neededKw) {
return { label: `${INV_SIZES[i]} kW`, exact: false };
}
}
return { label: `>= ${Math.ceil(neededKw)} kW`, exact: true };
}
function syncFacilityDefaults() {
const opt = elType.options[elType.selectedIndex];
const sun = parseFloat(opt.getAttribute("data-sun") || "4.0");
elSunHours.value = sun.toFixed(1);
if (elMode.value === "bill_vnd") {
elLabel.textContent = "Hóa đơn tiền điện / tháng (VNĐ)";
if (parseFloat(elUsage.value) < 50000) elUsage.value = "4500000";
} else {
elLabel.textContent = "Sản lượng điện / tháng (kWh)";
if (parseFloat(elUsage.value) >= 50000) elUsage.value = "1500";
}
}
function calculate() {
const facilityName = elType.value;
const phase = elPhase.value;
const opt = elType.options[elType.selectedIndex];
const pr = parseFloat(opt.getAttribute("data-pr") || "0.80");
const avgRate = parseFloat(opt.getAttribute("data-rate") || "2800");
const rawUsage = Math.max(0, parseFloat(elUsage.value) || 0);
const monthlyKwh = elMode.value === "bill_vnd" ? rawUsage / avgRate : rawUsage;
const dailyKwh = monthlyKwh / 30;
const selfUseRatio = Math.min(1, Math.max(0, (parseFloat(elSelfUse.value) || 0) / 100));
const sunHours = Math.max(1, parseFloat(elSunHours.value) || 4.0);
const panelWp = Math.max(100, parseFloat(elPanelWp.value) || 585);
const targetDailyKwh = dailyKwh * selfUseRatio;
const rawKwp = targetDailyKwh / (sunHours * pr);
const panelCount = Math.max(1, Math.ceil((rawKwp * 1000) / panelWp));
const actualKwp = (panelCount * panelWp) / 1000;
const backupKw = Math.max(0, parseFloat(elBackupKw.value) || 0);
const backupHours = Math.max(0, parseFloat(elBackupHours.value) || 0);
const dodRatio = Math.min(0.95, Math.max(0.2, (parseFloat(elDod.value) || 80) / 100));
const rteRatio = Math.min(0.99, Math.max(0.5, (parseFloat(elRte.value) || 90) / 100));
const usableNeededKwh = backupKw * backupHours;
const nominalBattKwh = usableNeededKwh > 0 ? usableNeededKwh / (dodRatio * rteRatio) : 0;
const roundedBattKwh = nominalBattKwh > 0 ? Math.ceil(nominalBattKwh * 2) / 2 : 0;
const invType = (roundedBattKwh > 0 || backupKw > 0) ? "Hybrid lưu trữ" : "Hòa lưới bám tải";
const neededInvKw = Math.max(actualKwp / 1.15, backupKw * 1.15);
const invPick = pickInverterSize(neededInvKw);
elDailyNote.textContent = `Tiêu thụ ước tính: ${monthlyKwh.toFixed(0)} kWh/tháng (~${dailyKwh.toFixed(1)} kWh/ngày)`;
elOutKwp.textContent = `${actualKwp.toFixed(2)} kWp`;
elOutKwpSub.textContent = `Lý thuyết: ${rawKwp.toFixed(2)} kWp | PR: ${(pr * 100).toFixed(0)}%`;
elOutPanels.textContent = `${panelCount} tấm`;
elOutPanelsSub.textContent = `Loại ${panelWp} Wp | Nắng: ${sunHours.toFixed(1)} h/ngày`;
elOutInv.textContent = invPick.label;
elOutInvSub.textContent = `${invType} (${phase}) | Cần tối thiểu ~${neededInvKw.toFixed(1)} kW`;
if (roundedBattKwh > 0) {
elOutBatt.textContent = `${roundedBattKwh.toFixed(1)} kWh`;
elOutBattSub.textContent = `Tải ưu tiên ${backupKw.toFixed(1)} kW × ${backupHours.toFixed(0)}h (khả dụng ~${usableNeededKwh.toFixed(1)} kWh)`;
} else {
elOutBatt.textContent = `0 kWh`;
elOutBattSub.textContent = `Chưa chọn tải dự phòng khi mất điện`;
}
latestSummaryText = [
`Cơ sở: ${facilityName} | Lưới điện: ${phase}`,
`Tiêu thụ ước tính: ${monthlyKwh.toFixed(0)} kWh/tháng (~${dailyKwh.toFixed(1)} kWh/ngày)`,
`Dàn pin đề xuất: ${actualKwp.toFixed(2)} kWp (${panelCount} tấm × ${panelWp} Wp)`,
`Inverter gợi ý: ${invPick.label} (${invType}, ${phase})`,
`Pin lưu trữ danh định: ${roundedBattKwh.toFixed(1)} kWh (DoD ${(dodRatio * 100).toFixed(0)}%, dự phòng ${backupKw.toFixed(1)} kW trong ${backupHours.toFixed(0)}h)`
].join("\n");
}
function transferToSurveyForm() {
calculate();
const externalField = document.querySelector(
"textarea[name*='survey' i], textarea[name*='note' i], textarea[name*='message' i], textarea[id*='survey' i], textarea[id*='note' i]"
);
if (externalField && externalField !== elSurveyNote) {
externalField.value = latestSummaryText;
externalField.dispatchEvent(new Event("input", { bubbles: true }));
externalField.dispatchEvent(new Event("change", { bubbles: true }));
externalField.scrollIntoView({ behavior: "smooth", block: "center" });
if (elTransferStatus) {
elTransferStatus.textContent = "Đã chuyển cấu hình vào ô ghi chú form khảo sát trên trang.";
}
return;
}
if (elSurveyNote) {
elSurveyNote.value = latestSummaryText;
elSurveyNote.dispatchEvent(new Event("input", { bubbles: true }));
}
const surveyBox = document.getElementById("bas-survey-box");
if (surveyBox) {
surveyBox.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
if (elTransferStatus) {
elTransferStatus.textContent = "Đã cập nhật cấu hình đề xuất vào phiếu khảo sát bên dưới.";
}
}
elType.addEventListener("change", function() {
syncFacilityDefaults();
calculate();
});
if (elMode) {
elMode.addEventListener("change", function() {
syncFacilityDefaults();
calculate();
});
}
[elPhase, elUsage, elSelfUse, elPanelWp, elSunHours, elBackupKw, elBackupHours, elDod, elRte].forEach(function(input) {
if (!input) return;
input.addEventListener("input", calculate);
input.addEventListener("change", calculate);
});
if (elBtn) elBtn.addEventListener("click", calculate);
if (elSendBtn) elSendBtn.addEventListener("click", transferToSurveyForm);
if (elSurveyForm) {
elSurveyForm.addEventListener("submit", function(e) {
e.preventDefault();
if (elSurveyNote && !elSurveyNote.value.trim()) {
elSurveyNote.value = latestSummaryText;
}
if (elSurveyFeedback) {
elSurveyFeedback.textContent = "Đã ghi nhận thông tin cấu hình trên trình duyệt. Hãy nối form này với hệ thống nhận yêu cầu khảo sát của BinhAnSolar.vn.";
}
});
}
syncFacilityDefaults();
calculate();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initBasSolarCalc);
} else {
initBasSolarCalc();
}
})();