const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    active_tab:0,
    groupData:{},
    current:0,
    hidden:true,
    medals:[
      {oldSrc:'https://m.bbztx.com/img/medal/o1.png',newSrc:'https://m.bbztx.com/img/medal/n1.png',text:'宝贝勋章',days:3},
      {oldSrc:'https://m.bbztx.com/img/medal/o2.png',newSrc:'https://m.bbztx.com/img/medal/n2.png',text:'勇士勋章',days:6},
      {oldSrc:'https://m.bbztx.com/img/medal/o3.png',newSrc:'https://m.bbztx.com/img/medal/n3.png',text:'英雄勋章',days:9},
      {oldSrc:'https://m.bbztx.com/img/medal/o4.png',newSrc:'https://m.bbztx.com/img/medal/n4.png',text:'骑士勋章',days:15},
      {oldSrc:'https://m.bbztx.com/img/medal/o5.png',newSrc:'https://m.bbztx.com/img/medal/n5.png',text:'王者勋章',days:21},
      {oldSrc:'https://m.bbztx.com/img/medal/o6.png',newSrc:'https://m.bbztx.com/img/medal/n6.png',text:'彩虹勋章·赤',days:27},
      {oldSrc:'https://m.bbztx.com/img/medal/o7.png',newSrc:'https://m.bbztx.com/img/medal/n7.png',text:'彩虹勋章·橙',days:36},
      {oldSrc:'https://m.bbztx.com/img/medal/o8.png',newSrc:'https://m.bbztx.com/img/medal/n8.png',text:'彩虹勋章·黄',days:45},
      {oldSrc:'https://m.bbztx.com/img/medal/o9.png',newSrc:'https://m.bbztx.com/img/medal/n9.png',text:'彩虹勋章·绿',days:54},
      {oldSrc:'https://m.bbztx.com/img/medal/o10.png',newSrc:'https://m.bbztx.com/img/medal/n10.png',text:'彩虹勋章·青',days:66},
      {oldSrc:'https://m.bbztx.com/img/medal/o11.png',newSrc:'https://m.bbztx.com/img/medal/n11.png',text:'彩虹勋章·蓝',days:78},
      {oldSrc:'https://m.bbztx.com/img/medal/o12.png',newSrc:'https://m.bbztx.com/img/medal/n12.png',text:'彩虹勋章·紫',days:90},
      {oldSrc:'https://m.bbztx.com/img/medal/o13.png',newSrc:'https://m.bbztx.com/img/medal/n13.png',text:'白虎勋章',days:105},
      {oldSrc:'https://m.bbztx.com/img/medal/o14.png',newSrc:'https://m.bbztx.com/img/medal/n14.png',text:'朱雀勋章',days:120},
      {oldSrc:'https://m.bbztx.com/img/medal/o15.png',newSrc:'https://m.bbztx.com/img/medal/n15.png',text:'青龙勋章',days:135},
      {oldSrc:'https://m.bbztx.com/img/medal/o16.png',newSrc:'https://m.bbztx.com/img/medal/n16.png',text:'玄武勋章',days:153},
      {oldSrc:'https://m.bbztx.com/img/medal/o17.png',newSrc:'https://m.bbztx.com/img/medal/n17.png',text:'终极勋章',days:171},
    ],
    share:false,//是否是分享
    currentPages:0,
    user:[],
    cardNo:'',
    cardType:'',
    avatar:'',
    name:'',
  },
  onShow:function(){
    let that = this;
    if(this.data.hidden){
      if(getCurrentPages().length == 1){
        // var getLocation = util.promise();
        // getLocation.then(function(value) {
          that.getUser();
          that.setData({currentPages:getCurrentPages().length})
        // }, function(error) {
        // });
      }else{
        that.getUser();
      }
    }
  },
  getUser:function(){
    let that = this;
    util.ajax({
      func:"user/childs",
    },function(res){
      if(res.code == 0){
        let user = [],current = 0;
        user = res.data;
        if (user.length > 0) {
          that.setData({ user});
          that.userData(current);
        }else{
          that.setData({ hidden :false });
        }
      }
    })
  },
  userData:function(current){
    let that = this,user = this.data.user;
    util.ajax({
      func:"user/medal_child",
      data: { "cardNo": user[current].cardNo, "cardType": user[current].cardType},
      load:false
    },function(res){
      let datas = res.data || {},medals = that.data.medals;
      for(let i in medals){
        medals[i].show = false
      }
      if(datas && datas.medal && datas.medal.length>0){
        for(let i in datas.medal){
          let l = datas.medal[i].match(/img\/x(\S*).png/)[1];
          medals[l-1].show = true;
        }
      }
      if(datas && datas.goldSeal && datas.goldSeal.length > 0){
        for(let i in datas.goldSeal){
          datas.goldSeal[i] = util.config.imgUrl + datas.goldSeal[i]
        }
      }
      that.setData({medals,groupData:datas,hidden:false});
    })
  },
  choose(e) {
    this.userData(e.detail)
    this.setData({current:e.detail})
  },
  imgdetails:function(e){
    let index = e.currentTarget.dataset.index,seals = this.data.groupData.seals;
    wx.previewImage({
      current: seals[index], // 当前显示图片的http链接
      urls: seals // 需要预览的图片http链接列表
    })
  },
  imgGold:function(e){
    let index = e.currentTarget.dataset.index,seals = this.data.groupData.goldSeal;
    wx.previewImage({
      current: seals[index], // 当前显示图片的http链接
      urls: seals // 需要预览的图片http链接列表
    })
  },
  bingImg:function(e){
    let index = e.currentTarget.dataset.index,medals = this.data.medals,urls=[];
    for(let i in medals){
      urls.push(medals[i].src)
    }
    wx.previewImage({
      current: urls[index], // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  // tab栏切换
  changeTab(e){
    console.log(e)
    let val = e.currentTarget.dataset.value;
    this.setData({active_tab:val})
  },
  // 点击图片跳转
  jumpToBuy(){
    wx.navigateTo({
      url: '/pages/product/goods/detail?aid=7cb01701-98d9-4f17-a24e-53568fa095fd',
    })
  },
  onShareAppMessage: function () {
    return {
      title: '儿童参加活动的印章勋章记录',
      path: '/pages/account/passport/passport'
    }
  },
})