<template>
  <div style="width: 1200px;padding: 20px">
    <toptitle name="售后总览">
    </toptitle>
    <el-row>
      <el-col :span="6">
        <el-row style="margin-bottom: 40px">
          <span style="font-size: 16px;font-weight: normal;text-align: left">选择月份&nbsp</span>
          <el-select @change="init()" v-model="value" placeholder="请选择">
            <el-option
              v-for="item in dateList"
              :key="item.value"
              :label="item.key"
              :value="item.value">
            </el-option>
          </el-select>
        </el-row>
        <div class="text_body">
          <el-row style="padding-top: 20px">
            <el-col :span="24">总售后率 <el-tooltip class="item" effect="dark" content="售后数量/销售数量" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip> {{userData.totalAfterSalesRate}}</el-col>
          </el-row>
          <el-row style="margin-top: 20px">
            <el-col :span="12">
              <div>{{userData.salesAmount}}</div>
              <div>
                销售数量<el-tooltip class="item" effect="dark" content="售后商品的销售数量" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              </div>
            </el-col>
            <el-col :span="12">
              <div>{{userData.afterSalesAmount}}</div>
              <div>售后数量<el-tooltip class="item" effect="dark" content="申请售后的商品数量" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip></div>
            </el-col>
          </el-row>
          <el-row style="margin-top: 20px">
            <el-col :span="12">
              <div>{{userData.completedAfterSalesAmount}}</div>
              <div>售后已完成</div>
            </el-col>
            <el-col :span="12">
              <div>{{userData.uncompletedAfterSalesAmount}}</div>
              <div>售后未完成</div>
              <p style="font-size: 14px;color: #3399ff;cursor: pointer" @click="goPage3">前往查看</p>
            </el-col>
          </el-row>
        </div>
      </el-col>
      <el-col :span="18" v-if="chartData.length>0">
        <div style="width: 800px;height: 700px" id="main"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import toptitle from "@/components/common/topTitle";

  import { _getStorage } from "@/utils/utils";
  import * as echarts from "echarts";

  export default {
    components: {
      toptitle,
    },
    name: "salesOverview",
    data() {
      return {
        value: "",
        brandId: "",
        chartData: [],
        dateList: [],
        userData:{},
      };
    },
    mounted() {
      let { brandId } = _getStorage("info");
      this.brandId = brandId;
      this.getDate();
    },
    methods: {
      goPage3(){
        let page ='/aftersale/indexsale?status='
        this.$router.push(page+4);
      },
      getDate() {
        let params = { brandId: this.brandId };
        this.$axios.get("/order/afterSalesOverviewFilter", { params }).then((res) => {
          let data = res.data.data;
          if (res.data.code == 200) {
            this.dateList = data;
            this.value=data[0].value
            this.init();
          }
        }).catch((err) => {
          console.log(err);
        });
      },
      init() {
        let params = { brandId: this.brandId };
        if (this.value) {
          params.time = this.value;
        }
        this.$axios.get("/order/afterSalesOverview", { params }).then((res) => {
          let { code, data } = res.data;
          this.chartData = [];
          if (code == 200 && data) {
            this.userData=data.overview
            if (data.chartDate.length > 0) {
              data.chartDate.forEach(item => {
                this.chartData.push({ value: item.afterSalesAmount, name: item.afterSalesTypeText });
              });
              this.$nextTick(() => {
                this.getEcharts();
              });
            }
          }
        }).catch((err) => {
          console.log(err);
        });
      },
      getEcharts() {

        var chartDom = document.getElementById("main");
        var myChart = echarts.init(chartDom);
        var option;
        option = {
          legend: {
            top: "bottom",
          },
          tooltip: {
            trigger: "item",
            formatter: "{a} <br/>{b} : {c} ({d}%)",
          },
          series: [
            {
              name: '售后总览',
              type: "pie",
              radius: [50, 250],
              center: ["50%", "50%"],
              roseType: "radius",
              itemStyle: {
                borderRadius: 8,
              },
              data: this.chartData,
            },
          ],
        };
        myChart.on("click", function (param) { //添加点击事件
          console.log(param);
        });
        myChart.setOption(option);
      },
    },
  };
</script>

<style scoped lang="scss">
  .text_body {
    width: 273px;
    height: 350px;
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    font-weight: 500;
    border-radius: 8px;
    border: 1px solid #e4e4e4;
  }
</style>
