/**
* @Author Think
* @Date 2018/11/24
*/

<template>
  <div
    :class="cusClass" class="content">
    <slot/>
  </div>
</template>

<script>
import { detectOS } from '../../common/utils/index'
export default {
  name: 'HContent',
  props: {
    cusClass: {
      type: String,
      default: '',
    },
    calContent: {
      type: Boolean,
      default: true,
    },
  },
  data () {
    return {
      docmHeight: '0', // 默认屏幕高度
      showHeight: '0', // 实时屏幕高度
      topHeight: '0', // content距离顶部的高度
      contentsHeight: '0', // 内容区域本来高度
      isResize: false, // 默认屏幕高度是否已获取
      distance: 0,
      final: 0,
      fontSize: Number(window.document.documentElement.style.fontSize.replace('px', '')),
      winHeight: window.innerHeight,
      winWidth: window.innerWidth,
    }
  },
  watch: {
  },
  mounted () {
    if (this.calContent) {
      this.contentHeight()
    }
    this.contentsHeight = this.$el.style.height.slice(0, this.$el.style.height.length - 2)
    // if (detectOS() === 'ios') {
    //   document.body.addEventListener('focusin', () => {
    document.body.addEventListener('touchstart', (e) => {
      let keyboardHeight = this.getKeyBoardHeight()
      if (e.target.localName === 'input' || e.target.localName === 'textarea') {
        let pointY = e.touches[0].pageY
        console.log(pointY)
        this.distance = (innerHeight - pointY) < keyboardHeight ? (keyboardHeight - (innerHeight - pointY)) : 0
        // if (this.distance) {
        //   this.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
        //   this.$el.style.transform = 'translate(0px, -' + this.distance + 'px) scale(1) translateZ(0px)'
        //   // this.$el.style.paddingBottom = (distance + 20) + 'px'
        //   this.$el.style.height = parseInt(this.contentsHeight) + this.distance + 'px'
      }
    //   }
    // 软键盘弹出的事件处理
    // })
    })
    document.body.addEventListener('focusin', () => {
      // this.final = this.final + this.distance
      console.log('final:' + this.final)
      this.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
      this.$el.style.transform = 'translate(0px, -' + this.distance + 'px) scale(1) translateZ(0px)'
      // this.$el.style.height = parseInt(this.contentsHeight) + this.distance + 'px'
    //   this.$el.scrollTop = this.final
    })
    document.body.addEventListener('focusout', () => {
      // 软键盘收起的事件处理
      this.$el.style.height = parseInt(this.contentsHeight) + 'px'
      this.$el.style.transform = 'translate(0px,  0px) scale(1) translateZ(0px)'
    })
    // document.body.addEventListener('focusin', () => {
    //   // 软键盘弹出的事件处理
    //   let keyboardHeight = this.getKeyBoardHeight()
    //   let activeElement = document.activeElement
    //   let offsetTop = activeElement.offsetTop
    //   let i = 0
    //   console.log(offsetTop, 11)
    //   let offsetParent = activeElement.offsetParent
    //   if (activeElement === document.body) {
    //     return
    //   }
    //   while (offsetParent !== document.body) {
    //     i++
    //     offsetTop += offsetParent.offsetTop
    //     console.log(offsetParent, i)
    //     console.log(offsetParent.offsetTop, 'offsetParent', i)
    //     offsetParent = offsetParent.offsetParent
    //     console.log(offsetTop, i, 'top')
    //   }
    //   // console.log(this.$el)
    //   // console.log(this.$el.offsetTop, 'top')
    //   // console.log(this.$el.style.height)
    //   // console.log(innerHeight - offsetTop)
    //   offsetTop = (innerHeight - offsetTop) < keyboardHeight ? (keyboardHeight - (innerHeight - offsetTop)) : 0
    //   console.log(offsetTop)
    //   if (offsetTop > 0) {
    //     this.$el.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s'
    //     this.$el.style.transform = 'translate(0px, -' + offsetTop + 'px) scale(1) translateZ(0px)'
    //     this.$el.style.height = parseInt(this.contentsHeight) + offsetTop + 'px'
    //   }
    // })
    // }

    // 当视图窗口大小发生改变时
    window.onresize = () => {
      return (() => {
        if (!this.isResize) {
          // 默认屏幕高度
          this.docmHeight = document.documentElement.clientHeight
          this.isResize = true
        }
        // 实时屏幕高度
        this.showHeight = document.body.clientHeight
      })()
    }
  },
  methods: {
    getHeaderHeight () {
      let vm = this
      let $el = vm.$el.previousElementSibling
      let headerHeight = 0
      do {
        if ($el) {
          let elHeight = window.getComputedStyle($el).height
          if (elHeight) {
            headerHeight += Number(elHeight.replace('px', ''))
          }
          if ($el._prevClass && $el._prevClass.indexOf('h-header') === 0) {
            if (detectOS() === 'ios' && vm.winWidth === 375 && vm.winHeight === 812) {
              headerHeight += 0.8 * vm.fontSize
            } else if (detectOS() === 'ios' && vm.winWidth === 414 && vm.winHeight === 896) {
              headerHeight += 0.8 * vm.fontSize
            } else if (detectOS() === 'ios') {
              headerHeight += 0.4 * vm.fontSize
            }
          }
          $el = $el.previousElementSibling
        }
      } while ($el)

      return headerHeight
    },
    getNextElementHeight () {
      let vm = this
      let nextElement = this.$el.nextElementSibling
      let nextHeight = 0
      do {
        if (nextElement) {
          let elHeight = window.getComputedStyle(nextElement).height
          if (elHeight) {
            nextHeight += Number(elHeight.replace('px', ''))
          }
          if (nextElement._prevClass && nextElement._prevClass.indexOf('h-bottom-tab') === 0) {
            if (detectOS() === 'ios' && vm.winWidth === 375 && vm.winHeight === 812) {
              nextHeight += vm.fontSize * 0.68
            } else if (detectOS() === 'ios' && vm.winWidth === 414 && vm.winHeight === 896) {
              nextHeight += vm.fontSize * 0.68
            }
          }
          nextElement = nextElement.nextElementSibling
        }
      } while (nextElement)
      return nextHeight
    },
    contentHeight () {
      let vm = this
      const headerHeight = vm.getHeaderHeight()
      const nextHeight = vm.getNextElementHeight()
      let content = vm.$el
      content.style.height = (window.innerHeight - nextHeight - headerHeight) + 'px'
    },

    // 键盘的高度
    getKeyBoardHeight () {
      var innerWidth = window.innerWidth
      if (detectOS() === 'ios') {
        if (innerWidth >= 375 && innerHeight >= 812) {
          return 460
        }
        return 400
      } else {
        return 325
      }
    },
  },
}
</script>

<style lang="less">
  .content {
    flex: 1;
    overflow: hidden;
    background-color: #fafafa;
    position: relative;
    overflow-y: scroll;
    height: 100%;
    -webkit-overflow-scrolling: touch;
    overflow-scrolling: touch;
  }

  // iPhoneX适配
  @media (device-width: 375px) and (device-height: 812px) and (-webkit-min-device-pixel-ratio: 3) {
    .platform-ios {
      .has-header {
        top: 84px;
      }
    }
  }

  // iPhoneX Max适配
  @media (device-width: 414px) and (device-height: 896px) {
    .platform-ios {
      .has-header {
        top: 84px;
      }
    }
  }
</style>
