//logs.js
var util = require('../../utils/util.js')
// var fakeData = require('../../utils/web-data')

Page({
  data: {
    inputData: '',
    todos: [],
    leftCount: 0,
    allCompleted: false,
    logs: [],
    todo_list_key: 'todo_list',
    todo_logs_key: 'todo_logs',
  },
  // 生命周期函数
  onLoad: function () {
    this.load()
    this.setData({
      // logs: (wx.getStorageSync('logs') || []).map(function (log) {
      //   return util.formatTime(new Date(log))
      // })
    })
  },

  // set and load data
  save: function() {
    wx.setStorageSync(this.data.todo_list_key, this.data.todos)
    wx.setStorageSync(this.data.todo_logs_key, this.data.logs)
  },

  load: function() {
    let todos = wx.getStorageSync(this.data.todo_list_key)
    if (todos) {
      let leftCount = todos.filter(item => !item.completed
      ).length
      this.setData({ todos: todos, leftCount: leftCount })
    }
    let logs = wx.getStorageSync(this.data.todo_logs_key)
    if (logs) {
      this.setData({ logs: logs })
    }
  },

  // handlers
  inputChangeHandle: function(e) {
    this.setData({ input: e.detail.value })
  },

  addTodoHandle: function(e) {
    if (!this.data.input || !this.data.input.trim()) return
    let todos = this.data.todos
    todos.push({ 
      completed: false, 
      name: this.data.input,
      timestamp: new Date(),
    })
    let logs = this.data.logs 
    logs.push({ timestamp: new Date(), action: 'New', name: this.data.input })
    this.setAndSave(null, todos, logs)
    // this.setData({
    //   input: '',
    //   todos: todos,
    //   leftCount: this.data.leftCount + 1,
    //   logs: logs
    // })
    // this.save()
  },

  toggleTodoHandle: function(e) {
    let index = e.currentTarget.dataset.index
    let todos = this.data.todos
    todos[index].completed = !todos[index].completed  // 切换完成状态
    let logs = this.data.logs 
    logs.push({
      timestamp: new Date(),
      action: todos[index].completed ? 'Mark Done' : 'Mark Todo',
      name: todos[index].name
    })
    this.setAndSave(null, todos, logs)
    // this.setData({
    //   input: '',
    //   todos: todos,
    //   leftCount: todos[index].completed ? this.data.leftCount - 1 : this.data.leftCount + 1,
    //   logs: logs
    // })
    // this.save()
    // console.log('toggleTodoHandle')
  },

  removeTodoHandle: function(e) {
    let index = e.currentTarget.dataset.index,
        todos = this.data.todos,
        remove = todos.splice(index, 1)[0],
        logs = this.data.logs
    logs.push({
      timestamp: new Date(),
      action: 'remove',
      name: remove.name,
    })
    this.setAndSave(null, todos, logs)
    // this.setData({
    //   todos: todos,
    //   leftCount: this.data.leftCount - (remove.completed ? 0 : 1),
    //   logs: logs
    // })
    // this.save()
  },

  toggleAllHandle: function(e) {
    this.data.allCompleted = !this.data.allCompleted
    let todos = this.data.todos
    todos.map(item=>item.completed=this.data.allCompleted)
    let logs = this.data.logs
    logs.push({
      timestamp: new Date(),
      action: this.data.allCompleted ? 'Mark Done' : 'Mark Todo',
      name: 'All tasks'
    })
    this.setAndSave(null, todos, logs)
    // this.setData({
    //   todos: todos,
    //   leftCount: this.data.allCompleted ? 0 : todos.length,
    //   logs: logs
    // })
    // this.save()
  },
  
  clearCompletedHandle: function(e) {
    let todos = this.data.todos,
        remains = []
    todos.map(item => {
      item.completed || remains.push(item)
    })
    let logs = this.data.logs
    logs.push({
      timestamp: new Date(),
      action: 'clear',
      name: 'Finished Task'
    })
    this.setAndSave(null, remains, logs)
    // this.setData({ todos: remains, logs: logs })
    // this.save()
  },

  // helpers
  setAndSave: function(input, todos, logs) {
    let leftCount = todos.filter(item => !item.completed).length
    let showTodos = todos.sort((a, b) => a.completed || a.timestamp < b.timestamp )
    this.setData({
      input: input || '',
      todos: showTodos,
      logs: logs,
      leftCount: leftCount
    })
    this.save()
  }

})


















