<style lang="scss" scoped>
  .nav-space {
    height: 44px;
  }
  .placeholder {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    top: 0px;
    left: 0px;
    text-align: center;
    background-color: $bc1;
    height: 100%;
    position: absolute;
    .text {
      margin-top: 10px;
      font-size: 14px;
      color: $tc3;
      margin-bottom: 30px;
    }
    img {
      width: 120px;
      height: 120px;
      display: inline-block;
    }
  }
</style>

<template>
  <div>
    <div class="placeholder" v-if="value === 0">
      <img mode="widthFix" class="img"
           :src="loadingImg || imgs[0]" />
      <div class="text">加载中...</div>
    </div>
    <slot v-if="value === 1"></slot>
    <slot v-if="value === 2" name="error">
      <div class="placeholder" @click="reload">
        <img :src="imgs[1]">
        <div class="text">{{errorText}}</div>
        <button v-if="actionText" @click.stop="action">{{actionText}}</button>
      </div>
    </slot>
    <slot v-if="value === 3" name="empty">
      <div class="placeholder" @click="reload">
        <img :src="imgs[2]">
        <div class="text">{{emptyText}}</div>
        <button v-if="actionText" @click.stop="action">{{actionText}}</button>
      </div>
    </slot>
  </div>
</template>

<script>
import defaultLoadingImg from '@/assets/img/common/loading.gif'
import defaultEmptyImg from '@/assets/img/common/load_error.png'
import defaultErrorImg from '@/assets/img/common/nodata.png'

export default {
  name: 'Container',
  props: {
    value: {
      type: Number,
      default: 0
    },
    loadingImg: {
      type: String,
      default: ''
    },
    emptyText: {
      type: String,
      default: '暂无数据'
    },
    errorText: {
      type: String,
      default: '加载失败'
    },
    actionText: {
      type: String
    },
    hasNav: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      status: this.value,
      imgs: [ defaultLoadingImg, defaultEmptyImg, defaultErrorImg ]
    }
  },
  watch: {
    value(val) {
      this.status = val
    }
  },
  methods: {
    action() {
      this.$emit('action', '')
    },
    reload() {
      this.status = 0
      this.$emit('reload', (e) => {
        console.log(e)
      })
      this.$emit('input', this.status)
    }
  }
}
</script>
