<template>
  <div class="crowd-analysis-wrap">
    <el-radio-group class="crowd-radio" v-model="radio" size="small" @change="updateRadio">
      <el-radio-button label="0">全部用户</el-radio-button>
      <el-radio-button label="1">参与项目</el-radio-button>
      <el-radio-button label="2">通过项目</el-radio-button>
    </el-radio-group>
    <div v-show="!showData" class="empty">
      <p>"没有相关数据，请重新选择查询范围"</p>
    </div>
    <div class="chart" v-show="showData">
      <div class="item" id="education" ref="education"></div>
      <div class="item item-right" id="job" ref="job"></div>
    </div>
    <div class="chart" v-show="showData">
      <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" /> -->
            <!-- <el-progress class="sex-img" :color="item.color" :stroke-width=12 type="circle" :percentage="item.rate"></el-progress> -->
            <div v-if="index == 0" class="sex-img" id="ring0" ref="ring0"></div>
            <div v-if="index == 1" class="sex-img" id="ring1" ref="ring1"></div>
            <div v-if="index == 2" class="sex-img" id="ring2" ref="ring2"></div>
            <p class="sex-num">{{ item.num }}</p>
            <p class="sex-type">{{ item.type }}</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 * as chartData from "../../utils/learning/chartData";
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: 0,
      sexData: [
        {
          color: '#1890FF',
          type: "男性",
          num: 0,
          rate: 0,
        },
        {
          color: '#FB5B52',
          type: "女性",
          num: 0,
          rate: 0,
        },
        {
          color: '#5D7092',
          type: "未完善信息",
          num: 0,
          rate: 0,
        }
      ],
      projectId: "",
      crowdData: {},
      showData: true,
      echartsData: {
        chart1: {},
        chart2: {},
        chart3: {},
      },
    };
  },
  created() {
    vm = this;
    this.projectId = vm.getUrlSearch(window.location.href, "id");
  },
  mounted: function() {
    // commonUtil.resizeHeight();
    //父组件传值
    this.$on("search", () => {
      vm.radio = 0;
      this.search();
    });
    this.$on("init", () => {
      this.$nextTick(function() {
        if (vm.formInline.region.length != 0) {
          this.search();
        }
      });
    });
    this.$on("reset", () => {
      this.showData = false;
      this.crowdData = {};
    });
    if (this.activeName == "second") {
    }
  },
  methods: {
    search() {
      if (vm.formInline.region.length == 0) {
        const h = this.$createElement;
        vm.$message({
          message: h('p', { style: 'color: #FF3399' }, '请先选择地区后再进行查询 ')
        });
        return;
      }
      let checkAll = operationData.hasAll(vm.formInline.organization);
      let req = {
        projectId: vm.projectId,
        beginDate: operationData.setDuringTime(vm.formInline.during, "begin"),
        endDate: operationData.setDuringTime(vm.formInline.during, "end"),
        hospitalIdList: vm.formInline.organization,
        originalFlag: vm.formInline.checked == false ? 0 : 1,
        regionId: vm.formInline.region[vm.formInline.region.length - 1],
        timeFlag: vm.formInline.timeFlag
      };
      // console.log("2 req", req);
      openLoading(vm);
      vm.POST("stats/report/userAnalysis_pc/"+vm.radio, req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          // if(res.data.educationList.length == 0 && res.data.jobTitle.length == 0 ) {
          //   vm.$message("没有相关数据，请重新选择查询范围");
          //   this.showData = false;
          // } else {
          //   this.showData = true;
          // }
          this.crowdData = res.data;
          setTimeout(function() {
            vm.educationDivision();
            vm.jobDivision();
            vm.getSexData();
            vm.ageDivision();
          },20);
        } else {
          vm.$message(res.message);
        }
      });
    },
    updateResize() {
      this.$nextTick(function() {
        window.onresize = function() {
          vm.echartsData.chart1.resize();
          vm.echartsData.chart2.resize();
          vm.echartsData.chart3.resize();
        };
      });
    },
    updateRadio(value) {
      console.log(value,vm.radio);
      this.search();
    },
    //用户性别分布
    getSexData() {
      let sexList = this.crowdData.sexList;
      let sexData = [
        {
          color: '#1890FF',
          type: "男性",
          num: 0,
          rate: 0,
        },
        {
          color: '#FB5B52',
          type: "女性",
          num: 0,
          rate: 0,
        },
        {
          color: '#5D7092',
          type: "未完善信息",
          num: 0,
          rate: 0,
        }
      ];
      let sum = 0;

      if(sexList.length > 0) {
        for(let i = 0 ; i<sexList.length; i++) {
          sum += sexList[i].value;
          // sexData[i].num =  sexList[i].value;
        }
        sexData[0].num =  sexList[1].value;
        sexData[1].num =  sexList[2].value;
        sexData[2].num =  sexList[0].value;
        sexData[0].rate = Math.floor(sexList[1].value / sum *10000) / 100;
        sexData[1].rate = Math.floor(sexList[2].value / sum *10000) / 100;
        sexData[2].rate = Math.floor(sexList[0].value / sum *10000) / 100;
      }

      // console.log('this.$refs.ring0',this.$refs.ring0[0]);
      let myChartRing0 = echarts.init(this.$refs.ring0[0]);
      let option0 = chartData.ringOption(sexData[0].rate, sexData[0].color);
      myChartRing0.setOption(option0);

      let myChartRing1 = echarts.init(this.$refs.ring1[0]);
      let option1 = chartData.ringOption(sexData[1].rate, sexData[1].color);
      myChartRing1.setOption(option1);

      let myChartRing2 = echarts.init(this.$refs.ring2[0]);
      let option2 = chartData.ringOption(sexData[2].rate, sexData[2].color);
      myChartRing2.setOption(option2);

      vm.sexData = sexData;
    },
    educationDivision(data) {
      // console.log('this.$refs.education',this.$refs.education)
      let myChart = echarts.init(this.$refs.education);
      let educationList = this.crowdData.eduList;
      let legendData = [];
      let seriesData = [];
      let colorData = ["#7453FF", "#3BA0FF", "#5D7092", "#39AF9A", "#FFBC3D", "#FF9A4B", "#8A2BE2"];

      for (let i = 0; i < educationList.length; i++) {
        legendData[i] = educationList[i].name;
        seriesData[i] = educationList[i];
      }
      let option = operationData.getPicOption(
        "人员学历发布",
        legendData,
        colorData,
        seriesData
      );
      myChart.setOption(option);
      vm.echartsData.chart1 = myChart;
      vm.updateResize();
    },
    jobDivision(data) {
      let myChart2 = echarts.init(this.$refs.job);
      let titleList = this.crowdData.titleList;
      let legendData = [];
      let seriesData = [];
      let colorData = ["#3BA0FF", "#366BCB", "#39AF9A", "#FFBC3D", "#FFBC3D", "#67E0E3", "#8A2BE2"];
      for (let i = 0; i < titleList.length; i++) {
        legendData[i] = titleList[i].name;
        seriesData[i] = titleList[i];
      }
      let option = operationData.getPicOption(
        "人员职务分布",
        legendData,
        colorData,
        seriesData
      );
      myChart2.setOption(option);
      vm.echartsData.chart2 = myChart2;
      vm.updateResize();
    },
    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;
        if(ageList[i].value != 0) {
          xAxisData.push(ageList[i].name);
        }
      }
      for (let i = 0; i < ageList.length; i++) {
        // seriesData[i] = ageList[i].value;
        if(ageList[i].value != 0) {
          seriesData.push(ageList[i].value);
        }
      }
      let option = operationData.getBarOption(xAxisData, seriesData);

      myChart3.setOption(option);
      vm.echartsData.chart3 = myChart3;
      vm.updateResize();
    }
  }
};
</script>
<style lang="scss">
.crowd-analysis-wrap {
  // background: #fff;
  overflow: hidden;
  // padding: 10px;
  .header-title {
    padding: 10px 12px;
    font-size: 12px;
    color: #449284;
    border-bottom: 1px solid #efefef;
  }
  .crowd-radio {
    position: relative;
    left: 50%;
    margin-top: 25px;
    margin-left: -119px;
    .is-active {
      .el-radio-button__inner {
        // background: #fff;
        // color: #409EFF;
      }
    }
  }
  .empty {
    height: 300px;
    line-height: 300px;
    color: #FF3399;
    text-align: center;
  }
  .chart {
    position: relative;
    margin: 20px auto 0 auto;
    width: 1124px;
    overflow-x: hidden;
    .item {
      position: relative;
      float: left;
      margin-right: 20px;
      padding: 0;
      // width: 48%;
      width: 552px;
      height: 360px;
      background: #ffffff;
      // border: 1px solid #dedede;
      .title {
        position: relative;
        margin-top: 20px;
        margin-left: 20px;
        font-size: 18px;
        font-weight: bold;
      }
      .sex {
        position: relative;
        top: 42px;
        left: 0;
        .sex-item {
          float: left;
          width: 184px;
          .sex-img {
            margin: 0 auto;
            position: relative;
            // left: 16%;
            height: 125px;
            width: 125px;
          }
          .sex-num {
            margin-top: 18px;
            height:24px;
            line-height:24px;
            font-size:24px;
            font-weight:600;
            text-align: center;
          }
          .sex-type {
            margin-top: 8px;
            height:14px;
            font-size:14px;
            color: #999999;
            text-align: center;
          }
        }
      }
    }
    .item-right {
      margin-right: 0;
    }
  }
}
</style>