<template>
  <view class="ml-modal" :class="[className || '', {'is-visible': visible}]" @tap="tapMaskEvent">
    <view v-if="visible" class="ml-modal-warpper" @tap.stop.prevent>
      <view v-if="showHeader" class="ml-modal-header">
        <slot name="header">
          <view class="ml-modal-header-title">{{ title }}</view>
          <view v-if="showClose" class="ml-modal-header-close" @click="closeEvent">
            <ml-icon name="close" />
          </view>
        </slot>
      </view>
      <view class="ml-modal-body" :style="bodyStyles">
        <scroll-view scroll-y>
          <slot></slot>
        </scroll-view>
      </view>
      <view v-if="showConfirmButton || showCancelButton || showFooter" class="ml-modal-footer">
        <slot name="footer">
          <ml-button v-if="showCancelButton" @click="cancelEvent">{{ cancelButtonText }}</ml-button>
          <ml-button v-if="showConfirmButton" status="primary" @click="confirmEvent">{{ confirmButtonText }}</ml-button>
        </slot>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'MlModal',
  props: {
    value: Boolean,
    title: String,
    height: String,
    width: String,
    showHeader: {
      type: Boolean,
      default: true
    },
    showFooter: Boolean,
    showClose: {
      type: Boolean,
      default: true
    },
    maskClosable: {
      type: Boolean,
      default: true
    },
    showConfirmButton: Boolean,
    confirmButtonText: {
      type: String,
      default: '确认'
    },
    confirmClosable: Boolean,
    showCancelButton: Boolean,
    cancelButtonText: {
      type: String,
      default: '取消'
    },
    cancelClosable: {
      type: Boolean,
      default: true
    },
    className: String
  },
  data () {
    return {
      visible: false
    }
  },
  computed: {
    bodyStyles () {
      const wSty = this.width ? `width:${this.width};` : ''
      const hSty = this.height ? `height:${this.height};` : ''
      return `${wSty}${hSty}`
    }
  },
  watch: {
    value (val) {
      if (val) {
        this.open()
      } else {
        this.close()
      }
    }
  },
  created () {
    this.$nextTick(() => {
      if (this.value) {
        this.open()
      } else {
        this.close()
      }
    })
  },
  methods: {
    close () {
      this.visible = false
      this.$emit('input', false)
      this.$emit('hide', {})
    },
    open () {
      this.visible = true
      this.$emit('show', {})
      this.$emit('input', true)
    },
    tapMaskEvent () {
      if (this.maskClosable) {
        this.close()
      }
    },
    closeEvent () {
      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-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: $ml-page-full-width;
  height: 100vh;
  z-index: 999;
  font-size: $ml-base-font-size;
  background-color: rgba(0, 0, 0, 0.4);
  // #ifdef H5
  left: 50%;
  transform: translateX(-50%);
  // #endif
  &.is-visible {
    display: block;
  }
  .ml-modal-warpper {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    border-radius: $ml-border-radius;
  }
  .ml-modal-header {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 10rpx 20rpx;
    min-height: 90rpx;
    font-size: 32rpx;
  }
  .ml-modal-header-title {
    flex-grow: 1;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .ml-modal-header-close {
    flex-shrink: 0;
    padding-left: 10rpx;
    font-size: 36rpx;
    // #ifdef H5
    cursor: pointer;
    // #endif
  }
  .ml-modal-body {
    padding: 20rpx;
    // #ifdef MP
    width: 600rpx;
    // #endif
    // #ifdef H5
    min-width: 600rpx;
    // #endif
    overflow: auto;
    max-height: 600rpx;
  }
  .ml-modal-footer {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 20rpx;
  }
}
</style>
