<template>
  <br />
  <el-card style="margin: 0 1em;">
    <div class="title_card" v-text="title"></div>
    <br />
    <el-button-group style="margin-left: 1.5em">
      <el-button
        size="small"
        type="primary"
        v-for="(item, i) in share_list"
        :key="'share' + i"
        :plain="share_act !== i"
        @click="() => share_change(i)"
      >
        {{ item.text }}
      </el-button>
    </el-button-group>
    <br />
    <div ref="share1"></div>
    <br />
    <div ref="share"></div>
  </el-card>
</template>

<script>
// @ is an alias to /src
// import config from "@/config/index";
import * as echarts from "echarts";
import { industryList } from "@/api/index.js";

let share1_chart = undefined,
  share_chart = undefined;
export default {
  name: "AdvertisingEye_tranche",
  data() {
    return {
      share_act: 0,
      share_list: [
        {
          text: "时长",
          proportionType: "timeSize",
          unit: "分",
          type: 2,
        },
        {
          text: "频次",
          proportionType: "pinci",
          unit: "次",
          type: 1,
        },
        {
          text: "费用",
          proportionType: "fee",
          unit: "万",
          type: 3,
        },
        {
          text: "客户数",
          proportionType: "custom",
          unit: "个",
          type: 4,
        },
      ],
    };
  },
  props: {
    title: String,
    industry: Number,
    industryRang: Number,
  },
  watch: {
    industryRang() {
      this.init();
    },
    industry() {
      this.init();
    },
  },
  mounted() {
    share1_chart = undefined;
    share_chart = undefined;
    this.init();
  },
  computed: {},
  filter: {},
  methods: {
    init() {
      Promise.all([
        industryList({
          tableType: 5,
          dataType: this.share_list[this.share_act].type,
          indexTime: this.industryRang + 1,
        }),
        industryList({
          tableType: 15,
          dataType: this.share_list[this.share_act].type,
          indexTime: this.industryRang + 1,
        }),
        industryList({
          tableType: 6,
          dataType: this.share_list[this.share_act].type,
          indexTime: this.industryRang + 1,
        }),
        industryList({
          tableType: 16,
          dataType: this.share_list[this.share_act].type,
          indexTime: this.industryRang + 1,
        }),
      ]).then(all => {
        let barAll = [...(all[1] || []), ...(all[3] || [])],
          barGroup = [];
        let k = {};
        for (let i = 0; i < barAll.length; i++) {
          const v = barAll[i];
          if (isNaN(k[v.name])) {
            k[v.name] = barGroup.length;
            barGroup.push([]);
          }
          if (i < 2) v.type = "y";
          else v.type = "r";
          barGroup[k[v.name]].push(v);
        }
        this.share1Chart(barGroup);
        let share = [[], []],
          s = 0;
        all[0].map((v, i) => {
          if (i >= 9)
            s += v[this.share_list[this.share_act].proportionType] - 0;
          i < 9 && share[0].push(v);
        });
        share[0].push({
          name: "其它",
          [this.share_list[this.share_act].proportionType]: s,
        });
        s = 0;
        all[2].map((v, i) => {
          if (i >= 9)
            s += v[this.share_list[this.share_act].proportionType] - 0;
          i < 9 && share[1].push(v);
        });
        share[1].push({
          name: "其它",
          [this.share_list[this.share_act].proportionType]: s,
        });
        this.shareChart([share[0], share[1]]);
      });
    },
    share_change(i) {
      this.share_act = i;
      this.init();
    },
    shareChart(list) {
      if (!this.$refs.share) return;
      if (share_chart && share_chart.dispose) {
        share_chart.dispose();
        share_chart = undefined;
      }
      share_chart = echarts.init(this.$refs.share);
      share_chart.resize({
        height: (this.$refs.share.offsetWidth * 6) / 16,
      });
      var option = {
        title: [
          {
            text: "重点媒体份额",
            top: 10,
            left: 50,
          },
          {
            text: "软广",
            left: "73%",
            top: 50,
          },
          {
            text: "硬广",
            left: "23%",
            top: 50,
          },
        ],
        tooltip: {
          trigger: "item",
          confine: true,
          formatter: item => {
            return (
              item.name +
              " " +
              (Number(item.value).toFixed(2) - 0) +
              (this.share_list[this.share_act].unit || "")
            );
          },
        },
        legend: {
          left: "center",
          bottom: 10,
        },
        color: [
          "#5470c6",
          "#91cc75",
          "#fac858",
          "#ee6666",
          "#73c0de",
          "#3ba272",
          "#fc8452",
          "#9a60b4",
          "#ea7ccc",
          "#20B2AA",
        ],
        series: list.map((v, i) => {
          return {
            name: i === 0 ? "硬广" : "软广",
            type: "pie",
            radius: ["30%", "60%"],
            center: [25 + i * 50 + "%", "50%"],
            avoidLabelOverlap: false,
            label: {
              show: true,
              formatter: item => {
                return (
                  item.name +
                  " \n" +
                  (Number(item.value || 0).toFixed(2) - 0) +
                  this.share_list[this.share_act].unit
                );
              },
            },
            labelLine: {
              show: true,
            },
            data: v.map(item => {
              return {
                name: item.name,
                value:
                  item[this.share_list[this.share_act].proportionType] || 0,
              };
            }),
          };
        }),
      };

      option && share_chart.setOption(option);
    },
    share1Chart(list) {
      if (!this.$refs.share1) return;
      if (share1_chart && share1_chart.dispose) {
        share1_chart.dispose();
        share1_chart = undefined;
      }
      share1_chart = echarts.init(this.$refs.share1);
      share1_chart.resize({
        height: (this.$refs.share1.offsetWidth * 6) / 25,
      });
      let t = -1;
      let k = this.share_list[this.share_act].proportionType || "";
      list[0][0] = list[0][0] || { [k]: 0 };
      list[0][1] = list[0][1] || { [k]: 0 };
      list[1][0] = list[1][0] || { [k]: 0 };
      list[1][1] = list[1][1] || { [k]: 0 };
      if (Number(list[0][0][k]) + Number(list[1][0][k]) === 0) t = 0;
      if (Number(list[0][1][k]) + Number(list[1][1][k]) === 0) t = 1;
      let li = list.map(v => {
        const item = v[0] || {};
        return {
          name: item.name,
          type: "bar",
          stack: "total",
          label: {
            show: true,
            formatter: item => {
              if (item.value === 0) return "";
              return item.value + "%";
            },
          },
          emphasis: {
            focus: "series",
          },
          data: v.reverse().map(o => ((o[k] || 0) * 100).toFixed(2) - 0),
        };
      });
      if (t > -1 && k === "fee") {
        li.push({
          type: "bar",
          stack: "total",
          color: "rgba(255,255,255,1)",
          confine: true,
          tooltip: {
            show: false,
          },
          label: {
            show: true,
            formatter: (v) => {
                console.log(v)
              if (v.value === 100 && t === 0) return "硬广无刊例价";
              if (v.value === 100 && t === 1) return "软广无刊例价";
              return ""
            },
          },
          data: t === 0 ? [0, 100] : [100, 0],
        });
      }
      var option = {
        tooltip: {
          confine: true,
          trigger: "item",
          axisPointer: {
            type: "shadow",
          },
          formatter: item => {
            return (
              item.name +
              "<br />" +
              item.seriesName +
              "<br />" +
              "占比：" +
              item.value +
              "%"
            );
          },
        },
        legend: {},
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        color: ["#1b9ade", "#fd5555"],
        xAxis: {
          type: "value",
          axisLabel: {
            formatter: item => {
              return item + "%";
            },
          },
        },
        yAxis: {
          type: "category",
          data: ["软广", "硬广"],
        },
        series: li,
      };
      option && share1_chart.setOption(option);
    },
  },
  beforeUnmount: function() {
    share1_chart && share1_chart.dispose();
    share_chart && share_chart.dispose();
    share1_chart && (share1_chart = undefined);
    share_chart && (share_chart = undefined);
  },
  components: {},
};
</script>

<style></style>
