<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="80px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="课程名称" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="岗位培训类型"
              prop="POSTTYPE"
              label-width="120"
            >
              <layout-learning-train-type
                v-model="searchForm.POSTTYPE"
                type="post"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="课件状态" prop="STATE">
              <el-select v-model="searchForm.STATE">
                <el-option
                  v-for="item in stateList"
                  :key="item.ID"
                  :label="item.NAME"
                  :value="item.ID"
                />
              </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 label="培训行业类型">
          <template v-slot="{ row }">
            <span v-if="row.CURRENCY === '1'">通用</span>
            <span v-else>{{ row.train_type_name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="teacherName" label="讲师名称" />
        <el-table-column
          prop="CLASSHOUR"
          label="课件学时（分钟）"
          width="120"
        />
        <el-table-column prop="CREATTIME" label="上传时间" width="140" />
        <el-table-column label="课件状态" width="100">
          <template v-slot="{ row }">
            {{ translationStatus(row.STATE, stateList) }}
          </template>
        </el-table-column>
        <el-table-column label="课件来源" width="100">
          <template v-slot="{ row }">
            <span v-if="row.CURRICULUM_ID_REMOTE">商城</span>
            <span v-else>自传</span>
          </template>
        </el-table-column>
        <el-table-column prop="NUMEXAMPLE" label="习题数" width="100" />
        <el-table-column label="操作" width="200">
          <template v-slot="{ row }">
            <el-button type="primary" text link @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: {
                    COURSEWAREID: row.VIDEOCOURSEWARE_ID,
                    COURSEWARETYPE: '1',
                  },
                })
              "
            >
              课件习题
            </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 LayoutLearningTrainType from "@/components/learning_train_type/index.vue";
import {
  getVideoCoursewareList,
  setVideoCoursewareJoin,
} from "@/request/platform_resource_library.js";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import { serialNumber, translationStatus } from "@/assets/js/utils.js";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import LayoutVideo from "@/components/video/index.vue";
import useAliYunVideoPreview from "@/assets/js/useAliYunVideoPreview.js";

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

const stateList = [
  { ID: 0, NAME: "启用" },
  { ID: 1, NAME: "禁用" },
];
const data = reactive({
  videoDialog: {
    src: "",
    vid: "",
    playAuth: "",
    visible: false,
  },
});
const fnAdd = debounce(1000, async (VIDEOCOURSEWARE_ID) => {
  await ElMessageBox.confirm("确定要添加吗？", { type: "warning" });
  await setVideoCoursewareJoin({ COURSEWARE_ID: VIDEOCOURSEWARE_ID });
  ElMessage.success("添加成功");
  fnResetPagination();
});
const fnPreviewVideo = async (row) => {
  const { src, vid, playAuth, visible } = await useAliYunVideoPreview(row);
  data.videoDialog.src = src;
  data.videoDialog.vid = vid;
  data.videoDialog.playAuth = playAuth;
  data.videoDialog.visible = visible;
};
</script>

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