// nova-travel/page/page-view/page-view.js
var Parse = getApp().Parse;
var company = getApp().globalData.company
var Nova = getApp().Nova;
const getTabs = require("../../utils/getTabs")

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    pageid: null,
    blocks: null,
    options: null,
    titleColor: {
      type: String,
      value: '#ffffff'
    },
    compcb: null,
    pulldownTime: { // 生命周期 pulldown：父组件在当前组件页面刷新是触发
      type: Number,
      value: null,
      observer: function (current, old) { // 属性值变化时执行
        console.log("pulldown diypage", current, old)
        if (current != old) {
          this.initPageBlocks().then(result => {
            wx.stopPullDownRefresh();
          });
        }
      }
    }
    // {
    //   type: Object,
    //   value: null,
    //   observer:"update"
    // }
  },
  pageLifetimes: {
    // onPullDownRefresh: function(){
    //   console.log("pulldown diypage",this.data)
    //   setTimeout(() => {
    //     wx.stopPullDownRefresh();
    //   }, 2000);
    // }
  },
  lifetimes: {

    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  attached: async function () {
    // 在组件实例进入页面节点树时执行
    console.log("attached diypage", this.data)
    this.data.options && this.setData({
      pageid: this.data.options.id,
    })
    this.initPageBlocks()
    this.getActiveColor()
  },
  /**
   * 组件的初始数据
   */
  data: {
    activeColor: null,
    background: "#ffffff"
  },


  /**
   * 组件的方法列表
   */
  methods: {
    onPullDownRefresh() {
      console.log("onpulldown diypage")
      // wx.startPullDownRefresh();
      this.initPageBlocks().then(result => {
        wx.stopPullDownRefresh();
      });
    },
    // 初始化页面区块内容
    async initPageBlocks() {
      let { pageid } = this.data
      console.log(pageid);
      if (pageid) {
        console.log("initPageBlocks")
        let query = new Parse.Query("DiyPage");
        let tab = await query.get(pageid);
        if (tab && tab.id) {
          this.setData({
            titleName: tab.get("titleBar") && tab.get("titleBar").title,
            titleType: tab.get("titleBar") && tab.get("titleBar").type,
            blocks: tab.get("blocks"),
            background: tab.get("titleBar") && tab.get("titleBar").backgroundColor
          })
        }
        console.log(this.data)
        return true;
      } else {
        return false
      }
    },

    async getActiveColor() {
      let tabbarOption = await getTabs.getDiyTabs()
      let activeColor = tabbarOption.activeColor && tabbarOption.activeColor
      this.setData({
        activeColor
      })
    },


    // 获取父组件传递的数据
    update(newValue) {
      this.blocks = newValue
      console.log(this.blocks)
    },


  },
  pageLifetimes: {
    ready: function (ev) {
    },
    show: function (ev) {
      console.log("pageLifetimes", ev)
    }
  }
})
