<template>
  <div class="GBList">
    <div class="itemHead">
      <van-row>
        <van-col span="3" class="td"> 排名 </van-col>
        <van-col span="9" class="td"> 频率 </van-col>
        <van-col span="6" class="td"> 收听率 </van-col>
        <van-col span="6" class="td"> 占有率 </van-col>
      </van-row>
    </div>
    <van-row
      class="item"
      v-for="(v, o) in platfromData || []"
      :key="v.name"
    >
      <van-col
        span="3"
        :class="{ td: true }"
        :style="{ color: color[o] || '#000', fontWeight: 600 }"
      >
        {{ o + 1 }}
      </van-col>
      <van-col span="9" :class="{ td: true }">
        {{ v.name }}
      </van-col>
      <van-col span="6" :class="{ td: true }" style="color: #e1a74f">
        {{ formatNum(v.radio_rate) }}%
      </van-col>
      <van-col span="6" :class="{ td: true }" style="color: #e1a74f">
        {{ formatNum(v.market_rate) }}%
      </van-col>
    </van-row>
  </div>
</template>

<script>
// @ is an alias to /src
import {
  Col as vanCol,
  Row as vanRow,
} from "vant";
import "vant/lib/col/style/index";
import "vant/lib/row/style/index";
// import {} from "../utils/tool";
import { jsonDataRanking } from "../../../api/index";

import echarts from "../../../utils/echarts";

export default {
  name: "GBList",
  data() {
    return {
      setact: false,
      show: false,
      charts: undefined,
      ratios: [],
      platfromData: [],
      color: ["#ff0036", "#ff9b00", "#ffcc00"],
    };
  },
  props: {
    valDay: String,
  },
  watch: {
    valDay() {
      this.init();
    },
  },
  mounted() {
    this.init();
  },
  computed: {},
  methods: {
    init() {
      //   初始
      if (!this.valDay) return;
      this.setact = true;
      this.getData();
    },
    formatNum(num) {
      if (isNaN(num)) return 0;
      return Number(num).toFixed(4) - 0;
    },
    showPopup(i) {
      this.show = true;
      this.ratios = this.platfromData[i] || [];
      this.$nextTick(() => {
        this.upEcharts();
      });
    },
    upEcharts() {
      if (!this.ratios.ratios || !this.ratios.ratios.length) {
        this.echarts && this.echarts.dispose && this.echarts.dispose();
        return;
      }
      if (!this.echarts) {
        const doc = document.body;
        const width = doc.offsetWidth - 32;
        this.echarts = echarts.init(
          this.$refs.pie,
          {},
          {
            width,
            height: width,
          }
        );
      }
      const key = [],
        value = [];
      let max = 0;
      this.ratios.ratios.map(v => {
        let val = (v.value * 100).toFixed(2) - 0;
        key.push(v.name);
        value.push(val);
        max < val && (max = val);
      });
      max += max * 0.2;
      max > 100 && (max = 100);
      this.echarts.setOption({
        title: {
          text: "观看时长用户分布",
          textStyle: {
            fontSize: 14,
          },
        },
        radar: {
          shape: "circle",
          center: ["50%", "50%"],
          radius: 100,
          indicator: key.map((v, i) => {
            let m = value[i] / max,
              val = max;
            m <= 0.5 ? (val = 0.5 * max) : "";
            return {
              name: v,
              max: val,
            };
          }),
        },
        series: [
          {
            type: "radar",
            data: [
              {
                value,
                label: {
                  show: true,
                  formatter: function (params) {
                    return params.value + "%";
                  },
                },
                itemStyle: {
                  color: "#F9713C",
                },
                areaStyle: {
                  opacity: 0.1,
                },
              },
            ],
          },
        ],
      });
    },
    getData() {
      jsonDataRanking({
        url: ["fm", this.valDay].join("-"),
      })
        .then(res => {
          this.platfromData = res || [];
          this.$emit("start", {
            status: 200,
          });
        })
        .catch(err => {
          this.$emit("start", {
            status: err,
          });
        });
    },
  },
  beforeUnmount() {},
  components: {
    vanCol,
    vanRow,
  },
};
</script>

<style scoped>
.GBList {
  font-size: 15px;
  padding: 5px;
}
.td {
  text-align: center;
  line-height: 2.5em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.item {
  overflow: hidden;
  border-radius: 3px;
  margin-top: 11px;
  background-color: #f5f6f8;
}
.itemHead {
  margin-top: 0;
  background-color: #f5f6f8;
}
.main {
  padding: 0.5em;
}
</style>
<style>
.GBList .van-tab {
  font-size: 16px;
}
</style>
