import { w2grid, w2ui, w2popup, w2alert }
from 'https://cdn.jsdelivr.net/gh/vitmalina/w2ui@master/dist/w2ui.es6.min.js'
/* -------------------------------------------------
공통 유틸
------------------------------------------------- */
function destroyGrid(name) {
if (w2ui[name]) w2ui[name].destroy()
}
/* -------------------------------------------------
서비스 등록 팝업
------------------------------------------------- */
export function openServiceRegisterPopup(ctx) {
destroyGrid('serviceGrid')
destroyGrid('productList')
w2popup.open({
title: '서비스 등록',
width: 1300,
height: 720,
modal: true,
body: `
`,
onOpen(event) {
event.onComplete = () => {
createServiceGrid()
createProductList()
// ✅ 구매일 조회 버튼 바인딩 (여기서!)
document.getElementById('btnSearchBuy').addEventListener('click', () => {
const buyDate = document.getElementById('buyDate').value
if (!buyDate) {
w2alert('구매일을 선택하세요')
return
}
loadExistingPurchase(ctx.memberId, buyDate)
})
}
}
})
}
/* -------------------------------------------------
서비스 Grid
------------------------------------------------- */
function createServiceGrid() {
new w2grid({
name: 'serviceGrid',
box: '#serviceGrid',
show: {
footer: true,
// selectColumn: true
},
columns: [
{ field: 'recid', text: 'NO', size: '50px', attr: 'align=center' },
{ field: 'itm_cd', text: '상품명', size: '100px' },
{ field: 'itm_area', text: '면적(m²)', size: '100px', editable: { type: 'int' } },
{ field: 'itm_amt', text: '금액', size: '100px', attr: 'align=right' },
{ field: 'itm_qty', text: '수량', size: '80px', editable: { type: 'int' } },
{ field: 'dis_rt', text: '할인율', size: '80px', editable: { type: 'int' } },
{ field: 'supply', text: '공급가액', size: '120px', attr: 'align=right' },
{ field: 'vat_amt', text: '부가세', size: '100px', attr: 'align=right' },
{ field: 'sum_amt', text: '결제금액', size: '120px', attr: 'align=right' },
{ field: 'end_dt', text: '만료일자', size: '110px', editable: { type: 'date' } },
{ field: 'rmks', text: '비고', size: '150px', editable: { type: 'text' } }
],
records: [],
onChange() {
calcSummary()
}
})
}
/* -------------------------------------------------
상품 목록
------------------------------------------------- */
function createProductList() {
new w2grid({
name: 'productList',
box: '#productList',
columns: [
{ field: 'name', text: '상품명', size: '120px' },
{ field: 'area', text: '제공량', size: '100px', attr: 'align=right' },
{ field: 'price', text: '단가', size: '100px', attr: 'align=right' }
],
records: [
{ recid: 1, name: '다이아', area: 10000000, price: 10000000 },
{ recid: 2, name: '골드', area: 1000000, price: 5000000 },
{ recid: 3, name: '실버', area: 100000, price: 1000000 }
],
onDblClick(event) {
const rec = this.get(event.recid)
addServiceFromProduct(rec)
}
})
}
/* -------------------------------------------------
상품 → 서비스 Grid 추가
------------------------------------------------- */
function addServiceFromProduct(p) {
const g = w2ui.serviceGrid
const recid = g.records.length + 1
const supply = p.price
const vat = Math.floor(supply * 0.1)
const total = supply + vat
g.add({
recid,
itm_cd: p.name,
itm_area: p.area,
itm_amt: p.price,
itm_qty: 1,
dis_rt: 0,
supply,
vat_amt: vat,
sum_amt: total,
end_dt: '',
rmks: ''
})
calcSummary()
}
/* -------------------------------------------------
기존 구매 불러오기
------------------------------------------------- */
function loadExistingPurchase(memberId, buyDate) {
fetch(`/admin/api/service?member_id=${memberId}&buy_date=${buyDate}`)
.then(res => res.json())
.then(json => {
if (json.status !== 'success') {
w2alert(json.message || '조회 실패')
return
}
const g = w2ui.serviceGrid
g.clear()
json.records.forEach((r, i) => {
g.add({
recid: i + 1,
itm_cd: r.itm_cd,
itm_area: Number(r.itm_area),
itm_amt: Number(r.itm_amt),
itm_qty: Number(r.itm_qty),
dis_rt: Number(r.dis_rt),
supply: Number(r.supply),
vat_amt: Number(r.vat_amt),
sum_amt: Number(r.sum_amt),
end_dt: r.end_dt,
rmks: r.rmks,
_existing: true
})
// 🔒 기존 구매 회색 처리
g.set(i + 1, {
w2ui: { style: 'background:#f3f3f3;color:#555' }
})
})
g.refresh()
calcSummary()
})
.catch(err => {
console.error(err)
w2alert('서버 오류')
})
}
/* -------------------------------------------------
합계 계산
------------------------------------------------- */
function calcSummary() {
let supply = 0
let vat = 0
let total = 0
w2ui.serviceGrid.records.forEach(r => {
supply += Number(r.supply || 0)
vat += Number(r.vat_amt || 0)
total += Number(r.sum_amt || 0)
})
document.getElementById('sumSupply').innerText = supply.toLocaleString()
document.getElementById('sumVat').innerText = vat.toLocaleString()
document.getElementById('sumTotal').innerText = total.toLocaleString()
}