import * as echarts from '../../component/ec-canvas/echarts';

// 饼状图
let itemGap = 15;
function pieInitChart(chart, legendList, ydata) {
  var option = {
    tooltip: {
      show: false
    },
    color:['#ffa16d','#ffd560','#33edcf','#4dc8f9','#7485f5','#d643f6','#ff6d6d'],
    legend: {
      left: 'center',
      top: 'center',
      orient: 'vertical',
      // top: 10,
      textStyle:{
        color: '#4f5051',
        fontSize: 11
      },
      icon: "circle",
      itemWidth: 8,
      itemHeight: 8,
      itemGap: itemGap,//图例间隔
      data: legendList
    },
    series: [
      {
        // silent: true,//不响应和触发鼠标事件
        hoverAnimation: false,//是否开启 hover 在拐点标志上的提示动画效果。
        type: 'pie',
        // radius : '80%',
        radius: ['52%', '100%'],
        // center: ['50%', '60%'],
        // data:[
        //   {value:48, name:'静态训练'},
        //   {value:28, name:'动态训练'},
        //   {value:24, name:'单词训练'}
        // ],
        data: ydata,
        label:{//饼图图形上的文本标签
          normal:{
            show: true,
            position: 'inner', //标签的位置
            textStyle : {
              fontWeight : 300 ,
              fontSize : 14//文字的字体大小
            },
            formatter: '{d}%'
          }
        }
      }
    ]
  };

  chart.setOption(option);
}

var app = getApp();
Page({
  data: {
    avatar: "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJgjIlZNw29LLpbQJYlmjib7AuaqROweKxUib78ibPbp0M6mGKvRXNnpT1M4XkwDwFPAypwOFLXko0zQ/132",
    teenageName: '小明',
    pieChart: {
      // onInit: pieInitChart
      lazyLoad: true
    },
    beginDay: '',
    endDay: '',
    weekNumber: '',
    finishedStatus: '',
    weekDuration: '',
    rightEye: '',
    leftEye: '',
    weekLeftEye: '',
    weekRightEye: '',
    rightGlassesDegree: '',
    leftGlassesDegree: '',
    leftCorrectedEye: '',
    rightCorrectedEye: '',
    leftWeekEye: '',
    rightWeekEye: '',
    leftLeerEye: '',
    rightLeerEye: '',
    appList: [],
  },
  onLoad: function (options) {
    let me = this;
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'Reports/detail',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        // historyId: options.historyId
        historyId: 12
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            name: res.data.data.name,
            avatar: res.data.data.avatar,
            beginDay: res.data.data.beginDay,
            endDay: res.data.data.endDay,
            weekNumber: res.data.data.weekNumber,
            finishedStatus: res.data.data.finishedStatus,
            weekDuration: res.data.data.weekDuration,
            rightEye: res.data.data.rightEye,
            leftEye: res.data.data.leftEye,
            weekLeftEye: res.data.data.weekLeftEye,
            weekRightEye: res.data.data.weekRightEye,
            rightGlassesDegree: res.data.data.rightGlassesDegree,
            leftGlassesDegree: res.data.data.rightGlassesDegree,
            leftCorrectedEye: res.data.data.leftCorrectedEye,
            rightCorrectedEye: res.data.data.rightCorrectedEye,
            leftWeekEye: res.data.data.leftWeekEye,
            rightWeekEye: res.data.data.rightWeekEye,
            leftLeerEye: res.data.data.leftLeerEye,
            rightLeerEye: res.data.data.rightLeerEye,
            appList: res.data.data.appList,
          })
          let legendList = []
          let pieList = []
          res.data.data.typeTrainingList.map((item)=>{
            legendList.push(item.title)
            pieList.push({value: item.trainingDuration,name: item.title})
          })
          if(legendList.length>5){
            itemGap = 5
          }else{
            itemGap = 15
          }
          me.selectComponent('#reprotPieChart').init((canvas, width, height) => {
            const chart = echarts.init(canvas, null, {
              width: width,
              height: height
            });
            pieInitChart(chart, legendList, pieList)
            me.chart = chart;
            return chart;
          });
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  }
})
