<template>
  <div class="main-box">
    <el-tabs v-model="activeName">
      <el-tab-pane label="首见条文" name="1">
        <div class="card">
          <ClauseForm
            ref="formRef"
            :editable="isEditMode"
            :disabled-content="clauseInfo.annotationStatus !== AnnoStatusEnum.UNASSIGNED"
          />
          <el-form-item label-width="90px" v-auth="['article:first:update']">
            <el-button v-if="!isEditMode" type="primary" @click="isEditMode = true" :disabled="isDisabled()">编辑</el-button>
            <template v-else>
              <el-button type="primary" @click="cancelAction" plain>取消</el-button>
              <el-button type="primary" @click="submitAction">保存</el-button>
            </template>
          </el-form-item>
        </div>
      </el-tab-pane>
      <el-tab-pane label="相关条文" name="2" lazy>
        <RelatedClause :first-clause="clauseInfo" />
      </el-tab-pane>
    </el-tabs>
    <div class="back-btn">
      <el-button type="primary" plain @click.stop="goBack">返回</el-button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { Clause } from "@/api/interface";
import { getFirstClauseDetail } from "@/api/modules/corpus/first-clause";
import { usePageLeave } from "@/hooks/usePageLeave";
import ClauseForm from "@/components/ClauseForm/index.vue";
import { AnnoStatusEnum } from "@/enums";
import RelatedClause from "./components/RelatedClause.vue";

const formRef = ref<typeof ClauseForm>();
const isEditMode = ref(false);
const route = useRoute();
const router = useRouter();
const activeName = ref<string>((route.meta.tab as string) || "1");
const clauseInfo = ref({} as Clause.ClauseInfo);

usePageLeave("FirstClause");

// 获取详情
async function loadDetail() {
  const res = await getFirstClauseDetail({ id: Number(route.query.id) });
  clauseInfo.value = res.data || {};
  formRef.value?.setFormValue(clauseInfo.value);
}

// 提交
function submitAction() {
  formRef.value?.submit().then(() => {
    loadDetail();
    isEditMode.value = false;
  });
}

// 取消
function cancelAction() {
  isEditMode.value = false;
  formRef.value?.reset();
  formRef.value?.setFormValue(clauseInfo.value);
}

// 返回
function goBack() {
  router.go(-1);
}

// 是否禁用编辑  (审核通过或者已上传不可以删除)
const isDisabled = () => clauseInfo.value.annotationStatus >= AnnoStatusEnum.PASSED;
onMounted(() => {
  loadDetail();
});
</script>
<style lang="scss" scoped>
.main-box {
  position: relative;
  height: 100%;
  padding: 0;
  padding-bottom: 10px;
  .back-btn {
    position: absolute;
    top: 12px;
    right: 40px;
  }
  :deep(.el-tabs) {
    display: flex;
    flex-direction: column;
    width: 100%;
    margin-top: 1px;

    --el-tabs-header-height: 56px;
    .el-tabs__header {
      margin-bottom: 10px;
      background: #ffffff;
      .el-tabs__nav-wrap {
        padding-left: 20px;
      }
      .el-tabs__item {
        font-size: 16px;
        &.is-active {
          font-size: 18px;
        }
      }
      .el-tabs__nav-scroll {
        &::after {
          height: 1px;
          background: #dcdfe6;
        }
      }
    }
    .el-tabs__content {
      flex: 1;
      margin: 0 20px;
      .el-tab-pane {
        height: 100%;
      }
    }
  }
}
</style>
