<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="130px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="课程名称" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入课程名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="岗位培训类型" prop="POSTTYPE">
              <layout-learning-train-type
                v-model="searchForm.POSTTYPE"
                type="post"
                placeholder="请选择岗位培训类型"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="课件状态" prop="STATE">
              <el-select v-model="searchForm.STATE" placeholder="请选择状态">
                <el-option
                  v-for="item in data.courseStates"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        :data="list"
        @get-data="fnGetData"
        v-model:pagination="pagination"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="COURSEWARENAME"
          label="课件名称"
          show-overflow-tooltip
        />
        <el-table-column
          prop="post_type_name"
          label="岗位培训类型"
          show-overflow-tooltip
        />
        <el-table-column
          show-overflow-tooltip
          prop="train_type_name"
          label="培训行业类型"
        >
          <template v-slot="{ row }">
            <div v-if="row.CURRENCY === '1'">通用</div>
            <div v-else>{{ row.train_type_name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="teacherName" label="讲师名称" />
        <el-table-column prop="CLASSHOUR" label="课件学时（分钟）" />
        <el-table-column prop="CREATTIME" label="上传时间" />
        <el-table-column prop="STATE" label="课件状态">
          <template v-slot="{ row }">
            <template v-if="row.STATE === 0">
              <div>启用</div>
            </template>
            <template v-else-if="row.STATE === 1">
              <div>禁用</div>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="CURRICULUM_ID_REMOTE" label="课件来源">
          <template v-slot="{ row }">
            <template v-if="row.CURRICULUM_ID_REMOTE">
              <div>商城</div>
            </template>
            <template v-else>
              <div>自传</div>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="NUMEXAMPLE" label="习题数" />
        <el-table-column label="操作" width="200">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              text
              link
              v-if="
                (row.VIDEOCOURSEWARE_ID_REMOTE &&
                  row.VIDEOCOURSEWARE_ID_REMOTE.length > 0) ||
                (row.VIDEOFILES && row.VIDEOFILES.length > 0)
              "
              @click="fnPreviewVideo(row)"
            >
              预览
            </el-button>
            <el-button
              type="primary"
              text
              link
              :disabled="row.recordID !== '--'"
              @click="fnAdd(row.VIDEOCOURSEWARE_ID)"
            >
              添加
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/platform_resource_library/courseware/video/view',
                  query: {
                    COURSE_ID: row.VIDEOCOURSEWARE_ID, // 课件ID
                    COURSE_TYPE: '1', // 课件类型(1:视频课件、2:资料课件)
                  },
                })
              "
            >
              课件习题
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <layout-video
      :src="data.videoDialog.src"
      :vid="data.videoDialog.vid"
      :play-auth="data.videoDialog.playAuth"
      v-model:visible="data.videoDialog.visible"
    />
  </div>
</template>

<script setup>
import LayoutCard from "@/components/card/index.vue";
import LayoutLearningTrainType from "@/components/learning_train_type/index.vue";
import {
  getVideoCoursewareList,
  joinMyVideoCourseware,
} from "@/request/platform_resource_library.js";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import { serialNumber } from "@/assets/js/utils.js";
import LayoutTable from "@/components/table/index.vue";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import LayoutVideo from "@/components/video/index.vue";
import { getVideoAuth, getVideoSource } from "@/request/api.js";

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getVideoCoursewareList);

const data = reactive({
  courseStates: [
    { value: "0", label: "启用" },
    { value: "1", label: "禁用" },
  ],
  videoDialog: {
    src: "",
    vid: "",
    playAuth: "",
    visible: false,
  },
});

const fnAdd = debounce(1000, async (VIDEOCOURSEWARE_ID) => {
  await ElMessageBox.confirm("确定要添加吗？", { type: "warning" });
  await joinMyVideoCourseware({ COURSEWARE_ID: VIDEOCOURSEWARE_ID });
  ElMessage.success("添加成功");
  fnResetPagination();
});

const fnPreviewVideo = async (row) => {
  if (
    row.VIDEOCOURSEWARE_ID_REMOTE &&
    row.VIDEOCOURSEWARE_ID_REMOTE.length > 0
  ) {
    const respData = await getVideoSource({
      VIDEOCOURSEWARE_ID: row.VIDEOCOURSEWARE_ID_REMOTE,
      CURRICULUM_ID: row.CURRICULUM_ID_REMOTE,
    });
    if (respData && respData.result === "success") {
      data.videoDialog.playAuth = respData.playAuth;
      data.videoDialog.vid = row.VIDEOFILES;
      data.videoDialog.visible = true;
    }
  } else if (row.VIDEOFILES && row.VIDEOFILES.length > 0) {
    const respData = await getVideoAuth({
      videoId: row.VIDEOFILES,
    });
    if (respData && respData.result === "success") {
      data.videoDialog.playAuth = respData.playAuth;
      data.videoDialog.vid = row.VIDEOFILES;
      data.videoDialog.visible = true;
    }
  }
};
</script>

<style scoped lang="scss"></style>
