<template>
  <div>
    <el-form-item label="视频上传">
      <div class="upload_video" id="upload_video">
        <el-upload
          ref="upload"
          action="#"
          :http-request="uploadVideoToTxPcdn"
          accept=".mp4"
          :limit="1"
          :on-remove="handleRemove"
          :on-change="handleChange"
          :auto-upload="false"
          :key="uploadKey"
        >
          <el-button slot="trigger" size="small" type="primary">选取视频</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">点击上传</el-button>
          <!-- 线路一 -->
          <div class="progress-container" style="width:100% !important;">
            <span class="progress-label">线路一</span>
            <el-progress
              style="margin-top: 5px;"
              class="progress"
              :text-inside="true"
              :stroke-width="18"
              :percentage="txProgress"
              status="success">
            </el-progress>
          </div>

          <!-- 线路二 -->
<!--          <div class="progress-container">-->
<!--            <span class="progress-label">线路er</span>-->
<!--            <el-progress-->
<!--              style="margin-top: 5px;"-->
<!--              class="progress"-->
<!--              :text-inside="true"-->
<!--              :stroke-width="18"-->
<!--              :percentage="hwProgress"-->
<!--              status="success">-->
<!--            </el-progress>-->
<!--          </div>-->
          <div slot="tip" class="el-upload__tip">只能上传mp4文件，且不超过500M</div>
        </el-upload>
      </div>
    </el-form-item>
    <el-form-item label="视频播放">
      <video v-if="videoUrl" ref="myvideo" :src="videoUrl" id="video" width="100%" height="300px" controls></video>
      <div v-if="fileName">视频文件名: {{ fileName }}</div>
      <div v-if="fileKey">文件Key: {{ fileKey }}</div>
      <div v-if="fileSize">文件大小(MB): {{ (fileSize / (1024 * 1024)).toFixed(2) }} MB</div>
    </el-form-item>
    <!-- 仅当showControl为true时显示播放线路选择器 -->
    <el-form-item v-if="showControl" label="播放线路">
      <el-radio-group v-model="localUploadType">
        <el-radio :label="1" >线路一</el-radio>
<!--        <el-radio :label="2" >线路二</el-radio>-->
        <!--        <el-radio :label="3" >线路三</el-radio>-->
      </el-radio-group>
    </el-form-item>

  </div>
</template>

<script>
import { uploadObject } from "@/utils/cos.js";
import Pagination from "@/components/Pagination";

export default {
  components: {
    Pagination
  },
  props: {
    videoUrl: {
      type: String,
      default: "",
    },
    fileKey: {
      type: String,
      default: "",
    },
    fileSize: {
      type: Number,
      default: null,
    },
    fileName: {
      type: String,
      default: "",
    },
    line_1: {
      type: String,
      default: "",
    },
    line_2: {
      type: String,
      default: "",
    },
    line_3: {
      type: String,
      default: "",
    },
    thumbnail: {
      type: String,
      default: "",
    },
    uploadType: {
      type: Number,
      default: null,
    },
    type: {
      type: Number,
      default: null,
    },
    isPrivate: {
      type: Number,
      default: 0,
    },

    // 使用一个变量控制显示，默认为true显示所有控制项
    showControl: {
      type: Boolean,
      default: false,
    }
  },
  data() {
    return {
      videoName:'',
      isHidden : false,
      localUploadType: this.uploadType,
      duration: 0,
      fileId: "",
      uploadTypeOptions: [
        { dictLabel: "线路一", dictValue: 1 }, // 腾讯pcdn
        // { dictLabel: "线路二", dictValue: 2 }, // 华为云obs
        // { dictLabel: "华为云VOD", dictValue: 4 },
        // { dictLabel: "腾讯云VOD", dictValue: 5 },
      ],
      fileList: [],
      txProgress: 0,
      hwProgress: 0,
      uploadLoading: false,
      uploadKey: 0,
    };
  },
  mounted() {
    this.reset()
  },
  watch: {
    localUploadType(newType) {

      this.$emit("update:uploadType", newType);
    },
    uploadType(newType) {
      this.localUploadType = newType;
    },
  },
  methods: {
// 提供重置方法供父组件调用
    reset() {
      this.txProgress = 0;
      this.fileList = [];
      this.$refs.upload.clearFiles();
      // 重置其他状态...
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
      this.getVideoDuration(file.raw);
    },
    getVideoDuration(file) {
      const video = document.createElement("video");
      video.preload = "metadata";
      video.onloadedmetadata = () => {
        window.URL.revokeObjectURL(video.src);
        this.duration = parseInt(video.duration.toFixed(2));
        console.log("视频时长=========>",this.duration);
        this.$emit("video-duration", this.duration);
        console.log("文件大小=====>",file.size);
        this.$emit("update:fileSize", file.size);
      };
      video.src = URL.createObjectURL(file);
    },
    async submitUpload() {
      if (this.fileList.length < 1) {
        return this.$message.error("请先选取视频，再进行上传");
      }
      //同时上传个线路
      await this.uploadVideoToTxPcdn();
      // await this.uploadVideoToHwObs();
      this.$emit("update:fileName", this.fileList[0].name);
    },
    //更新华为线路进度条
    updateHwProgress(progress) {
      this.hwProgress = progress;
    },
    //更新腾讯线路进度条
    updateTxProgress(progressData) {
      this.txProgress = Math.round(progressData.percent * 100);
    },
    //上传腾讯云Pcdn
    async uploadVideoToTxPcdn() {
      try {
        const file = this.fileList[0].raw;
        const data = await uploadObject(file, this.updateTxProgress,this.type);
        console.log("腾讯COS返回========>",data);
        console.log("isPrivate=======>",this.isPrivate)
        let line_1='' ;
        if (this.isPrivate===0){
          line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
        }else {
          line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
        }
        line_1 = line_1.replace(/\.mp4$/, '.m3u8');

        let urlPathWithoutFirstSlash = data.urlPath.substring(1);
        this.$emit("update:fileKey", urlPathWithoutFirstSlash);

        console.log("文件key",urlPathWithoutFirstSlash);
        console.log("组装URL========>",line_1);
        this.$emit("update:videoUrl", line_1);
        this.$emit("update:line_1", line_1);
        this.$emit("change", line_1,line_1);
        // this.$emit("update:line_2", line_2);
        this.$message.success("线路一上传成功");
      } catch (error) {
        this.$message.error("线路一上传失败");
      }
    },
    // //上传华为云Obs
    // async uploadVideoToHwObs() {
    //   try {
    //     const file = this.fileList[0].raw;
    //     const data = await uploadToOBS(file, this.updateHwProgress,this.type);
    //     console.log("华为OBS返回========>",data);
    //     let line_2='' ;
    //     if (this.isPrivate===0){
    //       line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
    //     }else {
    //       line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
    //     }
    //     this.$emit("update:videoUrl", line_2);
    //     this.$emit("update:line_2", line_2);
    //     this.$message.success("线路二上传成功");
    //   } catch (error) {
    //     this.$message.error("线路二上传失败");
    //   }
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList.length);
    },
  }
};
</script>

<style>
.progress-container {
  margin-bottom: 5px; /* 进度条之间的间距 */
}

.progress-label {
  display: block;
  font-weight: bold;
  font-size: 13px;
  color: #303331; /* 标签颜色，可以根据需要调整 */
}

/* 视频库选择对话框样式 */
.library-search {
  margin-bottom: 15px;
}

.el-table .el-table__row:hover {
  cursor: pointer;
}
</style>
