import { w2grid, w2ui, w2popup, w2alert , w2confirm} from 'https://cdn.jsdelivr.net/gh/vitmalina/w2ui@master/dist/w2ui.es6.min.js' export function bindProductPopupEvents() { const g = w2ui.productGrid //행 추가 document.getElementById('prodAdd').onclick = () => { g.add({ recid: g.records.length + 1, name: '', volume: 0, price: 0, use_yn: 'Y' }) } //행 삭제 document.getElementById('prodRemove').onclick = () => { const sel = g.getSelection() if (!sel.length) { w2alert('삭제할 항목을 선택하세요.') return } g.remove(...sel) } //저장 document.getElementById('prodSave').onclick = () => { console.log('상품 저장', g.records) w2alert('저장 처리 예정') } } // [추가] 공통 함수 export function commonAdd(gridName, defaultData = {}) { const grid = w2ui[gridName]; if (!grid) return; // 중복되지 않는 recid 생성 (신규 행 식별용) const newRecid = 'new_' + Date.now(); grid.add({ recid: newRecid, is_new: true, // ★ 핵심: 이 플래그가 있어야 commonSave에서 검색됨 ...defaultData // 아래 2번 단계에서 전달받은 초기값들이 여기에 들어감 }); grid.scrollIntoView(newRecid); } // [행 삭제] 공통 함수 (체크박스 선택 기준) export function commonRemove(gridName) { // 인자가 문자열이 아닌 객체로 들어오는지 확인용 if (typeof gridName !== 'string') { console.error("그리드 이름은 반드시 문자열이어야 합니다. 전달된 값:", gridName); return; } const grid = w2ui[gridName]; if (!grid) { console.error(`그리드 '${gridName}'을 찾을 수 없습니다.`, Object.keys(w2ui)); return; } const sel = grid.getSelection(); if (sel.length === 0) { w2alert('삭제할 항목을 선택하세요.'); return; } w2confirm(`${sel.length}건을 삭제하시겠습니까?`).yes(() => { grid.remove(...sel); }); } // /js/adm_common.js export async function commonRemoveWithParams(gridName, deleteUrl) { const grid = w2ui[gridName]; // 1. 그리드 존재 확인 (문자열로 잘 들어왔는지 체크) if (!grid) { console.error(`[삭제실패] ${gridName} 그리드를 찾을 수 없습니다.`, Object.keys(w2ui)); return; } const sel = grid.getSelection(); if (sel.length === 0) { w2alert('삭제할 항목을 선택하세요.'); return; } w2confirm(`${sel.length}건을 삭제하시겠습니까?`).yes(async () => { try { grid.lock('삭제 중...', true); // 2. 프로시저에 필요한 데이터(member_id, sq_no) 추출 const selectedRows = sel.map(id => grid.get(id)); const response = await fetch(deleteUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ items: selectedRows }) // 통째로 전달 }); const responseText = await response.text(); // JSON 변환 전에 텍스트로 먼저 받음 console.log("서버 실제 응답 원문:", responseText); // 여기서 HTML 에러 내용 확인 가능 const result = await response.json(); if (result.status === 'success') { grid.remove(...sel); w2alert('성공적으로 삭제되었습니다.'); } else { throw new Error(result.message); } } catch (e) { w2alert('삭제 오류: ' + e.message); console.error("서버 응답이 JSON이 아닙니다:", responseText); w2alert("서버 오류가 발생했습니다. 콘솔을 확인하세요."); } finally { grid.unlock(); } }); } // [수정 및 저장] 공통 함수 export async function commonSave(gridName, saveUrl) { const grid = w2ui[gridName]; if (!grid) { console.error(`[저장 실패] '${gridName}' 그리드를 찾을 수 없습니다.`); return; } grid.save(); // 에디터에서 입력 중인 값 확정 // 변경된 내용이 있는지 확인 if (grid.getChanges().length === 0) { w2alert('변경사항이 없습니다.'); return; } try { grid.lock('저장 중...', true); const response = await fetch(saveUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ items: grid.records }) // 전체 데이터를 보낼지 changes만 보낼지 선택 }); const result = await response.json(); if (result.status === 'success') { w2alert('저장되었습니다.'); grid.mergeChanges(); // 그리드의 변경 표시(빨간색) 제거 } else { throw new Error(result.message); } } catch (e) { w2alert('저장 오류: ' + e.message); } finally { grid.unlock(); } }