const util = require('../../../utils/util.js');
import * as echarts from '../../../components/ec-canvas/echarts.js';
const app = getApp();
Page({
  data: {
    indexs:{
      child:0,
      activity:0,
      video: null,
      videoParent: null
    },
    noData: false,
    childs:[],
    activity:[],
    actives: [],
    list: [],
    ec: {
      lazyLoad: true // 延迟加载
    }
  },
  onShow() {
    this.getChild();
  },
  getChild(){
    let that = this;
    util.ajax({
      func: "user/childs",
      load: false
    }, function (res) {
      if (res.code == 0) {
        that.setData({ childs: res.data, noData: res.data.length > 0 ? true : false });
        if (!util.isObjEmpty(that.data.childs)) that.getActivity();
      }else {
        util.showTips(res.reason);
      }
    });
  },
  getActivity() {
    let that = this;
    util.ajax({
      func: "v2/child/camps",
      data: { "cardNo": that.data.childs[that.data.indexs.child].cardNo, "cardType": that.data.childs[that.data.indexs.child].cardType },
      load: false
    }, function (res) {
      if (res.code == 0) {
        if (!util.isObjEmpty(res.data)){
          that.setData({ activity: res.data, noData: true });
          that.getInfo();
        }else{
          that.setData({ activity: res.data, info: null, list: [], noData: false });
        }
        that.jsonDataArr();
      } else {
        util.showTips(res.reason);
      }
    });
  },
  jsonDataArr() {
    let self = this, arr = [];
    for (var i = 0; i < self.data.activity.length; i++) {
      arr.push(self.data.activity[i].sname + " " + self.data.activity[i].ptitle);
    }
    
    this.setData({ actives: arr });
  },
  getCourseList(load){
    let that = this;
    util.ajax({
      func:'v2/course/list',
      data: { "playerId": that.data.info.playerId, 'id': that.data.info.id},
      load: load
    },function(res){
      if(res.code == 0){
        that.setData({ list: res.data });
      }else
        util.showTips(res.reason);
    });
  },
  getInfo(){
    let that = this;
    util.ajax({
      func:"v2/course/center",
      data: { "cardNo": that.data.childs[that.data.indexs.child].cardNo, "cardType": that.data.childs[that.data.indexs.child].cardType, "aid": that.data.activity[that.data.indexs.activity].aid, "campId": that.data.activity[that.data.indexs.activity].campId},
      load: false
    },function(res){
      if(res.code == 0){
        if (!util.isObjEmpty(res.data)) {
          that.setData({ info: res.data, noData: res.data.courseNum });
          if (res.data.courseNum > 0){
            that.getCourseList(false);
            if (!util.isObjEmpty(res.data.xarr)) that.chartInit(res.data.show);
          }
        }else{
          that.setData({ noData: false, info: null, list: [] });
        }
      } else if (res.statusCode != 200) {
        that.setData({ noData: false });
      } else
        util.showTips(res.reason);
    })
  },
  bindChildChange(e) {
    this.setData({
      indexs: { child: e.detail.value, activity: 0},
      activity:[],
      info:null,
      noData: true
    });
    this.getActivity();
  },
  bindActivityChange(e) {
    this.data.indexs.activity = e.detail.value;
    this.setData({
      indexs: this.data.indexs,
      info: null,
      noData: true
    });
    this.getInfo();
  },
  updateHead(){
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.uploadFile({
          url: util.config.apiServer +'v2/child/avatar.do', // 仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'avatarFile',
          formData: {
            childId: that.data.info.childId,
            rid: app.globalData.userInfo ? app.globalData.userInfo.rid : ''
          },
          success(res) {
            let data = JSON.parse(res.data);
            if (res.statusCode==200&&data.code == 0){
              that.data.info.avatar = data.data;
              that.setData({info : that.data.info });
            }else
              util.showTips(res.reason);
          }
        })
      }
    })
  },
  playEnd(event){
    this.data.indexs.video = null;
    this.data.indexs.videoParent = null;
    this.setData({
      indexs: this.data.indexs
    })
  },
  videoPlay(event){
    let e = event.currentTarget, parentIndex = e.dataset.parentindex, index = e.dataset['index'];
    if (!this.data.indexs.video && !this.data.indexs.videoParent) {
      this.data.indexs.video = index;
      this.data.indexs.videoParent = parentIndex;
      this.setData({
        indexs: this.data.indexs
      });
      this.videoContext = wx.createVideoContext('video_' + parentIndex+'_'+ index)
      this.videoContext.play();
      this.videoContext.requestFullScreen();
    } else {
      this.videoContext = wx.createVideoContext('video_' + this.data.indexs.videoParent + '_' + this.data.indexs.video)
      this.videoContext.stop();
      this.videoContext.exitFullScreen();
      this.data.indexs.video = index;
      this.data.indexs.videoParent = parentIndex;
      this.setData({
        indexs: this.data.indexs
      })
      this.videoContext = wx.createVideoContext('video_' + parentIndex + '_' + index);
      this.videoContext.play();
      this.videoContext.requestFullScreen();
    }
  },
  fullscreen(e){
    if (!e.detail.fullScreen){
      this.videoContext.stop();
      this.data.indexs.video = null;
      this.data.indexs.videoParent = null;
      this.setData({
        indexs: this.data.indexs
      })
    }
  },
  navigatorUrl(e){
    let data = e.currentTarget.dataset;
    util.navigator(data.url);
  },
  chartInit(type){
    if (util.isObjEmpty(this.data.info.xarr)) return false;
    let columns = [], that = this;
    for (let i in this.data.info.xarr){
      columns.push({
        value: this.data.info.xarr[i],
        textStyle: {
          color: '#908f90'
        }
      });
    }
    this.selectComponent('#mychart').init((canvas, width, height) => {
      // 初始化图表
      var Chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      Chart.clear();
      let options = {
        xAxis: {
          type: 'category',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 0,
            rotate: 40
          },
          data: columns
        },
        yAxis: {
          show: false
        },
        grid: {
          left: '0',
          right: '0',
          bottom: '12%'
        },
        series: [{
          type: 'bar',
          barWidth: 38,
          label: {
            show: true,
            position: 'top',
            formatter: '{c}分',
            color: '#ee3a43'
          },
          itemStyle: {
            color:'#ffccd3'
          },
          data: that.data.info.yarr
        }]
      };
      if(type == 0){
        options.grid = {
          left: '5%',
          right: '5%',
          bottom: '40%'
        };
        options.dataZoom = [{
          type:'inside',
          start: 0,
          end: 30
        },{
          type: 'slider',
          showDetail:false,
          showDataShadow:false,
          handleStyle:{
            borderColor:'#ee3a43'
          },
          backgroundColor:'#fff0f3',
          handleSize:'60%',
          handleIcon:'path://M30.9,53.2c-14.1,0-25.6-11.5-25.6-25.6S16.8,2,30.9,2C45,2,56.4,13.5,56.4,27.6S45,53.2,30.9,53.2z'
        }];
      }
      Chart.setOption(options);
      return Chart;
    });
  },
  onShareAppMessage() {
    return {
      title: '活动营前课',
      path: '/pages/account/course/index'
    }
  }
})