<template>
  <el-row :gutter="12" class="panel-group" v-loading="loading">
    <el-col :xs="24" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel panel-device">
        <div class="card-panel-description">
          <div class="card-panel-text">
            设备总数
          </div>
          <count-to :start-val="0" :end-val="briefData.totalDeviceCount" :duration="2600" class="card-panel-num" />
          <div class="card-panel-compare">
            在线
            <span class="up">
              {{briefData.totalOnlineCount}} 台
            </span>
          </div>
        </div>
        <div class="card-panel-icon-wrapper">
          <small-line-chart class-name="panel-chart" :data="totalDeviceChartData" name="设备数量" color="#165DFF" />
        </div>
      </div>
    </el-col>
    <el-col :xs="24" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel panel-model">
        <div class="card-panel-description">
          <div class="card-panel-text">
            订单总数
          </div>
          <count-to :start-val="0" :end-val="briefData.rechargeCount" :duration="2600" class="card-panel-num" />
          <div class="card-panel-compare">
            今日
            <span class="up">
              {{briefData.todayRechargeCount}}
              <svg-icon icon-class="up" />
            </span>
          </div>
        </div>
        <div class="card-panel-icon-wrapper">
          <small-line-chart class-name="panel-chart" :data="rechargeCountChartData" name="日订单数量" color="#2CAB40"/>
        </div>
      </div>
    </el-col>
    <el-col :xs="24" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel panel-user">
        <div class="card-panel-description">
          <div class="card-panel-text">
            注册用户
          </div>
          <count-to :start-val="0" :end-val="briefData.userCount + briefData.tenantCount" :duration="3600" class="card-panel-num" />
          <div class="card-panel-compare">
            今日
            <span :class="compareUser >= 0 ? 'up' : 'down'">
              {{compareUser}}
              <svg-icon :icon-class="compareUser >= 0 ? 'up' : 'down'" />
            </span>
          </div>
        </div>
        <div class="card-panel-icon-wrapper">
          <user-round-pie-chart class-name="panel-chart" :data="peopleChartData" width="100%" height="100%"/>
        </div>
      </div>
    </el-col>
    <el-col :xs="24" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel panel-device" >
        <div class="card-panel-description">
          <div class="card-panel-text">
            用户设备数
          </div>
          <count-to :start-val="0" :end-val="briefData.deviceCount" :duration="3000" class="card-panel-num" :decimals="0"/>
          <div class="card-panel-compare">
            在线
            <span class="up">
              {{briefData.onlineCount | defaultValue}} 台
            </span>
          </div>
        </div>
        <div class="card-panel-icon-wrapper">
          <small-line-chart class-name="panel-chart" :data="deviceChartData" name="设备数量" color="#165DFF"/>
        </div>
      </div>
    </el-col>
    <el-col :xs="24" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel panel-model">
        <div class="card-panel-description">
          <div class="card-panel-text">
            订单金额
          </div>
          <count-to :start-val="0" :end-val="briefData.rechargeAmount" :duration="2600" class="card-panel-num" :decimals="2"/>
          <div class="card-panel-compare">
            今日
            <span class="up">
              {{briefData.todayRechargeAmount | money}}
              <svg-icon icon-class="up" />
            </span>
          </div>
        </div>
        <div class="card-panel-icon-wrapper">
          <small-line-chart class-name="panel-chart" :data="rechargeAmountChartData" name="日订单金额（元）" color="#2CAB40"/>
        </div>
      </div>
    </el-col>
    <el-col :xs="24" :sm="12" :lg="8" class="card-panel-col">
      <div class="card-panel panel-user">
        <div class="card-panel-description">
          <div class="card-panel-text">
            商户余额
          </div>
          <count-to :start-val="0" :end-val="briefData.totalUserBalance" :duration="3200" class="card-panel-num" :decimals="2"/>
          <div class="card-panel-compare">
            较昨日
            <span :class="compareBalance >= 0 ? 'up' : 'down'">
              {{compareBalance | money}}
              <svg-icon :icon-class="compareBalance >= 0 ? 'up' : 'down'" />
            </span>
          </div>
        </div>
        <div class="card-panel-icon-wrapper">
          <small-line-chart class-name="panel-chart" :data="userBalanceChartData" name="商户余额" color="#8D4EDA"/>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from 'vue-count-to'
import {brief} from "@/api/system/dashboard";
import UserRoundPieChart from "@/views/dashboard/component/UserRoundPieChart.vue";
import {parseTime} from "@/utils/ruoyi";
import Device from "@/views/system/device/index.vue";
import DeviceLineChart from "@/views/dashboard/component/DeviceLineChart.vue";
import OnlineLineChart from "@/views/dashboard/component/OnlineLineChart.vue";
import ModelBarChart from "@/views/dashboard/component/ModelBarChart.vue";
import SmallLineChart from '@/views/dashboard/component/SmallLineChart.vue'

export default {
  components: {
    SmallLineChart,
    ModelBarChart,
    DeviceLineChart,
    Device,
    UserRoundPieChart,
    CountTo,
    OnlineLineChart
  },
  data() {
    return {
      loading: false,
      // 舆情分析数据
      briefData: {
        deviceCount: 0,
        onlineCount: 0,
        modelCount: 0,
        userCount: 0,
        tenantCount: 0,
        agentCount: 0,
        history: [],
      },
      queryParams: {},
      today:  parseTime(new Date(), "{y}-{m}-{d}")
    }
  },
  computed: {
    // 昨日数据（最后一次历史数据）
    yesterdayData() {
      if (this.briefData == null || this.briefData.history == null) {
        return null;
      }
      return this.briefData.history[this.briefData.history.length - 1];
    },
    // 订单数据
    rechargeCountChartData() {
      return this.parseToChart('todayRechargeCount', 'todayRechargeCount');
    },
    // 订单金额数据
    rechargeAmountChartData() {
      return this.parseToChart('todayRechargeAmount', 'todayRechargeAmount', 2);
    },
    // 月费数据
    monthAmountChartData() {
      return this.parseToChart('totalMonthAmount', 'totalMonth', 2);
    },
    // 用户余额数据
    userBalanceChartData() {
      return this.parseToChart('totalUserBalance', 'userBalance', 2);
    },
    // 用户设备数据
    deviceChartData() {
      return this.parseToChart('deviceCount', 'totalDevice');
    },
    // 总设备数据
    totalDeviceChartData() {
      return this.parseToChart('totalDeviceCount', 'deviceCount');
    },
    // 用户图表数据
    peopleChartData() {
      if (this.briefData == null) {
        return []
      }
      return [
        {value: this.briefData.userCount, name: "商户"},
        {value: this.briefData.tenantCount, name: "用户"},
        {value: this.briefData.agentCount, name: "代理商"},
      ]
    },
    // 用户差值
    compareUser() {
      if (this.briefData == null) {
        return 0;
      }
      if (this.yesterdayData == null) {
        return this.briefData.userCount + this.briefData.tenantCount + this.briefData.agentCount;
      }
      return this.briefData.userCount + this.briefData.tenantCount  + this.briefData.agentCount - this.yesterdayData.totalUser;
    },
    // 余额差值
    compareBalance() {
      if (this.briefData == null) {
        return 0;
      }
      if (this.yesterdayData == null) {
        return this.briefData.totalUserBalance;
      }
      return this.briefData.totalUserBalance - this.yesterdayData.userBalance;
    },
  },
  created() {
    this.getBriefData();
  },
  methods: {
    // 转换为图表
    parseToChart(currentProp, historyProp, fixed = 0) {
      if (this.briefData == null) {
        return []
      }
      let list = this.briefData?.history?.map(item => {
        return {
          x: item.createDate,
          value: item[historyProp]?.toFixed(fixed)
        }
      });
      list.push({
        x: this.today,
        value: this.briefData[currentProp]?.toFixed(fixed)
      })
      return list;
    },
    // 获取舆情分析数据
    getBriefData() {
      this.loading = true;
      brief(this.queryParams).then(response => {
        this.briefData = response.data;
      }).finally(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    display: flex;
    justify-content: space-between;
    height: 134px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    .card-panel-icon-wrapper {
      position: relative;
      height: 100%;
      flex: 1;
      padding: 0;

      .panel-chart {
        float: right;
      }
    }

    .card-panel-description {
      font-weight: 700;
      width: fit-content;
      min-width: 10em;
      padding: 20px 0 16px 20px;

      .card-panel-text {
        line-height: 24px;
        color: #1D252F;
        font-size: 16px;
        margin-bottom: 24px;
      }

      .card-panel-num {
        font-size: 24px;
        line-height: 26px;
        color: #1D2129;
      }

      .card-panel-compare {
        position: absolute;
        bottom: 16px;
        font-size: 12px;
        line-height: 20px;
        color: #4E5969;
        width: fit-content;

        .up {
          color: #F53F3F;
          font-size: 14px;
        }
        .down {
          color: #00B42A;
          font-size: 14px;
        }
      }
    }
  }
  .panel-user {
    background: linear-gradient(180deg, #F6F7FF -3%, #ECECFF 100%);
  }
  .panel-online {
    background: linear-gradient(180deg, #F2F9FE -3%, #E6F4FE 100%);
  }
  .panel-model {
    background: linear-gradient(180deg, #F5FEF2 -3%, #E6FEEE 100%);
  }
  .panel-device {
    background: linear-gradient(180deg, #F2F9FE -3%, #E6F4FE 100%);
  }
  .panel-yellow {
    background: linear-gradient(180deg, #fefaf2 -3%, #fef4e6 100%);
  }
  .panel-red {
    background: linear-gradient(180deg, #fef2f7 -3%, #ffeef1 100%);
  }

}

@media (max-width:400px) {
  .card-panel-icon-wrapper {
    display: none;
  }
}
</style>
