{% extends "base.html" %}

{% block title %}Log Time - {{ app_name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    {% from "_components.html" import page_header %}
    <div class="row">
        <div class="col-12">
            {% set actions %}
            <a href="{{ url_for('main.dashboard') }}" class="btn btn-outline-light btn-sm">
                <i class="fas fa-arrow-left"></i> Back
            </a>
            {% endset %}
            {{ page_header('fas fa-clock', 'Log Time', 'Create a manual time entry', actions) }}
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card shadow-sm border-0">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h6 class="m-0">
                        <i class="fas fa-clock me-2 text-primary"></i>Manual Entry
                    </h6>
                    <div class="d-none d-md-flex gap-2">
                        <a href="{{ url_for('timer.manual_entry') }}" class="btn btn-sm btn-outline-secondary">
                            <i class="fas fa-rotate-right me-1"></i> Reset
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('timer.manual_entry') }}">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-floating mb-3">
                                    <select class="form-select" id="project_id" name="project_id" required>
                                        <option value=""></option>
                                        {% set selected_project_id = (request.form.get('project_id') or '')|int %}
                                        {% for project in projects %}
                                            <option value="{{ project.id }}" {% if project.id == selected_project_id %}selected{% endif %}>{{ project.name }}</option>
                                        {% endfor %}
                                    </select>
                                    <label for="project_id"><i class="fas fa-project-diagram me-1"></i>Project *</label>
                                    <div class="form-text">Select the project to log time for</div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                {% set preselected_task_id = request.form.get('task_id') or request.args.get('task_id') %}
                                <div class="form-floating mb-3">
                                    <select class="form-select" id="task_id" name="task_id" data-selected-task-id="{{ preselected_task_id or '' }}" disabled>
                                        <option value=""></option>
                                    </select>
                                    <label for="task_id"><i class="fas fa-tasks me-1"></i>Task (optional)</label>
                                    <div class="form-text">Tasks load after selecting a project</div>
                                </div>
                            </div>
                        </div>

                        <div class="row g-3">
                            <div class="col-12 col-md-6">
                                <div class="card border-0 shadow-sm">
                                    <div class="card-body">
                                        <div class="mb-2 fw-semibold text-primary"><i class="fas fa-play me-1"></i>Start *</div>
                                        <div class="row g-2">
                                            <div class="col-6">
                                                <div class="form-floating">
                                                    <input type="date" class="form-control" name="start_date" id="start_date" required value="{{ request.form.get('start_date','') }}">
                                                    <label for="start_date">Date</label>
                                                </div>
                                            </div>
                                            <div class="col-6">
                                                <div class="form-floating">
                                                    <input type="time" class="form-control" name="start_time" id="start_time" required value="{{ request.form.get('start_time','') }}">
                                                    <label for="start_time">Time</label>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-12 col-md-6">
                                <div class="card border-0 shadow-sm">
                                    <div class="card-body">
                                        <div class="mb-2 fw-semibold text-primary"><i class="fas fa-stop me-1"></i>End *</div>
                                        <div class="row g-2">
                                            <div class="col-6">
                                                <div class="form-floating">
                                                    <input type="date" class="form-control" name="end_date" id="end_date" required value="{{ request.form.get('end_date','') }}">
                                                    <label for="end_date">Date</label>
                                                </div>
                                            </div>
                                            <div class="col-6">
                                                <div class="form-floating">
                                                    <input type="time" class="form-control" name="end_time" id="end_time" required value="{{ request.form.get('end_time','') }}">
                                                    <label for="end_time">Time</label>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-floating my-3">
                            <textarea class="form-control" id="notes" name="notes" style="height: 100px" placeholder="What did you work on?">{{ request.form.get('notes','') }}</textarea>
                            <label for="notes"><i class="fas fa-sticky-note me-1"></i>Notes</label>
                        </div>

                        <div class="row g-3 align-items-center">
                            <div class="col-12 col-md-8">
                                <div class="form-floating mb-3 mb-md-0">
                                    <input type="text" class="form-control" id="tags" name="tags" placeholder="tag1, tag2, tag3" value="{{ request.form.get('tags','') }}">
                                    <label for="tags"><i class="fas fa-tags me-1"></i>Tags</label>
                                    <div class="form-text">Separate tags with commas</div>
                                </div>
                            </div>
                            <div class="col-12 col-md-4">
                                <div class="form-check form-switch mt-2 d-flex align-items-center">
                                    <input class="form-check-input" type="checkbox" id="billable" name="billable" {% if request.form.get('billable') %}checked{% else %}checked{% endif %}>
                                    <label class="form-check-label fw-semibold ms-2" for="billable">
                                        <i class="fas fa-dollar-sign me-1"></i>Billable
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="d-flex justify-content-between flex-column flex-md-row mt-3">
                            <a href="{{ url_for('main.dashboard') }}" class="btn btn-outline-secondary mb-2 mb-md-0">
                                <i class="fas fa-arrow-left me-1"></i> Back
                            </a>
                            <div class="btn-group" role="group">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-save me-2"></i>Save Entry
                                </button>
                                <button type="reset" class="btn btn-outline-primary">
                                    <i class="fas fa-broom me-2"></i>Clear
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card shadow-sm border-0">
                <div class="card-header">
                    <h6 class="m-0">
                        <i class="fas fa-lightbulb me-2 text-warning"></i>Quick Tips
                    </h6>
                </div>
                <div class="card-body">
                    <div class="tip-item mb-3 d-flex gap-3">
                        <div class="tip-icon text-primary"><i class="fas fa-tasks"></i></div>
                        <div class="tip-content">
                            <strong>Use Tasks</strong>
                            <p class="small text-muted mb-0">Categorize time by selecting a task after choosing a project.</p>
                        </div>
                    </div>
                    <div class="tip-item mb-3 d-flex gap-3">
                        <div class="tip-icon text-success"><i class="fas fa-dollar-sign"></i></div>
                        <div class="tip-content">
                            <strong>Billable Time</strong>
                            <p class="small text-muted mb-0">Enable billable to include this entry in invoices.</p>
                        </div>
                    </div>
                    <div class="tip-item d-flex gap-3">
                        <div class="tip-icon text-info"><i class="fas fa-tags"></i></div>
                        <div class="tip-content">
                            <strong>Tag Entries</strong>
                            <p class="small text-muted mb-0">Add tags to filter entries in reports later.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.form-floating > label {
    background-color: transparent;
    color: var(--text-secondary);
}
.form-floating > .form-control:focus ~ label,
.form-floating > .form-control:not(:placeholder-shown) ~ label,
.form-floating > .form-select:focus ~ label,
.form-floating > .form-select:not([value=""]) ~ label {
    color: var(--primary-color);
}

.form-control:focus, .form-select:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 0.2rem rgba(59, 130, 246, 0.15);
}

.tip-icon { font-size: 18px; }

/* Match invoices page look-and-feel */
.card-header {
    border-bottom: 1px solid var(--border-color);
}
.card.shadow-sm {
    border: 1px solid var(--border-color);
}
.btn-outline-primary {
    border-width: 2px;
}
.btn-outline-primary:hover {
    color: #fff;
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Set default dates to today
    const today = new Date().toISOString().split('T')[0];
    const now = new Date().toTimeString().slice(0, 5);
    
    if (!document.getElementById('start_date').value) {
        document.getElementById('start_date').value = today;
    }
    if (!document.getElementById('end_date').value) {
        document.getElementById('end_date').value = today;
    }
    if (!document.getElementById('start_time').value) {
        document.getElementById('start_time').value = now;
    }
    if (!document.getElementById('end_time').value) {
        document.getElementById('end_time').value = now;
    }
    
    // Mobile-specific improvements
    if (window.innerWidth <= 768) {
        // Add mobile-specific classes
        const form = document.querySelector('form');
        form.classList.add('mobile-form');
        
        // Improve touch targets
        const inputs = document.querySelectorAll('.form-control, .form-select');
        inputs.forEach(input => {
            input.classList.add('touch-target');
        });
        
        // Improve buttons
        const buttons = document.querySelectorAll('.btn');
        buttons.forEach(btn => {
            btn.classList.add('touch-target');
        });
    }
    
    // Handle mobile viewport changes
    window.addEventListener('resize', function() {
        if (window.innerWidth <= 768) {
            document.body.classList.add('mobile-view');
        } else {
            document.body.classList.remove('mobile-view');
        }
    });

    // Dynamic task loading based on project selection
    const projectSelect = document.getElementById('project_id');
    const taskSelect = document.getElementById('task_id');

    async function loadTasksForProject(projectId) {
        if (!projectId) {
            taskSelect.innerHTML = '<option value="">No task</option>';
            taskSelect.disabled = true;
            return;
        }
        try {
            const resp = await fetch(`/api/tasks?project_id=${projectId}`);
            if (!resp.ok) throw new Error('Failed to load tasks');
            const data = await resp.json();
            const tasks = Array.isArray(data.tasks) ? data.tasks : [];
            taskSelect.innerHTML = '<option value="">No task</option>';
            tasks.forEach(t => {
                const opt = document.createElement('option');
                opt.value = String(t.id);
                opt.textContent = t.name;
                taskSelect.appendChild(opt);
            });
            // Preselect if provided
            const preId = taskSelect.getAttribute('data-selected-task-id');
            if (preId) {
                const found = Array.from(taskSelect.options).some(o => o.value === preId);
                if (found) taskSelect.value = preId;
                // Clear after first use
                taskSelect.setAttribute('data-selected-task-id', '');
            }
            taskSelect.disabled = false;
        } catch (e) {
            // On error, keep disabled
            taskSelect.innerHTML = '<option value="">No task</option>';
            taskSelect.disabled = true;
        }
    }

    // Initial load if project is already selected (from query/form)
    if (projectSelect && projectSelect.value) {
        loadTasksForProject(projectSelect.value);
    }

    // Reload tasks when project changes
    projectSelect.addEventListener('change', function() {
        // Clear any previous selection
        taskSelect.value = '';
        taskSelect.setAttribute('data-selected-task-id', '');
        loadTasksForProject(this.value);
    });
});
</script>
{% endblock %}


