const wx2my = require('../../../../wx2my');
const Behavior = require('../../../../Behavior');
// subPackage/page/pages/subPage/subPage.js
const app = getApp();
const wxService = require('../../../../utils/wxService');
const utils = require('../../../../utils/util');
const envInfo = require('../../../../config/index').envInfo;
let renderPage = null; // 渲染页面数据对象(安全渲染)
let handlerDataOnPage = null;
let config = require('../../../../configScreen/configScreen');
const logger = utils.logger;

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    pages: [],
    render: null,
    // 渲染页面数据对象(安全渲染)
    renderPageOver: false,
    // 渲染页面结束
    integralNum: 0,
    couponNum: 0,
    // isAllScreen: fullSucreen.isAllScreen
    isAllScreen: config.isAllScreen,
    token: '',
    isAuthorization: false,
    pageId: 3,
    baseUserInfo: null,
    cardMember: null,
    curMemberTrial: false, // 当前会员是不是体验者
    curAccessiblePathArr: [] // 当前体验版本对应的路径
  },

  /**
    * 生命周期函数--监听页面加载
    */
  onLoad(options) {
    console.log('load', options)
    const {
      pageId
    } = options || this.data;
    this.setData({
      token: wx2my.getStorageSync('token'),
      pages: [],
      // 清空当前页
      pageId
    }, () => {
      let currentPageId = pageId ? pageId : this.data.pageId;
      this.initPage(currentPageId);
    });
  },

  /** 
   * 监听子组件事件
   * */
  updatePage(ev) {// const {pageId} = ev.detail
    // this.setData({
    //   pages: [],
    //   pageId
    // },()=>{
    //   this.initPage(pageId)
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(options) {
    const baseUserInfo = wx2my.getStorageSync('_baseUserInfo') && wx2my.getStorageSync('_baseUserInfo').data
    const reLoginErrFlag = wx2my.getStorageSync('reLoginErr') && wx2my.getStorageSync('reLoginErr').data
    if (baseUserInfo && !reLoginErrFlag) {
      this.setData({
        token: wx.getStorageSync('token'),
        baseUserInfo,
        cardMember,
        isAuthorization: false
      }, () => {
        // 积分数量
        this.getMemberPointInfo()
        // 优惠券数量
        this.getMemberCouponInfo()
      })
    } else if (!baseUserInfo && reLoginErrFlag) {
      this.setData({
        isAuthorization: true
      })
    } else {
      this.setData({
        isAuthorization: true
      })
    }
  },

  // 查询积分信息
  getMemberPointInfo() {
    wx2my.showLoading({
      title: '加载中'
    });
    wxService.get(`/member/memberPoint/getMyPointInfo`).then(res => {
      if (res) {
        const {
          result,
          data
        } = res.data;

        if (result == 0) {
          wx2my.hideLoading();
          this.setData({
            integralNum: data && data.point || 0
          });
        }
      }
    });
  },

  // 查询优惠券数量
  getMemberCouponInfo() {
    wx2my.showLoading({
      title: '加载中'
    });
    wxService.post(`/coupon/coupon/memberCouponCount`).then(res => {
      if (res) {
        const {
          result,
          data
        } = res && res.data;

        if (result == 0) {
          wx2my.hideLoading();
          this.setData({
            couponNum: data && data[1] || 0
          });
        }
      } else {
        wx2my.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },

  // page 信息
  initPage(pageId) {
    this.setData({
      pages: [],
      products: [],
      groupLoadState: 'complete', // 活动分组加载标志
      homeApiLoading: true,
      render: null, // 渲染页面数据对象(安全渲染)
      isLastPage: false, // 页面段落加载完成
      renderPageOver: false, // 渲染页面结束
    }, () => {
      this.initHomePage()
    })
    handlerDataOnPage = new utils.HandlerDataOnPage()
    renderPage = require('../../../../utils/renderPage').getRenderPage()
    handlerDataOnPage.init(this, 'pages')
  },
  initHomePage() {
    wx2my.showLoading({
      title: '加载中'
    });
    wxService.post(`/merchant/modelPage/getDetailById?pageId=${pageId}&brandId=${app.globalData.brandId}`).then(res => {
      if (!res) return false;
      const {
        result,
        data
      } = res.data;

      if (result == 0) {
        // 动态设置title
        wx2my.setNavigationBarTitle({
          title: data.page.title
        }); // 段落

        const products = data.paragraphs;
        const filterResult = products.map((res, index) => {
          return {
            ...res,
            index
          };
        }).filter(item => {// wx.hideLoading()
        });
        const render = renderPage.initPaging(filterResult).initRenderData(products);
        this.data.render = render;
        const renderData = render.next();
        this.data.renderPageOver = renderData.done;
        this.renderPageFunc({
          products: renderData.value
        });
      }
    }).finally(() => {
      wx2my.hideLoading();
    });
  },

  /**
  * 渲染数据(合并之前的数据)
  * @param products 数组数据
  * @param isLastPage
  * @param now
  */
  renderPageFunc({
    products
  }) {
    let productData = handlerDataOnPage.handlerArray(products);
    logger.log('productData.keysNum', productData.keysNum(), productData);
    this.setData({
      ...productData
    }, () => {
      wx2my.hideLoading();
    });
  }

});