{% extends "base.html" %}

{% block title %}我的数据{% endblock %}

{% block content %}
<div class="content-header">
    <h1>我的数据</h1>
    <p>查看和管理您录入的所有数据</p>
</div>

<!-- 搜索框 -->
<div class="search-container">
    <form method="GET" action="{{ url_for('my_data') }}" class="search-form">
        <div class="search-input-group">
            <input type="text" name="keyword" value="{{ keyword }}" placeholder="搜索我的数据..." class="search-input">
            <button type="submit" class="search-btn">
                <i class="search-icon">🔍</i>
                搜索
            </button>
        </div>
    </form>
    {% if keyword %}
        <div class="search-info">
            <span>搜索关键词: "{{ keyword }}"</span>
            <a href="{{ url_for('my_data') }}" class="clear-search">清除搜索</a>
        </div>
    {% endif %}
</div>

<!-- 数据统计 -->
<div class="data-stats">
    <div class="stat-item">
        <span class="stat-number">{{ data|length }}</span>
        <span class="stat-label">条记录</span>
    </div>
</div>

<!-- 数据列表 -->
<div class="data-container">
    {% if data %}
        <div class="data-grid">
            {% for item in data %}
                <div class="data-card">
                    <!-- 图片显示 -->
                    {% if item.image %}
                        <div class="card-image">
                            <img src="{{ url_for('serve_image', filename=item.image) }}" alt="数据图片" onclick="openImageModal('{{ url_for('serve_image', filename=item.image) }}')">
                        </div>
                    {% endif %}
                    
                    <!-- 数据内容 -->
                    <div class="card-content">
                        {% for key, value in item.items() %}
                            {% if key not in ['_id', 'image', 'user_id'] %}
                                <div class="data-field">
                                    <span class="field-label">{{ key }}:</span>
                                    <span class="field-value">{{ value }}</span>
                                </div>
                            {% endif %}
                        {% endfor %}
                    </div>
                    
                    <!-- 操作按钮 -->
                    <div class="card-actions">
                        <a href="{{ url_for('edit_entry', doc_id=item._id) }}" class="btn btn-edit">
                            <i class="icon">✏️</i>
                            编辑
                        </a>
                        <button onclick="confirmDelete('{{ item._id }}')" class="btn btn-delete">
                            <i class="icon">🗑️</i>
                            删除
                        </button>
                    </div>
                </div>
            {% endfor %}
        </div>
    {% else %}
        <div class="empty-state">
            <div class="empty-icon">📝</div>
            <h3>暂无数据</h3>
            <p>{% if keyword %}没有找到匹配 "{{ keyword }}" 的数据{% else %}您还没有录入任何数据{% endif %}</p>
            <a href="{{ url_for('index') }}" class="btn btn-primary">开始录入数据</a>
        </div>
    {% endif %}
</div>

<!-- 图片预览模态框 -->
<div id="imageModal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="closeImageModal()">&times;</span>
        <img id="modalImage" src="" alt="图片预览">
    </div>
</div>

<!-- 删除确认模态框 -->
<div id="deleteModal" class="modal">
    <div class="modal-content modal-small">
        <h3>确认删除</h3>
        <p>您确定要删除这条数据吗？此操作不可撤销。</p>
        <div class="modal-actions">
            <button onclick="closeDeleteModal()" class="btn btn-secondary">取消</button>
            <form id="deleteForm" method="POST" style="display: inline;">
                <button type="submit" class="btn btn-danger">确认删除</button>
            </form>
        </div>
    </div>
</div>

<style>
.content-header {
    margin-bottom: 30px;
    text-align: center;
}

.content-header h1 {
    color: #333;
    font-size: 28px;
    margin-bottom: 10px;
}

.content-header p {
    color: #666;
    font-size: 16px;
}

.search-container {
    background: white;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.search-form {
    margin-bottom: 15px;
}

.search-input-group {
    display: flex;
    gap: 10px;
    max-width: 500px;
    margin: 0 auto;
}

.search-input {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 16px;
    transition: border-color 0.3s ease;
}

.search-input:focus {
    outline: none;
    border-color: #667eea;
}

.search-btn {
    padding: 12px 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: transform 0.2s ease;
}

.search-btn:hover {
    transform: translateY(-2px);
}

.search-info {
    text-align: center;
    color: #666;
    font-size: 14px;
}

.clear-search {
    color: #667eea;
    text-decoration: none;
    margin-left: 10px;
}

.clear-search:hover {
    text-decoration: underline;
}

.data-stats {
    background: white;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    text-align: center;
}

.stat-item {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
}

.stat-number {
    font-size: 32px;
    font-weight: 700;
    color: #667eea;
}

.stat-label {
    font-size: 14px;
    color: #666;
    margin-top: 5px;
}

.data-container {
    background: white;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.data-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 20px;
}

.data-card {
    border: 2px solid #e9ecef;
    border-radius: 12px;
    overflow: hidden;
    transition: all 0.3s ease;
    background: #f8f9fa;
}

.data-card:hover {
    border-color: #667eea;
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
}

.card-image {
    height: 200px;
    overflow: hidden;
    background: #f0f0f0;
}

.card-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.3s ease;
}

.card-image img:hover {
    transform: scale(1.05);
}

.card-content {
    padding: 20px;
}

.data-field {
    margin-bottom: 12px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.field-label {
    font-weight: 600;
    color: #555;
    min-width: 80px;
}

.field-value {
    color: #333;
    flex: 1;
    word-break: break-word;
}

.card-actions {
    padding: 15px 20px;
    background: white;
    border-top: 1px solid #e9ecef;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s ease;
}

.btn-edit {
    background: #28a745;
    color: white;
}

.btn-edit:hover {
    background: #218838;
    transform: translateY(-1px);
}

.btn-delete {
    background: #dc3545;
    color: white;
}

.btn-delete:hover {
    background: #c82333;
    transform: translateY(-1px);
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 12px 24px;
    font-size: 16px;
}

.btn-primary:hover {
    transform: translateY(-2px);
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-secondary:hover {
    background: #5a6268;
}

.btn-danger {
    background: #dc3545;
    color: white;
}

.btn-danger:hover {
    background: #c82333;
}

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: #666;
}

.empty-icon {
    font-size: 64px;
    margin-bottom: 20px;
}

.empty-state h3 {
    font-size: 24px;
    margin-bottom: 10px;
    color: #333;
}

.empty-state p {
    font-size: 16px;
    margin-bottom: 30px;
}

/* 模态框样式 */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
}

.modal-content {
    position: relative;
    margin: 5% auto;
    padding: 20px;
    width: 90%;
    max-width: 800px;
    background: white;
    border-radius: 12px;
    text-align: center;
}

.modal-small {
    max-width: 400px;
    margin: 15% auto;
}

.close {
    position: absolute;
    top: 15px;
    right: 25px;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
    color: #666;
}

.close:hover {
    color: #333;
}

#modalImage {
    max-width: 100%;
    max-height: 70vh;
    border-radius: 8px;
}

.modal-actions {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 20px;
}

@media (max-width: 768px) {
    .data-grid {
        grid-template-columns: 1fr;
    }
    
    .search-input-group {
        flex-direction: column;
    }
    
    .card-actions {
        justify-content: center;
    }
}
</style>

<script>
// 图片预览功能
function openImageModal(imageSrc) {
    document.getElementById('modalImage').src = imageSrc;
    document.getElementById('imageModal').style.display = 'block';
}

function closeImageModal() {
    document.getElementById('imageModal').style.display = 'none';
}

// 删除确认功能
function confirmDelete(docId) {
    document.getElementById('deleteForm').action = '/delete/' + docId;
    document.getElementById('deleteModal').style.display = 'block';
}

function closeDeleteModal() {
    document.getElementById('deleteModal').style.display = 'none';
}

// 点击模态框外部关闭
window.onclick = function(event) {
    const imageModal = document.getElementById('imageModal');
    const deleteModal = document.getElementById('deleteModal');
    
    if (event.target === imageModal) {
        closeImageModal();
    }
    if (event.target === deleteModal) {
        closeDeleteModal();
    }
}
</script>
{% endblock %}