{% extends "base.html" %}

{% block title %}编辑数据{% endblock %}

{% block content %}
<div class="edit-container">
    <div class="edit-header">
        <h1>编辑数据</h1>
        <p>修改您的数据信息</p>
    </div>

    <form method="POST" class="edit-form" id="editForm">
        <!-- 隐藏字段 -->
        <input type="hidden" name="_id" value="{{ data._id }}">
        <input type="hidden" name="image" value="{{ data.image }}">

        <!-- 图片显示 -->
        {% if data.image %}
            <div class="image-preview">
                <h3>关联图片</h3>
                <img src="{{ url_for('serve_image', filename=data.image) }}" alt="数据图片" onclick="openImageModal('{{ url_for('serve_image', filename=data.image) }}')">
            </div>
        {% endif %}

        <!-- 数据字段编辑 -->
        <div class="fields-container">
            <h3>数据字段</h3>
            {% for key, value in data.items() %}
                {% if key not in ['_id', 'image', 'user_id'] %}
                    <div class="form-group">
                        <label for="{{ key }}">{{ key }}:</label>
                        <input type="text" id="{{ key }}" name="{{ key }}" value="{{ value }}" class="form-input">
                    </div>
                {% endif %}
            {% endfor %}
        </div>

        <!-- 操作按钮 -->
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">
                <i class="icon">💾</i>
                保存修改
            </button>
            <a href="{{ url_for('my_data') }}" class="btn btn-secondary">
                <i class="icon">↩️</i>
                取消
            </a>
        </div>
    </form>
</div>

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

<style>
.edit-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
}

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

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

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

.edit-form {
    background: white;
    border-radius: 12px;
    padding: 30px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.image-preview {
    margin-bottom: 30px;
    text-align: center;
}

.image-preview h3 {
    color: #333;
    margin-bottom: 15px;
    font-size: 18px;
}

.image-preview img {
    max-width: 100%;
    max-height: 300px;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.3s ease;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.image-preview img:hover {
    transform: scale(1.02);
}

.fields-container {
    margin-bottom: 30px;
}

.fields-container h3 {
    color: #333;
    margin-bottom: 20px;
    font-size: 18px;
    padding-bottom: 10px;
    border-bottom: 2px solid #e9ecef;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    color: #555;
    font-weight: 600;
    font-size: 14px;
    text-transform: capitalize;
}

.form-input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 16px;
    transition: all 0.3s ease;
    background: #f8f9fa;
}

.form-input:focus {
    outline: none;
    border-color: #667eea;
    background: white;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-actions {
    display: flex;
    gap: 15px;
    justify-content: center;
    padding-top: 20px;
    border-top: 1px solid #e9ecef;
}

.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 600;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

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

.btn-secondary:hover {
    background: #5a6268;
    transform: translateY(-2px);
}

/* 模态框样式 */
.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;
}

.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;
}

@media (max-width: 768px) {
    .edit-container {
        padding: 10px;
    }
    
    .edit-form {
        padding: 20px;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .btn {
        width: 100%;
        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';
}

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

// 表单验证
document.getElementById('editForm').addEventListener('submit', function(e) {
    const inputs = this.querySelectorAll('.form-input');
    let hasEmptyFields = false;
    
    inputs.forEach(input => {
        if (!input.value.trim()) {
            hasEmptyFields = true;
            input.style.borderColor = '#dc3545';
        } else {
            input.style.borderColor = '#e9ecef';
        }
    });
    
    if (hasEmptyFields) {
        e.preventDefault();
        alert('请填写所有字段！');
        return false;
    }
});

// 输入时清除错误状态
document.querySelectorAll('.form-input').forEach(input => {
    input.addEventListener('input', function() {
        if (this.value.trim()) {
            this.style.borderColor = '#e9ecef';
        }
    });
});
</script>
{% endblock %}