<template>
  <div class="vux-actionsheet">
    <transition name="vux-actionsheet-mask">
      <div
        v-show="show"
        class="weui-mask weui-mask_transparent"
        @click="onClickingMask"
      />
    </transition>
    <div
      v-if="theme === 'android'"
      class="weui-skin_android"
    >
      <transition name="vux-android-actionsheet">
        <div
          v-show="show"
          class="weui-actionsheet"
        >
          <div class="weui-actionsheet__menu">
            <div
              v-for="(text, key) in menus"
              :key="text.label || text"
              class="weui-actionsheet__cell"
              @click="onMenuClick(text, key)"
            >
              {{ text.label || text }}
            </div>
          </div>
        </div>
      </transition>
    </div>
    <div
      v-else
      class="weui-actionsheet"
      :class="{'weui-actionsheet_toggle': show}"
    >
      <div class="weui-actionsheet__menu">
        <div
          v-if="hasHeaderSlot"
          class="weui-actionsheet__cell"
        >
          <slot name="header" />
        </div>
        <div
          v-for="(text, key) in menus"
          :key="text.label || text"
          class="weui-actionsheet__cell"
          :class="`vux-actionsheet-menu-${text.type || 'default'}`"
          @click="onMenuClick(text, key)"
        >
          {{ text.label || text }}
        </div>
      </div>
      <div
        v-if="showCancel"
        class="weui-actionsheet__action"
        @click="emitEvent('on-click-menu', 'cancel')"
      >
        <div class="weui-actionsheet__cell">
          {{ cancelText || '取消' }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Actionsheet',
  props: {
    value: Boolean,
    showCancel: Boolean,
    cancelText: String,
    theme: {
      type: String,
      default: 'ios'
    },
    menus: {
      type: [Object, Array],
      default: () => ({})
    },
    closeOnClickingMask: {
      type: Boolean,
      default: true
    },
    closeOnClickingMenu: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      hasHeaderSlot: false,
      show: false
    }
  },
  watch: {
    show (val) {
      this.$emit('input', val)
      if (val) {
        this.fixIos(-1)
      } else {
        setTimeout(() => {
          this.fixIos(100)
        }, 200)
      }
    },
    value: {
      handler: function (val) {
        this.show = val
      },
      immediate: true
    }
  },
  mounted () {
    this.hasHeaderSlot = !!this.$slots.header
    this.$nextTick(() => {
      this.$tabbar = document.querySelector('.weui-tabbar')
    })
  },
  beforeDestroy () {
    this.fixIos(100)
  },
  methods: {
    onMenuClick (text, key) {
      if (typeof text === 'string') {
        this.emitEvent('on-click-menu', key, text)
      } else {
        if (text.type !== 'disabled' && text.type !== 'info') {
          if (text.value || text.value === 0) {
            this.emitEvent('on-click-menu', text.value, text)
          } else {
            this.emitEvent('on-click-menu', '', text)
            this.show = false
          }
        }
      }
    },
    onClickingMask () {
      this.$emit('on-click-mask')
      this.closeOnClickingMask && (this.show = false)
    },
    emitEvent (event, menu, item) {
      if (event === 'on-click-menu' && !/.noop/.test(menu)) {
        let _item = item
        if (typeof _item === 'object') {
          _item = JSON.parse(JSON.stringify(_item))
        }
        this.$emit(event, menu, _item)
        this.$emit(`${event}-${menu}`)
        this.closeOnClickingMenu && (this.show = false)
      }
    },
    fixIos (zIndex) {
      if (this.$el.parentNode && this.$el.parentNode.className.indexOf('v-transfer-dom') !== -1) {
        return
      }
      if (this.$tabbar && /iphone/i.test(navigator.userAgent)) {
        this.$tabbar.style.zIndex = zIndex
      }
    }
  }
}
</script>
