<template>
  <div
    class="o-card has-padding u-pointer"
    @click="go"
  >
    <div class="l-flex--row center c-sibling-item--v">
      <i class="l-flex__none c-sibling-item o-card__icon o-icon medium el-icon-user u-bold" />
      <div class="c-sibling-item far o-card__main u-bold u-text-center">总租户</div>
    </div>
    <div class="c-sibling-item--v o-card__tip u-text-center">
      <i
        v-if="loading"
        class="el-icon-loading"
      />
      <template v-else>
        <el-link
          v-if="error"
          type="warning"
          @click.stop="onInit"
        >
          点击重试
        </el-link>
        <div class="u-ellipsis">{{ value }}</div>
      </template>
    </div>
  </div>
</template>

<script>
import { getTenantCount } from '@/api/user'

export default {
  name: 'TenantCard',
  data () {
    return {
      loading: false,
      error: true,
      value: ''
    }
  },
  created () {
    this.onInit()
  },
  methods: {
    onInit () {
      this.loading = true
      this.error = false
      getTenantCount().then(
        ({ count }) => {
          this.value = count
        },
        () => {
          this.error = true
        }
      ).finally(() => {
        this.loading = false
      })
    },
    go () {
      this.$router.push({
        name: 'org'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.o-card {
  display: inline-flex;
  flex-direction: column;
  height: 100px;
  color: $black;
  font-size: 16px;
  font-weight: bold;
  border-radius: $radius;
  background-color: #fff;

  &__icon {
    color: $primary;
    background-color: rgba($primary, 0.1);
    border-radius: 50%;
  }

  &__main {
    min-width: 0;
  }

  &__tip {
    padding-left: 48px;
  }
}
</style>
