// pages/skinRecord/components/plan/plan.js
import {
  post,
} from '../../../../utils/http';
Component({
  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true
  },
  properties: {
    analysis: {
      type: Object,
      value: null,
    },
    plan: {
      type: Object,
      value: null,
      observer(newVal) {
        if(newVal && this.data.fromList == "1") {
          this.setData({
            toView: "btm-btn"
          })
        }
      }
    },
    analysis_id: {
      type: String,
      value: ''
    },
    fromList: {
      type: String,
      value: '',
    }
  },

  pageLifetimes: {
    show: function() {
      // 页面被展示
    },
    hide: function() {
      // 页面被隐藏
      this.setData({
        visible: false
      })
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    visible: false,
    toView: ""
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onAddPlan() {
      let that = this;
      let plan = this.data.plan
      if(plan.is_has_plan == 1) {
        this.setData({
          visible: true
        })
      } else {
        that.addPlanApi()
      }
    },
    goToBeautyPlan() {
      wx.navigateTo({
        url: '/pages/beautyPlan/beautyPlan?analysis_id=' + this.data.analysis_id,
      })
    },
    addPlanApi() {
      post('api/user/plan/add',{
        analysis_id: this.data.analysis_id
      },(res) => {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        this.setData({
          ['plan.is_add_plan']: 1
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/beautyPlan/beautyPlan?analysis_id=' + this.data.analysis_id,
          })
        }, 1000);
      })
    },
    onCancelUse() {
      this.setData({ visible: false })
    },
  }
})
