<template>
  <div class="incidents-container" v-loading="changeLoading">
    <div class="card-group">
      <div class="card-item-view" :style="'max-width:' + size + 'px;'" v-if="incidentData.bootNotification">
        <div class="card-title">Boot Notification</div>
        <div class="card-value">{{ incidentData.bootNotification.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'" v-if="incidentData.connectionEstablished">
        <div class="card-title">Connection Established</div>
        <div class="card-value">{{ incidentData.connectionEstablished.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'" v-if="incidentData.disconnected">
        <div class="card-title">Disconnected</div>
        <div class="card-value error">{{ incidentData.disconnected.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'" v-if="incidentData.statusFailure">
        <div class="card-title">Status Failure</div>
        <div class="card-value error">{{ incidentData.statusFailure.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'" v-if="incidentData.idlingFeeCharge">
        <div class="card-title">Idling Fee Charge</div>
        <div class="card-value">{{ incidentData.idlingFeeCharge.total || 0 }}</div>
      </div>
    </div>
    <div class="card-group card-list-group">
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="title-underline">
          <span>STATION ID</span>
          <span>COUNT</span>
        </div>
        <div v-if="incidentData.bootNotification.items">
          <div
            class="details-row"
            v-for="(item, index) in incidentData.bootNotification.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.count}}</b>
          </div>
        </div>
        <el-skeleton
          v-else
          :rows="10"
          animated
          style="width: 100%; flex: 1"/>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="title-underline">
          <span>STATION ID</span>
          <span>COUNT</span>
        </div>
        <div v-if="incidentData.connectionEstablished.items">
          <div
            class="details-row"
            v-for="(item, index) in incidentData.connectionEstablished.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.count}}</b>
          </div>
        </div>
        <el-skeleton
          v-else
          :rows="10"
          animated
          style="width: 100%; flex: 1"/>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="title-underline">
          <span>STATION ID</span>
          <span>COUNT</span>
        </div>
        <div v-if="incidentData.disconnected.items">
          <div
            class="details-row"
            v-for="(item, index) in incidentData.disconnected.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.count}}</b>
          </div>
        </div>
        <el-skeleton
          v-else
          :rows="10"
          animated
          style="width: 100%; flex: 1"/>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="title-underline">
          <span>STATION ID</span>
          <span>COUNT</span>
        </div>
        <div v-if="incidentData.statusFailure.items">
          <div
            class="details-row"
            v-for="(item, index) in incidentData.statusFailure.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.count}}</b>
          </div>
        </div>
        <el-skeleton
          v-else
          :rows="10"
          animated
          style="width: 100%; flex: 1"/>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="title-underline">
          <span>STATION ID</span>
          <span>COUNT</span>
        </div>
        <div v-if="incidentData.idlingFeeCharge.items">
          <div
            class="details-row"
            v-for="(item, index) in incidentData.idlingFeeCharge.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.count}}</b>
          </div>
        </div>
        <el-skeleton
          v-else
          :rows="10"
          animated
          style="width: 100%; flex: 1"/>
      </div>
    </div>
  </div>
</template>

<script>
import api from '../../../api/dashboard';
export default {
  name: "Incidents",
  props: {
    filters: {
      type: Object,
      default: () => {}
    },
    size: {
      type: String | Number,
      default: ""
    }
  },
  data() {
    return {
      changeLoading: false,
      incidentData: {
        disconnected: {},
        statusFailure: {},
        idlingFeeCharge: {},
        bootNotification: {},
        connectionEstablished: {}
      }
    };
  },
  watch: {
    filters: {
      deep: true,
      handler(val) {
        this.getData()
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    init() {
      this.incidentData = {
        disconnected: {},
        statusFailure: {},
        idlingFeeCharge: {},
        bootNotification: {},
        connectionEstablished: {}
      }
    },
    getData() {
      this.changeLoading = true;
      api.getIncidendtsData(this.filters).then(res => {
        this.changeLoading = false
        if (res.data) {
          this.incidentData = res.data
        } else {
          this.init()
        }
      }).catch(err => {
        this.changeLoading = false
        this.$message({
          type: 'error',
          message: err
        })
        this.init()
      })
    }
  }
}
</script>

<style scoped lang="scss">
  @import '../../../styles/element-ui.scss';
  .incidents-container {
    margin: 5px -5px 0;
    min-height:  calc(100vh - 130px);
  }
  
  .card-group {
    display: flex;
    flex-wrap: wrap;
    flex-flow: wrap;
    & > .card-item-view {
      margin: 5px;
      min-width: 240px;
    }
  }

  .card-item-view {
    flex: 1;
    padding: 10px 20px;
    border-radius: 6px;
    background-color: white;
  }
  
  .card-list-group .card-item-view {
    min-height: 65vh;
  }
  
  .card-title {
    color: #333333;
    font-size: 16px;
    font-weight: bold;
    text-align: center;
  }
  
  .card-value {
    color: $--color-accent;
    cursor: pointer;
    padding: 10px 0;
    font-size: 42px;
    line-height: 63px;
    text-align: center;
    transition: all .3s;
    font-family: sans-serif;
    &.error {
      color: #FF3500;
    }
    &:hover {
      transform: scale(1.2);
    }
  }
  
  .title-underline {
    width: 100%;
    color: #333333;
    font-size: 14px;
    line-height: 21px;
    display: flex;
    align-items: center;
    font-weight: bold;
    white-space: nowrap;
    padding-bottom: 20px;
    font-family: sans-serif;
    text-decoration: underline;
    justify-content: space-between;
  }
  
  .details-row {
    width: 100%;
    color: #333333;
    font-size: 14px;
    line-height: 21px;
    display: flex;
    align-items: center;
    padding-bottom: 10px;
    font-family: sans-serif;
    justify-content: space-between;
  }
</style>