<template>
  <div class="connectors-container" v-loading="changeLoading">
    <div class="card-group">
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="card-title">Available</div>
        <div class="card-value avaliable">{{ connectorData.available.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="card-title">In Use</div>
        <div class="card-value in-use">{{ connectorData.inUse.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="card-title">Reserved</div>
        <div class="card-value reserved">{{ connectorData.reserved.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="card-title">Faulted</div>
        <div class="card-value faulted">{{ connectorData.faulted.total || 0 }}</div>
      </div>
      <div class="card-item-view" :style="'max-width:' + size + 'px;'">
        <div class="card-title">Unavailable</div>
        <div class="card-value unavailable">{{ connectorData.unavailable.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</span>
          <span>CONNECTOR</span>
        </div>
        <div v-if="connectorData.available.items">
          <div
            class="details-row"
            v-for="(item, index) in connectorData.available.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.connector}}</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</span>
          <span>CONNECTOR</span>
        </div>
        <div v-if="connectorData.inUse.items">
          <div
            class="details-row"
            v-for="(item, index) in connectorData.inUse.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.connector}}</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</span>
          <span>CONNECTOR</span>
        </div>
        <div v-if="connectorData.reserved.items">
          <div
            class="details-row"
            v-for="(item, index) in connectorData.reserved.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.connector}}</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</span>
          <span>CONNECTOR</span>
        </div>
        <div v-if="connectorData.faulted.items">
          <div
            class="details-row"
            v-for="(item, index) in connectorData.faulted.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.connector}}</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>CONNECTOR</span>
        </div>
        <div v-if="connectorData.unavailable.items">
          <div
            class="details-row"
            v-for="(item, index) in connectorData.unavailable.items"
            :key="index">
            <span>{{item.stationId}}</span>
            <b>{{item.connector}}</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: "Connectors",
  props: {
    filters: {
      type: Object,
      default: () => {}
    },
    size: {
      type: String | Number,
      default: ""
    }
  },
  data() {
    return {
      changeLoading: false,
      connectorData: {
        inUse: {},
        faulted: {},
        reserved: {},
        available: {},
        unavailable: {}
      }
    };
  },
  watch: {
    filters: {
      deep: true,
      handler(val) {
        this.getData()
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    init() {
      this.connectorData = {
        inUse: {},
        faulted: {},
        reserved: {},
        available: {},
        unavailable: {}
      }
    },
    getData() {
      this.changeLoading = true;
      api.getConnectorsData(this.filters).then(res => {
        this.changeLoading = false
        if (res.data) {
          this.connectorData = res.data
        } else {
          this.init()
        }
      }).catch(err => {
        this.changeLoading = false
        this.$message({
          type: 'error',
          message: err
        })
        this.init()
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .connectors-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: #333;
    cursor: pointer;
    padding: 10px 0;
    font-size: 42px;
    line-height: 63px;
    text-align: center;
    transition: all .3s;
    font-family: sans-serif;
    &.avaliable {
      color: #1ABD00;
    }
    &.in-use {
      color: #236EDE;
    }
    &.reserved {
      color: #FDA802;
    }
    &.faulted {
      color: #EF3340;
    }
    &.unavaliable {
      color: #000000;
    }
    &: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>