// pages/garden/components/growth.js
const config = require("../../../config/config");
const {
  api
} = require("../../../utils/api");
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    plantId: {
      type: String,
      require: true
    },
  },
  observers: {
    // 监听 visible 的变化
    'plantId': function (newVal) {
      console.log(newVal);
      this.setData({
        id: newVal
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    visible: false,
    tags: [{
        label: '日常记录',
        checked: true
      },
      {
        label: '🌱 发芽',
        checked: false
      },
      {
        label: '🌼 开花',
        checked: false
      },
      {
        label: '🍒 结果',
        checked: false
      }
    ],
    id: '',
    pic: '',
    form: {
      name: '',
      desc: '',
      content: '',
      ossIds: [],
      tag: '日常记录'
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    change() {
      const tmp = !this.data.visible
      this.setData({visible:tmp})
      if (tmp === false) {
        this.setData({
          pic:'',
          form:{}
        })
      }
     
    },
    check(e) {
      const index = e.currentTarget.dataset.index
      const tmps = this.data.tags.map(e => {
        e.checked = false
        return e
      })
      tmps[index].checked = true
      this.setData({
        tags: tmps
      })
      this.data.form.tag = tmps[index].label
    },
    input(e) {
      const value = e.detail.value
      this.data.form.content = value
    },
    upload() {
      const _this = this
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        success(res) {
          const avatarUrl = res.tempFiles[0].tempFilePath
          wx.showLoading({
            title: '请稍后...',
          })
          _this.setData({
            pic: avatarUrl
          })
          wx.uploadFile({
            filePath: avatarUrl,
            name: 'file',
            header: {
              'Authorization': 'Bearer ' + wx.getStorageSync('token'),
            },
            url: config.baseUrl + '/oss/upload',
            success: res => {
              wx.hideLoading()
              var data = JSON.parse(res.data);
              if (data.code === 200) {
                const ossIds = [data.data.file.id]
                _this.data.form.ossIds = ossIds
              }
            }
          })
        }
      })
    },
    submit() {
      this.data.form.plantId = this.data.id
      if (this.data.form.ossIds.length === 0) {
        wx.showModal({
          content: '请添加照片'
        })
        return
      }
      wx.showToast({
        icon: 'loading',
      })
      api('/plant/grow/addRecord', 'POST', this.data.form, 'json').then(res => {
        if (res.code === 200) {
          const reset = {
            name: '',
            desc: '',
            content: '',
            ossIds: [],
            tag: '日常记录'
          }
          this.setData({
            visible: false,
            form: reset
          })
          wx.showToast({
            icon: 'success',
            title: res.msg,
          })

          this.triggerEvent('ok', {
            value: ""
          });
        }
      })
    }
  }
})