{% extends "admin/base.html" %}

{% block title %}编辑待办事项{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">
        <i class="fas fa-edit me-2"></i>编辑待办事项
    </h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">
            <a href="/admin/todos/{{ todo.id }}" class="btn btn-sm btn-outline-secondary">
                <i class="fas fa-arrow-left me-1"></i>返回详情
            </a>
        </div>
    </div>
</div>

<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/admin">首页</a></li>
        <li class="breadcrumb-item"><a href="/admin/todos{% if todo.device_id %}?device_id={{ todo.device_id }}{% endif %}">待办事项管理</a></li>
        <li class="breadcrumb-item"><a href="/admin/todos/{{ todo.id }}">{{ todo.title }}</a></li>
        <li class="breadcrumb-item active">编辑</li>
    </ol>
</nav>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-clipboard-list me-2"></i>待办事项信息
                </h5>
            </div>
            <div class="card-body">
                <form method="post" action="/admin/todos/{{ todo.id }}/edit">
                    <div class="mb-3">
                        <label for="title" class="form-label">标题 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="title" name="title" value="{{ todo.title }}" required>
                    </div>
                    
                    <div class="mb-3">
                        <label for="description" class="form-label">描述</label>
                        <textarea class="form-control" id="description" name="description" rows="3">{{ todo.description or '' }}</textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label for="device_id" class="form-label">关联设备 <span class="text-danger">*</span></label>
                        <select class="form-select" id="device_id" name="device_id" required>
                            {% for device in devices %}
                            <option value="{{ device.device_id }}" {% if device.device_id == todo.device_id %}selected{% endif %}>
                                {{ device.name or device.device_id }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label for="due_date" class="form-label">截止时间</label>
                        <input type="datetime-local" class="form-control" id="due_date" name="due_date" 
                               {% if todo.due_date %}value="{{ todo.due_date.strftime('%Y-%m-%dT%H:%M') }}"{% endif %}>
                        <div class="form-text">留空表示无截止时间</div>
                    </div>
                    
                    <div class="mb-3">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="is_completed" name="is_completed" 
                                   {% if todo.is_completed %}checked{% endif %}>
                            <label class="form-check-label" for="is_completed">
                                标记为已完成
                            </label>
                        </div>
                    </div>
                    
                    <div class="d-flex justify-content-between">
                        <a href="/admin/todos/{{ todo.id }}" class="btn btn-secondary">
                            <i class="fas fa-times me-1"></i>取消
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save me-1"></i>保存更改
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-info-circle me-2"></i>当前信息
                </h5>
            </div>
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-4 fw-bold">创建时间:</div>
                    <div class="col-sm-8">{{ todo.created_at.strftime('%Y-%m-%d %H:%M') }}</div>
                </div>
                
                <div class="row mb-2">
                    <div class="col-sm-4 fw-bold">更新时间:</div>
                    <div class="col-sm-8">{{ todo.updated_at.strftime('%Y-%m-%d %H:%M') }}</div>
                </div>
                
                {% if todo.is_completed and todo.completed_at %}
                <div class="row mb-2">
                    <div class="col-sm-4 fw-bold">完成时间:</div>
                    <div class="col-sm-8">{{ todo.completed_at.strftime('%Y-%m-%d %H:%M') }}</div>
                </div>
                {% endif %}
                
                <hr>
                
                <p class="text-muted">修改待办事项后，更新将发送到关联的设备。</p>
            </div>
        </div>
    </div>
</div>
{% endblock %}