<template>
  <div class="app-container">
    <div style="padding-bottom: 20px">
      <span v-if="courseName!=null">{{ courseName }}</span>
    </div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小节名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入小节名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="userCourseVideoList">
      <el-table-column label="视频ID" align="center" prop="videoId" />
      <el-table-column label="小节名称" align="center" show-overflow-tooltip prop="title" />
      <el-table-column label="视频文件名称" align="center" show-overflow-tooltip  prop="fileName" >
      </el-table-column>
      <el-table-column label="视频时长" align="center" prop="duration">
        <template slot-scope="{ row }">
          {{ formatDuration(row.duration) }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="courseSort" />
      <el-table-column label="上传时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="chooseVideo(scope.row)"
          >选择此课</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { getVideoListByCourseId } from "@/api/course/userCourseVideo";
export default {
  name: "userCourseVideo",

  data() {
    return {
      uploadLoading:false,
      courseId:null,
      videoName:'',
      title: "",
      // 是否显示弹出层
      open: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      files:[],
      fileList: [],
      // 上传成功后的地址
      videoURL: '',
      // 进度条百分比
      progress: 0,
      // 上传视频获取成功后拿到的fileID【备用】
      fileId: '',
      courseName:null,
      userCourseVideoList:[],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 7,
        courseId:null,
        title:null
      },
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "小节名称不能为空", trigger: "change" }
        ],
        courseSort: [
          { required: true, message: "排序不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    formatDuration(seconds) {
      if (seconds === null || seconds === undefined) {
        return '未上传视频'; // 或者您可以根据具体需求返回其他默认值
      }
      const hours = Math.floor(seconds / 3600);
      const minutes = Math.floor((seconds % 3600) / 60);
      const remainingSeconds = seconds % 60;

      const formattedHours = hours > 0 ? hours.toString() + ':' : '';
      const formattedMinutes = minutes.toString().padStart(2, '0');
      const formattedSeconds = remainingSeconds.toString().padStart(2, '0');

      return `${formattedHours}${formattedMinutes}:${formattedSeconds}`;
    },

    getList() {
      this.loading = true;
      getVideoListByCourseId(this.queryParams).then(response => {
        this.userCourseVideoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    setQueryParams(val){
      this.queryParams.courseId = val;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        videoId: null,
        title: null,
        description: null,
        url: null,
        thumbnail: null,
        duration: null,
        createTime: null,
        userId: null,
        cateId: null,
        courseId: null,
        likes: null,
        views: null,
        comments: null,
        status: 0,
        courseSort: null,
        isHot: null,
        isShow: null,
        isAudit: null,
        auditBy: null,
        auditTime: null,
        updateTime: null,
        source: null,
        isDel: null,
        shares: null,
        tags: null,
        productId: null,
        productJson: null
      };
      this.videoURL = '';
      this.progress=0;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //选择课程
    chooseVideo(val){
      this.$emit('choose-video',val)
    },
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

</style>
