/* ============================================================ 期次区间选择器(工作台 / 后台共用) —— 一个输入框,点一下弹出「一个」面板,面板里一次选完「起」「止」两个月,不再弹两次。 布局(2026-09-29 你定的两排式): 第 1 排 起始 [年下拉][月下拉] 第 2 排 结束 [年下拉][月下拉] 第 3 排 清空 止=起 第 4 排 确定(主操作,占整行) 用法: mkPeriod(el, onChange) 把 el 变成期次区间输入框 PVAL(el) 取值 {f,t,fs,ts} 月序号 + "2026-01" 字符串 setPeriod(el, from, to) 赋值("2026-01" / "2026-12"),传空串清空 ============================================================ */ (function () { const pad = n => String(n).padStart(2, '0'); const m2s = m => (m == null || m === '' ? '' : Math.floor(m / 12) + '-' + pad(m % 12 || 12)); const s2m = s => { const m = /^(\d{4})-(\d{1,2})$/.exec((s || '').trim()); return m ? (+m[1]) * 12 + (+m[2]) : null; }; let box = null, curEl = null, st = { fy: 0, fm: 0, ty: 0, tm: 0 }; const Y0 = 2015, Y1 = 2035; function ensureBox() { if (box) return box; box = document.createElement('div'); box.className = 'pcal'; box.innerHTML = '
期次区间
' + '
起始
' + '
结束
' + '
' + '
' + '
' + '
' + '
' + '
'; document.body.appendChild(box); // 年 / 月 下拉(起始、结束各一组) box.querySelectorAll('select.py').forEach(sel => { for (let y = Y0; y <= Y1; y++) { const o = document.createElement('option'); o.value = y; o.textContent = y + ' 年'; sel.appendChild(o); } sel.onchange = () => { const k = sel.dataset.k; if (k === 'f') st.fy = +sel.value; else st.ty = +sel.value; fixOrder(); paint(); }; }); box.querySelectorAll('select.pmo').forEach(sel => { for (let m = 1; m <= 12; m++) { const o = document.createElement('option'); o.value = m; o.textContent = m + ' 月'; sel.appendChild(o); } sel.onchange = () => { const k = sel.dataset.k; if (k === 'f') st.fm = +sel.value; else st.tm = +sel.value; fixOrder(); paint(); }; }); box.querySelector('[data-a="clear"]').onclick = () => { setPeriod(curEl, '', ''); close(); }; box.querySelector('[data-a="same"]').onclick = () => { st.ty = st.fy; st.tm = st.fm; paint(); }; box.querySelector('[data-a="ok"]').onclick = () => { apply(); close(); }; document.addEventListener('mousedown', e => { if (!box.contains(e.target) && e.target !== curEl) close(); }); return box; } // 止早于起 → 自动把止拉到起(跟以前一样,不改规则) function fixOrder() { if (st.ty * 12 + st.tm < st.fy * 12 + st.fm) { st.ty = st.fy; st.tm = st.fm; } } function paint() { const F = st.fy * 12 + st.fm, T = st.ty * 12 + st.tm; box.querySelector('select.py[data-k="f"]').value = st.fy; box.querySelector('select.pmo[data-k="f"]').value = st.fm; box.querySelector('select.py[data-k="t"]').value = st.ty; box.querySelector('select.pmo[data-k="t"]').value = st.tm; box.querySelector('.pcal-prev').textContent = m2s(F) + ' ~ ' + m2s(T); } function apply() { if (!curEl) return; setPeriod(curEl, st.fy + '-' + pad(st.fm), st.ty + '-' + pad(st.tm)); } function open(el) { curEl = el; ensureBox(); const v = PVAL(el), now = new Date(); const fy = v.fs ? +v.fs.slice(0, 4) : now.getFullYear(); const fm = v.fs ? +v.fs.slice(5, 7) : now.getMonth() + 1; const ty = v.ts ? +v.ts.slice(0, 4) : fy; const tm = v.ts ? +v.ts.slice(5, 7) : fm; st = { fy, fm, ty, tm }; fixOrder(); paint(); const r = el.getBoundingClientRect(); box.style.display = 'block'; const W = 252, H = 210; let left = r.left, top = r.bottom + 4; if (left + W > window.innerWidth) left = Math.max(8, window.innerWidth - W - 10); if (top + H > window.innerHeight) top = Math.max(8, r.top - H - 4); box.style.left = left + 'px'; box.style.top = top + 'px'; } function close() { if (box) box.style.display = 'none'; } window.mkPeriod = function (el, onChange) { if (!el) return; el.classList.add('perinp'); el.readOnly = true; el.onclick = () => open(el); if (onChange) el._onChange = onChange; }; window.setPeriod = function (el, from, to) { if (!el) return; el.dataset.pf = from || ''; el.dataset.pt = to || ''; el.value = (from && to) ? (from === to ? from : from + ' ~ ' + to) : (from || ''); if (el._onChange) el._onChange(); }; window.PVAL = function (el) { const fs = (el && el.dataset.pf) || '', ts = (el && el.dataset.pt) || ''; return { fs, ts, f: s2m(fs), t: s2m(ts) }; }; // 样式(两个页面共用,不写进各自的