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){
        var list = [
          {name:"支",data:[{name:"北京",value:30,},{name:"上海",value:30,},{name:"杭州",value:20,},{name:"天津",value:30,}]},
          {name:"包",data:[{name:"北京",value:50,},{name:"上海",value:30,},{name:"杭州",value:30,},{name:"天津",value:30,}]},
          {name:"箱",data:[{name:"北京",value:50,},{name:"上海",value:30,},{name:"杭州",value:30,},{name:"天津",value:30,}]},
          {name:"托盘",data:[{name:"北京",value:50,},{name:"上海",value:30,},{name:"杭州",value:30,},{name:"天津",value:30,}]},
        ]
        this.initChart(list);
      }
    },
    moved: function () { },
    detached: function () { },
  },
  properties:{
    prductIndex:{
      type:String,
      value:'',
    }
  },
  observers:{
    'prductIndex':function(nv,ol){
      var token = wx.getStorageSync('token');
      if(token && nv){
        this.getCityPackageTypeState(nv);
      }
    }
  },
  methods:{
    getCityPackageTypeState(productName){
      app.requestP({
        url: '/daping/cityPackageTypeState?productName='+productName,
        method: 'post',
      }).then(res=>{
        var packageType = [{name:'支',type:10},{name:'包',type:20},{name:'箱',type:30},{name:'托盘',type:40},{name:'其他',type:50}];
        var datas = [];
        for (var index = 0; index < packageType.length; index++) {
          var types = [];
          res.data.data.map((item,i)=>{
            types.push({
              name:item.city,value:item[packageType[index].type],
            })
            if(i == res.data.data.length-1){
              datas.push(
                {name:packageType[index].name, data:types}
              )
            }
          })
        }
        this.initChart(datas);
      })
    },
    initChart(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:["#7A4AFF","#28E556","#2D59E6","#FFAE36","#EA1C43",],
        title:{
          text:"不同产品包装",
          show:0,
          textStyle:{
            color:fontColor,
            fontSize:fontSize,
          },
          left:'1%',
          top:0,
        },
        grid: {
          top: '20%',
          left: '6%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        legend:{
          icon:"circle",
          right:'10%',
          top:0,
          itemWidth:fontSize*0.8,
          itemHeight:fontSize*0.8,
          textStyle:{
            fontSize:fontSize,
            color:fontColor,
          },
          data:list.map(item=>item.name)
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
          }
        },
        yAxis: {
          type: 'value',
          axisTick:{show:false,},
          axisLabel:{
            fontSize:fontSize,
            color:fontColor,
          },
        },
        xAxis: {
          type: 'category',
          axisTick:{show:false,},
          axisLabel:{
            fontSize:fontSize,
            color:fontColor,
          },
          data:list.length ? list[0].data.map(item=>item.name) : []
        },
			  series:this.setOptItem(list),
      }
    },
    setOptItem(list){
      var data = []
      for(var i in list){
        data.push({
          type: 'bar',
          silent:1,
          barMaxWidth:this.fontSize*1.5,
          name:list[i].name,
          data:list[i].data
        })
      }
      return data
    },
  },
})