📅 2026-07-11T13:02:52.381Z
👁️ 41 katselukertaa
🔓 Julkinen


<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>極簡手機記帳本 v2</title>
    <!-- 引入 Tailwind CSS -->
    <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
    </style>
</head>
<body class="bg-gray-100 min-h-screen flex justify-center">

    <!-- 手機版容器外殼 -->
    <div class="w-full max-w-md bg-white min-h-screen shadow-lg flex flex-col justify-between p-4">
        
        <!-- 上方資產儀表板與列表 -->
        <div class="flex flex-col flex-1">
            <h2 class="text-center text-xl font-bold text-gray-700 mb-4">我的錢包 v2</h2>
            
            <!-- 錢包餘額卡片 -->
            <div class="bg-gradient-to-r from-blue-500 to-indigo-600 rounded-2xl p-6 text-white shadow-md mb-4">
                <p class="text-sm opacity-80">錢包剩多少(目前餘額)</p>
                <p class="text-3xl font-extrabold mt-1" id="balanceText">$0</p>
            </div>

            <!-- 收入與支出小卡 -->
            <div class="grid grid-cols-2 gap-3 mb-4">
                <div class="bg-green-50 rounded-xl p-3 border border-green-200">
                    <p class="text-xs text-green-600 font-medium">總收入</p>
                    <p class="text-lg font-bold text-green-700 mt-1" id="incomeText">$0</p>
                </div>
                <div class="bg-red-50 rounded-xl p-3 border border-red-200">
                    <p class="text-xs text-red-600 font-medium">總支出(花多少錢)</p>
                    <p class="text-lg font-bold text-red-700 mt-1" id="expenseText">$0</p>
                </div>
            </div>

            <!-- 歷史紀錄列表 -->
            <h3 class="font-bold text-gray-700 mb-2">明細紀錄</h3>
            <!-- 給列表固定的滾動高度,防止輸入法彈出時頂爛版面 -->
            <div class="space-y-2 overflow-y-auto flex-1 max-h-[30vh] pb-4" id="historyList">
                <!-- 紀錄會由 JavaScript 自動生成在這裡 -->
            </div>
        </div>

        <!-- 下方記帳輸入表單(大按鈕方便手機單手操作) -->
        <div class="bg-gray-50 p-4 -mx-4 -mb-4 border-t border-gray-200 sticky bottom-0 z-10">
            <!-- 第一排:日期、收支類型 -->
            <div class="flex gap-2 mb-2">
                <input id="dateInput" type="date" class="w-1/2 p-3 bg-white border border-gray-300 rounded-xl font-medium focus:outline-none focus:ring-2 focus:ring-indigo-500">
                <select id="typeInput" class="w-1/2 p-3 bg-white border border-gray-300 rounded-xl font-medium focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    <option value="expense">支出 💸</option>
                    <option value="income">收入 💰</option>
                </select>
            </div>

            <!-- 第二排:項目名稱、金額 -->
            <div class="flex gap-2 mb-2">
                <input id="descInput" type="text" placeholder="項目 (如: 晚餐)" class="w-1/2 p-3 bg-white border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-indigo-500">
                <input id="amountInput" type="number" inputmode="numeric" placeholder="金額" class="w-1/2 p-3 bg-white border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-indigo-500">
            </div>
            
            <!-- 第三排:備註、記帳按鈕 -->
            <div class="flex gap-2">
                <input id="noteInput" type="text" placeholder="備註內容 (可留空)" class="w-2/3 p-3 bg-white border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-indigo-500">
                <button onclick="addRecord()" class="w-1/3 bg-indigo-600 hover:bg-indigo-700 text-white font-bold p-3 rounded-xl transition shadow">
                    記帳
                </button>
            </div>
        </div>

    </div>

    <!-- 核心邏輯 JavaScript -->
    <script>
        // 從瀏覽器記憶體載入舊資料
        let records = JSON.parse(localStorage.getItem('my_ledger_v2')) || [];

        // 自動將今天日期填入輸入框 (格式化為 YYYY-MM-DD)
        function setTodayDate() {
            const today = new Date();
            const yyyy = today.getFullYear();
            const mm = String(today.getMonth() + 1).padStart(2, '0');
            const dd = String(today.getDate()).padStart(2, '0');
            document.getElementById('dateInput').value = `${yyyy}-${mm}-${dd}`;
        }

        // 初始化
        setTodayDate();
        updateUI();

        // 新增一筆紀錄
        function addRecord() {
            const date = document.getElementById('dateInput').value;
            const type = document.getElementById('typeInput').value;
            const desc = document.getElementById('descInput').value.trim() || (type === 'income' ? '日常收入' : '日常支出');
            const amount = parseFloat(document.getElementById('amountInput').value);
            const note = document.getElementById('noteInput').value.trim(); // 獲取備註

            if (!date) {
                alert('請選擇日期喔!');
                return;
            }

            if (isNaN(amount) || amount <= 0) {
                alert('請輸入正確的金額數字喔!');
                return;
            }

            // 建立新紀錄物件(包含日期、備註)
            const newRecord = {
                id: Date.now(),
                date: date,
                type: type,
                desc: desc,
                amount: amount,
                note: note
            };

            records.push(newRecord);
            
            // 按照日期排序(新的日期在上面)
            records.sort((a, b) => new Date(b.date) - new Date(a.date));

            saveData();
            
            // 清空輸入框並重設日期
            document.getElementById('descInput').value = '';
            document.getElementById('amountInput').value = '';
            document.getElementById('noteInput').value = '';
            setTodayDate();
            
            updateUI();
        }

        // 刪除紀錄
        function deleteRecord(id) {
            records = records.filter(item => item.id !== id);
            saveData();
            updateUI();
        }

        function saveData() {
            localStorage.setItem('my_ledger_v2', JSON.stringify(records));
        }

        // 更新畫面
        function updateUI() {
            let totalIncome = 0;
            let totalExpense = 0;
            const historyList = document.getElementById('historyList');
            
            historyList.innerHTML = '';

            records.forEach(item => {
                if (item.type === 'income') {
                    totalIncome += item.amount;
                } else {
                    totalExpense += item.amount;
                }

                const isIncome = item.type === 'income';
                const sign = isIncome ? '+' : '-';
                const colorClass = isIncome ? 'text-green-600' : 'text-red-600';
                
                // 格式化日期顯示,去掉年份讓畫面更乾淨 (例如 2026-07-11 變成 07-11)
                const displayDate = item.date ? item.date.substring(5) : '日常';

                // 如果有備註,才顯示備註的 HTML 區塊
                const noteHTML = item.note ? `<p class="text-xs text-gray-400 mt-0.5 italic">📝 ${item.note}</p>` : '';

                const recordRow = `
                    <div class="flex justify-between items-center p-3 bg-gray-50 rounded-xl border border-gray-100">
                        <div class="flex items-center gap-3">
                            <!-- 左側日期小標籤 -->
                            <span class="text-xs font-mono bg-gray-200 text-gray-600 px-1.5 py-0.5 rounded">
                                ${displayDate}
                            </span>
                            <div>
                                <span class="font-medium text-gray-700">${item.desc}</span>
                                ${noteHTML}
                            </div>
                        </div>
                        <div class="flex items-center gap-3">
                            <span class="font-bold ${colorClass}">${sign}$${item.amount.toLocaleString()}</span>
                            <button onclick="deleteRecord(${item.id})" class="text-gray-300 hover:text-red-500 text-sm font-bold px-1">✕</button>
                        </div>
                    </div>
                `;
                historyList.insertAdjacentHTML('beforeend', recordRow);
            });

            const totalBalance = totalIncome - totalExpense;

            document.getElementById('incomeText').innerText = `$${totalIncome.toLocaleString()}`;
            document.getElementById('expenseText').innerText = `$${totalExpense.toLocaleString()}`;
            document.getElementById('balanceText').innerText = `$${totalBalance.toLocaleString()}`;
            
            if (totalBalance < 0) {
                document.getElementById('balanceText').className = "text-3xl font-extrabold mt-1 text-red-200 animate-pulse";
            } else {
                document.getElementById('balanceText').className = "text-3xl font-extrabold mt-1 text-white";
            }
        }
    </script>
</body>
</html>