var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    info: [],
    other: [],
    user: [],
    param: {
      page: 1,
      no_auto_hide_toast: true//关闭自动隐藏弹窗
    },
    goods_list: {},
    header_img: '/images/header_1.jpg',
    down_num:0,
    up_num:0,
    goods_list:[],
    tabTxt: ['出售中', '已下架'],//tab文案
    tab: [false, true],
    ttype: [0, 2],
    tordertype: [14, 14],
    list:{},
    is_desc:'',//排序方式
    hasMore: false,//无更多数据提示
    dataNull: true,//未获取到数据提示
    is_true:0,
    total_page: 1,
  },
  onLoad: function (options) {//接收上个页面传的参数
    var self = this;
    if (options.invite_key) {
      app.globalData.invite_key = options.invite_key;
    }
    if (options.lshop_id){ // 分享的小店商品 保存该小店
      app.globalData.lshop_id=options.lshop_id;
    }
  },
  onShow: function () {
    var self = this;
    app.getUserInfo(function (ret) {
      if (ret.username != ' ') {
        self.setData({
          info: ret,
        });
        self.getIndex();
      } else {
        self.setData({
          show_q: true
        })
      }
    });

  },
  getIndex: function (e) {
    var self = this;
    var bodyparam = new Object;
    bodyparam = self.data.param;
    if(e){
      var event = e.currentTarget.dataset;
      bodyparam.desc = event.desc;
      self.setData({
        is_desc: event.desc,
      });
    }
    var action = 'c=LittleShop&a=my_goods_up';
    app.showLoading('加载中...');
    // console.log(bodyparam); console.log('6---------');
    app.ajaxRequest(action, 'POST', bodyparam, function (ret) {
      app.hideLoading(); 
      console.log(ret);console.log('8-------------');
      if (ret.status == 1) {
        self.setData({
          down_num: ret.data.down_num,
          up_num: ret.data.up_num,
        });
        var arrs = self.data.tabTxt;
        arrs[0] = '出售中' + '(' + ret.data.up_num+')';
        arrs[1] = '已下架' + '(' + ret.data.down_num + ')';
        self.setData({
          tabTxt: arrs,
        });
        if (self.data.param.page >= ret.data.totalpage){
            self.setData({
              total_page: 0
            });
          $.init_page_empty_list(self, ret.msg);
        }
        if (ret.data.upgoods_list) {
          $.init_page_list(self, ret.data.upgoods_list);
          
        }
        self.setData({ total_page: ret.data.totalpage });
      }else if (ret.status == 2) {
        self.setData({
          total_page: 0
        });
        $.init_page_empty_list(self, ret.msg);
      }
    }, true);
  },
   
  // 选项卡
  filterTab: function (e) {
    var data = [true, true, true], index = e.currentTarget.dataset.index, o_type = e.currentTarget.dataset.otype, ordertype = e.currentTarget.dataset.ordertype;

    // console.log(this.data.tab[index]); console.log('2--------');
      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
          },
          hasMore: false,//无更多数据提示
          dataNull: true,//未获取到数据提示
        });
        this.getIndex();
      }
  },
  //是否推荐控制
  go_isshow:function(event){
    var self = this;
    var event = event.currentTarget.dataset;
    var bodyparam = new Object;
    bodyparam = self.data.param;
    bodyparam.is_show = event.is_show;
    bodyparam.goodsid = event.goodsid;
    var action = 'c=LittleShop&a=go_isshow';
    app.showLoading('加载中...');
    // console.log(bodyparam);
    app.ajaxRequest(action, 'POST', bodyparam, function (ret) {
      app.hideLoading(); 
      // console.log(ret);console.log('1-------------');
      if (ret.status == 1) {
        self.getIndex();
      }
    }, true);
  },
  gotocont: function (e) {
    var event = e.currentTarget.dataset;
    var goods_id = event.goodsid;
    app.HCnavigateTo('../../../pages/takeout/cont/cont?id=' + goods_id + '&name=' + event.name);
   },
  //下架
  update_myshop: function (event){
    var self = this;
    var event = event.currentTarget.dataset;
    var bodyparam = new Object;
    bodyparam = self.data.param;
    bodyparam.goods_id = event.goodsid;
    if (event.is_putaway==0) bodyparam.ptype = 1;
    if (event.is_putaway == 1) bodyparam.ptype = 0;   
    var action = 'c=LittleShop&a=update_myshop';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', bodyparam, function (ret) {
      app.hideLoading(); 
      if (ret.status == 1) {
        self.setData({
          down_num: ret.data.down_num,
          up_num: ret.data.up_num,
        });
        var arrs = self.data.tabTxt;
        arrs[0] = '出售中' + '(' + ret.data.up_num + ')';
        arrs[1] = '已下架' + '(' + ret.data.down_num + ')';
        self.setData({
          tabTxt: arrs,
        });
        self.getIndex();
      }
    }, true);
  },
  //分享
  onshare:function(){
    wx.showShareMenu({
      widthShareTicket:true
    })
  },
  onShareAppMessage:function(){
    return {
      title: '商品',
      desc: '选择您心仪的宝贝!',
      path: '/' + $.getCurrentPageUrl() + '?invite_key=' + app.globalData.init_invite_key + '&id=' + this.data.param.id+'&lshop_id='+app.globalData.lshop_id
    }
  },
  
  onReachBottom: function () {
    var self = this;
    var param = self.data.param;
    param.page++;
    self.setData({
      param: param
    });
    self.getIndex();
  },
  //获取用户信息授权
  // bindGetUserInfo: function (e) {
  //   var self = this;
  //   app.getUserInfo(function (ret) {
  //     self.setData({
  //       show_q: false,
  //       info: ret,
  //     })
  //     self.getIndex();
  //   })
  // },
  
})