const util = require('../../../utils/util.js');
const app = getApp();
import { radar } from '../../../components/radar/radar';
Page({
  radar,
  data: {
    datas:{},
    current:0,
    radarData:[],
    load:false,
    user:[],
    userData:{},
    currentPages:0,
    noData:false,
    noChildData:false,
    onshow:false,
    cardNo:'',
    cardType:'',
    avatar:'',
    name:'',
    npsOptons: {},
    npsStatus: false,//nps组件是否显示
    isGet: true,
    pageIndex:1,
    pageSize: 20,
    isLoadMore: true,
    showPoter:false,
    posterInfo:{}
  },
  onShow:function(){
    let currentPages = getCurrentPages().length;
    if(!this.data.onshow){
      this.setData({current:0})
      let that = this,rid = this.data.rid;
      if (currentPages == 1){
        // var getLocation = util.promise();
        // getLocation.then(function(value) {
          that.getUser();
          that.setData({currentPages});
        // }, function(error) {
        // });
      }else{
        that.getUser();
      }
    }
  },
  bindchange(e){
    let index= e.currentTarget.dataset.index,current=e.detail.current;
    this.data.datas[index].currentInx=current+1;
    this.setData({datas:this.data.datas})
  },
  showPoster(e){
    let data =e.currentTarget.dataset.item;
    wx.reportAnalytics('show_poster', {
      aid: data.aid,
      title: data.atitle,
    });
    data.ChildName=this.data.user[this.data.current].name;
    // this.setData({
    //   showPoter:true,
    //   posterInfo:data
    // })
    let urlData=[];
    urlData.push('aid='+data.aid)
    urlData.push('atitle='+data.atitle)
    urlData.push('ChildName='+data.ChildName)
    urlData.push('id='+data.id)
    if (!util.isEmpty(data.picWidthHeight)) {
      urlData.push('picWidthHeight='+data.picWidthHeight)
    }
    if (data.pics.length>0) {
      urlData.push('pic='+data.pics[0])
    }
    wx.navigateTo({
      // url: '/components/poster/poster?aid='+data.aid+'&atitle='+data.atitle+'&ChildName='+data.ChildName+'&id='+data.id+'&picWidthHeight='+data.picWidthHeight+'&pic='+data.pics[0],
      url:'/components/poster/poster?'+urlData.join('&')
    })
  },
  closePoster(e){
    this.setData({showPoter:false})
  },
  selectImage(e) {
    this.setData({ isGet: e.detail.isGet });
  },
  close(e) {//关闭nps
    this.setData({ npsStatus: false });
  },
  // 查看图片
  showImg(e){
    let data = e.currentTarget.dataset;
    wx.previewImage({
      current: data.url,
      urls: data.item
    })
  },
  getUser(){
    let that = this;
    util.ajax({
      func:"user/childs"
    },function(res){
      if(res.code == 0){
        that.setData({ user: res.data || [], onshow:true});
        that.userData();
      }else
        util.showTips(res.reason);
    })
  },
  userData(aload){  
    let that = this, user = this.data.user;
    if (user.length <= 0){
      that.setData({ noData: true });
      return false;
    }
    util.ajax({
      func:"v2/child/activity/list",
      data: { "cardNo": user[that.data.current].cardNo, "cardType": user[that.data.current].cardType, "pageIndex": that.data.pageIndex, "pageSize": that.data.pageSize},
      load: aload
    },function(res){
      if (res.data.list&&res.data.list.length>0) {
        res.data.list.forEach(el => {
          if (el.pics.length>0) {
            util.getImageInfo(el.pics[0]).then(res=>{
              el.picWidthHeight=res;
            })
          }
        });
      }
      let newData = res.data.list || [], isLoadMore = true, datas = !aload ? newData : [...that.data.datas, ...newData];
      if (newData.length < that.data.pageSize && datas.length > that.data.pageSize) {
        isLoadMore = false;
      }
      if(datas && datas.length > 0){
        let tempIndex = false;
        for(let i in datas){
          if (datas[i].status != 1 || (datas[i].status == 1 && util.isEmpty(datas[i].comment))){
            datas[i].isDisplay = true;
            datas[i].link = false;
          }else{
            datas[i].isDisplay = false;
            datas[i].link = true;
          }
          if (tempIndex == false && datas[i].status == 1 && !util.isEmpty(datas[i].comment)){
            datas[i].isDisplay = true;
            tempIndex = true;
          }
          datas[i].canvasId = 'radarCanvas' + i;
        }
      }
      var noChildData = false;
      if(util.isObjEmpty(datas)){
        noChildData = true
      }
      let timer =setTimeout(() => {
         that.setData({ datas, noChildData, isLoadMore, load:true});
         clearTimeout(timer)
      }, 200);
     
      that.Canvas();
      wx.hideLoading();
    })
  },
  choose(e){
    this.setData({ current: e.detail, pageIndex: 1, isLoadMore:true});
    this.userData();
  },
  Canvas(){
    let datas = this.data.datas;
    if(datas && datas.length > 0){
      for (let i in datas){
        if (datas[i].scores && datas[i].scores.length > 0){
          this.radar.draw(datas[i].scores, datas[i].canvasId, datas[i].grades);
        }
      }
    }
  },
  onPullDownRefresh() {
    var self = this;
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.setData({ pageIndex: 1 });
      self.userData(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom() {
    var self = this;
    if (self.data.isLoadMore == false) return false;
    setTimeout(function () {
      self.setData({ pageIndex: self.data.pageIndex + 1 });
      self.userData(true);
    }, 1000);
  },
  details(e){
    var i = parseInt(e.currentTarget.dataset.i), pics = this.data.datas[e.currentTarget.dataset.index].pics;
    wx.previewImage({
      current: pics[i], // 当前显示图片的http链接
      urls: pics // 需要预览的图片http链接列表
    });
  },
  copy(e){
    let index=e.currentTarget.dataset.index,activitys=this.data.datas;
    wx.setClipboardData({
      data: activitys[index].comment,
      success: function(res) {
        util.showTips('复制成功');
      }
    })
  },
  onShareAppMessage() {
    return {
      title: '儿童参加活动的成长记录',
      path: '/pages/account/record/record'
    }
  }
})