var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    user: [],
    tabTxt: ['订单详情', '订单状态'],//tab文案
    tab: [false, true],
    type: 14,
    order_info: [],//数据
    order_goods:[],
    param: {
      type: 14,
    },
    is_showstatus:false,//显示状态
    is_showinfo:true,
    is_showbutton:0
  },
  onLoad: function (options) {//接收上个页面传的参数
    app.HCsetNavigationBarTitle('订单');
    var self = this;
    var data = [true, true], index = 0;
    data[index] = false;
    self.setData({
      tab: data,
      param:{
        type:14,
        id:options.id
      }
    })
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    }
    if (options.is_notshow ==1) {
        self.setData({
        is_showbutton: 0
      })
    }else{
       self.setData({
        is_showbutton: 1
      })
    }
  },
  onReady: function () {
    var self = this;
    app.getUserInfo(function (ret) {
      self.setData({
        user: ret
      });
        self.getData();
      });
  },
  // 选项卡
  filterTab: function (e) {
    if (this.data.user != '') {
      var data = [true, true], index = e.currentTarget.dataset.index;
      if (this.data.tab[index] == true) {
        data[index] = false;
        this.setData({
          tab: data,
        });
        if(index==1){
          this.setData({
            is_showstatus:true,
            is_showinfo:false
          })
        }else{
          this.setData({
            is_showstatus: false,
            is_showinfo: true
          })
        }
        this.getData();
      }
    }
  },
  //加载数据
  getData: function (callback) {
    var self = this;
    app.showLoading('加载中...');
    var action = 'c=Member&a=myorder_info';
    app.ajaxRequest(action, 'GET', $.ObjectToStr(self.data.param), function (ret) {
     // console.log(ret);
      if (ret.status == 1) {
        self.setData({
          order_info:ret.data.order_info,
          order_goods: ret.data.goods_info,
          s_money:ret.data.s_money,
          distribute: ret.data.distribute,
        })
        if (ret.data.reqstatus) {
          self.setData({
            reqstatus: ret.data.reqstatus,
          })
        }
        if (ret.data.shipping_info) {
          self.setData({
            shipping_info: ret.data.shipping_info
          })
        }
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  call: function (event) {
    var mobile = event.currentTarget.dataset.id;
    if (mobile) {
      app.HCmakePhoneCall(mobile);
    } else {
      app.HCerror('提示', '暂无联系方式', false);
    }
  },
  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 'refund_goods':
        app.HCerror('提示', '您确定要退货吗？', true, function (ret) {
          if(ret.confirm){
            app.HCnavigateTo('../../userinfo/refund_order/index?order_id=' + order_id);
          }
        })
        break;
        case 'go_dp':
          app.HCnavigateTo('../../comment/cont/cont?order_id=' + order_id);
        break;
    }
  },
  gotoSend: function () {
    var self = this;
    var id = self.data.shipping_info.express_id;
    var code = self.data.shipping_info.send_type;
    var sn = self.data.shipping_info.notice_sn;
    app.HCnavigateTo('../logistics/index?id=' + id + '&code=' + code + '&sn=' + sn);
  }
});