/* task-detail 系列页面共享 — 备注弹窗 + Toast + 星星打分 */

/* ---- 星星打分交互 ---- */
function initNoteRatings() {
    document.querySelectorAll('.note-rating-row').forEach(function (row) {
        var stars = row.querySelectorAll('.nr-star');
        var dragging = false;

        function setScore(idx) {
            row.dataset.score = idx + 1;
            stars.forEach(function (s, i) {
                s.classList.toggle('active', i <= idx);
            });
        }

        /* 点击 */
        stars.forEach(function (star, idx) {
            star.addEventListener('click', function () { setScore(idx); });
        });

        /* 触摸拖动 */
        row.addEventListener('touchstart', function (e) { dragging = true; handleTouch(e); }, { passive: false });
        row.addEventListener('touchmove', function (e) { if (dragging) { e.preventDefault(); handleTouch(e); } }, { passive: false });
        row.addEventListener('touchend', function () { dragging = false; });

        function handleTouch(e) {
            var touch = e.touches[0];
            for (var i = 0; i < stars.length; i++) {
                var rect = stars[i].getBoundingClientRect();
                if (touch.clientX >= rect.left && touch.clientX < rect.right) {
                    setScore(i);
                    break;
                }
            }
        }

        /* 鼠标拖动 */
        row.addEventListener('mousedown', function (e) { dragging = true; handleMouse(e); });
        document.addEventListener('mousemove', function (e) { if (dragging) handleMouse(e); });
        document.addEventListener('mouseup', function () { dragging = false; });

        function handleMouse(e) {
            for (var i = 0; i < stars.length; i++) {
                var rect = stars[i].getBoundingClientRect();
                if (e.clientX >= rect.left && e.clientX < rect.right) {
                    setScore(i);
                    break;
                }
            }
        }
    });
}

function resetNoteRatings() {
    document.querySelectorAll('.note-rating-row').forEach(function (row) {
        row.dataset.score = '0';
        row.querySelectorAll('.nr-star').forEach(function (s) { s.classList.remove('active'); });
    });
}

/* 页面加载后初始化 */
document.addEventListener('DOMContentLoaded', initNoteRatings);

/* ---- 弹窗控制 ---- */
function showNoteModal() {
    document.getElementById('noteModal').classList.remove('hidden');
    document.getElementById('noteModal').classList.add('flex');
    document.getElementById('noteText').value = '';
    resetNoteRatings();
    /* 重置展开状态 */
    var sec = document.getElementById('noteRatingSection');
    var btn = document.getElementById('noteExpandBtn');
    if (sec) sec.classList.add('hidden');
    if (btn) btn.classList.remove('hidden');
}

function expandNoteRating() {
    var sec = document.getElementById('noteRatingSection');
    var btn = document.getElementById('noteExpandBtn');
    if (sec) sec.classList.remove('hidden');
    if (btn) btn.classList.add('hidden');
}

function hideNoteModal() {
    document.getElementById('noteModal').classList.add('hidden');
    document.getElementById('noteModal').classList.remove('flex');
}

function saveNote() {
    var text = document.getElementById('noteText').value.trim();
    if (!text) { showToast('请输入备注内容'); return; }
    hideNoteModal();
    showToast('备注已保存');
}

function confirmDeleteNote() {
    document.getElementById('deleteNoteModal').classList.remove('hidden');
    document.getElementById('deleteNoteModal').classList.add('flex');
}

function hideDeleteNoteModal() {
    document.getElementById('deleteNoteModal').classList.add('hidden');
    document.getElementById('deleteNoteModal').classList.remove('flex');
}

function deleteNote() {
    hideDeleteNoteModal();
    showToast('备注已删除');
}

function showToast(msg) {
    var toast = document.getElementById('toast');
    toast.textContent = msg;
    toast.classList.remove('hidden');
    setTimeout(function () { toast.classList.add('hidden'); }, 1500);
}
