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()
}
}
function loadBaseCode(mainCd) {
return fetch(`/kngil/bbs/adm_comp.php?action=base_code&main_cd=${mainCd}`)
.then(res => res.json())
.then(json => {
if (json.status !== 'success') {
throw new Error(json.message || '공통코드 로딩 실패')
}
return json.items
})
}
/* =================================================
faq 팝업 (슈퍼관리자 전용)
================================================= */
export function openfaqPopup() {
if (w2ui.faqGrid) {
w2ui.faqGrid.destroy()
}
w2popup.open({
title: 'FAQ 내용등록',
width: 900,
height: 600,
modal: true,
body: `
`,
onOpen(event) {
event.onComplete = () => {
// 1. 그리드를 생성합니다.
createfaqGrid('#faqGrid');
// 1. 추가 버튼
document.getElementById('faqAdd').onclick = () => {
const g = w2ui.faqGrid
if (!g) return
// 신규 row용 recid (음수로 충돌 방지)
const newRecid = -Date.now()
g.add({
fa_subject: '',
recid: newRecid,
__isNew: true, // ⭐ 신규 플래그
fa_content: ''
}, true)
g.select(newRecid)
g.scrollIntoView(newRecid)
};
// 삭제 버튼 이벤트 연결
const removeBtn = document.getElementById('faqRemove');
if (removeBtn) {
removeBtn.onclick = () => {
const g = w2ui.faqGrid;
if (!g) return;
// 1. 선택된 행 가져오기
const sel = g.getSelection();
if (!sel.length) {
alert('삭제할 항목을 선택하세요.');
return;
}
// 2. faq 추출
const ids = sel
.map(recid => g.get(recid)?.fa_id)
.filter(Boolean);
if (!ids.length) {
alert('삭제 가능한 항목이 없습니다.');
return;
}
// 3. 브라우저 기본 확인창 사용 (가장 확실함)
if (confirm(`선택한 ${ids.length}개의 상품을 삭제하시겠습니까?`)) {
fetch('/kngil/bbs/adm_faq_popup_delete.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'delete', ids: ids })
})
.then(res => {
// 서버 응답이 오면 일단 텍스트로 받아서 확인
return res.text();
})
.then(text => {
try {
const json = JSON.parse(text);
if (json.status === 'success') {
alert('삭제 완료');
loadfaqData(); // 목록 새로고침
} else {
alert(json.message || '삭제 실패');
}
} catch (e) {
console.error('응답 파싱 에러:', text);
alert('서버 응답 처리 중 오류가 발생했습니다.');
}
})
.catch(err => {
console.error('통신 에러:', err);
alert('서버와 통신할 수 없습니다.');
});
}
};
}
// 3. 저장 버튼
document.getElementById('faqSave').onclick = () => {
// 현재 사용 중인 grid
const g = w2ui.faqGrid
if (!g) return
const changes = g.getChanges()
if (!changes.length) {
w2alert('변경된 내용이 없습니다.')
return
}
const inserts = []
const updates = []
changes.forEach(c => {
const rec = g.get(c.recid)
if (!rec) return
// 🔥 핵심: 원본 rec + 변경값 c 병합
const merged = { ...rec, ...c }
if (rec.__isNew) {
// INSERT → 전체 row 필요
inserts.push(merged)
} else {
// UPDATE → PK + 변경 컬럼
updates.push({
fa_id : merged.fa_id,
fa_subject : merged.fa_subject ,
fa_content : merged.fa_content ,
use_yn : merged.use_yn,
sq_no : merged.sq_no
})
}
})
console.log('INSERTS', inserts)
console.log('UPDATES', updates)
fetch('/kngil/bbs/adm_faq_popup_save.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'save',
inserts,
updates
})
})
.then(res => {
// 서버가 준 응답이 괜찮은지 확인
if (!res.ok) throw new Error('서버 응답 오류');
return res.json(); // 처음부터 깔끔하게 데이터로 읽기
})
.then(json => {
// 이제 json.status를 바로 쓸 수 있습니다.
if (json.status === 'success') {
w2alert('저장 완료');
w2ui.faqGrid.save(); // 빨간 삼각형 없애기
loadfaqData(); // 목록 새로고침
} else {
w2alert(json.message || '저장 실패');
}
})
.catch(err => {
console.error('에러 발생:', err);
w2alert('처리 중 오류가 발생했습니다.');
});
};
}
}
})
}
//faq 그리드
export async function createfaqGrid(boxId) {
if (w2ui.faqGrid) w2ui.faqGrid.destroy();
destroyGrid('faqGrid')
const authItems = await loadBaseCode('BS210')
const grid = new w2grid({
name: 'faqGrid',
box: boxId,
recordHeight: 80, // ⭐ 행 높이를 80px로 설정 (3줄 정도 높이)
show: {
footer: true,
selectColumn: true,
lineNumbers: true // 행 번호를 표시하면 디버깅이 편합니다.
},
columns: [
/*
{ field: 'row_status', text: ' ', size: '30px', attr: 'align=center',
render: function (record) {
// 1. 신규 추가된 행 (recid가 임시값이거나 DB에 없는 경우)
if (record.is_new) return 'I';
// 2. 수정된 데이터 (w2ui.changes 객체가 존재하는 경우)
if (record.w2ui && record.w2ui.changes) return 'U';
// 3. 일반 상태
return ' ';
}
},
*/
{ field: 'fa_id', text: 'FAID', size: '60',attr: 'align=center',style: 'text-align: center', attr: 'align=center' ,hidden: true,sortable: true}, // name 아님!
{ field: 'fa_subject', text: '질문', size: '300', editable: { type: 'text' }
,render: function(record) {
if (!record.fa_content) return '';
// 엔터값(\n)을 브라우저가 인식하는
로 바꿔서 리턴
return record.fa_content.replace(/\n/g, '
');
}
}, // name 아님!
{ field: 'fa_content', text: '답글', size: '300', editable: { type: 'text' } }, // volume 아님!
{ field: 'sq_no', text: '순번', size: '60', attr: 'align=center',style: 'text-align: center',render: 'number:0', editable: { type: 'int' } ,sortable: true}, //
{ field: 'use_yn', text: '사용여부', size: '80px',attr: 'align=center',style: 'text-align: center', attr: 'align=center',
editable: { type: 'list', items: authItems, filter: false ,showAll: true } ,
render(record) {
const item = authItems.find(i => i.id === record.use_yn)
return item ? item.text : record.use_yn
},sortable: true
},
{ field: 'rmks', text: '비고', size: '200px', attr: 'align=center', editable: { type: 'text' } } // memo 아님!
],
records: [] // 처음엔 비워둠
});
// 데이터 호출 함수 실행
loadfaqData();
return grid;
}
// 데이터를 서버에서 불러오는 함수
async function loadfaqData() {
try {
w2ui.faqGrid.lock('조회 중...', true);
const response = await fetch('/kngil/bbs/adm_faq_popup.php'); // PHP 파일 호출
const data = await response.json();
w2ui.faqGrid.clear();
w2ui.faqGrid.add(data);
w2ui.faqGrid.unlock();
} catch (e) {
console.error(e);
w2ui.faqGrid.unlock();
w2alert('데이터 로드 실패');
}
}