<template>
  <div class="la-panel" :class="panelClass" :tabindex="1">
    <div class="la-panel__header" v-if="!hideTitle" @dblclick="handleDblClick">
      <i class="la-panel__icon" :class="panelIcon"></i>

      <div v-if="!collapse" style="display: inline-block;">
        <slot v-if="$slots.title" name="title"/>
        <span v-else>{{ title }}</span>
      </div>


      <span v-if="collapse" class="la-panel__colbtn" @click="handleCollapse">
        <i class="la-panel__colbtn__icon el-icon-d-arrow-right"/>
        <span>{{ title }}</span>
      </span>

      <el-popover v-if="$slots.help" placement="top" v-model="showHelp" popper-class="la-panel-help-popper">
        <div>
          <slot name="help"/>
        </div>
        <template slot="reference">
          <i style="color:#2d8cf0;cursor:pointer;margin-left:4px" class="el-icon-question"/>
        </template>
      </el-popover>

      <div class="la-panel__buttons">
        <slot name="buttons"/>
        <el-button v-if="maxable"
                   :icon="state!=='maximize'?'el-icon-fa-window-maximize':'el-icon-fa-window-minimize'"
                   type="text"
                   title="点击最大化" @click="handleMaximize"/>
      </div>

      <div style="clear:both"></div>
    </div>

    <div class="la-panel__body ">
      <slot/>
      <div style="clear:both"></div>
    </div>

    <div style="clear:both"></div>

  </div>

</template>

<script>
const panelManager = {
  idx: 0,
  next() {
    ++this.idx;
  }
};

export default {
  name: "panel",
  props: {
    maxable: {type: Boolean, default: false},
    collapse: {type: Boolean, default: false},
    light: {type: Boolean, default: false},
    icon: {type: String},
    full: {type: Boolean, default: false},
    title: {type: String},
    hideTitle: {type: Boolean, default: false},
  },
  computed: {
    panelIcon() {
      return this.light && !this.collapse ? "el-icon-d-arrow-right" : this.icon;
    },
    panelClass() {
      return [
        {
          "la-panel--full": this.full,
          "la-panel--collapse": this.collapse,
          "la-panel--light": this.light,
          "la-panel--icon": !!this.icon
        },
        "la-panel--" + this.state
      ];
    }
  },
  data() {
    return {
      showHelp: false,
      state: "opened"
    };
  },
  beforeMount() {
    if (this.collapse) {
      this.state = "closed";
    }
  },
  mounted() {
    this.$el.tabindex = panelManager.next();
    this.$el.addEventListener("keydown", e => {
      if (e.keyCode == 112) {
        if (this.$slots.help) {
          this.showHelp = !this.showHelp;
        }
        e.preventDefault();
      }
    });

    this.$el.addEventListener("mousedown", e => {
      // document
      //   .querySelectorAll(".la-panel")
      //   .forEach(i => i.classList.remove("la-panel--focus"));
      // e.currentTarget.classList.add("la-panel-focus");
    });
  },
  methods: {
    handleCollapse() {
      if (this.state === "closed") {
        this.state = "opened";
        this.$emit('opened');
      } else {
        this.state = "closed";
        this.$emit('closed');
      }
    },
    handleMaximize() {
      // 允许外部手动触发放大
      this.state = this.state === "maximize" ? "" : "maximize";
    },

    handleDblClick() {
      // 限制双击放大的触发条件
      if (!this.maxable) {
        return;
      }
      this.handleMaximize();
    }
  }
};
</script>
