<template>
  <div class="base_form">
    <div class="label">
      <Icon iconName="icondd" class="pre-tag" size="18"></Icon>
      <div class="label-text">{{ property.label }}</div>
    </div>
    <div class="flex-grow">
      <div class="up flex items-center justify-center" @click="upFile">
        <i class="iconfont iconyinpin2"></i>
        &nbsp;上传音频
      </div>
      <div
        class="show w-full"
        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="true" style="outline: none" :src="Aurl"></audio>
      </div>
    </common-dialog>
  </div>
</template>

<script lang="ts">
import { GO_URLS, syCommonDialog as CommonDialog } from "cqk-sy-ui";
import Icon from "@/components/Icon/index.vue";
import { defineComponent, PropType } from "vue";
import { ElMessage } from "element-plus";
import { $ajax } from "@/service";
export default defineComponent({
  inject: ["showTip"],
  props: {
    property: {
      type: Object as PropType<any>,
      required: true,
    },
    setPropertyValue: {
      type: Function as PropType<any>,
      required: true,
    },
  },
  data() {
    return {
      localObjects: [] as any,
      localHash: [] as any, // 本地hash
      showAudio: false,
      Aurl: "",
    };
  },
  components: {
    CommonDialog,
    Icon,
  },
  mounted() {
    if (Array.isArray(this.property.value)) {
      // this.property.value = this.property.value.filter(
      //   (item) => item.value !== ""
      // );
      this.setPropertyValue(
        this.property.value.filter((item) => item.value !== "")
      );
    }
    if (this.property.value.length > 0) {
      let hashes = [] as any;
      for (let index = 0; index < this.property.value.length; index++) {
        const element = this.property.value[index];
        if (element.value) {
          hashes.push(element.value);
        }
      }
      if (hashes.length === this.property.value.length) {
        this.getAudios(hashes);
      }
    }
  },
  watch: {
    localHash: {
      handler(newV, oldV) {
        // this.property.value = newV;
        this.setPropertyValue(newV);
      },
      deep: true,
    },
  },
  methods: {
    play(item) {
      this.Aurl = item.url;
      this.showAudio = true;
    },
    deleteAudios(index) {
      (this as any).showTip(
        "此操作将永久删除该文件, 是否继续?",
        () => {
          this.localHash.splice(index, 1);
          this.localObjects.splice(index, 1);
          ElMessage({
            type: "success",
            message: "删除成功!",
          });
        },
        () => {
          ElMessage({
            type: "info",
            message: "已取消删除",
          });
        }
      );
    },
    // 上传文件
    upFile() {
      (this.$refs as any).inputDom.click();
    },
    //  获取旧文件
    async getAudios(hashes) {
      const data = await $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];
      e.target.value = null; //清空值
      if (!file) return;
      // 限制相同文件的上传
      const state = this.localObjects.some((item) => item.name === file.name);
      if (state) {
        ElMessage({
          message: "不可重复上传该音频文件",
          type: "warning",
        });
        return;
      }
      if (file.size > 10485760) {
        ElMessage({
          message: "上传音频大小不可超过10M",
          type: "warning",
        });
        return;
      }
      const formData = new FormData();
      formData.append("file_data", file);
      const res = await $ajax({
        type: "post",
        url: GO_URLS.upload + `?file_type=mp3`,
        params: formData,
      });
      if (res) {
        // 上传成功
        ElMessage({
          message: "上传成功",
          type: "success",
        });
        this.localHash.push(res.data.hash);
        this.localObjects.push(res.data);
      }
    },
  },
});
</script>

<style lang="scss" 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: 100px;
  flex-shrink: 0;
  cursor: pointer;
  color: #797d84;
  line-height: 44px;
  font-size: 14px;
  font-weight: 300;
  position: relative;
}
.pre-tag {
  position: absolute;
  left: -50px;
  top: 14px;
  display: none;
}
.label-text {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.up {
  box-sizing: content-box;
  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;
  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>
