/ ===== 全局状态与通用工具 ===== /
window.S = {
meta: {name:"", type:"park", climate:"华东", siteArea:0, population:0, waterAsGreen:false},
scale: {pxPerM:0, calibrated:false},
image: {dataUrl:"", w:0, h:0},
shapes: [], // {id,kind:'poly'|'line',cat,name,pts:[{x,y}],area,len}
land: {}, // catKey -> 面积 m²
plants: [], // 苗木配置行
priceBook: {}, // 导入的价格清单:规范化品种名 -> {price,spec,note}
cost: {} // 造价参数
};

const LS_KEY = "landscape_toolkit_v1";
let _seq = 1;
const uid = () => "s" + (_seq++) + Date.now().toString(36).slice(-3);

const $ = (s, r) => (r || document).querySelector(s);
const $$ = (s, r) => Array.from((r || document).querySelectorAll(s));

function fmt(n, d) {
if (!isFinite(n)) return "0";
d = d === undefined ? 2 : d;
return Number(n).toLocaleString("zh-CN", {minimumFractionDigits:d, maximumFractionDigits:d});
}
const fmt0 = n => fmt(n, 0);
const money = n => "¥" + fmt(n, 0);
const pct = n => fmt(n, 1) + "%";

let _toastTimer;
function toast(msg) {
const t = $("#toast");
t.textContent = msg;
t.classList.add("show");
clearTimeout(_toastTimer);
_toastTimer = setTimeout(() => t.classList.remove("show"), 2200);
}

const catByKey = k => LAND_CATS.find(c => c.k === k) || LAND_CATS[0];

/ ===== 存档 ===== /
function collectState() {
S.meta = {

name: $("#projName").value.trim(),
type: $("#projType").value,
climate: $("#climate").value,
siteArea: +$("#siteArea").value || 0,
population: +$("#population").value || 0,
waterAsGreen: $("#waterAsGreen").checked

};
S.cost = {

planting:+$("#cPlanting").value||0, maint:+$("#cMaint").value||0,
soil:+$("#cSoil").value||0, pave:+$("#cPave").value||0, road:+$("#cRoad").value||0,
water:+$("#cWater").value||0, mep:+$("#cMep").value||0, furn:+$("#cFurn").value||0,
risk:+$("#cRisk").value||0

};
return S;
}

function applyState(d) {
if (!d) return;
S.meta = Object.assign(S.meta, d.meta || {});
S.scale = Object.assign({pxPerM:0, calibrated:false}, d.scale || {});
S.image = Object.assign({dataUrl:"", w:0, h:0}, d.image || {});
S.shapes = d.shapes || [];
S.land = d.land || {};
S.plants = d.plants || [];
S.priceBook = d.priceBook || {};
S.cost = Object.assign(S.cost, d.cost || {});

$("#projName").value = S.meta.name || "";
$("#projType").value = S.meta.type || "park";
$("#climate").value = S.meta.climate || "华东";
$("#siteArea").value = S.meta.siteArea || 0;
$("#population").value = S.meta.population || 0;
$("#waterAsGreen").checked = !!S.meta.waterAsGreen;

const c = S.cost;
if (c.planting !== undefined) {

$("#cPlanting").value=c.planting; $("#cMaint").value=c.maint; $("#cSoil").value=c.soil;
$("#cPave").value=c.pave; $("#cRoad").value=c.road; $("#cWater").value=c.water;
$("#cMep").value=c.mep; $("#cFurn").value=c.furn; $("#cRisk").value=c.risk;

}
Measure.restore();
PlantUI.renderTable();
PlantUI.syncPriceBookState();
renderLandTable();
recalcAll();
}

function saveLocal(silent) {
try {

localStorage.setItem(LS_KEY, JSON.stringify(collectState()));
if (!silent) toast("已保存到本机(浏览器本地存储)");

} catch (e) {

toast("保存失败:图片过大,请用「工程存档 JSON」导出");

}
}