/**
 * m-management.js — 관리함 모바일 페이지 로직
 */

var currentUser = null;
var issues = [];
var projects = [];
var filteredIssues = [];
var currentTab = 'in_progress';
var currentIssueId = null;
var rejectIssueId = null;

function cleanManagementComment(text) {
    if (!text) return '';
    return text.replace(/\[완료 반려[^\]]*\][^\n]*\n*/g, '').trim();
}

// ===== 초기화 =====
async function initialize() {
    currentUser = await mCheckAuth();
    if (!currentUser) return;

    await loadProjects();
    await loadIssues();
    renderBottomNav('management');
    hideLoading();
}

async function loadProjects() {
    try {
        var resp = await fetch(API_BASE_URL + '/projects/', {
            headers: { 'Authorization': 'Bearer ' + TokenManager.getToken() }
        });
        if (resp.ok) {
            projects = await resp.json();
            var sel = document.getElementById('projectFilter');
            sel.innerHTML = '<option value="">전체 프로젝트</option>';
            projects.forEach(function (p) {
                sel.innerHTML += '<option value="' + p.id + '">' + escapeHtml(p.project_name) + '</option>';
            });
        }
    } catch (e) { console.error('프로젝트 로드 실패:', e); }
}

async function loadIssues() {
    try {
        var resp = await fetch(API_BASE_URL + '/issues/admin/all', {
            headers: { 'Authorization': 'Bearer ' + TokenManager.getToken() }
        });
        if (resp.ok) {
            var all = await resp.json();
            var filtered = all.filter(function (i) { return i.review_status === 'in_progress' || i.review_status === 'completed'; });

            // 프로젝트별 순번
            filtered.sort(function (a, b) { return new Date(a.reviewed_at) - new Date(b.reviewed_at); });
            var groups = {};
            filtered.forEach(function (issue) {
                if (!groups[issue.project_id]) groups[issue.project_id] = [];
                groups[issue.project_id].push(issue);
            });
            Object.keys(groups).forEach(function (pid) {
                groups[pid].forEach(function (issue, idx) { issue.project_sequence_no = idx + 1; });
            });

            issues = filtered;
            filterIssues();
        }
    } catch (e) { console.error('이슈 로드 실패:', e); }
}

// ===== 탭 전환 =====
function switchTab(tab) {
    currentTab = tab;
    document.getElementById('tabInProgress').classList.toggle('active', tab === 'in_progress');
    document.getElementById('tabCompleted').classList.toggle('active', tab === 'completed');
    document.getElementById('additionalInfoBtn').style.display = tab === 'in_progress' ? 'flex' : 'none';
    filterIssues();
}

// ===== 통계 =====
function updateStatistics() {
    var pid = document.getElementById('projectFilter').value;
    var pi = pid ? issues.filter(function (i) { return i.project_id == pid; }) : issues;

    document.getElementById('totalCount').textContent = pi.length;
    document.getElementById('inProgressCount').textContent = pi.filter(function (i) { return i.review_status === 'in_progress' && !i.completion_requested_at; }).length;
    document.getElementById('pendingCompletionCount').textContent = pi.filter(function (i) { return i.review_status === 'in_progress' && i.completion_requested_at; }).length;
    document.getElementById('completedCount').textContent = pi.filter(function (i) { return i.review_status === 'completed'; }).length;
}

// ===== 필터 =====
function filterIssues() {
    var pid = document.getElementById('projectFilter').value;
    filteredIssues = issues.filter(function (i) {
        if (i.review_status !== currentTab) return false;
        if (pid && i.project_id != pid) return false;
        return true;
    });
    filteredIssues.sort(function (a, b) { return new Date(b.report_date) - new Date(a.report_date); });
    renderIssues();
    updateStatistics();
}

// ===== 이슈 상태 =====
function getIssueStatus(issue) {
    if (issue.review_status === 'completed') return 'completed';
    if (issue.completion_requested_at) return 'pending_completion';
    if (issue.expected_completion_date) {
        var diff = (new Date(issue.expected_completion_date) - new Date()) / 86400000;
        if (diff < 0) return 'overdue';
        if (diff <= 3) return 'urgent';
    }
    return 'in_progress';
}

function getStatusBadgeHtml(status) {
    var map = {
        'in_progress': '<span class="m-badge in-progress"><i class="fas fa-cog"></i> 진행 중</span>',
        'urgent': '<span class="m-badge urgent"><i class="fas fa-exclamation-triangle"></i> 긴급</span>',
        'overdue': '<span class="m-badge overdue"><i class="fas fa-clock"></i> 지연됨</span>',
        'pending_completion': '<span class="m-badge pending-completion"><i class="fas fa-hourglass-half"></i> 완료 대기</span>',
        'completed': '<span class="m-badge completed"><i class="fas fa-check-circle"></i> 완료됨</span>'
    };
    return map[status] || map['in_progress'];
}

// ===== 렌더링 =====
function renderIssues() {
    var container = document.getElementById('issuesList');
    var empty = document.getElementById('emptyState');

    if (!filteredIssues.length) { container.innerHTML = ''; empty.classList.remove('hidden'); return; }
    empty.classList.add('hidden');

    // 날짜별 그룹
    var grouped = {};
    var dateObjs = {};
    filteredIssues.forEach(function (issue) {
        var dateToUse = currentTab === 'completed' ? (issue.actual_completion_date || issue.report_date) : issue.report_date;
        var d = new Date(dateToUse);
        var key = d.toLocaleDateString('ko-KR');
        if (!grouped[key]) { grouped[key] = []; dateObjs[key] = d; }
        grouped[key].push(issue);
    });

    var html = Object.keys(grouped)
        .sort(function (a, b) { return dateObjs[b] - dateObjs[a]; })
        .map(function (dateKey) {
            var issues = grouped[dateKey];
            return '<div class="m-date-group"><div class="m-date-header">' +
                '<i class="fas fa-calendar-alt"></i>' +
                '<span>' + dateKey + '</span>' +
                '<span class="m-date-count">(' + issues.length + '건)</span>' +
                '<span style="font-size:10px;padding:2px 6px;border-radius:8px;background:' +
                (currentTab === 'in_progress' ? '#dbeafe;color:#1d4ed8' : '#dcfce7;color:#15803d') + '">' +
                (currentTab === 'in_progress' ? '업로드일' : '완료일') + '</span>' +
                '</div>' +
                issues.map(function (issue) {
                    return currentTab === 'in_progress' ? renderInProgressCard(issue) : renderCompletedCard(issue);
                }).join('') +
            '</div>';
        }).join('');

    container.innerHTML = html;
}

function renderInProgressCard(issue) {
    var project = projects.find(function (p) { return p.id === issue.project_id; });
    var status = getIssueStatus(issue);
    var isPending = status === 'pending_completion';
    var photos = getPhotoPaths(issue);

    // 관리 필드 표시
    var mgmtHtml = '<div style="margin-top:8px">' +
        '<div class="m-info-row"><i class="fas fa-lightbulb" style="color:#eab308"></i><span style="font-weight:600">해결방안:</span> <span>' + escapeHtml(cleanManagementComment(issue.management_comment) || '-') + '</span></div>' +
        '<div class="m-info-row"><i class="fas fa-building" style="color:#3b82f6"></i><span style="font-weight:600">담당부서:</span> <span>' + getDepartmentText(issue.responsible_department) + '</span></div>' +
        '<div class="m-info-row"><i class="fas fa-user" style="color:#8b5cf6"></i><span style="font-weight:600">담당자:</span> <span>' + escapeHtml(issue.responsible_person || '-') + '</span></div>' +
        '<div class="m-info-row"><i class="fas fa-calendar-alt" style="color:#ef4444"></i><span style="font-weight:600">조치예상일:</span> <span>' + (issue.expected_completion_date ? formatKSTDate(issue.expected_completion_date) : '-') + '</span></div>' +
        '</div>';

    // 완료 대기 정보
    var completionInfoHtml = '';
    if (isPending) {
        var cPhotos = getCompletionPhotoPaths(issue);
        completionInfoHtml = '<div class="m-completion-info" style="margin-top:8px">' +
            '<div style="font-size:12px;font-weight:600;color:#6d28d9;margin-bottom:4px"><i class="fas fa-check-circle" style="margin-right:4px"></i>완료 신청 정보</div>' +
            (cPhotos.length ? renderPhotoThumbs(cPhotos) : '') +
            '<div style="font-size:12px;color:#6b7280;margin-top:4px">' + escapeHtml(issue.completion_comment || '코멘트 없음') + '</div>' +
            '<div style="font-size:11px;color:#9ca3af;margin-top:2px">신청: ' + formatKSTDateTime(issue.completion_requested_at) + '</div>' +
            '</div>';
    }

    // 액션 버튼
    var actionHtml = '';
    if (isPending) {
        actionHtml = '<div class="m-action-row">' +
            '<button class="m-action-btn red" onclick="event.stopPropagation();openRejectSheet(' + issue.id + ')"><i class="fas fa-times"></i>반려</button>' +
            '<button class="m-action-btn green" onclick="event.stopPropagation();confirmCompletion(' + issue.id + ')"><i class="fas fa-check-circle"></i>최종확인</button>' +
            '</div>';
    } else {
        actionHtml = '<div class="m-action-row">' +
            '<button class="m-action-btn blue" onclick="event.stopPropagation();openEditMgmtSheet(' + issue.id + ')"><i class="fas fa-edit"></i>편집</button>' +
            '<button class="m-action-btn green" onclick="event.stopPropagation();confirmCompletion(' + issue.id + ')"><i class="fas fa-check"></i>완료처리</button>' +
            '</div>';
    }

    return '<div class="m-card border-blue">' +
        '<div class="m-card-header">' +
            '<div><span class="m-card-no">No.' + (issue.project_sequence_no || '-') + '</span>' +
            '<span class="m-card-project">' + escapeHtml(project ? project.project_name : '미지정') + '</span></div>' +
            getStatusBadgeHtml(status) +
        '</div>' +
        '<div class="m-card-title">' + escapeHtml(getIssueTitle(issue)) + '</div>' +
        '<div class="m-card-body">' +
            '<div style="font-size:13px;color:#6b7280;line-height:1.5;margin-bottom:6px" class="text-ellipsis-3">' + escapeHtml(getIssueDetail(issue)) + '</div>' +
            '<div style="display:flex;gap:8px;font-size:12px;color:#9ca3af;margin-bottom:6px">' +
                '<span><i class="fas fa-tag" style="margin-right:3px"></i>' + getCategoryText(issue.category || issue.final_category) + '</span>' +
                '<span><i class="fas fa-user" style="margin-right:3px"></i>' + escapeHtml(issue.reporter?.full_name || issue.reporter?.username || '-') + '</span>' +
            '</div>' +
            (photos.length ? renderPhotoThumbs(photos) : '') +
            mgmtHtml +
            completionInfoHtml +
        '</div>' +
        actionHtml +
        '<div class="m-card-footer">' +
            '<span>신고일: ' + formatKSTDate(issue.report_date) + '</span>' +
            '<span>ID: ' + issue.id + '</span>' +
        '</div>' +
    '</div>';
}

function renderCompletedCard(issue) {
    var project = projects.find(function (p) { return p.id === issue.project_id; });
    var completedDate = issue.completed_at ? formatKSTDate(issue.completed_at) : '-';

    return '<div class="m-card border-green" onclick="openDetailSheet(' + issue.id + ')">' +
        '<div class="m-card-header">' +
            '<div><span class="m-card-no" style="color:#16a34a">No.' + (issue.project_sequence_no || '-') + '</span>' +
            '<span class="m-card-project">' + escapeHtml(project ? project.project_name : '미지정') + '</span></div>' +
            '<span class="m-badge completed"><i class="fas fa-check-circle"></i> 완료</span>' +
        '</div>' +
        '<div class="m-card-title">' + escapeHtml(getIssueTitle(issue)) + '</div>' +
        '<div class="m-card-footer">' +
            '<span>완료일: ' + completedDate + '</span>' +
            '<span style="color:#3b82f6"><i class="fas fa-chevron-right" style="font-size:10px"></i> 상세보기</span>' +
        '</div>' +
    '</div>';
}

// ===== 편집 시트 =====
function openEditMgmtSheet(issueId) {
    currentIssueId = issueId;
    var issue = issues.find(function (i) { return i.id === issueId; });
    if (!issue) return;

    // 프로젝트 셀렉트 채우기
    var projSel = document.getElementById('editProject');
    projSel.innerHTML = '<option value="">선택하세요</option>';
    projects.forEach(function (p) {
        projSel.innerHTML += '<option value="' + p.id + '"' + (p.id == issue.project_id ? ' selected' : '') + '>' + escapeHtml(p.project_name || p.job_no) + '</option>';
    });
    projSel.disabled = (issue.review_status === 'completed');

    document.getElementById('editManagementComment').value = cleanManagementComment(issue.management_comment) || '';
    document.getElementById('editResponsibleDept').value = issue.responsible_department || '';
    document.getElementById('editResponsiblePerson').value = issue.responsible_person || '';
    document.getElementById('editExpectedDate').value = issue.expected_completion_date ? issue.expected_completion_date.split('T')[0] : '';
    openSheet('editMgmt');
}

async function saveManagementEdit() {
    if (!currentIssueId) return;
    try {
        var updates = {
            management_comment: document.getElementById('editManagementComment').value.trim() || null,
            responsible_department: document.getElementById('editResponsibleDept').value || null,
            responsible_person: document.getElementById('editResponsiblePerson').value.trim() || null,
            expected_completion_date: document.getElementById('editExpectedDate').value ? document.getElementById('editExpectedDate').value + 'T00:00:00' : null
        };

        // 프로젝트 변경 확인
        var newProjectId = parseInt(document.getElementById('editProject').value);
        var issue = issues.find(function (i) { return i.id === currentIssueId; });
        if (newProjectId && issue && newProjectId !== issue.project_id) {
            // 프로젝트 변경은 /issues/{id} PUT으로 별도 호출
            var projResp = await fetch(API_BASE_URL + '/issues/' + currentIssueId, {
                method: 'PUT',
                headers: { 'Authorization': 'Bearer ' + TokenManager.getToken(), 'Content-Type': 'application/json' },
                body: JSON.stringify({ project_id: newProjectId })
            });
            if (!projResp.ok) {
                var projErr = await projResp.json();
                throw new Error(projErr.detail || '프로젝트 변경 실패');
            }
        }

        var resp = await fetch(API_BASE_URL + '/issues/' + currentIssueId + '/management', {
            method: 'PUT',
            headers: { 'Authorization': 'Bearer ' + TokenManager.getToken(), 'Content-Type': 'application/json' },
            body: JSON.stringify(updates)
        });

        if (resp.ok) {
            showToast('저장되었습니다.', 'success');
            closeSheet('editMgmt');
            await loadIssues();
        } else {
            var err = await resp.json();
            throw new Error(err.detail || '저장 실패');
        }
    } catch (e) { showToast('오류: ' + e.message, 'error'); }
}

// ===== 완료 처리 =====
async function confirmCompletion(issueId) {
    if (!confirm('완료 처리하시겠습니까?')) return;
    try {
        var resp = await fetch(API_BASE_URL + '/inbox/' + issueId + '/status', {
            method: 'POST',
            headers: { 'Authorization': 'Bearer ' + TokenManager.getToken(), 'Content-Type': 'application/json' },
            body: JSON.stringify({ review_status: 'completed' })
        });

        if (resp.ok) {
            showToast('완료 처리되었습니다.', 'success');
            await loadIssues();
        } else {
            var err = await resp.json();
            throw new Error(err.detail || '완료 처리 실패');
        }
    } catch (e) { showToast('오류: ' + e.message, 'error'); }
}

// ===== 반려 =====
function openRejectSheet(issueId) {
    rejectIssueId = issueId;
    document.getElementById('rejectReason').value = '';
    openSheet('reject');
}

async function submitReject() {
    if (!rejectIssueId) return;
    var reason = document.getElementById('rejectReason').value.trim();
    if (!reason) { showToast('반려 사유를 입력해주세요.', 'warning'); return; }

    try {
        var resp = await fetch(API_BASE_URL + '/issues/' + rejectIssueId + '/reject-completion', {
            method: 'POST',
            headers: { 'Authorization': 'Bearer ' + TokenManager.getToken(), 'Content-Type': 'application/json' },
            body: JSON.stringify({ rejection_reason: reason })
        });

        if (resp.ok) {
            showToast('반려 처리되었습니다.', 'success');
            closeSheet('reject');
            await loadIssues();
        } else {
            var err = await resp.json();
            throw new Error(err.detail || '반려 실패');
        }
    } catch (e) { showToast('오류: ' + e.message, 'error'); }
}

// ===== 추가 정보 =====
function openAdditionalInfoSheet() {
    var inProgressIssues = issues.filter(function (i) { return i.review_status === 'in_progress'; });
    var sel = document.getElementById('additionalIssueSelect');
    sel.innerHTML = '<option value="">이슈 선택</option>';
    inProgressIssues.forEach(function (i) {
        var p = projects.find(function (pr) { return pr.id === i.project_id; });
        sel.innerHTML += '<option value="' + i.id + '">No.' + (i.project_sequence_no || '-') + ' ' + escapeHtml(getIssueTitle(i)) + '</option>';
    });
    document.getElementById('additionalCauseDept').value = '';
    document.getElementById('additionalCausePerson').value = '';
    document.getElementById('additionalCauseDetail').value = '';
    openSheet('additional');
}

function loadAdditionalInfo() {
    var id = parseInt(document.getElementById('additionalIssueSelect').value);
    if (!id) return;
    var issue = issues.find(function (i) { return i.id === id; });
    if (!issue) return;
    document.getElementById('additionalCauseDept').value = issue.cause_department || '';
    document.getElementById('additionalCausePerson').value = issue.responsible_person_detail || '';
    document.getElementById('additionalCauseDetail').value = issue.cause_detail || '';
}

async function saveAdditionalInfo() {
    var id = parseInt(document.getElementById('additionalIssueSelect').value);
    if (!id) { showToast('이슈를 선택해주세요.', 'warning'); return; }

    try {
        var resp = await fetch(API_BASE_URL + '/issues/' + id + '/management', {
            method: 'PUT',
            headers: { 'Authorization': 'Bearer ' + TokenManager.getToken(), 'Content-Type': 'application/json' },
            body: JSON.stringify({
                cause_department: document.getElementById('additionalCauseDept').value || null,
                responsible_person_detail: document.getElementById('additionalCausePerson').value.trim() || null,
                cause_detail: document.getElementById('additionalCauseDetail').value.trim() || null
            })
        });

        if (resp.ok) {
            showToast('추가 정보가 저장되었습니다.', 'success');
            closeSheet('additional');
            await loadIssues();
        } else {
            var err = await resp.json();
            throw new Error(err.detail || '저장 실패');
        }
    } catch (e) { showToast('오류: ' + e.message, 'error'); }
}

// ===== 완료됨 상세보기 =====
function openDetailSheet(issueId) {
    var issue = issues.find(function (i) { return i.id === issueId; });
    if (!issue) return;
    var project = projects.find(function (p) { return p.id === issue.project_id; });
    var photos = getPhotoPaths(issue);
    var cPhotos = getCompletionPhotoPaths(issue);

    document.getElementById('detailSheetTitle').innerHTML =
        '<span style="font-weight:800;color:#16a34a">No.' + (issue.project_sequence_no || '-') + '</span> 상세 정보';

    document.getElementById('detailSheetBody').innerHTML =
        // 기본 정보
        '<div style="margin-bottom:16px">' +
            '<div style="font-size:14px;font-weight:700;color:#111827;margin-bottom:8px;padding-bottom:6px;border-bottom:1px solid #e5e7eb"><i class="fas fa-info-circle" style="color:#3b82f6;margin-right:6px"></i>기본 정보</div>' +
            '<div class="m-info-row"><span style="font-weight:600">프로젝트:</span> <span>' + escapeHtml(project ? project.project_name : '-') + '</span></div>' +
            '<div class="m-info-row"><span style="font-weight:600">부적합명:</span> <span>' + escapeHtml(getIssueTitle(issue)) + '</span></div>' +
            '<div style="font-size:13px;color:#6b7280;line-height:1.5;margin:6px 0;white-space:pre-wrap">' + escapeHtml(getIssueDetail(issue)) + '</div>' +
            '<div class="m-info-row"><span style="font-weight:600">분류:</span> <span>' + getCategoryText(issue.final_category || issue.category) + '</span></div>' +
            '<div class="m-info-row"><span style="font-weight:600">확인자:</span> <span>' + escapeHtml(getReporterNames(issue)) + '</span></div>' +
            (photos.length ? '<div style="margin-top:6px"><div style="font-size:12px;font-weight:600;color:#6b7280;margin-bottom:4px">업로드 사진</div>' + renderPhotoThumbs(photos) + '</div>' : '') +
        '</div>' +

        // 관리 정보
        '<div style="margin-bottom:16px">' +
            '<div style="font-size:14px;font-weight:700;color:#111827;margin-bottom:8px;padding-bottom:6px;border-bottom:1px solid #e5e7eb"><i class="fas fa-cogs" style="color:#3b82f6;margin-right:6px"></i>관리 정보</div>' +
            '<div class="m-info-row"><span style="font-weight:600">해결방안:</span> <span>' + escapeHtml(cleanManagementComment(issue.management_comment) || '-') + '</span></div>' +
            '<div class="m-info-row"><span style="font-weight:600">담당부서:</span> <span>' + getDepartmentText(issue.responsible_department) + '</span></div>' +
            '<div class="m-info-row"><span style="font-weight:600">담당자:</span> <span>' + escapeHtml(issue.responsible_person || '-') + '</span></div>' +
            '<div class="m-info-row"><span style="font-weight:600">원인부서:</span> <span>' + getDepartmentText(issue.cause_department) + '</span></div>' +
        '</div>' +

        // 완료 정보
        '<div>' +
            '<div style="font-size:14px;font-weight:700;color:#111827;margin-bottom:8px;padding-bottom:6px;border-bottom:1px solid #e5e7eb"><i class="fas fa-check-circle" style="color:#22c55e;margin-right:6px"></i>완료 정보</div>' +
            (cPhotos.length ? '<div style="margin-bottom:6px"><div style="font-size:12px;font-weight:600;color:#6b7280;margin-bottom:4px">완료 사진</div>' + renderPhotoThumbs(cPhotos) + '</div>' : '<div class="m-info-row"><span>완료 사진 없음</span></div>') +
            '<div class="m-info-row"><span style="font-weight:600">완료 코멘트:</span> <span>' + escapeHtml(issue.completion_comment || '-') + '</span></div>' +
            (issue.completion_requested_at ? '<div class="m-info-row"><span style="font-weight:600">완료 신청일:</span> <span>' + formatKSTDateTime(issue.completion_requested_at) + '</span></div>' : '') +
            (issue.completed_at ? '<div class="m-info-row"><span style="font-weight:600">최종 완료일:</span> <span>' + formatKSTDateTime(issue.completed_at) + '</span></div>' : '') +
        '</div>';

    openSheet('detail');
}

// ===== 시작 =====
document.addEventListener('DOMContentLoaded', initialize);
