<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>

    </ul>

    <upload
      ref="uploadFile"
      :failString="'格式错误，请重新上传'"
      :limitFailStr="'过大，请重新上传'"
      accept-type="audio/mp3"
      media-type="audio"
      :limit="20"
      @file-change="onFileChange"
    ></upload>

    <eidt-panel
      v-if="showPanel"
      @save="save"
      :data="musicInfo"
      @delmusic="()=>{musicInfo.music = {url:'',name:''}}"
      @updateMD="data=>{musicInfo.musicDesc = data}"
      @openMaterial="handleUpload"
      :show="showPanel"
      @close="close"
    ></eidt-panel>
  </setting-panel>
</template>
<script>
import EidtPanel from "./EditPanel";
import { mapGetters } from "vuex";
import Upload from "@/components/shared/uploads/Upload";
import { uploadFileBySceneCode } from "@/api";
import { $waiting } from '@/components/shared/loading'

let cacheMusic = {
    musicDesc: "",
    music: "",
  }

export default {
  components: {
    EidtPanel,
    Upload
  },
  computed: {
    ...mapGetters({
      activeItem: "activeItem",
      info: "info",
      backupInfo: "backupInfo",
    }),
  },
  data() {
    return {
      editLink: "",
      showPanel: false,
      musicInfo:{},
    };
  },
  watch: {
    "$route.name": function() {
      this.showPanel = false;
    },
    activeItem: {
      immediate: true,
      handler: function(newVal) {
        this.musicInfo = newVal.musicInfo || {
          musicDesc: "",
          music: {url:'',name:''},
        };
      },
    },
    showPanel(newVal) {
      this.$store.commit("UpdateIsEditingState", newVal);
      this.$bus.emit("canEdit", !newVal);
    },
  },
  mounted() {
   
  },
  methods: {
    onFileChange(file) {
      this.musicInfo.music = {
        url: URL.createObjectURL(file),
        name: file.name
      };
      $waiting.show()
      uploadFileBySceneCode(
          {
            sceneCode:this.activeItem.sceneCode,
            file: file,
          },
          (data) => {
            this.musicInfo.music.url = this.$cdn + data.data.filePath + `?_=${Math.random()}`
           $waiting.hide()
          }
        );
    },
    handleUpload() {
      this.$nextTick(() => {
        this.$refs.uploadFile.click();
      });
    },
   
    close(data) {
      if (data) {
        this.musicInfo = cacheMusic
      }
      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) {
      this.activeItem.musicInfo = {
        ...data
      };

      this.updateInfo()
      this.$msg.success("保存成功")
    },
  
    open() {
      cacheMusic = {...this.activeItem.musicInfo}
      this.showPanel = true;
    },
    
  },
};
</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>
