<template>
  <div class="ui-page">
    <div class="ui-page__head" v-if="showHead">
      <div class="ui-page__head-left">
        <span
          v-if="backUrl || $listeners.back"
          class="ui-page__back"
          @click="handleBackClick"
        ></span>
        <strong
          v-if="title"
          class="ui-page__title"
        >{{ title }}</strong>
        <div
          v-if="$slots.navbar"
          class="ui-page__navbar"
        >
          <slot name="navbar" />
        </div>
        <slot name="head-left"></slot>
      </div>
      <div class="ui-page__head-right">
        <div class="ui-page__addon">
          <slot name="addon" />
        </div>
        <div class="ui-page__head-more" v-if="homeUrl">
          <page-home-setting
            :home-url="homeUrl"
          ></page-home-setting>
        </div>
      </div>
    </div>
    <div ref="body" class="ui-page__body">
      <slot v-if="!loading" />
      <page-skeleton :visible="loading"></page-skeleton>
    </div>
  </div>
</template>
<script>
import PageSkeleton from './page-skeleton.vue'
import PageHomeSetting from './page-home-setting'

export default {
  name: 'UiPage',
  components: {
    PageSkeleton,
    PageHomeSetting
  },
  provide() {
    return {
      UiPage: this
    }
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    homeUrl: {
      type: String
    },
    backUrl: {
      type: String
    },
    queryMethod: {
      type: Function
    }
  },
  data() {
    return {
      loading: false
    }
  },
  computed: {
    showHead() {
      return this.title || this.homeUrl || this.$slots.addon || this.$slots.navbar || this.$slots['head-left']
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    getMainHeight() {
      return this.$refs.body.clientHeight
    },
    handleBackClick() {
      if (this.backUrl) {
        this.$router.replace({
          path: this.backUrl
        })
      }
      this.$emit('back')
    },
    loadData() {
      if (!this.queryMethod) {
        return
      }
      this.loading = true
      this.queryMethod().finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
<style lang="less">
.ui-page{
  height: 100%;
  display: flex;
  flex-direction: column;
}
.ui-page__head{
  height: 32px;
  line-height: 32px;
  padding: 12px 24px;
  border-bottom: 1px solid #D5D8DC;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  box-sizing: content-box;
}
.ui-page__head-left{
  display: flex;
  flex-direction: row;
  align-items: center;
}
.ui-page__title{
  font-size: 18px;
  font-family: PingFangSC-Semibold, PingFang SC, -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Source Han Sans", "Noto Sans CJK Sc", "Microsoft YaHei", "Microsoft Jhenghei", sans-serif;
}
.ui-page__body {
  flex: 1;
  min-height: 0;
  overflow: auto;
}
.ui-page__navbar{
  position: relative;
  padding-left: 32px;
  margin-left: 20px;
}
.ui-page__navbar:before{
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
  width: 1px;
  height: 20px;
  background-color: #D5D8DC;
}
.ui-page__head-right{
  display: flex;
  flex-direction: row;
  align-items: center;
}
.ui-page__head-more{
  margin-left: 16px;
}
.ui-page__back{
  display: inline-block;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #FFFFFF;
  box-shadow: 0px 0px 4px 0px rgba(169,169,169,0.5000);
  margin-right: 12px;
  position: relative;
  background: url('./icons/back-line.svg') no-repeat 50% 50%;
  background-size: 85%;
  transition: all .25s;
  cursor: pointer;
  &:hover {
    background-color: #f3f3f3;
  }
}
</style>
