// pages/member/rankingListSecond/rankingListSecond.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
// 一卡通首页
const CARD_INDEX = "plugin.universal-card.api.home.index";
//获取门店分类和日期
const GET_SEARCH = "plugin.universal-card.frontend.store-discount.store-data";
//特权折扣
const PD_URL = "plugin.universal-card.frontend.store-discount.privilege";
//专属礼券
const EC_URL = "plugin.universal-card.frontend.index.couponList.index";
//获取专属礼券
const GET_COUPON = "plugin.universal-card.frontend.index.couponGet.index";
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    tabShow: true,
    language: '',
    //特权折扣 平日折扣 专属礼券tab切换显示隐藏
    tabIndex: 0,
    privilege_discount_show: true,
    weekday_discount_show: false,
    exclusive_coupons_show: false,
    //特权折扣和平日折扣index
    pd_index: 0,
    pd_date_index: 0,
    wd_index: 0,
    //特权折扣--搜索条件
    pd_classfy_arr: [], //门店分类
    pd_date_arr: [], //日期数组
    //搜索的键
    pd_category_id: 0, //pd门店分类id
    wd_category_id: 0, //wd门店分类id
    pd_date: {}, //日期对象
    //会员权益--折扣
    wd_discount: "",
    //开通状态
    card_status: false,
    card_bk: "",
    //特权折扣--数组和分页数据
    pd_loading: false,
    pd_allLoaded: false,
    pd_goload: true,
    pd_isLoadMore: true,
    pd_page: 1,
    pd_total_page: 0,
    pd_arr: [],
    //专属礼券--数组和分页数据
    ec_loading: false,
    ec_allLoaded: false,
    ec_goload: true,
    ec_isLoadMore: true,
    ec_page: 1,
    ec_total_page: 0,
    ec_arr: [],
    //定位信息
    address: "",
    point: {
      lat: '',
      lng: ''
    },
    city: '',
    title: '',
    //开通会员信息
    info: {},
    //tab切换
    tabArr: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
    this._getLocation(() => {
      this.getCardIndex();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //初始化数据
  initData() {
    //tab切换初始化数据
    this.setData({
      privilege_discount_show: true,
      weekday_discount_show: false,
      exclusive_coupons_show: false,
      tabIndex: 0,
      pd_index: 0,
      pd_date_index: 0,
      wd_index: 0,
      pd_classfy_arr: [],
      pd_date_arr: [],
      pd_category_id: 0,
      wd_category_id: 0,
      pd_date: {},
      wd_discount: "",
      card_status: false,
      card_bk: ""
    });
    this.pdInitData();
    this.ecInitData();
  },
  //特权折扣--分页
  pdInitData() {
    this.setData({
      pd_loading: false,
      pd_allLoaded: false,
      pd_goload: true,
      pd_isLoadMore: true,
      pd_page: 1,
      pd_total_page: 0,
      pd_arr: []
    });
  },
  //专属礼券--分页
  ecInitData() {
    this.setData({
      ec_loading: false,
      ec_allLoaded: false,
      ec_goload: true,
      ec_isLoadMore: true,
      ec_page: 1,
      ec_total_page: 0,
      ec_arr: []
    });
  },

  //一卡通首页
  getCardIndex() {
    let that = this;
    let urlStr = app.getNetAddresss(CARD_INDEX);
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            card_status: true,
            info: res.data,
            ['tabArr[0]']: res.data.privilege_name,
            ['tabArr[1]']: res.data.coupon_name,
            titles: res.data.title
          });
          if (app._isTextEmpty(that.data.tabArr[0]) && app._isTextEmpty(that.data.tabArr[1])) {
            that.setData({
              tabShow: false
            });
          }
          wx.setNavigationBarTitle({
            title: that.data.titles
          });
          //获取门店和日期分类
          that.getClassfyData();
          //获取专属礼券数据
          that.getEcData();
        } else {
          that.setData({
            card_status: false,
            card_bk: res.data.card_thumb,
            ['tabArr[0]']: res.data.privilege_name ? res.data.privilege_name : '',
            ['tabArr[1]']: res.data.coupon_name ? res.data.coupon_name : '',
            titles: res.data.title
          });
          if (app._isTextEmpty(that.data.tabArr[0]) && app._isTextEmpty(that.data.tabArr[1])) {
            that.setData({
              tabShow: false
            });
          }
          wx.setNavigationBarTitle({
            title: that.data.titles
          });
          that.getClassfyData();
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取--门店分类--日期
  getClassfyData() {
    let that = this;
    let urlStr = app.getNetAddresss(GET_SEARCH);
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            pd_classfy_arr: res.data.store_category,
            pd_date_arr: res.data.date_time,
            pd_category_id: res.data.store_category[0].id,
            pd_date: res.data.date_time[0],
            wd_category_id: res.data.store_category[0].id,
          });
          //初始化分页数据
          that.pdInitData();
          //获取数据
          that.getPdData();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getPdData() {
    let that = this;
    let urlStr = app.getNetAddresss(PD_URL);
    urlStr += '&lng=' + this.data.point.lng;
    urlStr += '&lat=' + this.data.point.lat;
    urlStr += '&week=' + this.data.pd_date.week_num;
    urlStr += '&category_id=' + this.data.pd_category_id;
    urlStr += '&day=' + this.data.pd_date.day;
    urlStr += '&city_name=' + this.data.city;
    urlStr += '&page=' + this.data.pd_page;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            pd_total_page: res.data.last_page,
            pd_arr: res.data.data
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getEcData() {
    let that = this;
    let urlStr = app.getNetAddresss(EC_URL);
    urlStr += '&page=' + this.data.ec_page;
    urlStr += '&lng=' + this.data.point.lng;
    urlStr += '&lat=' + this.data.point.lat;
    urlStr += '&city_name=' + this.data.city;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            ec_total_page: res.data.last_page,
            ec_arr: res.data.data
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //立即开通
  gotoOpen() {
    wx.navigateTo({
      url: '/packageC/o2o/card/CardOpenMembership/CardOpenMembership?'
    });
  },
  //已开通--续费
  gotoFare() {
    let info = this.data.info;
    console.log(info);
    if (!app._isTextEmpty(info.goods_spec)) {
      wx.navigateTo({
        url: '/packageC/o2o/card/new_card/new_card?level_id=' + info.level_id + '&spec=goods_spec'
      });
    } else {
      wx.navigateTo({
        url: '/packageC/o2o/card/new_card/new_card?level_id=' + info.level_id,
      });
    }
  },
  //tab切换
  toggle(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex: index
    });
    if (index == 0) {
      this.setData({
        privilege_discount_show: true,
        exclusive_coupons_show: false
      });
    } else if (index == 1) {
      this.setData({
        privilege_discount_show: false,
        exclusive_coupons_show: true
      });
    }
  },
  //特权折扣--搜索--门店分类
  pdClassfySearch(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    this.setData({
      pd_index: index, //改变选中状态
      pd_category_id: id //修改搜索参数--门店分类
    });
    this.pdInitData();
    this.getPdData();
  },
  //特权折扣--搜索--日期
  pdDateSearch(e) {
    let date = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    this.setData({
      pd_date_index: index, //改变选中状态
      pd_date: date //修改搜索参数--日期
    });
    this.pdInitData();
    this.getPdData();
  },
  //跳转至门店首页
  gotoShop(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageC/o2o/o2oStore/o2oStore?store_id=' + id
    });
  },
  //领取优惠券
  getCoupon(e) {
    let id = e.currentTarget.dataset.id;
    let urlStr = app.getNetAddresss(GET_COUPON);
    urlStr += '&coupon_id=' + id;
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: '领取成功',
            duration: 1500
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  gotoCouponLog() {
    wx.navigateTo({
      url: "/packageB/member/couponLog/couponLog"
    });
  }
});