<template>
  <div class="wrapper">
    <div class="header">
      <div class="title">{{ title }}</div>
      <div class="operation">
        <div :class="['btn', prop === activeTime ? 'active' : '']"
             v-for="({ prop, label }, index) in operation"
             :key="prop || index"
             @click="handleSwitch(label, prop, index)">{{ label }}</div>
      </div>
    </div>

    <div class="list-content">
      <template v-if="data.length">
        <div class="list-item" v-for="(item, index) in data">
          <div class="numerical">{{ index + 1 }}</div>
          <div class="detail">
            <div class="head_portrait">
              <img :src="`${baseUrl}${item.avatar}`" alt="">
            </div>
            <div class="user-info">
              <div class="user-name">{{ item.nickname }}</div>
              <div class="user-id">{{ item.usercode }}</div>
            </div>
          </div>
          <div class="count">
            <slot v-if="$scopedSlots.default" :data="item" :$index="index"></slot>
            <count-to v-else :start-val="0" :options="options" :end-val="item.value" :duration="2600" class="card-panel-num" />
          </div>
        </div>
      </template>
      <el-empty v-else :image-size="100" description="暂无数据"></el-empty>

    </div>
  </div>
</template>

<script>
import CountTo from 'vue-countup-v2'
import { staticRank } from '@/api/cai/static'
import publisherInstance from '@/utils/pubsub'
import store from "@/store";
export default {
  name: 'index',
  components: {
    CountTo
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    type: [Number, String],
    operation: {
      type: Array,
      default: ()=> ([{ label: '总', prop: 'Total' }, { label: '日', prop: 'Day' }])
    },
    url: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      options: {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        prefix: '',
        suffix: ''
      },
      activeTime: 'Total',
      data: [],
      baseUrl: store.getters.filePrefix
    }
  },
  created() {
    publisherInstance.subscribe('refresh', this.getRankList);
    this.getRankList();
  },
  methods: {
    async getRankList() {
      const { code, data, msg } = await staticRank(this.type, this.activeTime);
      if (code === 200) {
        return (this.data = data);
      }
    },

    handleSwitch(label, prop, index) {
      this.activeTime = prop;
      this.getRankList();
      this.$emit('switch-change', { label, prop, index });
    }
  }
}
</script>

<style scoped lang="scss">
.wrapper {
  padding: 16px;
  border-radius: 14px;
  box-sizing: border-box;
  width: 100%;
  background: #fff;
  .header {
    height: 26px;
    line-height: 26px;
    margin-bottom: 16px;
    .title {
      float: left;
      font-size: 16px;
      font-weight: 700;
      color: #202224;
    }
    .operation {
      float: right;
      display: flex;
      padding: 2px;
      box-sizing: border-box;
      background: #F5F6FA;
      .btn {
        width: 26px;
        height: 22px;
        border-radius: 4px;
        line-height: 22px;
        text-align: center;
        font-size: 14px;
        font-weight: 550;
        transition: all 0.1s linear;
        cursor: pointer;
        color: #858585;
        &.active {
          background: #fff;
          color: #202224;
        }
      }
    }
  }

  .list-content {
    width: 100%;
    height: 323px;
    overflow: auto;
    padding-right: 17px;
    .list-item {
      height: 37px;
      line-height: 37px;
      margin-bottom: 16px;
      &:nth-of-type(1) {
        .numerical {
          color: #F1691C;
        }
      }
      &:nth-of-type(2) {
        .numerical {
          color: #F5AB3C;
        }
      }
      &:nth-of-type(3) {
        .numerical {
          color: #4379EE;
        }
      }
      .numerical {
        font-size: 14px;
        font-weight: 600;
        color: #202224;
        width: 20px;
        text-align: left;
        margin-right: 8px;
        float: left;
      }
      .detail {
        display: flex;
        float: left;
        .head_portrait {
          width: 28px;
          height: 28px;
          border-radius: 50%;
          overflow: hidden;
          margin-top: 4.5px;
          margin-right: 4px;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .user-info {
          line-height: normal;
          .user-name {
            font-size: 14px;
            color: #202224;
            margin-bottom: 2px;
          }
          .user-id {
            font-size: 12px;
            color: #767D85;
          }
        }
      }

      .count {
        float: right;
        font-size: 14px;
        color: #202224;
      }
    }
  }
}
</style>
