<template>
  <div class="base_form">
    <div class="label">
      <img class="pre-tag" src="../../../assets/images/template/pre-tag.png" />
      <div class="label-text">{{ property.label }}</div>
    </div>
    <div>
      <div class="up" @click="upFile">
        <i class="iconfont iconyinpin2"></i>
        上传音频
      </div>
      <div class="show" v-for="(item, index) in localObjects" :key="index">
        <i class="iconfont iconyinpin-copy"></i>
        <p @click="play(item)">{{ item.name }}</p>
        <div class="show-rigth">
          <i class="iconfont iconxingzhuang2"></i>上传成功
          <i class="iconfont iconshanchu1" @click="deleteAudios(index)"></i>
        </div>
      </div>
    </div>
    <input
      type="file"
      hidden
      ref="inputDom"
      accept="audio/*"
      @change="fileChange"
    />
    <common-dialog
      v-if="showAudio"
      @closePopup="showAudio = false"
      showMask
      @ClickingBackground="showAudio = false"
    >
      <div class="audio-box">
        <audio controls="controls" style="outline: none" :src="Aurl"></audio>
      </div>
    </common-dialog>
  </div>
</template>

<script>
import { GO_URLS } from "@/config/URLS";
import CommonDialog from "@/components/CommonDialog.vue";
export default {
  props: ["property"],
  data() {
    return {
      localObjects: [],
      localHash: [], // 本地hash
      showAudio: false,
      Aurl: "",
    };
  },
  components: {
    CommonDialog,
  },
  mounted() {
    if (this.property.value.length > 0) {
      let hashes = [];
      for (let index = 0; index < this.property.value.length; index++) {
        const element = this.property.value[index];
        hashes.push(element.value);
      }
      if (hashes.length === this.property.value.length) {
        this.getAudios(hashes);
      }
    }
  },
  watch: {
    localHash: {
      handler(newV, oldV) {
        this.property.value = newV;
      },
      deep: true,
    },
  },
  methods: {
    play(item) {
      this.Aurl = item.url;
      this.showAudio = true;
    },
    deleteAudios(index) {
      this.localHash.splice(index, 1);
      this.localObjects.splice(index, 1);
      this.$message({
        type: "success",
        message: "删除成功!",
      });
    },
    // 上传文件
    upFile() {
      this.$refs.inputDom.click();
    },
    //  获取旧文件
    async getAudios(hashes) {
      const data = await this.$ajax({
        type: "post",
        params: { hashes },
        url: GO_URLS.getFiles,
      });
      if (data) {
        this.localObjects = data.data.files;
        for (let index = 0; index < data.data.files.length; index++) {
          const element = data.data.files[index];
          this.localHash.push(element.hash);
        }
      }
    },
    async fileChange(e) {
      const file = e.target.files[0];
      if (!file) return;
      if (file.size > 10485760) {
        this.$message({
          message: "上传文件超过最大限制",
          type: "warning",
        });
        return;
      }
      const formData = new FormData();
      formData.append("file_data", file);
      const res = await this.$ajax({
        type: "post",
        url: GO_URLS.upload + `?file_type=mp3`,
        params: formData,
      });
      if (res) {
        // 上传成功
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.localHash.push(res.data.hash);
        this.localObjects.push(res.data);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.base_form {
  flex-shrink: 0;
  padding-left: 18px;
  font-size: 0;
  padding-top: 10px;
  padding-bottom: 10px;
  display: flex;
  justify-content: flex-start;
}
.label {
  display: inline-block;
  width: 150px;
  flex-shrink: 0;
  cursor: pointer;
  color: #797d84;
  line-height: 44px;
  font-size: 14px;
  font-weight: 300;
  position: relative;
}
.pre-tag {
  position: absolute;
  left: -30px;
  top: 14px;
  display: none;
}
.label-text {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.up {
  width: 151px;
  height: 36px;
  cursor: pointer;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  text-align: center;
  line-height: 36px;
  color: rgba(121, 125, 132, 1);
  background: #f6f6f7;
  border: 1px solid #DCDFE6;
  .iconyinpin2 {
    font-size: 24px;
    vertical-align: sub;
  }
}
.show {
  line-height: 1;
  padding-top: 20px;
  padding-bottom: 4px;
  width: 800px;
  p {
    font-size: 14px;
    margin: 0;
    display: inline-block;
    padding: 0;
    cursor: pointer;
    font-weight: 400;
    color: #353535;
  }
  .iconyinpin-copy {
    color: #33c8ff;
    font-size: 16px;
    margin-right: 6px;
  }
  .show-rigth {
    float: right;
    font-size: 14px;
    .iconshanchu1 {
      color: #b6b5ba;
      font-size: 12px;
      margin-left: 10px;
      cursor: pointer;
    }
    .iconxingzhuang2 {
      margin-right: 2px;
      color: #33c724;
    }
  }
}
.audio-box {
  padding: 20px;
}
</style>