<template>
  <div class="echarts">
    <ECharts :option="option" :resize="false" />
  </div>
</template>

<script setup>
import ECharts from "@/components/ECharts/index.vue";
import { ranking1, ranking2, ranking3, ranking4 } from "../assets/ranking-icon";
import echarts from "@/components/ECharts/config";
import { getReturnRateByPlatform } from '@/api/dataScreen';
import { computed } from "vue";
import { reactive, ref } from 'vue';
const load =ref(false);
const timer =ref(null);
const salvProName =ref([]);
const salvProValue =ref([]);
const dataZoom  =ref({
  startValue: 0,
  endValue: 5
});
const props = defineProps({
  isAdmin: Boolean,
  tenantId: [String, Number, Object]
});

const option = computed(() => {
  return {
    grid: {
        left: '2%',
        right: '2%',
        bottom: '2%',
        top: '2%',
        containLabel: true
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'none'
        },
        formatter: function(params) {
            return params[0].name  + ' : ' + params[0].value + '%'
        }
    },
    dataZoom: [
      {
        type: 'slider',
        orient: 'vertical',
        show: false,
        ...dataZoom.value
      },
    ],
    xAxis: {
        show: false,
        type: 'value'
    },
    yAxis: [{
        type: 'category',
        inverse: true,
        axisLabel: {
            show: false,
            textStyle: {
                color: '#fff',
                fontSize: '14'
            },
        },
        splitLine: {
            show: false
        },
        axisTick: {
            show: false
        },
        axisLine: {
            show: false
        },
        data: salvProName.value
    }, {
        type: 'category',
        inverse: true,
        axisTick: 'none',
        axisLine: 'none',
        show: true,
        axisLabel: {
            textStyle: {
                color: '#ffffff',
                fontSize: '14'
            },
            formatter: function (a, b) {
              return a + '%'
            }
        },
        data:salvProValue.value
    }],
    series: [{
            name: '值',
            type: 'bar',
            zlevel: 1,
            itemStyle: {
                normal: {
                    // barBorderRadius: 30,
                    color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                        offset: 0,
                        color: 'rgb(57,89,255,1)'
                    }, {
                        offset: 1,
                        color: 'rgb(46,200,207,1)'
                    }]),
                },
            },
            label: {
              normal: {
                color: '#fff',
                show: true,
                position: [0, '-24px'],
                textStyle: {
                  fontSize: 16
                },
                formatter: function (a, b) {
                  return a.name
                }
              }
            },
            barWidth: 50,
            data: salvProValue.value
        },
        {
            name: '背景',
            type: 'bar',
            barWidth: 50,
            barGap: '-100%',
            data: salvProValue.value.map(v => 100),
            itemStyle: {
                normal: {
                    color: '#142166',
                    // barBorderRadius: 30,
                }
            },
        },
    ]
  }
})

  const query =() => {
    const param = {}
if (!props.isAdmin) {
  param['tenant'] = props.tenantId
}
load.value = true
getReturnRateByPlatform(param).then(res => {
    if (res.result) {
      const arr = res.result
      arr.sort((a,b) =>  b.rate - a.rate)
      load.value = false
      salvProName.value = arr.map(v => v.loanPlatform)
      salvProValue.value = arr.map(v => v.rate?.toFixed(2))
      if (salvProName.value.length > 6) {
        if (timer.value) {
          clearInterval(timer.value)
          timer.value = null
        }
        timer.value = setInterval(() => {
          dataZoom.value.startValue++;
          if (dataZoom.value.endValue++ >= salvProName.value.length - 1) {
            dataZoom.value.startValue = 0;
            dataZoom.value.endValue = 5;
          }
          // chart?.setOption(option);
        }, 3000);
      }
    }
  })
  }
  query()
setInterval(() => {
  if (!load.value) {
    query()
  }
}, 3000)
</script>
<style lang="scss" scoped>
.echarts {
  width: 100%;
  height: calc(100%);
}
.echarts-header {
  box-sizing: border-box;
  display: flex;
  height: 36px;
  margin: 10px 10px 0;
  line-height: 36px;
  background: url("../images/rankingChart-bg.png") no-repeat;
  background-size: 100% 100%;
  span {
    width: 18%;
    margin-left: 4px;
    font-size: 14px;
    font-weight: bold;
    color: #fdbc52;
    text-align: center;
    &:nth-child(2) {
      margin-left: 4px;
    }
    &:last-child {
      width: 20%;
      margin-left: 60px;
    }
  }
}
</style>
