// ═══════════════════════════════════════════════════════════════════
// SURVEY PAGE — Lapangan survey management
// ═══════════════════════════════════════════════════════════════════
function renderSurveyPage() {
const search = (document.getElementById('survey-search')?.value || '').toLowerCase().trim();
const statusF = document.getElementById('survey-filter-status')?.value || 'all';
const programF = document.getElementById('survey-filter-program')?.value || 'all';
const filtered = APP_STATE.surveys.filter(s => {
const matchS = !search || s.namaOfficer.toLowerCase().includes(search) || s.daerah.toLowerCase().includes(search) || s.issueId.toLowerCase().includes(search) || s.temuanUtama.toLowerCase().includes(search);
const matchSt = statusF === 'all' || s.status === statusF;
const matchP = programF === 'all' || s.programId === programF;
return matchS && matchSt && matchP;
});
const stats = {
total: APP_STATE.surveys.length,
verified: APP_STATE.surveys.filter(s => s.status === 'verified').length,
submitted: APP_STATE.surveys.filter(s => s.status === 'submitted').length,
draft: APP_STATE.surveys.filter(s => s.status === 'draft').length
};
document.getElementById('survey-body').innerHTML = `
${stats.total}
Total Survey
${stats.verified}
Diverifikasi
${stats.submitted}
Menunggu Verifikasi
${filtered.length === 0
? `📋
Tidak ada survey
Tidak ada survey yang cocok dengan filter yang dipilih
`
: filtered.map(s => renderSurveyCard(s)).join('')
}
`;
}
function renderSurveyCard(survey) {
const issue = APP_STATE.issues.find(i => i.id === survey.issueId);
const prog = getProgramById(survey.programId);
const skorColors = ['','var(--red)','var(--orange)','var(--amber)','var(--green)','var(--blue)'];
const skorLabel = ['','Sangat Kritis','Kritis','Perlu Perhatian','Minor','Normal'];
return `
Temuan Utama
${survey.temuanUtama}
${survey.rekomendasi ? `
💡 Rekomendasi
${survey.rekomendasi.slice(0,120)}${survey.rekomendasi.length > 120 ? '...' : ''}
` : ''}
`;
}
function openSurveyDetail(surveyId) {
const s = APP_STATE.surveys.find(sv => sv.id === surveyId);
if (!s) return;
const issue = APP_STATE.issues.find(i => i.id === s.issueId);
const prog = getProgramById(s.programId);
const skorColors = ['','var(--red)','var(--orange)','var(--amber)','var(--green)','var(--blue)'];
const skorLabel = ['','Sangat Kritis — Tindakan Segera','Kritis','Perlu Perhatian Khusus','Minor — Tetap Perlu Ditangani','Kondisi Normal'];
document.getElementById('sd-title').textContent = `Survey Lapangan — ${s.id}`;
document.getElementById('sd-body').innerHTML = `
${getProgramBadge(s.programId)}
${{draft:'Draft',submitted:'Diajukan',verified:'Diverifikasi'}[s.status]}
${issue ? getSevBadge(issue.severity) : ''}
OFFICER SURVEI
${s.namaOfficer}
${s.jabatan}
DETAIL SURVEI
📅 ${formatDate(s.tanggalSurvey)}
📍 ${s.daerah}, ${s.provinsi}
${issue ? `
ISSUE TERKAIT
${issue.id} — ${issue.judul.slice(0,60)}${issue.judul.length>60?'...':''}
Klik untuk lihat detail issue →
` : ''}
📋 Temuan Utama
${s.temuanUtama}
${s.rekomendasi ? `
💡 Rekomendasi Tindak Lanjut
${s.rekomendasi}
` : ''}
${s.skorKeparahan ? `
Penilaian Keparahan
${s.skorKeparahan}
${skorLabel[s.skorKeparahan]}
Skala 1 (paling parah) — 5 (normal)
` : ''}
${s.verifiedBy ? `
✅
Diverifikasi oleh ${s.verifiedBy}
Survey ini telah divalidasi dan dapat dijadikan dasar tindak lanjut
` : `
⏳
Menunggu Verifikasi
Survey belum diverifikasi oleh atasan
`}
`;
openModal('survey-detail-modal');
}
function verifySurvey(surveyId) {
const s = APP_STATE.surveys.find(sv => sv.id === surveyId);
if (!s) return;
s.status = 'verified';
s.verifiedBy = 'Admin KSP';
showToast('Survey Diverifikasi', `${surveyId} berhasil diverifikasi`, 'success');
closeModal('survey-detail-modal');
renderSurveyPage();
}
// ─── NEW SURVEY MODAL ─────────────────────────────────────────────
function openNewSurveyModal(prefillIssueId) {
// Populate issue select
const issueSelect = document.getElementById('ns-issue');
const openIssues = APP_STATE.issues.filter(i => i.status !== 'resolved' && i.status !== 'closed');
issueSelect.innerHTML = '' +
openIssues.map(i => ``).join('');
// Set today's date
const today = new Date().toISOString().split('T')[0];
document.getElementById('ns-tanggal').value = today;
if (prefillIssueId) {
const issue = APP_STATE.issues.find(i => i.id === prefillIssueId);
if (issue) {
document.getElementById('ns-program').value = issue.programId;
document.getElementById('ns-daerah').value = issue.daerah;
}
}
openModal('new-survey-modal');
}
function submitNewSurvey(e) {
e.preventDefault();
const issueId = document.getElementById('ns-issue').value;
const issue = APP_STATE.issues.find(i => i.id === issueId);
const id = `SRV-${String(APP_STATE.surveys.length + 1).padStart(3,'0')}`;
const now = new Date().toISOString().split('T')[0];
const ts = new Date().toLocaleTimeString('id-ID') + ' WIB';
const newSurvey = {
id, issueId: issueId || null,
programId: document.getElementById('ns-program').value,
namaOfficer: document.getElementById('ns-officer').value,
jabatan: document.getElementById('ns-jabatan').value,
tanggalSurvey:document.getElementById('ns-tanggal').value,
daerah: document.getElementById('ns-daerah').value,
provinsi: issue?.provinsi || '',
status: 'submitted',
temuanUtama: document.getElementById('ns-temuan').value,
rekomendasi: document.getElementById('ns-rekomendasi').value,
skorKeparahan: null, verifiedBy: null, createdAt: now
};
APP_STATE.surveys.push(newSurvey);
// Update issue if linked
if (issue && !issue.surveyIds.includes(id)) {
issue.surveyIds.push(id);
issue.timeline.push({ time: ts, action: `Survey lapangan dibuat oleh ${newSurvey.namaOfficer}`, actor: newSurvey.namaOfficer, type: 'survey' });
issue.status = 'in_progress';
}
document.getElementById('new-survey-form').reset();
closeModal('new-survey-modal');
updateIssuesBadge();
if (APP_STATE.currentPage === 'survey') renderSurveyPage();
showToast('Survey Dibuat', `${id} berhasil diajukan`, 'success');
}