<template>
  <setting-panel :scrollbar="false">
    <ul class="view-setting" :class="{ disable: false }">
      <li class="top" app-border dir-bottom>
        <div class="title">热点设置</div>
        <div class="ui-remark">在全景图中添加图标热点，并设置热点的效果。</div>
        <button
          class="ui-button submit"
          :class="{ disable: !activeItem }"
          @click="open(null)"
        >
          添加热点
        </button>
      </li>
      <li app-border class="list">
        <div class="tips" v-if="someData.hotspots.length>0">当前全景图热点（{{ someData.hotspots.length }}）</div>
        <div class="hots">
          <ul v-if="someData.hotspots.length > 0">
            <li
              v-for="(item, key) in someData.hotspots"
              :key="key"
              @click="open(item)"
            >
              <div>
                <i class="style" :class="{ default: true }">
                  <em
                    :style="{
                      'background-image': require('@/assets/images/img_panorama_dot.png'),
                    }"
                    v-show="item.styleId == null"
                  ></em>
                </i>
                <span>{{ item.hotspotTitle }}</span>
              </div>
              <i class="iconfont icon_delete" @click.stop="deleIndex = key"></i>
              <a
                href="javascript:;"
                v-clickoutside="clickoutside"
                :class="{ active: deleIndex == key }"
                @click.stop="deleteHot(item)"
                >删除</a
              >
            </li>
          </ul>
        </div>
      </li>
    </ul>
    <eidt-panel
      v-if="showPanel"
      :editTitle="editTitle"
      @save="save"
      @select="
        (data) => {
          $emit('select', data);
        }
      "
      :show="showPanel"
      @close="close"
    ></eidt-panel>
  </setting-panel>
</template>
<script>
import EidtPanel from "./EditPanel";
import { mapGetters } from "vuex";
import browser from "@/utils/browser";
export default {
  components: {
    EidtPanel,
  },
  computed: {
    ...mapGetters({
      activeItem: "activeItem",
      hotspot:'hotspot',
      info:"info"
    }),
  },
  data() {
    return {
      editLink: "",
      showPanel: false,
      someData: { hotspots: [] },
      deleIndex: -1,
      editTitle: "编辑",
    };
  },
  watch: {
    "$route.name": function() {
      this.showPanel = false;
    },
    activeItem: {
      immediate: true,
      handler: function(newVal) {
        this.someData = newVal.someData || "";
        this.someData = this.someData
          ? JSON.parse(this.someData)
          : { hotspots: [] };
      },
    },
    showPanel(newVal) {
      this.$store.commit("UpdateIsEditingState", newVal);
      this.$bus.emit("canEdit", !newVal);
    },
  },
  mounted() {
    this.$bus.on("openHotspot", (data) => {
      let idx = this.someData.hotspots.findIndex((item) => item.name == data);
      let beforeIdx = this.someData.hotspots.findIndex((item) => item.name == this.hotspot.name);
      if (beforeIdx == idx) {
        return
      }
      if (this.editTitle == '新增') {
        if (this.showPanel) {
          return this.$confirm({
              content: "热点内容未编辑完，确定要关闭吗",
              ok: () => {
                this.deleteKRHotspot(this.hotspot)
                this.open(this.someData.hotspots[idx]);
              }
          });
        }

      }

      this.open(this.someData.hotspots[idx]);

    });
  },
  methods: {
    deleteKRHotspot(data) {
      let krpano = document.getElementById("krpanoSWFObject");
      krpano.call("removehotspot(" + data.name + ",true);");
      krpano.call("removeplugin(" + ("tooltip_" + data.name) + ",true);");
    },
    close(data) {
      if (data) {
          if (data.type == 'edit') {
              this.deleteKRHotspot(data.data)
              this.$bus.emit('addhotspot',data.data)
              let idx = this.someData.hotspots.findIndex(item=>item.name == data.data.name)
              this.someData.hotspots[idx] =data.data
          }
          else{
              this.deleteKRHotspot(data.data)
          }
      }
      this.showPanel = false;
    },

    updateInfo(){
      let iidx = this.info.scenes.findIndex(item=>this.activeItem.sceneCode == item.sceneCode)
      if (iidx>-1) {
        this.info.scenes[iidx] = {
          ...this.activeItem
        }
      }
      this.$store.commit("SetInfo", this.info);
    },
    save(data) {
      var krpano = document.getElementById("krpanoSWFObject");
      let HV = window.__krfn.utils.getHotspotHV(krpano, data.name);
      data.ath = HV.ath;
      data.atv = HV.atv;
      let idx = this.someData.hotspots.findIndex((item) => item.name === data.name)
      if (idx<=-1) {
        this.someData.hotspots.push(data);
      }
      else{
        this.someData.hotspots[idx] = data
      }

      this.activeItem.someData = JSON.stringify(this.someData);
      this.$msg.success(this.editTitle + "成功")

      let iidx = this.info.scenes.findIndex(item=>this.activeItem.sceneCode == item.sceneCode)
      if (iidx>-1) {
        this.info.scenes[iidx] = {
          ...this.activeItem
        }
      }
      
      this.updateInfo()

    },
    deleteHot(data) {
      this.someData.hotspots.splice(
        this.someData.hotspots.findIndex((item) => item.name === data.name),
        1
      );
      this.deleteKRHotspot(data);
      this.activeItem.someData = JSON.stringify(this.someData);
      this.updateInfo()
      this.$msg.success("删除成功")
    },
    open(data) {
      this.editTitle = "新增";
      let temp = data ? browser.CloneObject(data) : {
        name: "_"+this.$randomWord(true,8,8),
        hotspotTitle:'',
        fontSize:12,
        type:'',
        img:'',
        link:'',
        visible: true,
        ath: '',
        atv: '',
        icontype: 'ditu',
        size:1,
        hotspotType:'scene',
        secne:'',
        hyperlink:'',
        textarea:'',
        image:[],
        audio:'',
        video:''
      }      

      this.$store.commit("SetHotspot", temp);
      this.showPanel = true;

      if (data) {
        this.editLink = temp.link;
        this.editTitle = "编辑";
        var krpano = document.getElementById("krpanoSWFObject");
        window.__krfn.utils.looktohotspot(krpano, data.name);
      }
    },
    clickoutside() {
      if (this.deleIndex > -1) {
        this.deleIndex = -1;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.view-setting {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 0!important;
  li {
    display: flex;
    &.top{
      flex-direction: column;
    }
    &.list {
      position: relative;
      flex: 1;
      padding: 0;
      width: 100%;
      flex-direction: column;
      > div {
        margin: 0;
        display: block;
      }
    }
    .ui-remark{
        margin-bottom: 10px;
    }
  }

  .tips {
    padding: 10px;
  }

  .hots {
    padding-top: 40px;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    ul {
      width: 100%;
      max-height: 100%;
      overflow-y: auto;
      overflow-x: hidden;
    }
    li {
      cursor: pointer;
      position: relative;
      height: 40px;
      line-height: 40px;
      padding: 0 10px;
      background: #373938;
      margin-bottom: 10px;
      &::after {
        display: none;
        content: "";
        position: absolute;
        width: 4px;
        height: 100%;
        right: 0;
        top: 0;
        background-color: @color;
      }
      &:hover {
        &::after {
          display: block;
        }
      }

      > div {
        position: relative;
        width: 100%;
        height: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        padding-left: 25px;
        padding-right: 5px;
      }
      a {
        position: absolute;
        top: 0;
        right: -100%;
        width: 82px;
        height: 100%;
        line-height: 40px;
        color: #fff;
        text-align: center;
        text-decoration: none;
        background-color: @color;
        box-shadow: -5px 0px 20px rgba(0, 0, 0, 0.5);
        z-index: 10;
        opacity: 0;
        transition: right 0.3s, opacity 0.1s;
        &.active {
          right: 0px;
          opacity: 1;
        }
      }
      .style {
        position: absolute;
        left: 0;
        top: 50%;
        width: 20px;
        height: 20px;
        transform: translateY(-50%);
        background-position: center center;
        background-size: contain;
        background-repeat: no-repeat;
        border-radius: 50%;
        &.default {
          border: solid 1px rgba(255, 255, 255, 0.1);
          em {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 80%;
            height: 80%;
            border: solid 0.5px #fff;
            background: @color url("~@/assets/images/img_panorama_dot.png")
              no-repeat;
            background-size: 80% 80%;
            background-position: center center;
            border-radius: 50%;
          }
        }
      }
      .icon_delete {
        font-size: 25px;
        padding: 0 5px;
        color: #999;
        &:hover {
          color: #fff;
        }
      }
    }
  }

  .ui-button {
    width: 100%;
  }
}
</style>
