import { w2grid, w2ui, w2popup, w2alert } from 'https://cdn.jsdelivr.net/gh/vitmalina/w2ui@master/dist/w2ui.es6.min.js' // 파일이 실제 존재하는지 확인 필수! 존재하지 않으면 아래 코드가 모두 멈춥니다. import { bindProductPopupEvents } from '/kngil/js/adm_common.js' export function openPurchaseHistoryPopup(memberId = '',isSuperAdmin='') { // 기존 그리드 안전하게 제거 if (w2ui.purchaseHistoryGrid) { w2ui.purchaseHistoryGrid.destroy(); } w2popup.open({ title: '서비스 구매 이력', width: 1100, height: 600, modal: true, body: `
회원 ID
구입일 ~
`, onOpen(event) { event.onComplete = () => { const searchBtn = document.getElementById('btnSearchHistory'); const inputs = { mid: document.getElementById('searchMemberId'), fdt: document.getElementById('searchFbuydt'), tdt: document.getElementById('searchTbuydt') }; // 1. 날짜 필드 캘린더 연결 (W2UI 스타일) if (inputs.fdt) inputs.fdt.type = 'date'; if (inputs.tdt) inputs.tdt.type = 'date'; // 2. 조회 버튼 클릭 if (searchBtn) { searchBtn.onclick = () => loadPurchaseHistoryData(); } // 3. 모든 입력창 엔터키 지원 Object.values(inputs).forEach(el => { if (el) el.onkeydown = (e) => { if (e.key === 'Enter') searchBtn.click(); }; }); createPurchaseHistoryGrid(memberId); loadPurchaseHistoryData(memberId); } } }); } function createPurchaseHistoryGrid(memberId) { new w2grid({ name: 'purchaseHistoryGrid', box: '#purchaseHistoryGrid', show: { //toolbar: true, footer: true, lineNumbers: true // 행 번호를 표시하면 디버깅이 편합니다. }, columns: [ { field: 'member_id', text: '회원ID', size: '80px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'sq_no', text: '순번', size: '50px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'user_nm', text: '구매자', size: '80px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'co_nm', text: '회사명', size: '100px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'bs_no', text: '사업자번호', size: '100px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'buy_dt', text: '구입일자', size: '80px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'itm_nm', text: '상품명', size: '80px', style: 'text-align: center', attr: 'align=center', sortable: true }, // --- 숫자/면적 데이터: 제목(center), 데이터(right) --- { field: 'area', text: '상품면적', size: '80px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'itm_qty', text: '수량', size: '80px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'itm_area', text: '면적', size: '100px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'add_area', text: '추가면적', size: '100px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'sum_area', text: '합계면적', size: '100px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'itm_amt', text: '단가', size: '100px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'dis_rt', text: '할인율', size: '80px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'buy_amt', text: '공급금액', size: '100px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'vat_amt', text: '부가세', size: '100px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, { field: 'sum_amt', text: '합계', size: '100px', render: 'number:0', style: 'text-align: center', attr: 'align=right', sortable: true }, // -------------------------------------------------- { field: 'end_dt', text: '종료일', size: '80px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'ok_yn', text: '승인여부', size: '80px', style: 'text-align: center', attr: 'align=center', sortable: true }, { field: 'rmks', text: '비고', size: '150px', style: 'text-align: center', attr: 'align=left', sortable: true } ] }); } async function loadPurchaseHistoryData() { try { const grid = w2ui.purchaseHistoryGrid; if (!grid) return; const sMid = document.getElementById('searchMemberId')?.value || ''; const sfdt = document.getElementById('searchFbuydt')?.value || ''; const stdt = document.getElementById('searchTbuydt')?.value || ''; grid.lock('조회 중...', true); const searchParams = new URLSearchParams(); searchParams.append('member_id', sMid); searchParams.append('fbuy_dt', sfdt); searchParams.append('tbuy_dt', stdt); const response = await fetch('/admin/api/purchase-history', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: searchParams }); const data = await response.json(); grid.clear(); if (data && data.length > 0) { // 1. 일반 데이터 추가 grid.add(data); // 2. 합계(Summary) 계산 let totals = { recid: 'summary', w2ui: { summary: true, style: 'background-color: #efefef; font-weight: bold;' }, user_nm: '합계', // 합계 문구를 표시할 컬럼 area: 0, itm_area: 0, add_area: 0, sum_area: 0, itm_amt: 0, buy_amt: 0, vat_amt: 0, sum_amt: 0 }; data.forEach(row => { totals.area += Number(row.area || 0); totals.itm_area += Number(row.itm_area || 0); totals.add_area += Number(row.add_area || 0); totals.sum_area += Number(row.sum_area || 0); totals.itm_amt += Number(row.itm_amt || 0); totals.buy_amt += Number(row.buy_amt || 0); totals.vat_amt += Number(row.vat_amt || 0); totals.sum_amt += Number(row.sum_amt || 0); }); // 3. 그리드 하단에 합계 행 주입 grid.summary = [totals]; } grid.unlock(); grid.refresh(); // 변경사항 반영 } catch (e) { console.error(e); if (w2ui.purchaseHistoryGrid) w2ui.purchaseHistoryGrid.unlock(); w2alert('조회 실패: ' + e.message); } } ////////////////////////////////////////////////////////////////////////