<template>
  <div>
    <div class="visualizing laonianren">
      <div class="visualizing-left">
        <div>
          <data-title title="老年人年龄统计" />
        </div>
        <div id="lnrLeftChart" style="height: 400px"></div>
      </div>
      <div class="visualizing-right">
        <div>
          <data-title title="老年人居住情况统计" />
        </div>
        <div id="lnrRightChart" style="height: 400px"></div>
      </div>
    </div>
  </div>
</template>

<script>
import dataTitle from "@/views/dataBoard/renfang/visualizing/components/dataTitle.vue";
import * as echarts from "echarts";

function dataFormatter(arr) {
  return arr.map((item) => {
    return {
      name: item.classification,
      value: Number(item.classificationNum),
    };
  });
}
export default {
  name: "laonianren",
  props: {
    title: {
      type: String,
      default: "详情",
    },
  },
  components: {
    dataTitle,
  },
  data() {
    return {
      lnrLeft: [
        { value: 108, name: "60-69" },
        { value: 38, name: "70-79" },
        { value: 92, name: "80-89" },
        { value: 22, name: "90岁及以上" },
      ],
      lnrRight: [
        { value: 66, name: "与子女同住" },
        { value: 70, name: "空巢" },
        { value: 71, name: "独居" },
        { value: 53, name: "其他" },
      ],
    };
  },
  created() {
    const query = this.$route.query;
    this.getData();
  },
  activated() {},
  methods: {
    getData() {
      this.$http
        .get("/actual/base/peopleRoomOverview/oldPeopleAgePie")
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.lnrLeft = dataFormatter(res.data);
          this.initLeftCharts();
        })
        .catch(() => {});
      this.$http
        .get("/actual/base/peopleRoomOverview/oldPeopleResidePie")
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.lnrRight = dataFormatter(res.data);
          this.initRightCharts();
        })
        .catch(() => {});
    },
    initLeftCharts() {
      let div = document.getElementById("lnrLeftChart");
      this.myChart1 = echarts.init(div);
      var option = {
        color: ["#60bce1", "#04c78f", "#fdaa00", "#f23800"],
        legend: {
          orient: "vertical",
          top: 20,
          left: 10,
          icon: "rect",
          itemHeight: 14,
          itemWidth: 14,
          textStyle: {
            color: "#ffffff",
            rich: {
              name: {
                width: 90,
                fontSize: 12,
                color: "#dddee7",
              },
              value: {
                width: 30,
                align: "right",
                fontSize: 18,
              },
            },
          },
          data: this.lnrLeft,
          formatter: (name) => {
            if (this.lnrLeft.length) {
              const item = this.lnrLeft.filter((item) => item.name === name)[0];
              return `{name|${name}}{value| ${item.value}}`;
            }
          },
        },
        toolbox: {
          show: false,
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        series: [
          {
            name: "Nightingale Chart",
            type: "pie",
            radius: [30, 130],
            center: ["50%", "50%"],
            roseType: "area",
            itemStyle: {
              borderRadius: 0,
            },
            top: 20,
            left: -40,
            label: {
              position: "inside",
              formatter: "{d}%",
              textStyle: {
                color: "#ffffff",
                fontSize: 12,
              },
            },
            data: this.lnrLeft,
          },
        ],
      };
      this.myChart1.setOption(option);
      this.myChart1.on("click", (a, b) => {
        this.$router.push({
          path: "/dataBoard/renfang/resi-class",
          query: {
            org_id: "7b6f9a9f9f38d5f9fa7ce94a93d6eb28",
            type_id: "aged",
            type_name: "老年人",
            pageType: "normal",
          },
        });
      });
      window.addEventListener("resize", () => this.myChart1.resize());
    },
    initRightCharts() {
      let div = document.getElementById("lnrRightChart");
      this.myChart2 = echarts.init(div);
      var option = {
        color: ["#1b94fe", "#5a75ff", "#01c690", "#fdaa01"],
        legend: {
          orient: "vertical",
          top: 20,
          left: 10,
          icon: "rect",
          itemHeight: 14,
          itemWidth: 14,
          textStyle: {
            color: "#ffffff",
            rich: {
              name: {
                width: 80,
                fontSize: 12,
                color: "#dddee7",
              },
              value: {
                width: 30,
                align: "right",
                fontSize: 18,
              },
            },
          },
          data: this.lnrRight,
          formatter: (name) => {
            if (this.lnrRight.length) {
              const item = this.lnrRight.filter(
                (item) => item.name === name
              )[0];
              return `{name|${name}}{value| ${item.value}}`;
            }
          },
        },
        toolbox: {
          show: false,
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        series: [
          {
            name: "Nightingale Chart",
            type: "pie",
            radius: [30, 130],
            center: ["50%", "50%"],
            roseType: "area",
            itemStyle: {
              borderRadius: 0,
            },
            top: 20,
            left: -40,
            label: {
              position: "inside",
              formatter: "{d}%",
              textStyle: {
                color: "#ffffff",
                fontSize: 12,
              },
            },
            data: this.lnrRight,
          },
        ],
      };
      this.myChart2.on("click", (a, b) => {
        this.$router.push({
          path: "/dataBoard/renfang/resi-class",
          query: {
            org_id: "7b6f9a9f9f38d5f9fa7ce94a93d6eb28",
            type_id: "aged",
            type_name: "老年人",
            pageType: "normal",
          },
        });
      });
      this.myChart2.setOption(option);
      window.addEventListener("resize", () => this.myChart2.resize());
    },
  },
};
</script>

<style
  lang="scss"
  src="@/assets/scss/dataBoard/renfang/index.scss"
  scoped
></style>
<style lang="scss" scoped>
.laonianren {
  padding: 140px 240px;
}
</style>
