// pages/personorder/personorder.js
const app = getApp()
import orderApi from '../../api/order'
import storeApi from '../../api/store'
import productApi from '../../api/product'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 0,
    orderHeight: 0,
    tabs: [],
    orderList: [],
    pageNo: 1,
    loadingEnd: false,
    tabs: [{
        title: '全部'
      },
      {
        title: '代消费'
      },
      {
        title: '待评价'
      },
      {
        title: '已完成'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function () {
    console.log(this)
    const isAuth = await app.isAuth()
    if (!isAuth) {
      wx.redirectTo({
        url: '/pages/prompt/prompt?page=' + this.route,
      })
    }


    this.getOrderHeight();

  },
  //动态生成产品的父盒子高度
  getOrderHeight: function () {
    var h = wx.getSystemInfoSync().windowHeight * (750 / wx.getSystemInfoSync().windowWidth);
    var orderHeight = h - 95;
    this.setData({
      orderHeight
    })
  },
  getOrderList: async function (params) {
    var pageNo = this.data.pageNo;
    var orderList = await orderApi.getAllOrderList(pageNo, 3);
    var list = await this.handelOrderList(orderList.list);
    //代消费订单
    var writeOffOrderList = [];
    //待评价订单
    var commentOffOrderList = [];
    //已完成订单
    var finishOrderList = [];
    list.forEach(function (item, u, array) {
      if (item.status == 2 && item.writeOffStatus == 1) {
        writeOffOrderList.push(item)
      }
      if (item.status == 2 && item.writeOffStatus == 2) {
        finishOrderList.push(item)
      }
    })
    var tabs = this.data.tabs;
    tabs[0].list = list;

  },

  // 获取全部列表
  getAllOrderList: async function () {
    const self = this
    var pageNo = this.data.pageNo;
    // 获取全部订单
    var orderList = await orderApi.getAllOrderList(pageNo, 4);
    var list = await this.handelOrderList(orderList.list);
    var tabs = this.data.tabs;
    tabs[0].list = list;
    this.setData({
      tabs
    })
    // return list;
  },
  // 获取代消费列表
  getwriteOffOrderList: async function () {
    const self = this
    var pageNo = this.data.pageNo;
    // 获取代消费订单
    var orderList = await orderApi.getOrderList(pageNo, 4, 2, 1);
    var list = await this.handelOrderList(orderList.list);
    var tabs = this.data.tabs;
    tabs[1].list = list;
    this.setData({
      tabs
    })
    // return list;
  },
  // 获取待评价列表
  getcommentOffOrderList: async function () {
    const self = this
    var pageNo = this.data.pageNo;
    // 获取代消费订单
    var orderList = await orderApi.getOrderList(pageNo, 4, 2, 1);
    var list = await this.handelOrderList(orderList.list);
    var tabs = this.data.tabs;
    tabs[2].list = list;
    this.setData({
      tabs
    })
    // return list;
  },

  // 获取已经完成的列表
  getfinishOrderList: async function (params) {
    const self = this
    var pageNo = this.data.pageNo;
    // 获取代消费订单
    var orderList = await orderApi.getOrderList(pageNo, 4, 2, 1);
    var list = await this.handelOrderList(orderList.list);
    var tabs = this.data.tabs;
    tabs[3].list = list;
    this.setData({
      tabs
    })
    // return list;
  },
  onTabClick(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },


  onChange(e) {
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
  },
  handleClick(e) {

  },




  handelOrderList: async function (list) {

    list.forEach(async function (item, i, array) {
      let store = await storeApi.getStoreById(item.storeId);
      let product = await productApi.getProductById(item.productId);
      item.address = store.info.address;
      item.productName = product.info.productName;
      item.productImg = product.info.productImg;
    })
    return list

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: async function () {
    await this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  gotoComment: function (e) {

    wx.navigateTo({
      url: '../commentinfo/commentinfo',
    })
  }
})