<template>
  <view class="ml-drawer" :class="[className || '', `placement-${placement || 'bottom'}`, {'is-visible': visible, 'is-animate': isAnimate}]" @tap="tapMaskEvent">
    <view v-if="visible" class="ml-drawer-warpper" @tap.stop.prevent>
      <view v-if="title || showHeader" class="ml-drawer-header">
        <slot name="header">
          <view v-if="title" class="ml-drawer-header-title">{{ title }}</view>
        </slot>
      </view>
      <view class="ml-drawer-body" :style="bodyStyles">
        <scroll-view scroll-y>
          <slot></slot>
        </scroll-view>
      </view>
      <view v-if="showConfirmButton || showCancelButton || showFooter" class="ml-drawer-footer">
        <slot name="footer">
          <ml-button v-if="showCancelButton" :status="cancelButtonStatus" @click="cancelEvent">{{ cancelButtonText }}</ml-button>
          <ml-button v-if="showConfirmButton" :status="confirmButtonStatus" @click="confirmEvent">{{ confirmButtonText }}</ml-button>
        </slot>
      </view>
    </view>
  </view>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'MlDrawer',
  props: {
    modelValue: Boolean,
    title: String,
    height: String,
    width: String,
    showHeader: {
      type: Boolean,
      default: true
    },
    showFooter: Boolean,
    placement: String,
    maskClosable: {
      type: Boolean,
      default: true
    },
    showConfirmButton: Boolean,
    confirmButtonStatus: {
      type: String,
      default: 'primary'
    },
    confirmButtonText: {
      type: String,
      default: '确认'
    },
    confirmClosable: Boolean,
    showCancelButton: Boolean,
    cancelButtonStatus: String,
    cancelButtonText: {
      type: String,
      default: '取消'
    },
    cancelClosable: {
      type: Boolean,
      default: true
    },
    className: String
  },
  data () {
    return {
      visible: false,
      isAnimate: false,
      animateTime: null as any
    }
  },
  computed: {
    bodyStyles () {
      const wSty = this.width ? `width:${this.width};` : ''
      const hSty = this.height ? `height:${this.height};` : ''
      return `${wSty}${hSty}`
    }
  },
  watch: {
    modelValue (val) {
      if (val) {
        this.open()
      } else {
        this.close()
      }
    }
  },
  created () {
    this.$nextTick(() => {
      if (this.modelValue) {
        this.open()
      } else {
        this.close()
      }
    })
  },
  methods: {
    close () {
      clearTimeout(this.animateTime)
      this.isAnimate = true
      this.isAnimate = false
      this.animateTime = setTimeout(() => {
        this.visible = false
        this.$emit('update:modelValue', false)
        this.$emit('hide', {})
      }, 300)
    },
    open () {
      clearTimeout(this.animateTime)
      this.isAnimate = false
      this.visible = true
      this.animateTime = setTimeout(() => {
        this.isAnimate = true
        this.$emit('show', {})
      }, 20)
      this.$emit('update:modelValue', true)
    },
    tapMaskEvent () {
      if (this.maskClosable) {
        this.close()
      }
    },
    cancelEvent () {
      if (this.cancelClosable) {
        this.close()
      }
      this.$emit('cancel', {})
    },
    confirmEvent () {
      if (this.confirmClosable) {
        this.close()
      }
      this.$emit('confirm', {})
    }
  }
})
</script>

<style lang="scss">
.ml-drawer {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: $ml-page-full-width;
  height: 100vh;
  z-index: 999;
  background-color: rgba(0, 0, 0, 0.4);
  // #ifdef H5
  left: 50%;
  transform: translateX(-50%);
  // #endif
  &.is-visible {
    display: block;
  }
  &.placement-left {
    &.is-animate {
      .ml-drawer-warpper {
        transform: translateX(0);
      }
    }
    .ml-drawer-warpper {
      height: 100%;
      transform: translateX(-100%);
    }
  }
  &.placement-bottom {
    &.is-animate {
      .ml-drawer-warpper {
        transform: translateY(0);
      }
    }
    .ml-drawer-warpper {
      width: 100%;
      transform: translateY(100%);
    }
  }
  .ml-drawer-warpper {
    position: absolute;
    bottom: 0;
    left: 0;
    padding-bottom: 30rpx;
    background: #fff;
    transition: transform 0.25s ease, transform 0.3s ease;
  }
  .ml-drawer-header {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 10rpx 20rpx;
    min-height: 90rpx;
    font-size: 32rpx;
  }
  .ml-drawer-header-title {
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
  }
  .ml-drawer-body {
    padding: 20rpx;
    overflow: auto;
    height: 500rpx;
  }
  .ml-drawer-footer {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 20rpx;
  }
}
</style>
