<!--
 * @Author: your name
 * @Date: 2021-10-16 11:36:20
 * @LastEditTime: 2021-10-16 12:01:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /data-show/src/views/BrandComparison/weiboUserPortrait/index.vue
-->
<template>
  <div class="wup-outter">
    <v-label-div
      title="微博用户画像"
      :showLine="false"
      :eStyle="{ 'border-style': 'none' }"
    >
      <v-tab-group
        :btns="this.brandArr"
        @change="handlerTab"
      ></v-tab-group>
    </v-label-div>
    <div class="wup-inner">
      <div class="d1">
        <div class="dd1">
          <v-echarts :opt="opt1"></v-echarts>
        </div>
        <div class="dd2">
          <v-label-ctx v-for="(item,index) in showSexData " 
            :key="index"
            :label="item.key"
            :cont="item.value"
            :percentage="((item.value / totalSexData) * 100).toFixed(2) + '%'"
            :color="colors[index]"
            :eStyle="{ height: '7.35rem' }"
          ></v-label-ctx>
        </div>
      </div>
      <div class="d1">
        <div class="dd1">
          <v-echarts :opt="opt2"></v-echarts>
        </div>
        <div class="dd2" style="margin-right: 16px">
          <v-label-ctx v-for="(item,index) in showAttestData " 
            :key="index"
            :label="item.key"
            :cont="item.value"
            :percentage="((item.value / totalAttestData) * 100).toFixed(2) + '%'"
            :color="colors[index]"
            :eStyle="{ height: '7.35rem' }"
          ></v-label-ctx>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getSexMergeWeiBoC } from "@/api/BrandComparison/BrandWeibo.js";
import createOpt1 from "./opt1";
import createOpt2 from "./opt2";
export default {
  name: "weiboUserPortrait",
  data() {
    return {
      form: {
        token: "",
        sBrand: "",
      },
      opt1: {},
      opt2: {},
      sourceData: [],
      showSexData: [],
      totalSexData: 0,
      totalAttestData: 0,
      showAttestData: [],
      brandArr: [],
      colors: [
        "#54BF93",
        "#3373CC",
        "#CC9D12",
        "#f15c80",
        "#e4d354",
        "#8085e8",
        "#8d4653",
        "#91e8e1",
        "#f7a35c",
        "#90ed7d",
        "#54BF93",
        "#3373CC",
        "#CC9D12",
        "#f15c80",
        "#e4d354",
        "#8085e8",
        "#8d4653",
        "#91e8e1",
        "#f7a35c",
        "#90ed7d",
      ],
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      this.form.token = this.getToken;
      let arr = this.getBComparison;
      let brands = [];
      arr.forEach((ele) => {
        brands.push(ele.name);
      });
      this.form.sBrand = brands.toString();
      this.getData();
    },
    getData() {
      return new Promise((resolve, reject) => {
        let obj = Object.assign({}, this.getCtime2, this.form);
        getSexMergeWeiBoC(obj)
          .then((res) => {
            let data = res.data;
            let brandList = [];
            let dataArr = []; //数组，传给createOpt方法
            data.forEach( e => {
                let o = {
                    name: e.Name,
                    sexData: this.toArr(e.Data.sex), //性别数据 type=arr
                    attestData: this.toArr(e.Data.attestation) //认证数据 type=arr
                }
                brandList.push(e.Name); //获取品牌名
                dataArr.push(o);
            })
            this.brandArr = brandList;
            this.sourceData = dataArr;
            this.doVal(0);
            resolve(data);
          })
          .catch(() => {
            reject(false);
          });
      });
    },
    // 将对象变成数组
    toArr(obj) {
      let arr = [];
      for (let key in obj) {
        let o = {
          key: key,
          value: obj[key],
        };
        arr.push(o);
      }
      return arr;
    },
    // 给页面变化赋值
    doVal(index) {
      this.showSexData = this.sourceData[index].sexData;
      this.showAttestData = this.sourceData[index].attestData;
      this.totalSexData = this.showSexData[0].value*1 + this.showSexData[1].value*1 + this.showSexData[2].value*1;
      this.totalAttestData = this.showAttestData[0].value*1+this.showAttestData[1].value*1+this.showAttestData[2].value*1;
      this.opt1 = createOpt1(this.showSexData, this.colors);
      this.opt2 = createOpt2(this.showAttestData, this.colors);
    },
    // 切换数据
    handlerTab(n) {
      this.doVal(n)
    },
  },
};
</script>

<style lang="less" scoped>
.wup-outter {
  width: 944px;
  height: 412px;
  .wup-inner {
    width: 100%;
    height: calc(100% - 48px);
    display: flex;
    justify-content: flex-start;
    .d1 {
      width: 50%;
      height: 100%;
      display: flex;
      justify-content: flex-start;
      overflow: hidden;
      .dd1 {
        width: 260px;
        height: 100%;
      }
      .dd2 {
        width: 240px;
        height: 100%;
      }
    }
  }
}
</style>
