<template>
  <div class="audit">
    <MenuPage only-key="/personal_workbench/check_task" />

    <CommonPreview :id="id" ref="preview" :project-id="project_id" :is-audit="true" type="audit">
      <template #operator="{ courseware }">
        <span v-if="isTrue(courseware.is_can_add_audit_remark)" class="link" @click="addRemark">添加审核批注</span>
        <span
          v-if="isTrue(courseware.is_can_finish_audit)"
          class="link"
          @click="finishCoursewareCurFlowNodeAudit('false', 'complete', courseware.cur_audit_flow_node_type)"
        >
          审核完成
        </span>
        <span
          v-if="isTrue(courseware.is_can_audit_pass)"
          class="link"
          @click="finishCoursewareCurFlowNodeAudit('true', 'pass', courseware.cur_audit_flow_node_type)"
        >
          审核通过
        </span>
        <span
          v-if="isTrue(courseware.is_can_audit_reject)"
          class="link"
          @click="finishCoursewareCurFlowNodeAudit('false', 'reject', courseware.cur_audit_flow_node_type)"
        >
          审核驳回
        </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 { isTrue } from '@/utils/validate';
import { FinishCoursewareCurFlowNodeAudit } from '@/api/project';

export default {
  name: 'AuditTaskPage',
  components: {
    MenuPage,
    CommonPreview,
  },
  data() {
    return {
      id: this.$route.params.id || '',
      project_id: this.$route.query.project_id || '',
      isSubmitting: false,
      actionTip: {
        complete: '审核完成',
        pass: '审核通过',
        reject: '审核驳回',
      },
      isTrue,
    };
  },
  methods: {
    /**
     * 提交课件到审核流程
     * @param {'true'|'false'} is_pass - 是否通过审核
     * @param {'complete'|'pass'|'reject'} action - 操作类型
     * @param {string} flow_node_type - 审核流程节点类型
     */
    async finishCoursewareCurFlowNodeAudit(is_pass, action, flow_node_type) {
      if (this.isSubmitting) return;

      try {
        await this.$confirm(`是否确认${this.actionTip[action]}？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        });

        if (flow_node_type === 2) {
          await this.$confirm(`当前为终审环节，请确认${this.actionTip[action]}？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          });
        }

        this.isSubmitting = true;
        await FinishCoursewareCurFlowNodeAudit({ id: this.id, is_pass });
        this.$message.success('操作成功');
        this.$refs.preview.getBookCoursewareInfo(this.id);
      } catch (err) {
        // 忽略用户取消确认的情况
        if (err === 'cancel' || err === 'close') return;
        // 若为请求错误，尽量展示友好信息
        const msg = (err && err.message) || (err && err.msg) || '操作失败';
        this.$message.error(msg);
      } finally {
        this.isSubmitting = false;
      }
    },
    goBackBookList() {
      this.$router.push({ path: `/personal_workbench/check_task` });
    },

    // 点击添加审核意见
    addRemark() {
      this.$refs.preview.addRemark();
    },
  },
};
</script>

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

.audit {
  @include page-content(true);

  overflow: hidden;

  &__header {
    .courseware {
      .flow-nodename {
        flex: 1;
      }
    }
  }

  .main-container {
    flex: 1;
    min-width: 1110px;
    overflow: auto;
  }

  main.preview-main {
    display: flex;
    flex: 1;
    flex-direction: column;
    row-gap: 5px;
    width: 1100px;
    min-width: 1100px;
    min-height: 100%;
    padding: 5px;
    margin: 0 5px;
    background-color: #fff;
    border: 3px solid #f44444;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 10%);

    .title {
      display: inline-flex;
      column-gap: 24px;
      align-items: center;
      width: 100%;
      min-width: 280px;
      height: 64px;
      padding: 18px 24px;
      font-size: 20px;
      color: #fff;
      background-color: #f44444;
      border-top-left-radius: 12px;
      border-bottom-right-radius: 16px;
    }
  }

  .audit-content {
    display: flex;
    column-gap: 20px;
    min-width: 1810px;
    height: calc(100vh - 175px);

    .remark-list {
      width: 300px;
      overflow: auto;
      border: 1px solid #e5e5e5;

      h5 {
        padding: 0 5px;
        margin: 0;
        font-size: 18px;
        line-height: 40px;
        background: #f2f3f5;
      }

      .delete-btn {
        padding-left: 10px;
        color: #f44444;
        border-left: 1px solid #e5e5e5;
      }

      ul {
        height: calc(100% - 40px);
        overflow: auto;

        li {
          border-bottom: 1px solid #e5e5e5;

          > p {
            padding: 5px;
          }

          :deep p {
            margin: 0;
          }

          .remark-bottom {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 5px;
            font-size: 14px;
            color: #555;
            border-top: 1px solid #e5e5e5;
          }
        }
      }
    }
  }
}

:deep .audit-dialog {
  .el-dialog__body {
    padding: 5px 20px;
  }
}
</style>
<style lang="scss">
.tox-tinymce-aux {
  z-index: 9999 !important;
}
</style>
