<!-- 95735 新增lc-popover气泡菜单组件 -->

<template>
  <div class="wrapper">
    <!-- 227837 【开发】【组件库】lighting-ui中的popover组件修复安卓点透问题 -->
    <div class="g-cover"
         ref="lc-cover"
         v-if="show"
         @click="hideAction"
         :style="coverStyle"></div>
    <div ref="lc-popover"
         class="g-popover"
         v-if="show && buttons"
         :style="contentStyle">
      <div class="u-popover-arrow" :style="arrowStyle"></div>
      <div class="u-popover-inner">
        <div :class="['i-btn',i === buttons.length-1 ? 'i-btn-noborder' : '']"
             v-for="(item,i) in buttons"
             :key="i"
             @click="lcButtonClicked(i,item.key)">
          <image :src="item.icon"
                 v-if="item.icon"
                 class="btn-icon"></image>
          <text class="btn-text" :style="textStyle">{{item.text}}</text>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
const animation = weex.requireModule('animation');
export default {
  props: {
    buttons: {
      type: Array,
      default: []
    },
    position: {
      type: Object,
      default: () => ({
        x: 0,
        y: 0
      })
    },
    arrowPosition: {
      type: Object,
      default:  () => ({
        pos: 'top',
        x: 0,
        y: 0
      })
    },
    coverColor: {
      type: String,
      default: 'rgba(0, 0, 0, 0.4)'
    },
    hasAnimation: {
      type: Boolean,
      default: true
    },
    textStyle: Object
  },
  data: () => ({
    popoverStyle: {},
    coverNowStyle: {},
    show: false,
    showIn: false
  }),
  computed: {
    coverStyle() {
      return this.coverColor ? { backgroundColor: this.coverColor, opacity:this.hasAnimation||!this.showIn?'0':'1', ...this.coverNowStyle} : '';
    },
    transformOrigin() {
      let {x=0, y=0, pos='top'} = this.arrowPosition,
          _origins = [];
      switch(pos){
        case 'top':
        case 'bottom':
        _origins = [x<0?'right':'left',pos];
          break;
        case 'left':
        case 'right':
        _origins = [pos,y<0?'bottom':'top'];
          break;
      }
      return _origins.join(' ');
    },
    contentTransform() {
      let {x=0, y=0, pos='top'} = this.arrowPosition,
          _translates=['scale(0)'];
          if(x>=0 && x<22){
            x = 22;
          }else if(x<0 && x>-22){
            x =-22;
          }
          if(y>=0 && y<22){
            y = 22;
          }else if(y<0 && y>-22){
            y =-22;
          }
      switch(pos){
        case 'top':
        case 'bottom':
          _translates[1] = `translateX(${x<0?(x-15):(x+15)}px)`;
          break;
        case 'left':
        case 'right':
          _translates[1] = `translateY(${y<0?(y-15):(y+15)}px)`
          break;
      }
      return _translates.join(' ');
    },
    contentStyle() {
      let { x = 0, y = 0 } = this.position,
        style = {};
      x < 0 ? (style.right = `${-x}px`) : (style.left = `${x}px`);
      y < 0 ? (style.bottom = `${-y}px`) : (style.top = `${y}px`);
      style.opacity = this.hasAnimation||!this.showIn?'0':'1';
      style.transform = this.hasAnimation||!this.showIn?this.contentTransform:'scale(1)';
      style.transformOrigin = this.transformOrigin;
      style = Object.assign({}, style, this.popoverStyle);
      return style;
    },
    arrowStyle() {
      let { pos = 'top', x = 0, y = 0 } = this.arrowPosition,
        style = {};
      switch (pos) {
        case 'top':
          style.top = '6px';
        case 'bottom':
          !style.top && (style.bottom = '6px');
          style.transform = 'scaleX(0.8) rotate(45deg)';
          if(x>=0 && x<22){
            x = 22;
          }else if(x<0 && x>-22){
            x =-22;
          }
          x < 0 ? (style.right = `${-x}px`) : (style.left = `${x}px`);
          break;
        case 'left':
          style.left = '6px';
        case 'right':
          !style.left && (style.right = '6px');
          style.transform = 'scaleY(0.8) rotate(45deg)';
          if(y>=0 && y<22){
            y = 22;
          }else if(y<0 && y>-22){
            y =-22;
          }
          y < 0 ? (style.bottom = `${-y}px`) : (style.top = `${y}px`);
          break;
        default:
          break;
      }
      return style;
    }
  },
  methods: {
    lcPopoverShow() {
      if(!!this.animationLock){
        return;
      }
      this.show = true;
      if(this.hasAnimation){
        setTimeout(()=>this.lcPopoverAnimationShow(),40)
      }else{
        setTimeout(()=>this.showIn=true,40);
      }
    },
    /**
    * smooth in
    **/
    lcPopoverAnimationShow(){
      const popoverEl = this.$refs['lc-popover'];
      const coverEl = this.$refs['lc-cover'];
      if (!coverEl || !popoverEl) {
        return;
      }
      this.setAnimationLock();
      let a1End = false,a2End = false;
      const styles = {
          opacity: 1,
          transform: "scale(1)",
          transformOrigin: this.transformOrigin
        };
      animation.transition(popoverEl, {
        styles: styles,
        delay:0,
        duration: 250,
        timingFunction: 'ease-out'
      }, (e) => {
        this.popoverStyle = {...styles};
        a1End = true;
        if(a1End && a2End){
          this.animationLock = false
        }
      });

      animation.transition(coverEl, {
        styles: {
          opacity: 1
        },
        delay:0,
        duration: 250,
        timingFunction: 'ease-in'
      }, (e) => {
        this.coverNowStyle = { opacity: 1 };
        a2End = true;
        if(a1End && a2End){
          this.animationLock = false
        }
      });
    },
    lcButtonClicked(index, key) {
      if(!!this.animationLock){
        return;
      }
      this.$emit('LcPopoverButtonClicked', { key, index });
      this.hideAction();
    },
    /**
    * 隐藏操作
    */
    hideAction(){
      if(!!this.animationLock){
        return;
      }
      if(this.hasAnimation){
        this.setAnimationLock()
        const popoverEl = this.$refs['lc-popover'];
        const coverEl = this.$refs['lc-cover'];
        if (!popoverEl || !coverEl) {
          return;
        }
        let a1End = false,a2End = false;
        const styles = {
          opacity: 0,
          transform: this.contentTransform,
          transformOrigin: this.transformOrigin
        };
        animation.transition(popoverEl, {
          styles: styles,
          duration: 10 //92479 需求97505：在H5下weex-vue-render4light会报lines错误-快速点击有动画效果的按钮，几次之后，发现不再弹出气泡弹层
        }, () => {
          this.popoverStyle = {...styles};
          a1End = true;
          if(a1End && a2End){
            this.show = false;
            this.showIn = false;
            this.animationLock = false
          }
        });
        animation.transition(coverEl, {
          styles: {
            opacity: 0
          },
          duration: 10 //92479 需求97505：在H5下weex-vue-render4light会报lines错误-快速点击有动画效果的按钮，几次之后，发现不再弹出气泡弹层
        }, () => {
          this.coverNowStyle = { opacity: 0 };
          a2End = true;
          if(a1End && a2End){
            this.show = false;
            this.showIn = false;
            this.animationLock = false
          }
        });
      }else {
        this.show = false;
        this.showIn = false;
      }
    },
    /**
    * 设置动画锁
    */
    setAnimationLock(){
      this.animationLock = true;
    }

  }
};
</script>

<style scoped>
.wrapper{
  z-index: 999;
}
.g-cover {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 1;
}
.g-popover {
  position: fixed;
  padding:15px;
  z-index: 10;
}
.u-popover-arrow {
  position: absolute;
  border-radius: 4px;
  width: 30px;
  height: 30px;
  background-color: #ffffff;
}
.u-popover-inner {
  border-radius: 10px;
  background-color: #ffffff;
}
.i-btn {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  margin-left: 20px;
  margin-right: 20px;
  padding-left: 20px;
  padding-right: 20px;
  border-bottom-width: 1px;
  border-bottom-color: #dddddd;
}
.i-btn-noborder {
  border-bottom-color: #ffffff;
}
.btn-icon {
  width: 32px;
  height: 32px;
  margin-right: 16px;
}
.btn-text {
  flex: 1;
  height: 80px;
  font-size: 30px;
  line-height: 80px;
}
.text-align-center {
  text-align: center;
}
</style>
