<template>
  <div>
    <transition name="fade">
      <div class="aside-backdrop" v-show="visible" @click="close"></div>
    </transition>
    <transition :name="(placement === 'left') ? 'slideleft' : 'slideright'">
      <div class="aside"
        v-bind:style="{width:width + 'px'}"
        v-bind:class="{
        left:placement === 'left',
        right:placement === 'right'
        }"
        v-show="visible"
      >
        <div class="aside-dialog">
          <div class="aside-content">
            <div class="aside-header">
              <h4 class="aside-title">
                <slot name="header">
                  {{ header }}
                </slot>
              </h4>
              <div class="close" @click="close">
                <Icon type="ios-close-empty" size="28"></Icon>
              </div>
            </div>
            <div class="aside-body" ref="asideBody" id="asideBody">
              <slot></slot>
            </div>
            <div class="aside-footer">
              <Button type="ghost" size="large" @click="close">{{cancelText}}</Button>
              <Button type="primary" size="large" style="margin-left: 10px" @click="ok">{{okText}}</Button>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Aside',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    placement: {
      type: String,
      default: 'right'
    },
    header: {
      type: String
    },
    width: {
      type: [Number, String],
      default: '800'
    },
    okText: {
      type: String,
      default: '保存'
    },
    cancelText: {
      type: String,
      default: '取消'
    }
  },
  data() {
    return {
      visible: this.show
    }
  },
  computed: {

  },
  watch: {
    show(val) {
      this.visible = val;
      this.$nextTick(() => {
        this.$refs.asideBody.scrollTop = 0;
      });
    },
    visible(val) {
      this.$emit('on-aside-state-change', val);
      this.$emit('update:show', val);
    }
  },
  methods: {
    close () {
      this.visible = false;
    },
    ok() {
      this.$emit('on-ok');
      // this.close();
    }
  }
}
</script>

<style scoped>
.aside {
  position: fixed;
  top: 0;
  bottom: 0;
  z-index: 980;
  overflow: auto;
  background: #fff;
}
.aside.left {
  left: 0;
  right: auto;
}
.aside.right {
  left: auto;
  right: 0;
}
.slideleft-enter-active{
  animation: slideleft-in .3s;
}
.slideleft-leave-active{
  animation: slideleft-out .3s;
}
@keyframes slideleft-in {
  0% {
    transform: translateX(-100%);
    opacity: 0;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}
@keyframes slideleft-out {
  0% {
    transform: translateX(0);
    opacity: 1;
  }
  100% {
    transform: translateX(-100%);
    opacity: 0;
  }
}
.slideright-enter-active{
  animation: slideright-in .3s;
}
.slideright-leave-active{
  animation: slideright-out .3s;
}
@keyframes slideright-in {
  0% {
    transform: translateX(100%);
    opacity: 0;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}
@keyframes slideright-out {
  0% {
    transform: translateX(0);
    opacity: 1;
  }
  100% {
    transform: translateX(100%);
    opacity: 0;
  }
}
.aside:focus {
    outline: 0
}
@media (max-width: 991px) {
  .aside {
    min-width:240px
  }
}
.aside.left {
  right: auto;
  left: 0
}
.aside.right {
  right: 0;
  left: auto
}
.aside .aside-dialog .aside-header {
  padding: 13px 16px;
  border-radius: 3px 3px 0 0;
  background: #fff;
  color: #666;
  border-bottom: 1px solid #f4f4f4;
  min-height: 50px;
  border-radius: 0 0 3px 3px;
}
.aside .aside-dialog .aside-header .close {
  position: absolute;
  font-size: 28px;
  right: 16px;
  top: 7px;
  cursor: pointer;
}
.aside .aside-dialog .aside-header .close:hover{
  color: #000;
}
.aside .aside-dialog .aside-body {
  position: absolute;
  left: 0;
  top: 51px;
  bottom: 57px;
  padding: 15px;
  box-sizing: border-box;
  width: 100%;
  overflow: auto;
}
.aside-title{
  font-weight: 400;
  line-height: 24px;
  font-size: 14px;
}
.aside .aside-dialog .aside-footer .btn {
  margin-left: 5px;
  margin-bottom: 0
}
.aside .aside-dialog .aside-footer .btn-group .btn {
  margin-left: -1px
}
.aside .aside-dialog .aside-footer .btn-block {
  margin-left: 0
}
.aside-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 970;
  background: rgba(0, 0, 0, 0.5);
}
.fade-enter-active, .fade-leave-active {
  transition: opacity .3s
}
.fade-enter, .fade-leave-active {
  opacity: 0
}
.aside-footer{
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  border-top: 1px solid #f4f4f4;
  padding: 10px 16px 10px 10px;
  text-align: right;
  border-radius: 0 0 3px 3px;
  background-color: #fff;
}
</style>
