<template>
  <div class="vf-block__tooltip" :style="tooltipPosition">
    <h3 v-show="header || _slotContents.header">
      <slot name="header">{{ header }}</slot>
    </h3>
    <div class="vf-block__tooltip__content">
      <slot>{{ content }}</slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'tooltip',

  props: {
    header: String,
    content: String,
    visible: {
      type: Boolean,
      default: false
    }
  },

  ready() {
    document.body.appendChild(this.$el);
  },

  data() {
    return {
      tooltipPosition: {
        top: 0,
        left: 0
      }
    };
  },

  methods: {
    handleMousemove(ev) {
      var tooltip = this.$el;

      if (!tooltip) { return; }

      this.tooltipPosition.top = ev.clientY - tooltip.scrollHeight / 2 + 'px';
      this.tooltipPosition.left = ev.clientX - tooltip.scrollWidth - 10 + 'px';
    }
  },

  watch: {
    'visible': {
      immediate: true,
      handler: function(value, oldValue) {
        if (value) {
          document.body.addEventListener('mousemove', this.handleMousemove);
        } else {
          document.body.removeEventListener('mousemove', this.handleMousemove);
        }
      }
    }
  },

  beforeDestroy() {
    this.$remove();
  }
};
</script>

<style lang="css">
  .vf-block__tooltip {
    position: fixed;
    padding: 5px;
    text-align: left;
    font-family: arial,sans-serif;
    color: #333;
    font-size: 13px;
    background-color: #fff;
    border: 2px solid #ccc;
    border-radius: 10px;
    z-index: 100;
  }
  .vf-block__tooltip h3 {
    text-align: center;
    border-bottom: 1px solid #ccc;
    font-size: 13px;
    padding: 8px 0;
    margin: 0 0 10px;
  }
  .vf-block__tooltip__content {
    padding: 0 10px;
  }
  .vf-block__tooltip__content .ml {
    margin-left: -55px;
  }
  .vf-block__tooltip.tooltip-outflow {
    color: red;
    text-align: center;
  }
  .tooltip-block .vf-block__tooltip__content {
    padding-left: 65px;
  }
</style>
