<template>
  <div class="modal-dialog" :class="{'show-modal-dialog': showModal}">
    <div class="overlay-content">
      <div class="overlay" @click="(closeOnBackground) ? showModal = false : false" />
      <div 
        class="content-dialog"
        :style="{width: size ? '100%' : 'auto', maxWidth: size ? `${size}px` : 'auto'}"
      >
        <slot />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    show: {
      type: [Boolean],
      default: false
    },
    closeOnBackground: {
      type: [Boolean],
      default: true
    },
    size: {
      type: [Number],
      default: null
    }
  },
  data() {
    return {
      showModal: false
    }
  },
  watch: {
    show(newVal) {
      this.showModal = newVal
      if (newVal) {
        document.body.style.overflow = "hidden"
      } else {
        document.body.style.overflow = "auto"
      }
    },
    showModal(newVal) {
      if (!newVal) {
        this.$emit("handleClose")
      }
    }
  },
  mounted() {
    this.showModal = this.show
  },
}
</script>

<style lang="scss">
  .modal-dialog {
    position: fixed;
    top: 0px;
    left: 0px;
    height: calc(100vh - 40px);
    width: calc(100% - 40px);
    padding: 20px;
    align-items: center;
    justify-content: center;
    z-index: 1050;
    display: none;
    overflow: hidden;

    .wrap-content {
      border-radius: 4px;
    }

    .overlay-content {
      z-index: 1;
      width: 100%;
      position: relative;
      height: 100vh;
      overflow-x: hidden;
      overflow-y: auto;
      -ms-overflow-style: none;  /* Internet Explorer 10+ */
      scrollbar-width: none;
    }

    .content-dialog {
      position: relative;
      z-index: 9;
      box-shadow: 0 11px 15px -7px rgb(0 0 0 / 20%), 0 24px 38px 3px rgb(0 0 0 / 14%), 0 9px 46px 8px rgb(0 0 0 / 12%);
      background: #fff;
      border-radius: 4px;
      margin: 50px auto;
    }

    .overlay {
      position: fixed;
      top: 0px;
      left: 0px;
      opacity: 0.46;
      background-color: #212121;
      height: 100%;
      width: 100%;
      z-index: 1;
    }
  }

  .show-modal-dialog {
    display: flex;
    animation: fadeIn 0.3s;
    animation-fill-mode: forwards;
  }

  @keyframes fadeIn {
   0% {
     opacity: 0
   }
   100% {
     opacity: 1
   }
 }
</style>