<template>
  <a-drawer
    :visible="visible"
    :width="isMobile ? screenWidth : screenWidth * 0.85"
    placement="right"
    :title="title"
    @close="onClose"
  >
    <a-spin :spinning="loading">
      <a-page-header :title="getPageHeaderTitle">
        <div class="row">
          <div class="content">
            <slot name="contentDescriptions" :datas="details"></slot>
          </div>
          <div class="extra">
            <a-row class="status-list" v-for="(item,index) in extraStatusList" :key="'extraStatus' + index">
              <a-col :sm="24" :xs="12">
                <div class="text">{{ item.text }}</div>
                <div class="heading">
                  <x-badge :is-external-text="true" :value="item.key" :badge-key="item.keyMap ? item.keyMap : 'none'"/>
                </div>
              </a-col>
            </a-row>
          </div>
        </div>
        <!-- actions -->
        <template #extra>
          <a-button-group style="margin-right: 4px;">
            <a-button :loading="loading" type="dashed" @click="initView('1')">刷新</a-button>
          </a-button-group>
          <a-button-group style="margin-right: 4px;">
            <slot name="extraActions" :datas="details"></slot>
          </a-button-group>
        </template>
        <template #footer>
          <slot name="footerContent" :datas="details"></slot>
        </template>
      </a-page-header>
    </a-spin>
  </a-drawer>
</template>

<script>
import { mapState } from 'vuex'
import XBadge from '@vue2-client/base-client/components/common/XBadge/XBadge'

export default {
  name: 'InstructDetailsView',
  components: {
    XBadge
  },
  data () {
    return {
      // 页面宽度
      screenWidth: document.documentElement.clientWidth,
      // Tab页签
      tabActiveKey: '1',
      // 数据详情
      details: {},
      // 加载状态
      loading: true
    }
  },
  mounted () {
    this.initView('1')
  },
  computed: {
    ...mapState('account', { currUser: 'user' }),
    ...mapState('setting', ['isMobile'])
  },
  props: {
    title: {
      type: String,
      required: true
    },
    extraStatusList: {
      type: Array,
      default: () => {
        return []
      }
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 初始化组件
    initView (tabActiveKey) {
      this.tabActiveKey = tabActiveKey || '1'
      this.getDatas()
    },
    // 获取详情信息
    getDatas () {
      this.loading = true
      this.$emit('load').then(res => {
        this.details = res
      }, err => {
        console.error(err)
      }).finally(() => {
        this.loading = false
      })
    },
    // 获取页头标题
    getPageHeaderTitle () {
      this.$emit('getPageHeaderTitle', this.details)
    },
    // 关闭抽屉时回调
    onClose () {
      this.$emit('update:visible', false)
    }
  },
  watch: {
    'visible' (val) {
      if (val) {
        this.initView('1')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.detail-layout {
  margin-left: 44px;
}
.text {
  color: rgba(0, 0, 0, .45);
}

.heading {
  color: rgba(0, 0, 0, .85);
  font-size: 20px;
}

.no-data {
  color: rgba(0, 0, 0, .25);
  text-align: center;
  line-height: 64px;
  font-size: 16px;

  i {
    font-size: 24px;
    margin-right: 16px;
    position: relative;
    top: 3px;
  }
}

.mobile {
  .detail-layout {
    margin-left: unset;
  }
  .text {

  }
  .status-list {
    text-align: left;
  }
}

.row {
  display: flex;

  .content {
    -webkit-box-flex: 1;
    flex: auto;
    -ms-flex: auto;
  }

  .extra {
    flex: 0 1 auto;
    -webkit-box-flex: 0;
    -ms-flex: 0 1 auto;
    min-width: 242px;
    margin-left: 88px;
    text-align: right;
  }
}
</style>
