<template>
  <view class="custom-dialog" v-if="visible">
    <view class="dialog-mask" @click="closeDialog"></view>
    <view class="dialog-content" :class="{'dialog-show': visible}" :animation="animationData" :style="{ width: contentWidth, background: backgroundColor }">
      <view class="dialog-header" v-if="title && title.length > 0">
        <text class="dialog-title">{{title}}</text>
      </view>
      <view class="dialog-close" @click="closeDialog" v-if="!closeImg">×</view>
      <image v-if="closeImg" class="dialog-close-img" :src="closeImg" @click="closeDialog" mode="aspectFit" :style="{ top: closeImgTop && closeImgTop.length > 0 ? closeImgTop : '20rpx' }"></image>
      <view class="dialog-body">
        <slot></slot>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'CustomDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '提示'
    },
    contentWidth: {
      type: String,
      default: '600rpx'
    },
    backgroundColor: {
      type: String,
      default: '#fff'
    },
    closeImg: {
      type: String,
      default: ''
    },
    closeImgTop: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      animationData: {}
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.playShowAnimation()
      } else {
        this.playHideAnimation()
      }
    }
  },
  methods: {
    playShowAnimation() {
      const animation = uni.createAnimation({
        duration: 300,
        timingFunction: 'ease'
      })
      
      animation.scale(0.8).opacity(0).step({ duration: 0 })
      animation.scale(1.1).opacity(1).step({ duration: 150 })
      animation.scale(0.95).step({ duration: 75 })
      animation.scale(1).step({ duration: 75 })
      
      this.animationData = animation.export()
    },
    playHideAnimation() {
      const animation = uni.createAnimation({
        duration: 200,
        timingFunction: 'ease'
      })
      
      animation.scale(0.95).opacity(0.8).step({ duration: 100 })
      animation.scale(0.8).opacity(0).step({ duration: 100 })
      
      this.animationData = animation.export()
    },
    closeDialog() {
      this.$emit('update:visible', false)
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.custom-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  
  .dialog-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
  }
  
  .dialog-content {
    position: relative;
    border-radius: 16rpx;
    overflow: hidden;
    opacity: 0;
    transform: scale(0.8);
    
    &.dialog-show {
      opacity: 1;
      transform: scale(1);
    }
  }
  
  .dialog-header {
    position: relative;
    padding: 30rpx;
    text-align: center;
    border-bottom: 1rpx solid #eee;
    
    .dialog-title {
      font-size: 32rpx;
      font-weight: 500;
      color: #333;
    }
  }
  
  .dialog-close {
    position: absolute;
    right: 20rpx;
    top: 20rpx;
    width: 60rpx;
    height: 60rpx;
    line-height: 60rpx;
    text-align: center;
    font-size: 40rpx;
    color: #999;
    cursor: pointer;
    z-index: 1;
    
    &:active {
      opacity: 0.7;
    }
  }

  .dialog-close-img {
    position: absolute;
    right: 20rpx;
    width: 60rpx;
    height: 60rpx;
    z-index: 1;
    cursor: pointer;
    
    &:active {
      opacity: 0.7;
    }
  }
  
  .dialog-body {
    padding: 0rpx;
  }
}
</style> 