<template>
  <div>
    <div class="eachartTop">
      <SearchHeader
        title="时间筛选: "
        :isShowQuert="true"
        :dates="mdate"
        @changeValue="changeValue"
      />
    </div>
    <statistic :col="4" class="statistic" :cols="12">
      <statistic-item>
        <span>
          现金总收入: (元)
        </span>
        <span>
          <count-to :endVal="totalAmount" :duration="300" :decimals="2" class="blod" />
        </span>
      </statistic-item>
      <statistic-item>
        <span>
          管乐迷现金收入: (元)
        </span>
        <span>
          <count-to :endVal="gymTotal" :duration="300" :decimals="2" :class="'des'" />
        </span>
      </statistic-item>
      <statistic-item>
        <span>
          酷乐秀现金收入: (元)
        </span>
        <span>
          <count-to :endVal="colexiuTotal" :duration="300" :decimals="2" :class="'des'" />
        </span>
      </statistic-item>
      <statistic-item>
        <span>
          管乐团现金收入: (元)
        </span>
        <span>
          <count-to :endVal="gytTotal" :duration="300" :decimals="2" :class="'des'" />
        </span>
      </statistic-item>
      
    </statistic>
    <div></div>

    <div class="operateEcharts">
      <div class="eachartTitle">
        <div>
          管乐迷现金收入:
          <count-to :endVal="gymTotal" :duration="300" :decimals="2" class="des" /> 元
        </div>
        <el-select
          clearable
          filterable
          placeholder="请选择分部"
          v-model="organId"
          @change="
            () => {
              this.getData();
            }
          "
        >
          <el-option
            v-for="(item, index) in selects.branchs"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="gym-echarts"></div>
      <div class="eachartTitle">
        <div>
          酷乐秀现金收入:
          <count-to :endVal="colexiuTotal" :duration="300" :decimals="2" class="des" /> 元
        </div>
      </div>
      <div class="colexiu-echarts"></div>
      <div class="eachartTitle">
        <div>
          管乐团现金收入:
          <count-to :endVal="gytTotal" :duration="300" :decimals="2" class="des" /> 元
        </div>
      </div>
      <div class="gyt-echarts"></div>
      
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import CountTo from "vue-count-to";
import { multiDataSourceHomeStatsSummerIncome } from "../../api";
import SearchHeader from "./searchDayHeader";
import { getTimes } from "@/utils";

export default {
  components: {
    CountTo,
    SearchHeader
  },
  data() {
    return {
      organId: "",
      mdate: [],
      gymECharts: null,
      gymData: [],
      gymTotal: 0,
      gytEcharts: null,
      gytData: [],
      gytTotal: 0,
      colexiuEcharts: null,
      colexiuData: [],
      colexiuTotal: 0
    };
  },
  mounted() {
    this.mdate = this.getInitDate();
    this.getData();
    window.addEventListener("resize", this.resize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resize);
  },
  methods: {
    getInitDate() {
      const end = this.$helpers.dayjs(new Date()).format("YYYY-MM-DD");
      const start = this.$helpers
        .dayjs(new Date())
        // .set("month", 1)
        .subtract(1, "month")
        .format("YYYY-MM-DD");
      return [start, end];
    },
    getData() {
      multiDataSourceHomeStatsSummerIncome({
        ...getTimes(this.mdate, ["startTime", "endTime"]),
        organIds: this.organId
      }).then(res => {
        if (Array.isArray(res.data)) {
          const gym = res.data.find(item => item.platform == "gym");
          if (gym) {
            this.gymTotal = gym.totalAmount;
            this.gymData = gym.incomeByDates;
          }
          const gyt = res.data.find(item => item.platform == "gyt");
          if (gyt) {
            this.gytTotal = gyt.totalAmount;
            this.gytData = gyt.incomeByDates;
          }
          const clx = res.data.find(item => item.platform == "cls");
          if (clx) {
            this.colexiuTotal = clx.totalAmount;
            this.colexiuData = clx.incomeByDates;
          }
          this.init();
        }
      });
    },
    changeValue(date) {
      // 请求更改数据
      this.mdate = date;
      this.getData();
    },
    resize() {
      this.gymECharts.resize();
      this.gytEcharts.resize();
      this.colexiuEcharts.resize();
    },
    init() {
      this.createGym();
      this.createGyt();
      this.createColexiu();
    },
    createGym() {
      if (this.gymECharts) {
        this.gymECharts.dispose();
      }
      this.gymECharts = echarts.init(document.querySelector(".gym-echarts"));
      const option = {
        title: {
          top: "0%",
          text: "",
          textStyle: {
            color: "#333",
            fontSize: 14
          }
        },
        tooltip: {
          trigger: "axis"
        },
        grid: {
          left: 5,
          top: 30,
          right: 5,
          bottom: 5,
          containLabel: true
        },
        xAxis: {
          type: "category",
          data: this.gymData.map(item => item.date),
          axisLabel: {
            color: "#333",
            fontSize: 10
          }
        },
        yAxis: {
          type: "value",
          axisLabel: { formatter: "{value}元" }
        },
        series: [
          {
            data: this.gymData.map(item => item.totalAmount),
            type: "bar",
            color: "#00a79d",
            barWidth: 40
          }
        ]
      };
      option && this.gymECharts.setOption(option);
    },
    createGyt() {
      if (this.gytEcharts) {
        this.gytEcharts.dispose();
      }
      this.gytEcharts = echarts.init(document.querySelector(".gyt-echarts"));
      const option = {
        grid: {
          left: 5,
          top: 30,
          right: 5,
          bottom: 5,
          containLabel: true
        },
        tooltip: {
          trigger: "axis"
        },
        xAxis: {
          type: "category",
          data: this.gytData.map(item => item.date),
          axisLabel: {
            color: "#333",
            fontSize: 10
          }
        },
        yAxis: {
          type: "value",
          axisLabel: { formatter: "{value}元" }
        },
        series: [
          {
            data: this.gytData.map(item => item.totalAmount),
            type: "bar",
            color: "#00a79d",
            barWidth: 40
          }
        ]
      };
      option && this.gytEcharts.setOption(option);
    },
    createColexiu() {
      if (this.colexiuEcharts) {
        this.colexiuEcharts.dispose();
      }
      this.colexiuEcharts = echarts.init(
        document.querySelector(".colexiu-echarts")
      );
      const option = {
        grid: {
          left: 5,
          top: 30,
          right: 5,
          bottom: 5,
          containLabel: true
        },
        tooltip: {
          trigger: "axis"
        },
        xAxis: {
          type: "category",
          data: this.colexiuData.map(item => item.date),
          axisLabel: {
            color: "#333",
            fontSize: 10
          }
        },
        yAxis: {
          type: "value",
          axisLabel: { formatter: "{value}元" }
        },
        series: [
          {
            data: this.colexiuData.map(item => item.totalAmount),
            type: "bar",
            color: "#00a79d",
            barWidth: 40
          }
        ]
      };
      option && this.colexiuEcharts.setOption(option);
    }
  },
  computed: {
    totalAmount() {
      return this.gymTotal + this.gytTotal + this.colexiuTotal;
    }
  }
};
</script>
<style lang="less" scoped>
.eachartTop {
  margin-bottom: 20px;
  // display: flex;
  // align-items: center;
  // justify-content: space-between;
  // flex-wrap: wrap;
  // ::v-deep .box {
  //   margin-right: 5px;
  // }
  // ::v-deep .shape {
  //   display: none;
  // }
}
.eachartTitle {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.operateEcharts {
  // display: flex;
  .gym-echarts,
  .gyt-echarts,
  .colexiu-echarts {
    width: 100%;
    height: 350px;
    margin-bottom: 24px;
  }
}

.statistic .statistic-content > span {
  &:first-child {
    font-size: 14px !important;
  }
  font-size: 18px !important;
}
.blod {
  font-weight: bold !important;
  color: var(--color-primary) !important;
  font-size: 22px !important;
}
.des {
  font-size: 18px !important;
  color: var(--color-primary);
}
</style>
