const { api } = require("../../utils/api")

// index.js
Page({
  data: {
    list:[],
    dash:null
  },
  onLoad(options) {
    
  },
  onShow(){
    this.fetchList()
  },
  goInfo(e){
    const id  = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../garden/info?id=' + id,
    })
  },
  fetchList(){
    const user = wx.getStorageSync('user')
    const data = {userId: user.id,current:1,pageSize:20}
    api('/plant/todayCare','GET',data,'json').then(res => {
      if (res.code === 200){
        if (res.data === null){
          this.setData({list:[]})
          return
        }
        
        this.setData({list:res.data})
      }
    })

    api('/plant/taskProgress','GET').then(res => {
      if (res.code == 200){
        const dash = res.data
        dash.progress = parseFloat((dash.progress  * 100).toFixed(2))
        this.setData({dash:dash})
      }
    })
  },
  make(e){
    const item = e.currentTarget.dataset.item
    const status = parseInt(e.currentTarget.dataset.status)
    const data = {id:item.id,plantId:item.plantId,  name:item.name,remark:item.name,status: status}
    if (status != 3){
      this.changeStatus(data)
    } else {
      wx.showModal({
        content: '确认跳过该任务？',
        complete: (res) => {
          if (res.confirm) {
            this.changeStatus(data)
          }
        }
      })
    }
    
  },
  changeStatus(data){
    api('/plant/makeCare','POST',data,'json').then(res => {
      if (res.code === 200){
        wx.requestSubscribeMessage({
          tmplIds: ['iG5GYMPQAgKxIE9zZNOgKUghR6hP2WKKwws1RfLABuE'],
          success (res) { 
            
          }
        })
        this.fetchList()
      }
    })
  },
  goAdd(){
    wx.switchTab({
      url: '../community/index',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
