/* tkpurchase-partner-portal.js - Partner portal logic (2-step flow) */ let portalSchedules = []; let portalCheckins = {}; let partnerCompanyId = null; let companyWorkersCache = null; async function loadMySchedules() { try { const r = await api('/schedules/my'); portalSchedules = r.data || []; } catch(e) { console.warn('Load schedules error:', e); portalSchedules = []; } } async function loadMyCheckins() { try { const r = await api('/checkins/my'); const list = r.data || []; portalCheckins = {}; list.forEach(c => { if (c.schedule_id) portalCheckins[c.schedule_id] = c; }); } catch(e) { console.warn('Load checkins error:', e); portalCheckins = {}; } } async function loadCompanyWorkers() { if (companyWorkersCache) return companyWorkersCache; try { const r = await api('/partners/' + partnerCompanyId + '/workers'); companyWorkersCache = (r.data || []).filter(w => w.is_active !== 0); return companyWorkersCache; } catch(e) { console.warn('Load workers error:', e); companyWorkersCache = []; return []; } } async function renderScheduleCards() { await Promise.all([loadMySchedules(), loadMyCheckins()]); const container = document.getElementById('scheduleCards'); const noMsg = document.getElementById('noScheduleMessage'); if (!portalSchedules.length) { container.innerHTML = ''; noMsg.classList.remove('hidden'); return; } noMsg.classList.add('hidden'); container.innerHTML = portalSchedules.map(s => { const checkin = portalCheckins[s.id]; const isCheckedIn = checkin && !checkin.check_out_time; const isCheckedOut = checkin && checkin.check_out_time; // 2-step indicators const step1Class = checkin ? 'text-emerald-600' : 'text-gray-400'; const step2Class = isCheckedOut ? 'text-emerald-600' : 'text-gray-400'; return `

${escapeHtml(s.workplace_name || '작업장 미지정')}

${formatDate(s.start_date) === formatDate(s.end_date) ? formatDate(s.start_date) : formatDate(s.start_date) + ' ~ ' + formatDate(s.end_date)}
${s.work_description ? `

${escapeHtml(s.work_description)}

` : ''}
예상 ${s.expected_workers || 0}명 ${s.notes ? `${escapeHtml(s.notes)}` : ''}
1. 작업 시작
2. 작업 종료
${!checkin ? `

작업 시작

` : `
체크인 완료 (${formatTime(checkin.check_in_time)}) · ${checkin.actual_worker_count || 0}명
`}
${isCheckedIn ? `
` : ''} ${isCheckedOut ? `
작업 종료 완료 (${formatTime(checkin.check_out_time)})
` : ''}
`; }).join(''); } async function showCheckoutForm(checkinId, scheduleId) { const formContainer = document.getElementById('checkoutForm_' + checkinId); const toggleBtn = document.getElementById('checkoutFormToggle_' + checkinId); if (!formContainer) return; // 작업자 목록 로드 const workers = await loadCompanyWorkers(); const datalistHtml = workers.map(w => `