<template>
  <div class="org-final-preview">
    <ProjectMenu cur-key="org/final" />

    <CommonPreview ref="preview" :project-id="project_id">
      <template #operator="{ courseware }">
        <span class="link">查看项目信息</span>
        <span class="link">查看教材信息</span>
        <span v-if="isTrue(courseware.is_can_shangjia_book)" class="link" @click="openConfirmBookDialog">上架</span>
        <span
          v-if="isTrue(courseware.is_can_reject_shangjia_book_request)"
          class="link"
          @click="rejectShangjiaBookRequest"
        >
          驳回上架请求
        </span>
        <span v-if="isTrue(courseware.is_can_rollback_project)" class="link" @click="rollbackProject">退改</span>
        <span
          v-if="isTrue(courseware.is_can_reject_rollback_project_request)"
          class="link"
          @click="rejectRollbackProjectRequest"
        >
          驳回退改请求
        </span>
        <span class="link" @click="goBackBookList">返回项目列表</span>
      </template>
    </CommonPreview>

    <ConfirmBookInfo :project-id="project_id" :visible.sync="visible" @confirm="shangjiaBook" />
  </div>
</template>

<script>
import ProjectMenu from '@/views/project_manage/common/ProjectMenu.vue';
import CommonPreview from '@/components/CommonPreview.vue';
import ConfirmBookInfo from './components/ConfirmBookInfo.vue';

import { isTrue } from '@/utils/common';
import { ShangjiaBook, RollbackProject, RejectShangjiaBookRequest, RejectRollbackProjectRequest } from '@/api/project';

export default {
  name: 'OrgFinalPreview',
  components: {
    ProjectMenu,
    CommonPreview,
    ConfirmBookInfo,
  },
  data() {
    return {
      project_id: this.$route.params.projectId || '',
      isTrue,
      visible: false,
    };
  },
  methods: {
    openConfirmBookDialog() {
      this.visible = true;
    },
    shangjiaBook() {
      const loading = this.$loading('正在上架教材，请稍等...');
      ShangjiaBook({ project_id: this.project_id })
        .then(() => {
          this.$message.success('上架成功');
          this.$refs.preview.getProjectBaseInfo();
        })
        .finally(() => {
          loading.close();
          this.visible = false;
        });
    },
    rollbackProject() {
      this.$confirm('确定要退改吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          RollbackProject({ project_id: this.project_id }).then(() => {
            this.$message.success('退改成功');
            this.$refs.preview.getProjectBaseInfo();
          });
        })
        .catch(() => {});
    },
    rejectShangjiaBookRequest() {
      this.$confirm('确定要驳回上架请求吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          RejectShangjiaBookRequest({ project_id: this.project_id }).then(() => {
            this.$message.success('驳回上架请求成功');
            this.$refs.preview.getProjectBaseInfo();
          });
        })
        .catch(() => {});
    },
    rejectRollbackProjectRequest() {
      this.$confirm('确定要驳回退改请求吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          RejectRollbackProjectRequest({ project_id: this.project_id }).then(() => {
            this.$message.success('驳回退改请求成功');
            this.$refs.preview.getProjectBaseInfo();
          });
        })
        .catch(() => {});
    },
    goBackBookList() {
      this.$router.push({ path: '/project_manage/org/final' });
    },
  },
};
</script>

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

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