<template>
  <el-drawer
    class="ui-drawer"
    :visible.sync="innerVisible"
    :with-header="false"
    :size="innerSize"
    :modal="modal"
    :append-to-body="true"
    :wrapper-closable="modalClose"
  >
    <div class="ui-drawer__close" @click="handleCloseClick"></div>
    <div class="ui-drawer__main">
      <div v-if="title" class="ui-drawer__head">
        <strong class="ui-drawer__title">{{ title }}</strong>
      </div>
      <div class="ui-drawer__body">
        <slot v-if="visible"></slot>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import ElDrawer from 'element-ui/lib/drawer'
import 'element-ui/lib/theme-chalk/drawer.css'

export default {
  name: 'UiDrawer',
  components: {
    ElDrawer
  },
  model: {
    prop: 'visible',
    event: 'input'
  },
  props: {
    visible: {
      type: Boolean
    },
    title: {
      type: String
    },
    size: {
      type: String,
      default: 'mini',
      validator(value) {
        return ['mini', 'small', 'medium', 'auto'].includes(value)
      }
    },
    modal: {
      type: Boolean,
      default: true
    },
    modalClose: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    innerSize() {
      let size
      if (this.size === 'mini') {
        size = '420px'
      } else if (this.size === 'small') {
        size = '540px'
      } else if (this.size === 'medium') {
        size = '640px'
      } else if (this.size === 'auto') {
        size = 'auto'
      }
      return size
    },
    innerVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  methods: {
    handleCloseClick() {
      this.$emit('input', false)
    },
    close() {
      this.innerVisible = false
    }
  }
}
</script>
<style>
.ui-drawer .el-drawer{
  outline: 0;
  overflow: initial;
}
.ui-drawer.el-drawer__wrapper {
  outline: 0;
}
.ui-drawer .el-drawer__body{
  min-height: 0;
}
.ui-drawer .el-drawer__header > span{
  outline: 0;
}
.ui-drawer__close{
  position: absolute;
  left: -13px;
  top: 24px;
  width: 26px;
  height: 26px;
  line-height: 26px;
  text-align: center;
  cursor: pointer;
  transition: all .3s;
  transform: rotate(45deg);
  background-color: #9EACC3;
  border-radius: 50%;
  z-index: 100;
}
.ui-drawer__close:before{
  content: '';
  width: 16px;
  height: 2px;
  background-color: #fff;
  display: inline-block;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.ui-drawer__close:after{
  content: '';
  width: 2px;
  height: 16px;
  background-color: #fff;
  display: inline-block;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.ui-drawer__close:hover{
  opacity: 1;
  transform: rotate(225deg);
}
.ui-drawer__main{
  height: 100%;
  display: flex;
  flex-direction: column;
}
.ui-drawer__body{
  flex: 1;
}
.ui-drawer__title {
  display: block;
  font-size: 18px;
  font-weight: bold;
  height: 32px;
  line-height: 32px;
  padding: 21px 24px;
}
</style>
