var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
    data: {
        order_status: '',
        evaluate_status: '',
        orderList: [],
        page: 1,
        size: 10,
        totalPages:0,
        tabList: ['全部', '待付款', '待发货', '待收货', '待评价', '维权'],
        tabIndex: 0,
        orderIds:[],
        currentStoreId: ''
        // markers: [{
        //   iconPath: "/static/images/rider.png",
        //   id: 0,
        //   latitude: 31.834082,
        //   longitude: 117.232939,
        //   width: 40,
        //   height: 40,
        //   callout: {
        //     content: '距离你2.77km',
        //     color: '#fe7200',
        //     display: 'ALWAYS',
        //     padding: 10,
        //     borderRadius: 30
        //   }
        // }]
    },
    toggleTab(e) {
        this.setData({
            tabIndex: e.currentTarget.dataset.index,
            orderList: [],
            page: 1,
        });
        this.switchOrderType(e.currentTarget.dataset.index);
    },
    swiperChange(e) {
        this.setData({
            tabIndex: e.detail.current
        });
        this.switchOrderType(e.detail.current);
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        if (options.tabIndex) {
            this.setData({
                tabIndex: options.tabIndex
            });
        }
    },
    getOrderList() {
      let that = this;
      wx.showLoading({
        title: '加载中...',
      });
        util.request(api.OrderList,
            {
                order_status: that.data.order_status,
                evaluate_status: that.data.evaluate_status,
                page: that.data.page,
                size: that.data.size
            }).then(function (res) {
              if (res.errno === 0) {
                let orderList = that.data.orderList.concat(res.data.data);
                // console.log(orderList);
                that.setData({
                  orderList: orderList,
                  totalPages: res.data.totalPages
                }, () => {
                  //获取待付款倒计时
                  that.data.orderList.forEach((item, num) => {
                    if (item.pay_status == 0 || item.pay_status == 1) {
                      util.countdown(that, that.data.orderList, 'orderList', num)
                    }
                  })
                  });
                wx.hideLoading();
              }
        });
    },
  payOrder(event) {
        wx.redirectTo({
          url: '/pages/pay/pay?orderIds=' + event.target.dataset.orderId
                + '&actualPrice=' + event.target.dataset.actualPrice
        })
  }, 
  applyRefund(event) {
    wx.navigateTo({
      url: '/pages/ucenter/applyRefund/applyRefund?orderId=' + event.target.dataset.orderId
        + '&refundMoney=' + event.target.dataset.actualPrice + '&merchOrderSn=' + event.target.dataset.merchOrderSn 
    })
  }, 
    againBuy(event) {
      let orderId = event.target.dataset.orderId;
      // let goodType = event.target.dataset.goodType;
      // if (goodType!= '00'){
      //   wx.showModal({
      //     title: '提示信息',
      //     content: '非保税仓商品不允许再来一单，需门店扫描二维码进行购买',
      //     showCancel: false
      //   });
      //   return;
      // }
        util.request(api.CartAddByOrder, {orderId: orderId}).then(function (res) {
            if (res.errno === 0) {
                wx.switchTab({
                    url: '/pages/cart/cart',
                });
            } else {
                wx.showToast({
                    title: res.errmsg,
                    image: '/static/images/icon_error.png',
                    duration: 2000
                });
            }
        });
  },
  againBuyDisabel(event) {
    let goodsType = event.target.dataset.goodsType;
    let isStore = event.target.dataset.isStore;
    console.log(goodsType);
    if (goodsType != '00' && isStore) {
      wx.showModal({
        title: '提示信息',
        content: '非保税仓商品不允许再来一单，需门店扫描二维码进行购买',
        showCancel: false
      });
    }
    if (!isStore) {
      wx.showModal({
        title: '',
        content: '该订单非当前门店订单，不允许再来一单，需切换门店进行购买',
        showCancel: false
      });
    }
  },  
    switchOrderType(tabIndex) {
        let that = this;
        if (tabIndex == 0) {
            that.setData({
                order_status: '',
                evaluate_status: '',
            });
        } else if (tabIndex == 1) {
            that.setData({
                order_status: 0,
                evaluate_status: '',
            });
        } else if (tabIndex == 2) {
            that.setData({
                order_status: 201,
                evaluate_status: '',
            });
        } else if (tabIndex == 3) {
            that.setData({
              order_status: 300,
                evaluate_status: 0,
            });
        } else if (tabIndex == 4) {
          that.setData({
            order_status: 301,
            evaluate_status: 0,
          });
        } else if (tabIndex == 5) {
          that.setData({
            order_status: 401,
            evaluate_status: 0,
          });
        }
        that.getOrderList();
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
        let that = this;
        that.setData({ orderList: [] });//初始化列表
        that.switchOrderType(that.data.tabIndex);
        that.setData({
          currentStoreId: wx.getStorageSync('storeId')
        });
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    onReachBottom() {
      var that = this;
      if (that.data.page <= that.data.totalPages) {
        that.setData({
          page: that.data.page + 1,
        });
        that.getOrderList();
      }
    },
    getWuliuList(event) {
      let shippingNo = event.target.dataset.shippingNo;
      let shippingCode = event.target.dataset.shippingCode;
      let orderId = event.target.dataset.orderId;
      let goodsType = event.target.dataset.goodsType;
      wx.navigateTo({
        url: '/pages/ucenter/wuliu/wuliu?id=' + shippingNo + '&code=' + shippingCode + '&orderId=' + orderId + '&goodsType=' + goodsType,
      });
  }
})