// order.js
const app = getApp(),
  { hostname, business_id } = app.globalData,
  users_id = wx.getStorageSync('users_id')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    empty_box: {
      tips_img: 'https://ypx.shenmikj.com/canyin/Uploads/canyin/box_yellow.png',
      tips_word: '暂无订单',
      tips_btn: '返回首页',
    },

    tabIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getSubStore(that);
    that.getPayList()

  },

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

  },
  // 切换选项卡
  switchTab(e) {

    let { index } = e.target.dataset || 0
    if (Number(index) == 0) {
      this.getOrderList()
      this.getPayList()
    } else {
      this.goAppointmentList()
    }
    this.setData({ tabIndex: index })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/index/index?u_openid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid;  //当前页面url
        console.log(url, '/pages/index/index?u_openid=' + userid);
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },

  // 获取优惠购买订单列表
  getPayList() {
    let that = this,
      users_id = wx.getStorageSync("users_id")
    console.log(business_id)
    wx.request({
      url: hostname + 'kaiguan/getOrder',
      data: {
        business_id,
        users_id
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      dataType: 'JSON',
      success: (res) => {
        let payList = JSON.parse(res.data).data
        that.setData({ payList: payList ? payList : [] })
        console.log(payList, "payList")
        // wx.hideLoading();
      },
      fail: (res) => { },
      complete: (res) => {
        setTimeout(() => {
          wx.hideLoading();
        }, 500)
      },


    })
  },


  // 获取购买订单列表
  getOrderList() {
    let that = this,
      users_id = wx.getStorageSync("users_id")
    console.log(business_id)
    wx.request({
      url: hostname + 'Order/get_order_list',
      data: {
        business_id,
        users_id,
        page: this.data.page
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      dataType: 'JSON',
      success: (res) => {
        let orderList = JSON.parse(res.data).data
        that.setData({ orderList: orderList ? orderList : [] })
      },
      fail: (res) => { },
      complete: (res) => {
        setTimeout(() => {
          wx.hideLoading();
        }, 500)
      },
    })
  },


  // 获取预约订单列表
  goAppointmentList() {
    var that = this;
    var business_id = app.globalData.business_id;
    var user_id = wx.getStorageSync("users_id");
    console.log(business_id + user_id, "商户id和userid")
    wx.request({
      url: hostname + 'Order/get_app_list',
      data: {
        business_id,
        user_id
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      success: function (res) {
        console.log(res.data.data, "预约订单")

        var appointmentList = [];
        var appointmentListData = res.data.data
        if (appointmentListData != undefined) {
          for (var i = 0; i < appointmentListData.length; i++) {
            appointmentList.push(appointmentListData[i])
          }
          that.setData({
            appointmentList: appointmentList.reverse()
          })
          console.log(that.data.appointmentList)
        } else {
          appointmentList: []
        }
      },
      fail: (res) => { },
      complete: (res) => {
        setTimeout(() => {
          wx.hideLoading();
        }, 500)
      }
    })
  },

  //获取门店列表
  getSubStore(that) {
    wx.request({
      url: app.globalData.hostname + 'ManyStore/IndexSubsidiaryStore',
      data: { business_id },

      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: '',
      success: function (res) {

        if (res.statusCode == 200) {
          var recommend = [];
          var recommendData = res.data.data;

          if (recommendData.length != undefined) {
            for (var i = 0; i < recommendData.length; i++) {
              recommend.push(recommendData[i]);
            }
          }
          that.setData({
            recommend: recommend
          })
        } else {
          that.setData({
            recommend: []
          })
        }
      }
    })
  },


  // 删除优惠买单订单
  toDel: function (e) {
    var that = this;
    var did = e.currentTarget.dataset.did;
    wx.request({
      url: hostname + 'kaiguan/delOrder',
      data: {
        id: did
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      success: function (res) {
        console.log(res, '取消按钮')
        that.getPayList();
        // that.getAppointmentList();
        wx.showLoading({
          title: '正在删除',
        })

      }
    })
  },

  // 取消订单
  toCancel(e) {
    var that = this;
    var order_sn = e.currentTarget.dataset.order;
    wx.request({
      url: hostname + 'Order/delOrderInfo',
      data: {
        user_id: wx.getStorageSync('users_id'),
        order_sn
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      success: function (res) {
        console.log(res, '取消按钮')
        that.getOrderList();
        // that.getAppointmentList();
        wx.showLoading({
          title: '正在删除',
        })

      }
    })
  },

  //确认订单
  toSure(e) {
    var that = this;
    var order_sn = e.currentTarget.dataset.order;
    wx.request({
      url: hostname + 'Order/completeOrder',
      data: {
        user_id: wx.getStorageSync('users_id'),
        order_sn
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      success: function (res) {
        console.log(res, '取消按钮')
        that.getOrderList();
        // that.getAppointmentList();
        wx.showLoading({
          title: '正在删除',
        })

      }
    })
  },


  __emptyAction() {
    wx.switchTab({
      url: '../index/index',
    })
  }
})