<template>
  <div class="crowd-analysis-wrap">
    <el-radio-group class="crowd-radio" v-model="radio" size="small" @change="updateRadio">
      <el-radio-button label="1">全部用户</el-radio-button>
      <el-radio-button label="2">参与项目</el-radio-button>
      <el-radio-button label="3">通过项目</el-radio-button>
    </el-radio-group>
    <div class="chart">
      <div class="item" id="education" ref="education"></div>
      <div class="item item-right" id="job" ref="job"></div>
      <div class="item">
        <div class="title">用户性别分布</div>
        <div class="sex">
          <div class="sex-item" v-for="(item , index) in sexData" :key="index">
            <img class="sex-img" v-bind:src="item.src" />
            <p class="sex-type">{{ item.type }}</p>
            <p></p>
            <p class="sex-num">{{ item.num }}</p>
          </div>
        </div>
      </div>
      <div class="item item-right" id="age" ref="age"></div>
    </div>
  </div>
</template>
<script>
import BreadCrumb from "../../components/breadcrumb.vue";
import { create } from "domain";
import { mapGetters } from "vuex";
import { openLoading, closeLoading } from "../../utils/utils";
import * as commonUtil from "../../utils/utils";
import * as operationData from "../../utils/operation";
import echarts from "echarts";
import { setTimeout } from "timers";
let vm = null;
export default {
  components: {
    BreadCrumb
  },
  props: {
    formInline: {
      type: Object
    },
    activeName: {
      type: String
    },
    organizationList: {
      type: Array
    }
  },
  data() {
    return {
      radio: 1,
      sexData: [],
      projectId: "",
      crowdData: {}
    };
  },
  created() {
    vm = this;
    this.projectId = vm.getUrlSearch(window.location.href, "id");
  },
  mounted: function() {
    commonUtil.resizeHeight();
    //父组件传值
    this.$on("search", () => {
      this.search();
    });
    this.$on("init", () => {
      this.$nextTick(function() {
        if (vm.formInline.region.length != 0) {
          this.search();
        }
      });
    });
    if (this.activeName == "second") {
      // this.updateResize();
    }
  },
  methods: {
    search() {
      if (vm.formInline.region.length == 0) {
        vm.$message("请选择地区");
        return;
      }
      let checkAll = operationData.hasAll(vm.formInline.organization);
      let req = {
        projectId: vm.projectId,
        cityOrHospitalId: operationData.getIds(vm.formInline,vm.organizationList,checkAll),
        kind: operationData.getSearchType(vm.formInline,checkAll),
        type: vm.radio,
        originalFlag: vm.formInline.checked == false ? 0 : 1
      };
      console.log("2 req", req);
      openLoading(vm);
      vm.reportGET("report/portal/getPeoplesDetails", req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          this.crowdData = res.data;
          this.educationDivision();
          this.jobDivision();
          this.getSexData();
          this.ageDivision();
        }
      });
    },
    updateResize() {
      this.$nextTick(function() {
        let chartEducation = echarts.init(document.getElementById("education"));
        let chartJob = echarts.init(document.getElementById("job"));
        let chartAge = echarts.init(document.getElementById("age"));
        window.onresize = function() {
          chartEducation.resize();
          chartJob.resize();
          chartAge.resize();
        };
      });
    },
    updateRadio(value) {
      console.log(value,vm.radio);
      this.search();
    },
    //用户性别分布
    getSexData() {
      let sexList = this.crowdData.sexList;
      this.sexData = [
        {
          src: require("../../assets/image/man.png"),
          type: "男性",
          num: sexList[0].value
        },
        {
          src: require("../../assets/image/weman.png"),
          type: "女性",
          num: sexList[1].value
        },
        {
          src: require("../../assets/image/question.png"),
          type: "未完善信息",
          num: sexList[2].value
        }
      ];
    },
    educationDivision(data) {
      let myChart = echarts.init(this.$refs.education);
      let educationList = this.crowdData.educationList;
      let legendData = [];
      let seriesData = [];
      let colorData = ["#FF9F7F", "#FFDB5C", "#E062AE", "#37A2DA", "#67E0E3"];
      for (let i = 0; i < educationList.length; i++) {
        legendData[i] = educationList[i].name;
      }
      seriesData = educationList;
      let option = operationData.getPicOption(
        "用户学历分布",
        legendData,
        colorData,
        seriesData
      );
      myChart.setOption(option);
      window.onresize = function() {
        myChart.resize();
      }
    },
    jobDivision(data) {
      let myChart2 = echarts.init(this.$refs.job);
      let jobTitle = this.crowdData.jobTitle;
      let legendData = [];
      let seriesData = [];
      let colorData = ["#FF9F7F", "#37A2DA", "#67E0E3", "#FFDB5C"];
      for (let i = 0; i < jobTitle.length; i++) {
        legendData[i] = jobTitle[i].name;
      }
      seriesData = jobTitle;
      let option = operationData.getPicOption(
        "用户职务分布",
        legendData,
        colorData,
        seriesData
      );
      myChart2.setOption(option);
      window.onresize = function() {
        myChart2.resize();
      }
    },
    ageDivision(data) {
      let myChart3 = echarts.init(this.$refs.age);
      let xAxisData = [];
      let seriesData = [];
      let ageList = this.crowdData.ageList;
      for (let i = 0; i < ageList.length; i++) {
        xAxisData[i] = ageList[i].name;
      }
      for (let i = 0; i < ageList.length; i++) {
        seriesData[i] = ageList[i].value;
      }
      let option = operationData.getBarOption(xAxisData, seriesData);

      myChart3.setOption(option);
      window.onresize = function() {
        myChart3.resize();
      }
    }
  }
};
</script>
<style lang="scss">
.crowd-analysis-wrap {
  background: #fff;
  padding: 10px;
  .header-title {
    padding: 10px 12px;
    font-size: 12px;
    color: #449284;
    border-bottom: 1px solid #efefef;
  }
  .crowd-radio {
    position: relative;
    left: 50%;
    margin-left: -119px;
    .is-active {
      .el-radio-button__inner {
        // background: #fff;
        // color: #409EFF;
      }
    }
  }
  .chart {
    position: relative;
    margin: 20px auto 0 auto;
    width: 100%;
    overflow: auto;
    .item {
      position: relative;
      float: left;
      margin-top: 20px;
      padding: 0;
      width: 48%;
      // height: 20vw;
      height: 350px;
      border: 1px solid #dedede;
      .title {
        position: relative;
        margin: 10px auto;
        text-align: center;
        font-weight: bold;
      }
      .sex {
        position: relative;
        top: 0;
        left: 0;
        .sex-item {
          float: left;
          height: 350px;
          width: 33.3%;
          .sex-img {
            margin: 0 auto;
            position: relative;
            left: 10%;
            width: 80%;
          }
          .sex-type {
            text-align: center;
          }
          .sex-num {
            margin-top: 40px;
            font-size: 30px;
            font-weight: bold;
            text-align: center;
          }
        }
      }
    }
    .item-right {
      float: right;
    }
  }
}
</style>