// pages/SupplierShop/SupplierShop.js
var app = getApp();
const INDEX_URL = "plugin.supplier.frontend.shop.index.index";
const GOOD_URL = "plugin.supplier.frontend.shop.index.search-goods";
// const ACTIVITY_URL = "plugin.supplier.frontend.shop.index.goods-limit";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    customer_open: true,
    show1: false,
    service_QRcode: '',
    service_mobile: '',
    cservice:'',
    id: '', // 供应商id
    order_id: '', //  订单id参数(从订单页面过来的时候带
    //首页数据
    tab: 0,
    sliderShow: false,
    indexData: {},
    supplierInfo: {},
    //商品数据
    goodsArr: [],
    good_loading: false,
    good_allLoaded: false,
    good_goload: true,
    good_isLoadMore: true,
    good_page: 1,
    good_total_page: 0,
    order_field: '',
    order_by: '',
    //活动数据
    actGoodsArr: [],
    act_loading: false,
    act_allLoaded: false,
    act_goload: true,
    act_isLoadMore: true,
    act_page: 1,
    act_total_page: 0,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    roomdataBol: false,
    roomdata: [],
    keyword: null,
    categoryInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    wx.setNavigationBarTitle({
      title: '供应商店铺',
    });
    if (options.fromOrder == 1) {
      this.setData({
        id: '',
        order_id: options.id
      });
    }
    this.getIndexData();
  },
  onChange(e) {
    this.data.roomBol = false;
    this.setData({
      tab: e.currentTarget.dataset.key,
    });
    if (e.currentTarget.dataset.title == "商品") {
      this.setData({
        good_page: 1
      });
      this.getGoodsData();
    } else if (e.currentTarget.dataset.title == "活动") {
      this.getActivityData();
    } else if (e.currentTarget.dataset.title == "直播列表") {
      this.data.roomBol = true;
      this.getLiveList();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getActivityData() {
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.goods-limit");
    app._getNetWork({
      url: urlStr,
      data: { sid: this.data.id },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data.list.data);
          this.setData({
            actGoodsArr: res.data.list.data,
            act_total_page: res.data.list.last_page
          });
        }
      }
    });
  },
  getActivityMoreData() {
    this.data.act_isLoadMore = false;
    if (this.data.act_page >= this.data.act_total_page) {
      return;
    } else {
      this.data.act_page = this.data.act_page + 1;
      let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.goods-limit");
      app._getNetWork({
        url: urlStr,
        data: { sid: this.data.id, page: this.data.act_page },
        success: (resdata) => {
          var res = resdata.data;
          this.data.act_isLoadMore = true;
          if (res.result == 1) {
            let actGoodsArr = this.data.actGoodsArr.concat(res.data.list.data);//数组拼接
            this.setData({
              actGoodsArr
            });
          } else {
            this.data.act_page = this.data.act_page - 1;
            this.data.act_isLoadMore = false;
          }
        }
      });
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.good_total_page) {
      wx.showToast({
        title: '暂无更多',
        icon: 'none',
        duration: 1000
      });
    } else {
      if (this.data.tab == 1) {
        let is_page = this.data.good_page + 1;
        this.setData({
          good_page: is_page
        });
        this.nexTgetGoodsData();
      } else if (this.data.tab == 2) {
        this.getActivityMoreData();
      }

    }
    if (this.data.roomBol && this.data.roomdataBol) {
      if (this.data.isLoadMore) {
        this._getMoreData();
      } else {
        console.log('没有更多数据');
      }
    }
  },
  _getMoreData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.get-room");
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page: this.data.page,
          supplier_id: this.data.id,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              roomdata: that.data.roomdata.concat(res.data.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  init() {
    this.setData({
      customer_open: true,
      show1: false,
      service_QRcode: '',
      service_mobile: '',
      cservice:'',
    });
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.service_mobile,

    });
  },
  showPop() {
    if (this.data.customer_open) {
      this.setData({
        show1: true
      });
    }
  },
  closePop() {
    this.setData({
      show1: false
    });
  },
  //直播间跳转
  gopage(e) {
    let status = e.currentTarget.dataset.status;
    let id = e.currentTarget.dataset.id;
    let backid = e.currentTarget.dataset.backid;
    if (status == 1) {
      wx.navigateTo({
        url: '/packageD/directSeeding/livePage/livePage?id=' + id + '&playerType=live'
      });
    } else if (status == 2) {
      wx.navigateTo({
        url: '/packageD/directSeeding/livePage/livePage?id=' + id + '&playerType=video' + '&backid=' + backid
      });
    } else if (status == 3) {
      wx.navigateTo({
        url: '/packageD/directSeeding/foreshow/foreshow?id=' + id
      });
    }
  },
  goToSuppliereInfo() {
    wx.navigateTo({
      url: "/packageA/member/supplier/SupplierInfo/SupplierInfo?id=" + this.data.id
    });
  },
  gotoChat(evt){
    let url = evt.currentTarget.dataset.cservice;
    wx.navigateTo({
      url: url,
      fail:(err)=>{
        app.tips("客服路由出错");
      }
    });
  },
  //首页
  getIndexData() {
    let that = this;
    let urlStr = app.getNetAddresss(INDEX_URL);
    urlStr += '&sid=' + this.data.id;
    urlStr += '&order_id=' + this.data.order_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result == 1) {
          if (res.data.is_room && res.data.is_room == 1) {
            that.setData({
              roomdataBol: true
            });
          }
          that.setData({
            id: res.data.supplier.supplier_id,
          });
          for (let i = 0; i < res.data.list_is_recommand.length; i++) {
            if (parseInt(Number(res.data.list_is_recommand[i].market_price == 0))) {
              res.data.list_is_recommand[i].notMarket_price = 0;
            } else {
              res.data.list_is_recommand[i].notMarket_price = 1;
            }
          }
          for (let i = 0; i < res.data.advs.length; i++) {
            if (app._isTextEmpty(res.data.advs[i].img)) {
              res.data.advs[i].imagShow = false;
            } else {
              res.data.advs[i].imagShow = true;
            }
          }
          // console.log(res.data.list_is_recommand)
          that.setData({
            indexData: res.data,
            supplierInfo: res.data.supplier,
            customer_open: res.data.customer_open == '1' ? true : false,
            service_QRcode: res.data.service_QRcode,
            service_mobile: res.data.service_mobile,
            cservice:res.data.cservice,
          });
          // console.log(that.data.indexData.advs)
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getLiveList() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.get-room");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: this.data.page,
        supplier_id: this.data.id,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            total_page: res.data.last_page,
            roomdata: res.data.data,
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  //商品
  getGoodsData() {
    let that = this;
    let urlStr = app.getNetAddresss(GOOD_URL);
    urlStr += '&sid=' + this.data.id;
    urlStr += '&page=' + this.data.good_page;
    urlStr += '&order_field=' + this.data.order_field;
    urlStr += '&order_by=' + this.data.order_by;
    urlStr += `&search[keyword]=${this.data.keyword || ''}`;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goodsArr: res.data.data,
            good_total_page: res.data.last_page,
            current_page: res.data.current_page
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  nexTgetGoodsData() {
    let that = this;
    let urlStr = app.getNetAddresss(GOOD_URL);
    urlStr += '&sid=' + this.data.id;
    urlStr += '&page=' + this.data.good_page;
    urlStr += '&order_field=' + this.data.order_field;
    urlStr += '&order_by=' + this.data.order_by;
    urlStr += '&search[keyword]=';
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goodsArr: that.data.goodsArr.concat(res.data.data),
            good_total_page: res.data.last_page,
            current_page: res.data.current_page
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  toggle(e) {
    let idx = e.currentTarget.dataset.idx;
    let order_field = '';
    switch (idx) {
    case 2:
      this.setData({
        hascolor: 2
      });
      if (this.data.colorshow == '2_2') {
        this.setData({
          colorshow: '2_1',
        });
        order_field = 'show_sales';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '2_2',
        });
        order_field = 'show_sales';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    case 3:
      this.setData({
        hascolor: 3,
      });
      if (this.data.colorshow == '3_2') {
        this.setData({
          colorshow: '3_1'
        });
        order_field = 'price';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '3_2'
        });
        order_field = 'price';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    case 5:
      this.setData({
        hascolor: 5,
      });
      if (this.data.colorshow == '5_2') {
        this.setData({
          colorshow: '5_1'
        });
        order_field = 'id';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '5_2'
        });
        order_field = 'id';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    }
    this.getGoodsData();
  },
  //跳转搜素
  goToSearch() {
    //SupplierSearch sid:id
    wx.navigateTo({
      url: '/packageA/member/supplier/SupplierSearch/SupplierSearch?id='+this.data.id,
    });
  },
  toggleSlide() {
    this.setData({
      sliderShow: !this.data.sliderShow
    });
  },
  goToUrl(e) {
    let type = e.currentTarget.dataset.type;
    let url = e.currentTarget.dataset.url;
    if (type == 'route' && url == 'home') {
      wx.reLaunch({
        url: '/packageG/index/index'
      });
    } else if (type == 'route' && url == 'orderlist') {
      wx.navigateTo({
        url: '/packageA/member/myOrder_v2/myOrder_v2'
      });
    } else if (type == 'route' && url == 'member') {
      wx.reLaunch({
        url: '/packageG/member_v2/member_v2'
      });
    } else if (type == 'href') {
      console.log(url);
    }
  },
  goToDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id
    });
  },
  goToMoreHot(){
    wx.navigateTo({
      url: '/packageA/member/supplier/SupplierSearch/SupplierSearch?id='+this.data.id +'&hot=1',
    });
  },

  /**
   * 搜索本店商品输入框回车事件
   */
  searchConfirm() {
    this.setData({
      good_page: 1,
      tab: 1,
      goodsArr: []
    }, () => {
      this.getGoodsData();
    });
  }
});
