<template>
  <div v-loading="changeLoading">
    <div class="summary-view flexr">
      <div class="summary-card" :style="'max-width:' + size + 'px;'">
        <div class="summary-title">Total Sites</div>
        <div class="summary-max-value">{{summaryData.totalSites}}</div>
      </div>
      <div class="summary-card" :style="'max-width:' + size + 'px;'">
        <div class="summary-title">Total Stations</div>
        <div class="summary-max-value">{{summaryData.totalStations}}</div>
      </div>
      <div class="summary-card" :style="'max-width:' + size + 'px;'">
        <div class="summary-title">Total Connectors</div>
        <div class="summary-max-value">{{summaryData.totalConnectors}}</div>
      </div>
      <div class="summary-card" :style="'max-width:' + size + 'px;'">
        <div class="summary-title">Total No. Of Top Ups</div>
        <div class="summary-max-value">{{summaryData.totalNoOfTopUps}}</div>
      </div>
      <div class="summary-card" :style="'max-width:' + size + 'px;'">
        <div class="summary-title">Total Consumption (kWh)</div>
        <div class="summary-max-value">{{summaryData.totalConsumption}}</div>
      </div>
    </div>
    <div class="charts-card">
      <div class="flex">
        <div class="chart-title">Hourly Utilization(kWh)</div>
      </div>
      <LineChart
        index="hourly"
        :chartData="hourlyData"/>
    </div>
    <div class="summary-view flexr">
      <StatisticCard
        title="Total Transactions"
        :value="statisticsData.totalTransCard.totalTrans"
        v-if="statisticsData.totalTransCard"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Public Users</div>
          <div class="scard-item-value">{{statisticsData.totalTransCard.totalPublic}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Fleet Users</div>
          <div class="scard-item-value">{{statisticsData.totalTransCard.totalFleet}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Partners</div>
          <div class="scard-item-value">{{statisticsData.totalTransCard.totalPartner}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Users with Memberships</div>
          <div class="scard-item-value">{{statisticsData.totalTransCard.totalMember}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Total Users"
        :value="statisticsData.totalUsersCard.totalUsers"
        v-if="statisticsData.totalUsersCard"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Public Users</div>
          <div class="scard-item-value">{{statisticsData.totalUsersCard.totalPublic}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Fleet Users</div>
          <div class="scard-item-value">{{statisticsData.totalUsersCard.totalFleet}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Partners</div>
          <div class="scard-item-value">{{statisticsData.totalUsersCard.totalPartner}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Users with Memberships</div>
          <div class="scard-item-value">{{statisticsData.totalUsersCard.totalMember}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Total Revenue"
        :value="statisticsData.totalRevenue.totalRevenue"
        v-if="statisticsData.totalRevenue"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Charging Revenue</div>
          <div class="scard-item-value">{{statisticsData.totalRevenue.totalChargingRevenue}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Idle Fee Revenue</div>
          <div class="scard-item-value">{{statisticsData.totalRevenue.totalIdleFeeRevenue}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Reservation Revenue</div>
          <div class="scard-item-value">{{statisticsData.totalRevenue.totalReservationRevenue}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Discounts Given</div>
          <div class="scard-item-value">{{statisticsData.totalRevenue.totalDiscountsGiven}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Today’s Consumption"
        :value="statisticsData.totalTodayConsumption.totalTodayConsumption"
        v-if="statisticsData.totalTodayConsumption"
        unit="kWh"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Total Delivered</div>
          <div class="scard-item-value">{{statisticsData.totalTodayConsumption.totalTotalDelivered}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Highest Delivered</div>
          <div class="scard-item-value">{{statisticsData.totalTodayConsumption.totalHighestDelivered}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Average Delivered</div>
          <div class="scard-item-value">{{statisticsData.totalTodayConsumption.totalAverageDelivered}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Lowest Delivered</div>
          <div class="scard-item-value">{{statisticsData.totalTodayConsumption.totalLowestDelivered}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Today’s Incidents"
        :value="statisticsData.totalTodayIncidents.totalTodayIncidents"
        v-if="statisticsData.totalTodayIncidents"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Disconnected</div>
          <div class="scard-item-value">{{statisticsData.totalTodayIncidents.totalDisconnected}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Connection Established</div>
          <div class="scard-item-value">{{statisticsData.totalTodayIncidents.totalConnectionEstablished}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Boot Notification</div>
          <div class="scard-item-value">{{statisticsData.totalTodayIncidents.totalBootNotification}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Status Failure</div>
          <div class="scard-item-value">{{statisticsData.totalTodayIncidents.totalStatusFailure}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Today’s Revenue"
        :value="statisticsData.totalTodayRevenue.totalTodayRevenue"
        v-if="statisticsData.totalTodayRevenue"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Transactions Completed</div>
          <div class="scard-item-value">{{statisticsData.totalTodayRevenue.totalTransCompleted}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Transactions Ongoing</div>
          <div class="scard-item-value">{{statisticsData.totalTodayRevenue.totalTransOngoing}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Reservations</div>
          <div class="scard-item-value">{{statisticsData.totalTodayRevenue.totalReservations}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Idle Charges</div>
          <div class="scard-item-value">{{statisticsData.totalTodayRevenue.totalIdleCharges}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Credit Expenditure"
        :value="statisticsData.totalCreditExpenditure.totalCreditExpenditure"
        v-if="statisticsData.totalCreditExpenditure"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Wallet Expenditure</div>
          <div class="scard-item-value">{{statisticsData.totalCreditExpenditure.totalWalletExpenditure}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Fleet Credit Expenditure</div>
          <div class="scard-item-value">{{statisticsData.totalCreditExpenditure.totalFleetCreditExpenditure}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Monthly Fleet Credit Limit</div>
          <div class="scard-item-value">{{statisticsData.totalCreditExpenditure.totalMonthlyFleetCredit}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Discounts Given</div>
          <div class="scard-item-value">{{statisticsData.totalCreditExpenditure.totalDiscountsGiven}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Connectors In Use"
        :value="statisticsData.totalConnectorsInUse.totalConnectorsInUse"
        v-if="statisticsData.totalConnectorsInUse"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Available</div>
          <div class="scard-item-value">{{statisticsData.totalConnectorsInUse.totalAvailable}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Faulted</div>
          <div class="scard-item-value">{{statisticsData.totalConnectorsInUse.totalFaulted}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Reserved</div>
          <div class="scard-item-value">{{statisticsData.totalConnectorsInUse.totalReserved}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Finishing</div>
          <div class="scard-item-value">{{statisticsData.totalConnectorsInUse.totalFinishing}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Unavailable</div>
          <div class="scard-item-value">{{statisticsData.totalConnectorsInUse.totalUnavailable}}</div>
        </div>
      </StatisticCard>
      <StatisticCard
        title="Total Wallet Credit"
        :value="statisticsData.totalWalletCredit.totalWalletCredit"
        v-if="statisticsData.totalWalletCredit"
        :size="size2">
        <div class="scard-item-row">
          <div class="scard-item-title">Credits Purchase</div>
          <div class="scard-item-value">{{statisticsData.totalWalletCredit.totalCreditsPurchase}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Manual Top Up</div>
          <div class="scard-item-value">{{statisticsData.totalWalletCredit.totalManualTopUp}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Refunds</div>
          <div class="scard-item-value">{{statisticsData.totalWalletCredit.totalRefunds}}</div>
        </div>
        <div class="scard-item-row">
          <div class="scard-item-title">Credits Unused</div>
          <div class="scard-item-value">{{statisticsData.totalWalletCredit.totalCreditsUnused}}</div>
        </div>
      </StatisticCard>
    </div>
    <div class="charts-card">
      <div class="flexcr">
        <div class="chart-title flex1">Performance Summary</div>
        <div class="radio-group">
          <el-radio-group
            v-model="dateTabIndex"
            size="small"
            @change="changeBarDateTab">
            <el-radio-button
              v-for="(item,index) in dateRangeTab"
              :key="index"
              :label="item.value">
              {{item.name}}
            </el-radio-button>
          </el-radio-group>
        </div>
      </div>
      <el-row
        class="barGroup"
        v-loading="barLoading">
        <el-col
          class="barView"
          :sm="12"
          :md="8"
          v-if="barChartData.utilization">
          <BarChart 
            index="0"
            :chartData="barChartData.utilization"
            unit="%"
            title="Utilization Rate so far"/>
        </el-col>
        <el-col
          class="barView"
          :sm="12"
          :md="8"
          v-if="barChartData.consumption">
          <BarChart
            index="1"
            :chartData="barChartData.consumption"
            unit=" kWh"
            title="Consumption so far"/>
        </el-col>
        <el-col
          class="barView"
          :sm="12"
          :md="8"
          v-if="barChartData.revenue">
          <BarChart
            index="2"
            :chartData="barChartData.revenue"
            :unit="barChartData.currency + ' ' + barChartData.currencySymbol"
            :leftUnit="true"
            title="Revenue so far"/>
        </el-col>
      </el-row>
    </div>
    <div class="charts-card">
      <div class="flexcr">
        <div class="chart-title flex1">Revenue Tracking</div>
        <el-radio-group
          v-model="dateTrackIndex"
          size="small"
          @change="changeTrackDateTab">
          <el-radio-button
            v-for="(item,index) in dateRangeTab"
            :key="index"
            :label="item.value">
            {{item.name}}
          </el-radio-button>
        </el-radio-group>
      </div>
      <div style="padding: 10px 0;" v-loading="trackLoading">
        <LineChart
          index="tracking"
          :chartData="revenueTracking"/>
      </div>
    </div>
  </div>
</template>

<script>
import api from '../../../api/dashboard'
import BarChart from '../chart/BarChart.vue'
import LineChart from '../chart/LineChart.vue'
import CircleChart from '../chart/CircleChart.vue'
import StatisticCard from '../fragment/StatisticCard.vue'
export default {
  name: "SummaryV2",
  props: {
    filters: {
      type: Object,
      default: () => {}
    },
    size: {
      type: String | Number,
      default: ""
    },
    size2: {
      type: String | Number,
      default: ""
    }
  },
  components: {
    BarChart, LineChart, CircleChart, StatisticCard
  },
  data() {
    return {
      changeLoading: false,
      barLoading: true,
      trackLoading: true,
      hourlyData: {
        xdata: [],
        ydata: []
      },
      summaryData: {
        totalSites: 0,
        totalStations: 0,
        totalConnectors: 0,
        totalNoOfTopUps: 0,
        totalConsumption: 0
      },
      statisticsData: {
        /*totalRevenue: {},
        totalUsersCard: {},
        totalTransCard: {},
        totalTodayRevenue: {},
        totalWalletCredit: {},
        totalTodayIncidents: {},
        totalConnectorsInUse: {},
        totalTodayConsumption: {},
        totalCreditExpenditure: {}*/
      },
      dateTabIndex: 0,
      dateRangeTab: [],
      barChartData: [],
      dateTrackIndex: 0,
      revenueTracking: {
        xdata: [],
        ydata: []
      }
    };
  },
  watch: {
    filters: {
      deep: true,
      handler(val) {
        this.getData()
      }
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      this.changeLoading = true
      this.getSummaryData();
      this.getStatisticsData();
      this.getRangeTabList();
      this.getLineChart();
    },
    getSummaryData() {
      api.getSummaryCard(this.filters).then(res => {
        if (res.data) {
          this.summaryData = res.data
        }
      }).catch(err => {
        this.changeLoading = false
        this.$message({
          type: 'error',
          message: err
        })
        this.summaryData = {}
      })
    },
    getLineChart() {
      api.getHourlyUtilization(this.filters).then(res => {
        if (res.data.xdata && res.data.ydata) {
          this.hourlyData = res.data
        } else {
          this.hourlyData = undefined
        }
      }).catch(err => {
        this.changeLoading = false
        this.$message({
          type: 'error',
          message: err
        })
        this.hourlyData = undefined
      }).finally(() => {
        
      })
    },
    getStatisticsData() {
      api.getStatisticsCard(this.filters).then(res => {
        if (res.data) {
          this.statisticsData = res.data
        } else {
          this.statisticsData = {};
        }
      }).catch(err => {
        this.changeLoading = false
        this.$message({
          type: 'error',
          message: err
        })
        this.statisticsData = {}
      })
    },
    getRangeTabList() {
      if (this.dateRangeTab.length == 0) {
        api.getRangeTab().then(res => {
          if (res.data) {
            this.dateRangeTab = res.data
            if (res.data.length > 0) {
              this.dateTabIndex = res.data[0].value
              this.dateTrackIndex = res.data[0].value
              this.getBarChartData()
              this.getRevenueTrackingData()
            }
          } else {
            this.dateRangeTab = [];
          }
        }).catch(err => {
          this.changeLoading = false
          this.$message({
            type: 'error',
            message: err
          })
          this.dateRangeTab = [];
        })
      } else {
        this.getBarChartData()
        this.getRevenueTrackingData()
      }
    },
    getBarChartData() {
      api.getBarChart({
        tabType: this.dateTabIndex,
        ...this.filters
      }).then(res => {
        if (res.data) {
          this.barChartData = res.data
        } else {
          this.barChartData = []
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.barChartData = []
      }).finally(() => {
        this.barLoading = false
      })
    },
    getRevenueTrackingData() {
      api.getRevenueTracking({
        tabType: this.dateTrackIndex,
        ...this.filters
      }).then(res => {
        if (res.data) {
          this.revenueTracking = res.data
        } else {
          this.revenueTracking = {}
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.revenueTracking = {}
      }).finally(() => {
        this.trackLoading = false;
        this.changeLoading = false
      })
    },
    changeBarDateTab() {
      this.barLoading = true;
      setTimeout(() => {
        this.getBarChartData()
      }, 300);
    },
    changeTrackDateTab() {
      this.trackLoading = true;
      setTimeout(() => {
        this.getRevenueTrackingData()
      }, 300);
    }
  }
}
</script>

<style scoped lang="scss">
  @import '../../../styles/element-ui.scss';
 .summary-view {
    margin: 5px -5px 0;
  }
  .summary-view > div {
    margin: 5px;
  }
  .summary-view > div.summary-view {
    margin: 0;
  }
  .summary-view > .summary-card {
    margin: 5px;
    min-width: 240px;
  }
  .summary-card {
    flex: 1;
    padding: 10px 20px;
    border-radius: 6px;
    background-color: white;
  }
  .summary-title {
    color: #333333;
    font-size: 16px;
    font-weight: bold;
    text-align: center;
  }
  .summary-max-value {
    color: $--color-accent;
    cursor: pointer;
    padding: 10px 0;
    font-size: 42px;
    line-height: 63px;
    text-align: center;
    transition: all .3s;
  }
  .summary-max-value:hover {
    transform: scale(1.2);
  }
  .charts-card {
    display: flex;
    overflow: hidden;
    padding: 10px 20px;
    margin-top: 5px;
    margin-bottom: 10px;
    border-radius: 6px;
    flex-direction: column;
    background-color: white;
  }
  .chart-title {
    color: #333;
    font-size: 22px;
    padding-top: 5px;
    padding-bottom: 15px;
    &.hourly {
      color: #555;
      font-size: 20px;
      padding-left: 30px;
      transform: scaleX(1.3);
      -webkit-font-smoothing: antialiased;
    }
  }
  .radio-group {
    padding-top: 5px;
    text-align: center;
  }
  .barGroup {
    margin-top: 15px;
    padding-top: 10px;
    border-top: 1px solid #F0F5FC;
  }
  .barView {
    padding-top: 10px;
    padding-bottom: 10px;
  }
  @media screen and (min-width: 768px) {
    .barView + .barView {
      border-left: 10px solid #F0F5FC;
    }
    .barView + .barView + .barView {
      border-left: none;
    }
  }
  @media screen and (min-width: 992px) {
    .barView + .barView + .barView {
      border-left: 10px solid #F0F5FC;
    }
  }
  @media screen and (max-width: 768px) {
    .summary-card {
      min-width: 40vw;
      max-width: calc(50% - 10px);
    }
  }
</style>