// DataManager：任务数据管理器，负责任务的增删改查、分组、校验和变更通知
export default class DataManager {
  constructor(tasks = [], dimensions = []) {
    this.tasks = tasks;
    this.dimensions = dimensions;
    this.listeners = [];
  }
  // 新增任务
  addTask(task) {
    this.tasks.push(task);
    this.notify();
  }
  // 更新任务
  updateTask(id, data) {
    const idx = this.tasks.findIndex(t => t.id === id);
    if (idx !== -1) {
      this.tasks[idx] = { ...this.tasks[idx], ...data };
      this.notify();
    }
  }
  // 删除任务
  removeTask(id) {
    this.tasks = this.tasks.filter(t => t.id !== id);
    this.notify();
  }
  // 按维度分组
  groupByDimension(dim) {
    const groups = {};
    this.tasks.forEach(task => {
      const key = task.dimensions && task.dimensions[dim] ? task.dimensions[dim].id : '未分组';
      if (!groups[key]) groups[key] = { id: key, name: task.dimensions && task.dimensions[dim] ? task.dimensions[dim].name : '未分组', tasks: [] };
      groups[key].tasks.push(task);
    });
    return Object.values(groups);
  }
  // 变更监听
  onChange(cb) {
    this.listeners.push(cb);
  }
  notify() {
    this.listeners.forEach(cb => cb(this.tasks));
  }
} 