/* tkpurchase-workreport.js - Work report monitoring */
let reportPage = 1;
const reportLimit = 20;
async function loadCompaniesForFilter() {
try {
const r = await api('/partners?limit=100');
const list = r.data || [];
const sel = document.getElementById('filterCompany');
list.forEach(c => {
const opt = document.createElement('option');
opt.value = c.id;
opt.textContent = c.company_name;
sel.appendChild(opt);
});
} catch(e) { console.warn('Load companies error:', e); }
}
async function loadReports() {
const companyId = document.getElementById('filterCompany').value;
const dateFrom = document.getElementById('filterDateFrom').value;
const dateTo = document.getElementById('filterDateTo').value;
const confirmed = document.getElementById('filterConfirmed').value;
let query = `?page=${reportPage}&limit=${reportLimit}`;
if (companyId) query += '&company_id=' + companyId;
if (dateFrom) query += '&date_from=' + dateFrom;
if (dateTo) query += '&date_to=' + dateTo;
if (confirmed) query += '&confirmed=' + confirmed;
try {
const r = await api('/work-reports' + query);
renderReportTable(r.data || [], r.total || 0);
} catch(e) {
console.warn('Report load error:', e);
document.getElementById('reportTableBody').innerHTML = '
| 로딩 실패 |
';
}
}
function renderReportTable(list, total) {
const tbody = document.getElementById('reportTableBody');
if (!list.length) {
tbody.innerHTML = '| 업무현황이 없습니다 |
';
document.getElementById('reportPagination').innerHTML = '';
return;
}
tbody.innerHTML = list.map(r => {
const progressColor = r.progress_rate >= 80 ? 'bg-emerald-500' : r.progress_rate >= 50 ? 'bg-blue-500' : r.progress_rate >= 20 ? 'bg-amber-500' : 'bg-red-500';
const confirmedBadge = r.confirmed_at
? '확인'
: '';
return `
| ${formatDate(r.report_date || r.created_at)} |
${escapeHtml(r.company_name || '')} |
${escapeHtml(r.work_content || '')} |
${r.actual_workers || 0}명 |
|
${escapeHtml(r.issues || '')} |
${confirmedBadge} |
|
`;
}).join('');
// Pagination
const totalPages = Math.ceil(total / reportLimit);
renderReportPagination(totalPages);
}
function renderReportPagination(totalPages) {
const container = document.getElementById('reportPagination');
if (totalPages <= 1) { container.innerHTML = ''; return; }
let html = '';
if (reportPage > 1) {
html += ``;
}
for (let i = 1; i <= totalPages; i++) {
if (i === reportPage) {
html += ``;
} else if (Math.abs(i - reportPage) <= 2 || i === 1 || i === totalPages) {
html += ``;
} else if (Math.abs(i - reportPage) === 3) {
html += '...';
}
}
if (reportPage < totalPages) {
html += ``;
}
container.innerHTML = html;
}
function goToReportPage(p) {
reportPage = p;
loadReports();
}
async function confirmReport(id) {
if (!confirm('이 업무현황을 확인 처리하시겠습니까?')) return;
try {
await api('/work-reports/' + id + '/confirm', { method: 'PUT' });
showToast('확인 처리되었습니다');
loadReports();
} catch(e) {
showToast(e.message || '확인 처리 실패', 'error');
}
}
async function viewReportDetail(id) {
try {
const r = await api('/work-reports/' + id);
const d = r.data || r;
const progressColor = d.progress_rate >= 80 ? 'bg-emerald-500' : d.progress_rate >= 50 ? 'bg-blue-500' : d.progress_rate >= 20 ? 'bg-amber-500' : 'bg-red-500';
const html = `
업체
${escapeHtml(d.company_name || '')}
보고일
${formatDateTime(d.report_date || d.created_at)}
실투입 인원
${d.actual_workers || 0}명
작업내용
${escapeHtml(d.work_content || '-')}
${d.issues ? `
이슈사항
${escapeHtml(d.issues)}
` : ''}
${d.next_plan ? `
향후 계획
${escapeHtml(d.next_plan)}
` : ''}
확인 상태
${d.confirmed_at ? '확인완료 ' + formatDateTime(d.confirmed_at) : '미확인'}
${d.confirmed_by_name ? `
확인자
${escapeHtml(d.confirmed_by_name)}
` : ''}
${!d.confirmed_at ? `
` : ''}`;
document.getElementById('reportDetailContent').innerHTML = html;
document.getElementById('reportDetailPanel').classList.remove('hidden');
document.getElementById('reportDetailPanel').scrollIntoView({ behavior: 'smooth', block: 'start' });
} catch(e) {
showToast('상세 정보를 불러올 수 없습니다', 'error');
}
}
function closeReportDetail() {
document.getElementById('reportDetailPanel').classList.add('hidden');
}
function initWorkReportPage() {
if (!initAuth()) return;
// Set default date range to this month
const now = new Date();
const firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
document.getElementById('filterDateFrom').value = firstDay.toISOString().substring(0, 10);
document.getElementById('filterDateTo').value = now.toISOString().substring(0, 10);
loadCompaniesForFilter();
loadReports();
}