<template>
  <div class="task-preview">
    <MenuPage only-key="/personal_workbench/edit_task" />

    <CommonPreview :id="id" ref="preview" :project-id="project_id">
      <template #operator="{ courseware }">
        <span class="link" @click="saveCoursewareAsTemplate">保存为个人模板</span>
        <span v-if="isTrue(courseware.is_can_start_edit)" class="link" @click="editTask">开始编辑</span>
        <span v-if="isTrue(courseware.is_can_submit_audit)" class="link" @click="submitCoursewareToAuditFlow">
          提交审校
        </span>
        <span class="link" @click="goBackBookList">返回教材列表</span>
      </template>
    </CommonPreview>
  </div>
</template>

<script>
import MenuPage from '@/views/personal_workbench/common/menu.vue';
import CommonPreview from '@/components/CommonPreview.vue';

import { SubmitBookCoursewareToAuditFlow } from '@/api/project';
import { isTrue } from '@/utils/validate';
import { SaveCoursewareAsTemplatePersonal } from '@/api/template';

export default {
  name: 'TaskPreviewPage',
  components: {
    MenuPage,
    CommonPreview,
  },
  data() {
    return {
      id: this.$route.params.id,
      project_id: this.$route.query.project_id,
      isTrue,
    };
  },
  methods: {
    goBackBookList() {
      this.$router.push({ path: '/personal_workbench/edit_task' });
    },
    editTask() {
      this.$router.push({
        path: `/personal_workbench/edit_task/edit/${this.$refs.preview.select_node}`,
        query: { project_id: this.project_id },
      });
    },
    /**
     * 提交课件到审校流程
     */
    submitCoursewareToAuditFlow() {
      SubmitBookCoursewareToAuditFlow({ id: this.$refs.preview.select_node }).then(() => {
        this.$message.success('课件已提交审校');
        this.$refs.preview.getBookCoursewareInfo(this.$refs.preview.select_node);
      });
    },
    /**
     * 保存为个人模板
     */
    saveCoursewareAsTemplate() {
      this.$confirm('确定要将当前课件保存为个人模板吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          SaveCoursewareAsTemplatePersonal({ courseware_id: this.$refs.preview.select_node }).then(() => {
            this.$message.success('已保存为个人模板');
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
@use '@/styles/mixin.scss' as *;

.task-preview {
  @include page-content(true);
}
</style>
