</main></div><script src="<?=BASE_URL?>assets/js/app.js"></script>
<script>
(function(){
    // 사이드바 토글
    var sb=document.getElementById('mainSidebar'),btn=document.getElementById('sidebarToggle'),mc=document.querySelector('.main-content'),icon=document.getElementById('toggleIcon');
    if(sb&&btn){
        var key='sidebar_collapsed';
        function applySidebar(c){
            if(mc){mc.style.marginLeft=c?'60px':'260px';mc.style.width=c?'calc(100% - 60px)':'calc(100% - 260px)';}
            if(icon)icon.style.transform=c?'rotate(180deg)':'rotate(0deg)';
        }
        if(localStorage.getItem(key)==='1'){sb.classList.add('collapsed');applySidebar(true);}
        btn.addEventListener('click',function(){
            sb.classList.toggle('collapsed');
            var c=sb.classList.contains('collapsed');
            applySidebar(c);
            localStorage.setItem(key,c?'1':'0');
        });
    }
    // textarea auto resize
    function autoResize(t){
        var cs=getComputedStyle(t);
        var lh=parseFloat(cs.lineHeight)||parseFloat(cs.fontSize)*1.5;
        var pt=parseFloat(cs.paddingTop)+parseFloat(cs.paddingBottom);
        var rows=parseInt(t.getAttribute('rows'))||1;
        var minH=rows*lh+pt;
        var lines=(t.value.match(/\n/g)||[]).length+1;
        var contentH=lines*lh+pt;
        t.style.height=Math.max(minH,contentH)+'px';
        t.style.overflow='hidden';
    }
    document.addEventListener('input',function(e){if(e.target.tagName==='TEXTAREA')autoResize(e.target);});
    // 금액 입력 천단위 콤마 — 전체 적용
    function applyMoneyFormat(inp) {
        if (inp.dataset.moneyApplied) return;
        inp.dataset.moneyApplied = '1';
        if (inp.type === 'number') { inp.type = 'text'; inp.inputMode = 'numeric'; }
        var v = inp.value.replace(/[^0-9]/g,'');
        if (v) inp.value = parseInt(v).toLocaleString();
        inp.addEventListener('input', function(){
            var cur = this.selectionStart, oldLen = this.value.length;
            var v = this.value.replace(/[^0-9]/g,'');
            this.value = v ? parseInt(v).toLocaleString() : '';
            try { var d = this.value.length - oldLen; this.setSelectionRange(cur+d, cur+d); } catch(e) {}
        });
        var frm = inp.closest('form');
        if (frm && !frm._moneyHooked) {
            frm._moneyHooked = true;
            frm.addEventListener('submit', function(){
                this.querySelectorAll('[data-money-applied]').forEach(function(i){ i.value = i.value.replace(/,/g,''); });
            });
        }
    }
    // name 기반 자동 적용
    document.querySelectorAll('input[name="revenue"],input[name="cost"],input[name*="amount"],input[name*="price"],input[name*="profit"],input[name*="target_amount"]').forEach(applyMoneyFormat);
    // policy_settings: rule_value 중 금액 키만
    document.querySelectorAll('input[name="rule_value"]').forEach(function(inp){
        var row = inp.closest('tr') || inp.closest('form');
        if (!row) return;
        var keyCell = row.querySelector('td:first-child');
        var keyText = keyCell ? keyCell.textContent.trim() : '';
        if (/target|threshold|amount|revenue|profit|price|cost/i.test(keyText)) {
            var val = inp.value.replace(/[^0-9]/g,'');
            if (val && val.length >= 4) applyMoneyFormat(inp);
        }
    });
    // data-money 속성 수동 지정
    document.querySelectorAll('[data-money]').forEach(applyMoneyFormat);
})();
</script>
</body></html>
