{% extends "base.html" %}

{% block title %}Transactions - FINA{% endblock %}

{% block body %}
<div class="flex h-screen w-full">
    <!-- Sidebar (reuse from dashboard) -->
    <aside id="sidebar" class="hidden lg:flex w-64 flex-col bg-sidebar-light dark:bg-background-dark border-r border-border-light dark:border-[#233648]">
        <div class="p-6 flex flex-col h-full justify-between">
            <div class="flex flex-col gap-8">
                <div class="flex gap-3 items-center">
                    <img src="{{ current_user.avatar | avatar_url }}" alt="{{ current_user.username }}" class="size-10 rounded-full border-2 border-primary/30 object-cover">
                    <div class="flex flex-col">
                        <h1 class="text-text-main dark:text-white text-base font-bold leading-none">{{ current_user.username }}</h1>
                        <p class="text-text-muted dark:text-[#92adc9] text-xs font-normal mt-1">
                            {% if current_user.is_admin %}<span data-translate="user.admin">Admin</span>{% else %}<span data-translate="user.user">User</span>{% endif %}
                        </p>
                    </div>
                </div>
                
                <nav class="flex flex-col gap-2">
                    <a class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-text-muted dark:text-[#92adc9] hover:bg-background-light dark:hover:bg-[#233648] hover:text-text-main dark:hover:text-white transition-colors" href="{{ url_for('main.dashboard') }}">
                        <span class="material-symbols-outlined text-[20px]">dashboard</span>
                        <span class="text-sm font-medium" data-translate="nav.dashboard">Dashboard</span>
                    </a>
                    <a class="flex items-center gap-3 px-3 py-2.5 rounded-lg bg-primary/20 text-primary border border-primary/10" href="{{ url_for('main.transactions') }}">
                        <span class="material-symbols-outlined text-[20px]">receipt_long</span>
                        <span class="text-sm font-medium" data-translate="nav.transactions">Transactions</span>
                    </a>
                    <a class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-text-muted dark:text-[#92adc9] hover:bg-background-light dark:hover:bg-[#233648] hover:text-text-main dark:hover:text-white transition-colors" href="{{ url_for('main.reports') }}">
                        <span class="material-symbols-outlined text-[20px]">pie_chart</span>
                        <span class="text-sm font-medium" data-translate="nav.reports">Reports</span>
                    </a>
                    <a class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-text-muted dark:text-[#92adc9] hover:bg-background-light dark:hover:bg-[#233648] hover:text-text-main dark:hover:text-white transition-colors" href="{{ url_for('main.documents') }}">
                        <span class="material-symbols-outlined text-[20px]">folder_open</span>
                        <span class="text-sm font-medium" data-translate="nav.documents">Documents</span>
                    </a>
                    {% if current_user.is_admin %}
                    <a class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-text-muted dark:text-[#92adc9] hover:bg-background-light dark:hover:bg-[#233648] hover:text-text-main dark:hover:text-white transition-colors" href="{{ url_for('main.admin') }}">
                        <span class="material-symbols-outlined text-[20px]">admin_panel_settings</span>
                        <span class="text-sm font-medium" data-translate="nav.admin">Admin</span>
                    </a>
                    {% endif %}
                </nav>
            </div>
            
            <div class="flex flex-col gap-2">
                <button id="theme-toggle" class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-text-muted dark:text-[#92adc9] hover:bg-background-light dark:hover:bg-[#233648] hover:text-text-main dark:hover:text-white transition-colors">
                    <span class="material-symbols-outlined text-[20px]" id="theme-icon">light_mode</span>
                    <span class="text-sm font-medium" id="theme-text" data-translate="dashboard.lightMode">Light Mode</span>
                </button>
                <a class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-text-muted dark:text-[#92adc9] hover:bg-background-light dark:hover:bg-[#233648] hover:text-text-main dark:hover:text-white transition-colors" href="{{ url_for('main.settings') }}">
                    <span class="material-symbols-outlined text-[20px]">settings</span>
                    <span class="text-sm font-medium" data-translate="nav.settings">Settings</span>
                </a>
                <a class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-text-muted dark:text-[#92adc9] hover:bg-background-light dark:hover:bg-[#233648] hover:text-text-main dark:hover:text-white transition-colors" href="{{ url_for('auth.logout') }}">
                    <span class="material-symbols-outlined text-[20px]">logout</span>
                    <span class="text-sm font-medium" data-translate="nav.logout">Log out</span>
                </a>
            </div>
        </div>
    </aside>
    
    <main class="flex-1 flex flex-col h-full overflow-hidden relative bg-background-light dark:bg-background-dark">
        <header class="h-16 flex items-center justify-between px-6 lg:px-8 border-b border-border-light dark:border-[#233648] bg-card-light/95 dark:bg-background-dark/95 backdrop-blur z-10 shrink-0">
            <div class="flex items-center gap-4">
                <button id="menu-toggle" class="lg:hidden text-text-main dark:text-white">
                    <span class="material-symbols-outlined">menu</span>
                </button>
                <h2 class="text-text-main dark:text-white text-lg font-bold" data-translate="transactions.title">Transactions</h2>
            </div>
            <div class="flex items-center gap-3">
                <button id="export-csv-btn" class="bg-background-light dark:bg-[#1a2632] border border-border-light dark:border-[#233648] text-text-muted dark:text-[#92adc9] hover:text-text-main dark:hover:text-white h-9 px-4 rounded-lg text-sm font-medium transition-colors flex items-center gap-2">
                    <span class="material-symbols-outlined text-[18px]">download</span>
                    <span class="hidden sm:inline" data-translate="transactions.export">Export CSV</span>
                </button>
                <button id="import-csv-btn" class="bg-background-light dark:bg-[#1a2632] border border-border-light dark:border-[#233648] text-text-muted dark:text-[#92adc9] hover:text-text-main dark:hover:text-white h-9 px-4 rounded-lg text-sm font-medium transition-colors flex items-center gap-2">
                    <span class="material-symbols-outlined text-[18px]">upload</span>
                    <span class="hidden sm:inline" data-translate="transactions.import">Import CSV</span>
                </button>
                <button id="add-expense-btn" class="bg-primary hover:bg-primary/90 text-white h-9 px-4 rounded-lg text-sm font-semibold shadow-lg shadow-primary/20 transition-all flex items-center gap-2">
                    <span class="material-symbols-outlined text-[18px]">add</span>
                    <span class="hidden sm:inline" data-translate="transactions.addExpense">Add Expense</span>
                </button>
            </div>
        </header>
        
        <div class="flex-1 overflow-y-auto p-6 lg:p-8 bg-background-light dark:bg-background-dark">
            <div class="max-w-7xl mx-auto">
                <!-- Transactions Card -->
                <div class="bg-card-light dark:bg-card-dark border border-border-light dark:border-[#233648] rounded-xl overflow-hidden">
                    <!-- Header with Search and Filters -->
                    <div class="p-6 border-b border-border-light dark:border-[#233648]">
                        <div class="flex flex-col sm:flex-row gap-4 justify-between items-start sm:items-center">
                            <!-- Search Bar -->
                            <div class="relative flex-1 max-w-md">
                                <span class="material-symbols-outlined absolute left-3 top-1/2 -translate-y-1/2 text-text-muted dark:text-[#92adc9] text-[20px]">search</span>
                                <input 
                                    type="text" 
                                    id="filter-search" 
                                    data-translate="transactions.search"
                                    placeholder="Search transactions..." 
                                    class="w-full bg-background-light dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg pl-10 pr-4 py-2 text-text-main dark:text-white text-sm placeholder-text-muted dark:placeholder-[#5f7a96] focus:outline-none focus:ring-2 focus:ring-primary/50"
                                >
                            </div>
                            
                            <!-- Filter Buttons -->
                            <div class="flex flex-wrap gap-2">
                                <button id="date-filter-btn" class="flex items-center gap-2 px-3 py-2 bg-background-light dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg text-text-muted dark:text-[#92adc9] hover:text-text-main dark:hover:text-white hover:border-primary/50 transition-colors text-sm">
                                    <span class="material-symbols-outlined text-[18px]">calendar_today</span>
                                    <span data-translate="transactions.date">Date</span>
                                </button>
                                <select id="filter-category" class="px-3 py-2 bg-background-light dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg text-text-muted dark:text-[#92adc9] hover:text-text-main dark:hover:text-white hover:border-primary/50 transition-colors text-sm">
                                    <option value="" data-translate="transactions.allCategories">Category</option>
                                </select>
                                <button id="more-filters-btn" class="flex items-center gap-2 px-3 py-2 bg-background-light dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg text-text-muted dark:text-[#92adc9] hover:text-text-main dark:hover:text-white hover:border-primary/50 transition-colors text-sm">
                                    <span class="material-symbols-outlined text-[18px]">tune</span>
                                    <span data-translate="transactions.filters">Filters</span>
                                </button>
                            </div>
                        </div>
                        
                        <!-- Advanced Filters (Hidden by default) -->
                        <div id="advanced-filters" class="hidden mt-4 pt-4 border-t border-border-light dark:border-[#233648]">
                            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                <div>
                                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="transactions.startDate">Start Date</label>
                                    <input type="date" id="filter-start-date" class="w-full bg-background-light dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white text-sm">
                                </div>
                                <div>
                                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="transactions.endDate">End Date</label>
                                    <input type="date" id="filter-end-date" class="w-full bg-background-light dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white text-sm">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Transactions Table -->
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-background-light dark:bg-[#0f1419]">
                                <tr class="border-b border-border-light dark:border-[#233648]">
                                    <th class="p-5 text-left text-text-muted dark:text-[#92adc9] text-sm font-medium" data-translate="transactions.tableTransaction">Transaction</th>
                                    <th class="p-5 text-left text-text-muted dark:text-[#92adc9] text-sm font-medium" data-translate="transactions.tableCategory">Category</th>
                                    <th class="p-5 text-left text-text-muted dark:text-[#92adc9] text-sm font-medium" data-translate="transactions.tableDate">Date</th>
                                    <th class="p-5 text-left text-text-muted dark:text-[#92adc9] text-sm font-medium" data-translate="transactions.tablePayment">Payment</th>
                                    <th class="p-5 text-right text-text-muted dark:text-[#92adc9] text-sm font-medium" data-translate="transactions.tableAmount">Amount</th>
                                    <th class="p-5 text-center text-text-muted dark:text-[#92adc9] text-sm font-medium" data-translate="transactions.tableStatus">Status</th>
                                    <th class="p-5 text-right text-text-muted dark:text-[#92adc9] text-sm font-medium" data-translate="transactions.tableActions">Actions</th>
                                </tr>
                            </thead>
                            <tbody id="transactions-list" class="divide-y divide-border-light dark:divide-[#233648]">
                                <!-- Transactions will be loaded here -->
                            </tbody>
                        </table>
                    </div>
                    
                    <!-- Pagination Footer -->
                    <div class="p-4 border-t border-border-light dark:border-[#233648] flex flex-col sm:flex-row gap-4 justify-between items-center bg-card-light dark:bg-card-dark">
                        <span class="text-sm text-text-muted dark:text-[#92adc9]">
                            <span data-translate="transactions.showing">Showing</span> <span id="page-start" class="text-text-main dark:text-white font-medium">1</span> <span data-translate="transactions.to">to</span> 
                            <span id="page-end" class="text-text-main dark:text-white font-medium">10</span> <span data-translate="transactions.of">of</span> 
                            <span id="total-count" class="text-text-main dark:text-white font-medium">0</span> <span data-translate="transactions.results">results</span>
                        </span>
                        <div id="pagination" class="flex gap-2">
                            <!-- Pagination buttons will be added here -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
</div>

<!-- Hidden file input for CSV import -->
<input type="file" id="csv-file-input" accept=".csv" class="hidden">

<!-- Add/Edit Expense Modal -->
<div id="expense-modal" class="hidden fixed inset-0 bg-black/50 backdrop-blur-sm z-50 flex items-center justify-center p-4">
    <div class="bg-white dark:bg-card-dark border border-border-light dark:border-[#233648] rounded-2xl max-w-md w-full max-h-[90vh] overflow-y-auto shadow-xl">
        <div class="p-6">
            <div class="flex justify-between items-center mb-6">
                <h3 id="expense-modal-title" class="text-text-main dark:text-white text-xl font-bold" data-translate="modal.add_expense">Add Expense</h3>
                <button id="close-expense-modal" class="text-text-muted dark:text-[#92adc9] hover:text-text-main dark:hover:text-white">
                    <span class="material-symbols-outlined">close</span>
                </button>
            </div>
            
            <form id="expense-form" class="space-y-4">
                <input type="hidden" id="expense-id" name="expense_id">
                
                <div>
                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="form.amount">Amount</label>
                    <input type="number" step="0.01" name="amount" required class="w-full bg-slate-50 dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white focus:border-primary focus:ring-1 focus:ring-primary">
                </div>
                
                <div>
                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="form.description">Description</label>
                    <input type="text" name="description" required class="w-full bg-slate-50 dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white focus:border-primary focus:ring-1 focus:ring-primary">
                </div>
                
                <div>
                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="form.category">Category</label>
                    <select name="category_id" required class="w-full bg-slate-50 dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white focus:border-primary focus:ring-1 focus:ring-primary">
                        <option value="" data-translate="dashboard.selectCategory">Select category...</option>
                    </select>
                </div>
                
                <div>
                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="form.date">Date</label>
                    <input type="date" name="date" required class="w-full bg-slate-50 dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white focus:border-primary focus:ring-1 focus:ring-primary">
                </div>
                
                <div>
                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="form.tags">Tags (comma separated)</label>
                    <input type="text" name="tags" class="w-full bg-slate-50 dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white focus:border-primary focus:ring-1 focus:ring-primary">
                </div>
                
                <div>
                    <label class="text-text-muted dark:text-[#92adc9] text-sm mb-2 block" data-translate="form.receipt">Receipt (optional)</label>
                    <div id="current-receipt-info" class="hidden mb-2 p-3 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg">
                        <div class="flex items-center justify-between">
                            <div class="flex items-center gap-2">
                                <span class="material-symbols-outlined text-blue-600 dark:text-blue-400 text-[20px]">attach_file</span>
                                <span class="text-sm text-blue-900 dark:text-blue-100" data-translate="form.currentReceipt">Current receipt attached</span>
                            </div>
                            <button type="button" id="view-current-receipt" class="text-blue-600 dark:text-blue-400 hover:underline text-sm" data-translate="transactions.viewReceipt">View</button>
                        </div>
                    </div>
                    <input type="file" name="receipt" id="receipt-input" accept="image/*,.pdf" class="w-full bg-slate-50 dark:bg-[#111a22] border border-border-light dark:border-[#233648] rounded-lg px-4 py-2 text-text-main dark:text-white focus:border-primary focus:ring-1 focus:ring-primary file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-primary file:text-white hover:file:bg-primary/90">
                    <p class="text-xs text-text-muted dark:text-[#92adc9] mt-1" data-translate="form.receiptHelp">Upload a new file to replace existing receipt</p>
                </div>
                
                <button type="submit" id="expense-submit-btn" class="w-full bg-primary hover:bg-primary/90 text-white py-3 rounded-lg font-semibold transition-colors shadow-md" data-translate="actions.save">Save Expense</button>
            </form>
        </div>
    </div>
</div>

<!-- Receipt Viewer Modal -->
<div id="receipt-modal" class="hidden fixed inset-0 bg-black/70 backdrop-blur-sm z-50 flex items-center justify-center p-4">
    <div class="bg-white dark:bg-card-dark border border-border-light dark:border-[#233648] rounded-2xl max-w-4xl w-full max-h-[90vh] overflow-hidden shadow-2xl">
        <div class="p-6 border-b border-border-light dark:border-[#233648] flex justify-between items-center">
            <h3 class="text-text-main dark:text-white text-xl font-bold">Receipt</h3>
            <button id="close-receipt-modal" class="text-text-muted dark:text-[#92adc9] hover:text-text-main dark:hover:text-white transition-colors">
                <span class="material-symbols-outlined">close</span>
            </button>
        </div>
        <div class="p-6 overflow-auto max-h-[calc(90vh-120px)]">
            <div id="receipt-content" class="flex items-center justify-center min-h-[400px]">
                <!-- Receipt content will be loaded here -->
            </div>
        </div>
    </div>
</div>

<script src="{{ url_for('static', filename='js/transactions.js') }}"></script>
{% endblock %}
