const app = getApp();
import * as echarts from '../../../../ec-canvas/echarts' // 这个是自己实际的目录
Component({
  data:{
    Ec: { lazyLoad: true, },
  },
  lifetimes: {
    attached: function () {
      var token = wx.getStorageSync('token');
      if(token){
        this.getYearProduceSatateList();
      }else{
        var list = [
          {name:"生产",data:[{name:"2022",value:30,},{name:"2021",value:30,},{name:"2020",value:20,},{name:"2019",value:30,}]},
          {name:"销售",data:[{name:"2022",value:50,},{name:"2021",value:30,},{name:"2020",value:30,},{name:"2019",value:30,}]},
          {name:"在途",data:[{name:"2022",value:50,},{name:"2021",value:30,},{name:"2020",value:30,},{name:"2019",value:30,}]},
          {name:"交付",data:[{name:"2022",value:50,},{name:"2021",value:30,},{name:"2020",value:30,},{name:"2019",value:30,}]},
        ]
        this.init(list);
      }
    },
    moved: function () { },
    detached: function () { },
  },
  methods:{
    getYearProduceSatateList(){
      app.requestP({
        url: '/daping/yearProduceSatate',
        method: 'post',
      }).then(res=>{
        var orderType = [{name:'生产',type:'produce'},{name:'销售',type:'sell'},{name:'在途',type:'trans'},{name:'交付',type:'end'}];
        var datas = [];
        for (var index = 0; index < orderType.length; index++) {
          var types = [];
          res.data.data.map((item,i)=>{
            types.push({
              name:item.year,value:item[orderType[index].type],
            })
            if(i == res.data.data.length-1){
              datas.push(
                {name:orderType[index].name, data:types}
              )
            }
          })
        }
        this.init(datas)
      })
    },
    init(list){
      var that = this;
      that.selectComponent('#chartConDom').init((canvas, width, height, dpr) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr
        });
        var option = that.getOrderWarnOption(list);
        chart.setOption(option);
        return chart;
      });
    },
    getOrderWarnOption(list){
      var fontSize = 10
      var fontColor = "#999999"
      return {
        color:["#583DC8","#D1B645","#31CE7B","#5B7DEB"],
        title:{
          text:"产品年报数据",
          show:false,
          textStyle:{
            color:fontColor,
            fontSize:fontSize,
          },
          left:'10%',
          top:0,
        },
        grid: {
          top: '10%',
          left: '6%',
          right: '10%',
          bottom: '3%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
          }
        },
        legend:{
          show:true,
          icon:"circle",
          right:'',
          top:0,
          itemWidth:fontSize*0.8,
          itemHeight:fontSize*0.8,
          textStyle:{
            fontSize:fontSize,
            color:fontColor,
          },
          data:list.map(item=>item.name)
        },
        yAxis: {
          type: 'value',
          axisTick:{show:false,},
          axisLabel:{
            fontSize:fontSize,
            color:fontColor,
          },
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisTick:{show:false,},
          axisLabel:{
            fontSize:fontSize,
            color:fontColor,
          },
          data: list[0].data.map(item=>item.name)
        },
			  series:this.setOptItem(list),
      }
    },
    setOptItem(list){
      var data = []
      for(var i in list){
        data.push({
          type: 'line',
          stack: 'Total',
          areaStyle: {},
          lineStyle: { width:1,},
          silent:1,
          symbolSize:1,
          name:list[i].name,
          data:list[i].data
        })
      }
      return data
    },
  },
})