var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    user:[],
    tabTxt: ['全部', '待付款', '待发货','待收货', '待评价'],//tab文案
    tab: [false, true, true, true],
    ttype: [0, 2,3, 4, 5],
    tordertype: [14, 14,14, 14, 14],
    type: 0,
    ordertype: 0,
    list: [],//数据
    page: 1,//当前页码
    param: {
      type: 0,
      ordertype: 0
    },
    hasMore: false,//无更多数据提示
    dataNull: true//未获取到数据提示
  },
  onLoad: function (options) {//接收上个页面传的参数
    app.HCsetNavigationBarTitle('订单');
    var self=this;
    var data = [true, true,true, true, true], index = options.class, ttype = self.data.ttype, tordertype = self.data.tordertype;
    data[index] = false;
    self.setData({
      tab: data,
      page: 1,
      type: ttype[index],
      ordertype: tordertype[index]
    })
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    }
  },
  onReady: function () {
    var self = this;
    app.getUserInfo(function (ret) {
      self.setData({
        user: ret
      });
      if (self.data.user != '') {
        self.setData({
          param: {
            page: self.data.page,
            type: self.data.type,
            ordertype: self.data.ordertype
          }
        });
        self.getData();
      }
    });
  },
  // 选项卡
  filterTab: function (e) {
    if(this.data.user!=''){
      var data = [true, true, true, true, true, true], index = e.currentTarget.dataset.index, o_type = e.currentTarget.dataset.otype, ordertype = e.currentTarget.dataset.ordertype;
      if (this.data.tab[index] == true) {
        data[index] = false;
        this.setData({
          tab: data,
          page: 1,
          type: o_type,
          ordertype: ordertype,
          param: {
            page: 1,
            type: o_type,
            ordertype: ordertype
          }
        });
        this.getData();
      }
    }
  },
  //加载数据
  getData: function (callback) {
    var self = this;
    app.showLoading('加载中...');
    var action = 'c=Member&a=myorder_list';
    app.ajaxRequest(action, 'GET', $.ObjectToStr(self.data.param), function (ret) {
      console.log(ret.data); console.log('1----------');
      if (ret.status == 1) {
        $.init_page_list(self, ret.data);
      } else if (ret.status == 2) {
        $.init_page_empty_list(self, ret.msg);
      }
    }, true);
  },
  onReachBottom: function () {
    // console.log();
    var self = this;
    if (self.data.user != '') {
      self.data.page++;
      self.setData({
        param: {
          type: self.data.type,
          page: self.data.page,
        }
      });
      self.getData();
    }
  },
  goToInfo: function(event){
    if (this.data.user != '') {
      app.HCnavigateTo('../../buycart/order_infor/order_infor?id=' + event.currentTarget.dataset.id +'&invite_key=' + app.globalData.init_invite_key);
    }
  },

  operation:function(event){
    var op_type = event.currentTarget.dataset.opera;
    var order_id = event.currentTarget.dataset.id;
    switch (op_type){
      case 'continue_pay':
        var action ='c=Order&a=continu_pay';
        app.showLoading('加载中...');
        var meched='&order_id='+order_id;
        app.ajaxRequest(action, 'GET', meched, function (ret) {
          if (ret.status == 1) {
            app.HCnavigateTo('../../buycart/continue_pay/continue_pay?order_id=' + ret.data);
          } else {
            app.HCerror('提示', ret.msg); return;
          } 
        }, true);
      break;
      case 'cancel_order':
      var self=this;
      app.HCerror('提示', '您确定要取消订单吗？',true,function(ret){
        if (ret.confirm){
          var action = 'c=Order&a=cancel_order';
          app.showLoading('加载中...');
          var meched = '&order_id=' + order_id;
          app.ajaxRequest(action, 'GET', meched, function (ret) {
            if (ret.status == 1) {
              app.HCtoast('取消订单成功');
              self.getData();
            } else {
              app.HCerror('提示', ret.msg); return;
            }
          }, true);
        }
       
      })
        
      break;
      case 'confirm':
        var self = this;
        app.HCerror('提示', '您确定要收货吗？', true, function (ret) {
          if (ret.confirm) {
            var action = 'c=Order&a=confirm_order';
            app.showLoading('加载中...');
            var meched = '&order_id=' + order_id;
            app.ajaxRequest(action, 'GET', meched, function (ret) {
              if (ret.status == 1) {
                app.HCtoast('收货成功');
                self.getData();
              } else {
                app.HCerror('提示', ret.msg); return;
              }
            }, true);
          }

        })
        break;
      case 'go_dp':
        app.HCnavigateTo('../../comment/cont/cont?order_id=' + order_id);
        break; 
    }
  }
});