<template>
  <div>
    <el-form-item label="视频上传">
      <div class="upload_video" id="upload_video">
        <el-upload
          class="upload-demo"
          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>
          <!-- 仅当showControl为true时显示视频库选取按钮 -->
          <el-button v-if="showControl" style="margin-left: 10px;" size="small" type="warning" @click="openVideoLibrary">素材库选取</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 v-if="uploadLoading" class="el-upload__tip" style="color:#E6A23C">处理中，请稍候…</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>-->
<!--        &lt;!&ndash;        <el-radio :label="3" >线路三</el-radio>&ndash;&gt;-->
<!--      </el-radio-group>-->
<!--    </el-form-item>-->

    <!-- 视频库选择对话框 -->
    <el-dialog title="视频库选择" :visible.sync="libraryOpen" width="900px" append-to-body>
      <!-- 搜索条件 -->
      <el-form :inline="true" :model="libraryQueryParams" class="library-search">
        <el-form-item label="素材名称">
          <el-input
            v-model="libraryQueryParams.resourceName"
            placeholder="请输入素材名称"
            clearable
            size="small"
            @keyup.enter.native="handleLibraryQuery"
          />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="libraryQueryParams.typeId" @change="changeCateType" placeholder="请选择素材类型" clearable size="small">
            <el-option
              v-for="item in typeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="子类型">
          <el-select v-model="libraryQueryParams.typeSubId" placeholder="请选择素材子类型" clearable size="small">
            <el-option
              v-for="item in typeSubOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleLibraryQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetLibraryQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 视频列表 -->
      <el-table v-loading="libraryLoading" :data="libraryList" @row-click="handleLibrarySelect" highlight-current-row border>
        <el-table-column label="素材名称" align="center" prop="resourceName" />
        <el-table-column label="文件名称" align="center" prop="fileName" />
        <el-table-column label="缩略图" align="center">
          <template slot-scope="scope">
            <el-popover
              placement="right"
              title=""
              trigger="hover"
            >
              <img alt="" slot="reference" :src="scope.row.thumbnail" style="width: 80px; height: 50px" />
              <img alt="" :src="scope.row.thumbnail" style="max-width: 150px;" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="视频时长" align="center">
          <template slot-scope="scope">
            <span>{{ formatDuration(scope.row.duration) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="libraryTotal>0"
        :total="libraryTotal"
        :page.sync="libraryQueryParams.pageNum"
        :limit.sync="libraryQueryParams.pageSize"
        @pagination="getLibraryList"
      />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmVideoSelection">确 定</el-button>
        <el-button @click="cancelVideoSelection">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getSignature, uploadHuaWeiObs, uploadHuaWeiVod} from "@/api/common";
import {getThumbnail} from "@/api/course/userVideo";
import TcVod from "vod-js-sdk-v6";
import { uploadObject } from "@/utils/cos.js";
import { uploadToOBS } from "@/utils/obs.js";
import Pagination from "@/components/Pagination";
import { listVideoResource, listPublicVideoResource } from '@/api/course/videoResource';
import { getCateListByPid, getCatePidList, getPublicCateListByPid, getPublicCatePidList } from '@/api/course/userCourseCategory'

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,
    },
    isTranscode: {
      type: Number,
      default: null,
    },
    transcodeFileKey: {
      type: String,
      default: "",
    },

    // 使用一个变量控制显示，默认为true显示所有控制项
    showControl: {
      type: Boolean,
      default: true,
    },
    /** true：视频库选取走公域分类 + /course/videoResource/publicList；false：原接口 */
    usePublicVideoLibrary: {
      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,
      // 视频库选择相关数据
      libraryOpen: false,
      libraryLoading: false,
      libraryTotal: 0,
      libraryList: [],
      selectedVideo: null,
      libraryQueryParams: {
        isTranscode:1,
        pageNum: 1,
        pageSize: 10,
        resourceName: null,
        typeId: null,
        typeSubId: null,
      },
      typeOptions: [],
      typeSubOptions: []
    };
  },
  watch: {
    localUploadType(newType) {
      this.$emit("update:uploadType", newType);
      this.$emit("change");
    },
    uploadType(newType) {
      this.localUploadType = newType;
    },
  },
  methods: {
    // 打开视频库对话框
    openVideoLibrary() {
      this.libraryOpen = true;
      this.getRootTypeList()
      this.selectedVideo = null;
      this.getLibraryList();
    },
    getRootTypeList() {
      const req = this.usePublicVideoLibrary ? getPublicCatePidList() : getCatePidList()
      req.then(response => {
        this.typeOptions = response.data
      });
    },
    async changeCateType(val) {
      this.libraryQueryParams.typeSubId = null
      this.typeSubOptions = []
      if (!val) {
        return
      }
      const subReq = this.usePublicVideoLibrary ? getPublicCateListByPid(val) : getCateListByPid(val)
      await subReq.then(response => {
        this.typeSubOptions = response.data
      })
    },
    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("请先选取视频，再进行上传");
      }
      this.uploadLoading = true;
      this.txProgress = 0;
      try {
        await this.getFirstThumbnail();
        // 本地上传展示进度（线路一 COS）
        await this.uploadVideoToTxPcdn();
        // 华为线路可选，失败不阻断主流程
        try {
          await this.uploadVideoToHwObs();
        } catch (e) {
          console.warn('线路二上传失败', e);
        }
        this.$emit("update:fileName", this.fileList[0].name);
        this.$emit("video-ready");
      } finally {
        this.uploadLoading = false;
      }
    },
    //获取第一帧封面
    async getFirstThumbnail(){
      const file = this.fileList[0].raw;
      getThumbnail(file).then(response => {
        console.log("获取到第一帧为封面======>",response.url)
        this.$emit("update:thumbnail", response.url);
      })
    },
    //更新华为线路进度条
    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}`;
        }

        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("update:line_2", line_2);
        this.$emit("change");
        this.$message.success("线路一上传成功");
      } catch (error) {
        this.$message.error("线路一上传失败");
        throw 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", data);
        this.$emit("update:line_2", line_2);
        this.$message.success("线路二上传成功");
      } catch (error) {
        this.$message.error("线路二上传失败");
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList.length);
    },
    resetUpload() {
      // 重置内部状态
      this.txProgress = 0;
      this.hwProgress = 0;
      this.fileList = [];
      this.uploadKey++;
    },
    /** 查询视频库列表 */
    getLibraryList() {
      this.libraryLoading = true;
      const api = this.usePublicVideoLibrary ? listPublicVideoResource : listVideoResource
      api(this.libraryQueryParams).then(response => {
        this.libraryList = response.rows;
        this.libraryTotal = response.total;
        this.libraryLoading = false;
      });
    },
    /** 搜索视频库按钮操作 */
    handleLibraryQuery() {
      this.libraryQueryParams.pageNum = 1;
      this.getLibraryList();
    },
    /** 重置视频库查询按钮操作 */
    resetLibraryQuery() {
      this.libraryQueryParams = {
        pageNum: 1,
        pageSize: 10,
        resourceName: null,
        typeId: null,
        typeSubId: null
      };
      this.handleLibraryQuery();
    },
    /** 视频库选择行点击 */
    handleLibrarySelect(row) {
      this.selectedVideo = row;
    },
    /** 格式化视频时长 */
    formatDuration(seconds) {
      if (!seconds) return '00:00';

      const minutes = Math.floor(seconds / 60);
      const remainingSeconds = seconds % 60;

      return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
    },
    /** 确认选择视频 */
    confirmVideoSelection() {
      if (!this.selectedVideo) {
        this.$message.warning("请选择一个视频");
        return;
      }

      // 更新组件内部数据
      this.$emit("update:fileName", this.selectedVideo.fileName);
      this.$emit("update:thumbnail", this.selectedVideo.thumbnail);
      this.$emit("update:line_1", this.selectedVideo.line1);
      this.$emit("update:line_2", this.selectedVideo.line2);
      this.$emit("update:line_3", this.selectedVideo.line3);
      this.$emit("update:fileSize", this.selectedVideo.fileSize);
      this.$emit("update:fileKey", this.selectedVideo.fileKey);
      this.$emit("update:uploadType", this.selectedVideo.uploadType);
      this.$emit("video-duration", this.selectedVideo.duration);
      this.$emit("update:isTranscode", this.selectedVideo.isTranscode);
      this.$emit("update:transcodeFileKey", this.selectedVideo.transcodeFileKey);

      // 设置预览URL
      this.$emit("update:videoUrl", this.selectedVideo.videoUrl);

      // 题目
      this.$emit("selectProjects", this.selectedVideo.projectIds)
      this.$emit("video-ready");

      this.libraryOpen = false;
    },
    /** 取消视频选择 */
    cancelVideoSelection() {
      this.libraryOpen = false;
      this.selectedVideo = null;
    }
  }
};
</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>
