var e = getApp(),os = e.globalData.setting;
var utils = require('../../../../utils/util.js');

var ut = require('../../../../utils/util.js');
var regeneratorRuntime = require('../../../../utils/runtime.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    backClass: null,
    qy_list:null,
    currentItemId: 0,
    //会员信息
    userinfo:null,
    iurl:os.imghost,
    defaultAvatar:os.imghost+"/miniapp/images/no-head.jpg",

    cz_val:0,
    full_cz_val:0,
    gradeId:null,
    need_money:0,
    g_qy_list:null,
    click_gradeId:null,

    this_curr_id:0,
    GradeName:'',
    is_full:0,  //是否满级
    rfmName:'成长值',
    cur_g_num:0,//当前级需要多少钱
    userqy_color:null,//颜色值 
  },

  swiperChange: function (e) {
    var currentItemId = e.detail.current;
    this.setData({
       currentItemId: currentItemId
    })

    var g_id=this.data.backClass[currentItemId].Id;
    this.get_ca_qy(g_id);
  },

  //--卡的单击事件--
  clickChange: function (e) {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var use_info=getApp().globalData.userInfo;
      this.setData({userinfo:use_info});
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
        getApp().check_can_share();
         wx.showLoading();
         var cz_val=0,backClass=null,qy_list=null,gradeId=null,gradeEndDate=null,need_money=0,g_qy_list=null,GradeName='',cur_g_num=0;
         var gl_data=getApp().globalData;
         var sw_cur_id=0;
         var th=this;
         //颜色值 
         getApp().getConfig2(function(e) {
            var getuserqy_color=e.userqy_color;  
            if (getuserqy_color) getuserqy_color = JSON.parse(getuserqy_color)
            if(getuserqy_color){
               th.setData({userqy_color:getuserqy_color})
            }  
        })     
         //获取成长值
         await  getApp().request.promiseGet("/api/weshop/users/grade/aftervipinfo/get",{
             data:{ storeId:os.stoid,userId:gl_data.user_id }
         }).then(res=>{
             var data=res.data.data;
             cz_val=data.GradeSum;
             gradeId=data.GradeId;
             gradeEndDate=data.GradeEndDate;
             GradeName=data.GradeName;
         })

         let isRfm = wx.getStorageSync('isRfm')
         if (isRfm) {
           this.setData({
             rfmName:'综合分'
           })
         }

        //获取卡类列表
        await  getApp().request.promiseGet("/api/weshop/users/grade/vipgradeinfo/page",{
            data:{ storeId:os.stoid}
        }).then(res=>{
           if (res.data.data && res.data.data.pageData && res.data.data.pageData.length)
              backClass=res.data.data.pageData;
        })

        for(var i in backClass){
            var im=backClass[i];
            if(cz_val<im.BuyGradeSum && need_money==0){
                need_money=im.BuyGradeSum;
            }
            if(im.Id==gradeId){
                 sw_cur_id=i;
                 cur_g_num=im.BuyGradeSum;
            }
        }

        //所有的服务
        await  getApp().request.promiseGet("/api/weshop/users/grade/vipprivilegeinfo/page",{
            data:{storeId:os.stoid,pageSize:100}
        }).then(res=>{
          if (res.data.data && res.data.data.pageData && res.data.data.pageData.length)
              qy_list=res.data.data.pageData;
        })

        //获取当前等级的特权
        await  getApp().request.promiseGet("/api/weshop/users/grade/privilegeform/list/page",{
           data: { storeId: os.stoid, GradeId: gradeId, userId: gl_data.user_id, pageSize: 100 }
        }).then(res=>{
            if (res.data.data && res.data.data.pageData && res.data.data.pageData.length)
               g_qy_list=res.data.data.pageData;
        });

        for(var i in qy_list){
           var item=qy_list[i];
           var FromId=th.check_is_has_qy(item,g_qy_list);
           qy_list[i].ishas = 0;
           if(FromId){
                qy_list[i].ishas=1;
                qy_list[i].FromId=FromId;
           }
        }
        qy_list.sort(function(a,b){
              return b.ishas-a.ishas
        })

        this.setData({
            cz_val: cz_val, backClass: backClass,qy_list:qy_list,
            full_cz_val: backClass?backClass[backClass.length-1].BuyGradeSum:0,
            gradeEndDate:gradeEndDate,
            gradeId:gradeId,
            need_money:need_money,
            currentItemId:sw_cur_id,
            this_curr_id:sw_cur_id,
            GradeName:GradeName,cur_g_num:cur_g_num,
        });
        wx.hideLoading();
  },

  //判断会员是后有改服务项目
  check_is_has_qy:function(item,g_qy_list){
    if (!g_qy_list) return false;
      for(var i in g_qy_list){
          var iter=g_qy_list[i];
          if(iter.FormId==item.Id)  return g_qy_list[i].FormId;
      }
      return false;
  },

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

  },

  /**
   * 用户点击右上角分享
   */
   onShareAppMessage: function () {   	  getApp().globalData.no_clear=1

  },
  //--新品使用--
  go_url:function (e) {
      var th=this;
      var no=e.currentTarget.dataset.no;
      var ind=e.currentTarget.dataset.ind;
      var item=th.data.qy_list[ind];
      if(item.ishas==undefined || !item.ishas){
          var qt_txt = th.selectComponent("#pop_txt"); //组件的id
          var ob={title:"温馨提示",content:"您当前会员等级暂无此权益"};
          qt_txt.open(ob);
          return false;
      }

      if(th.data.click_gradeId!=null && th.data.click_gradeId!=th.data.gradeId){
          var qt_txt = th.selectComponent("#pop_txt"); //组件的id

          var text="您当前会员等级暂无此权益";
          if(th.data.this_curr_id>th.data.currentItemId){
              text="【"+th.data.backClass[th.data.currentItemId].GradeName+"】专享，您当前是【"+th.data.backClass[th.data.this_curr_id].GradeName+"】";
          }
          var ob={title:"温馨提示",content:text};
          qt_txt.open(ob);
          return false;
      }

      var url=e.currentTarget.dataset.img;
      if(!url || url==""){
        url=`/public/upload/userqy/quanbg.jpg`
      }
      switch (no) {
          case "01":
              getApp().goto("/packageB/pages/user/user_coupon/user_coupon?img="
                  +url+"&gradeId="+th.data.gradeId+"&FormId="+item.Id);
              break;
          case "02":
              getApp().goto("/packageE/pages/user/user_fw/user_fw?gradeId="+th.data.gradeId+"&FormId="+item.Id);
              break;
          case "03":
              getApp().goto("/packageH/pages/user/user_spsy/user_spsy?img="+url+"&gradeId="+th.data.gradeId+"&FormId="+item.Id);
              break;
          case "10":
              getApp().request.get("/api/weshop/users/grade/vipprivilegelist/other/get",{
                   data:{storeId:os.stoid, formId:item.Id,userId:getApp().globalData.user_id,gradeId:th.data.gradeId},
                   success:function (res) {
                       var val=res.data.data.Remark;
                       var title=res.data.data.PrivilegeName;
                       var qt_txt = th.selectComponent("#pop_txt"); //组件的id
                       var ob={title:title,content:val};
                       qt_txt.open(ob);
                   }
              })
             break;
      }
  },

  go_cz:function(){
    let isRfm = wx.getStorageSync('isRfm')
    //升级为RFM就不跳转成长值详情
    if (!isRfm) {
      getApp().goto("/packageE/pages/user/grow_value/grow_value")
    }
  },

  //获取卡类的gr_id
  get_ca_qy:async function (gr_id) {
      var g_qy_list=null,th=this,qy_list=this.data.qy_list;
      var gl_data=getApp().globalData;
      await getApp().request.promiseGet("/api/weshop/users/grade/privilegeform/list/page",{
          data: { storeId: os.stoid, GradeId: gr_id, userId: gl_data.user_id, pageSize: 100}
      }).then(res=>{
          if(ut.ajax_ok(res))  g_qy_list=res.data.data.pageData;
      })

      for(var i in qy_list){
          var item=qy_list[i];
          qy_list[i].ishas = 0;
          if(th.check_is_has_qy(item,g_qy_list)){
                qy_list[i].ishas=1;
                qy_list[i].FromId=item.Id;
          }else{
              qy_list[i].ishas=0;
          }
      }

      qy_list.sort(function (a, b) {
         return b.ishas - a.ishas
      })

      th.setData({
          qy_list: qy_list,
          click_gradeId:gr_id,
      })
  }


})