<script setup lang="ts">
import { stepState, fnToStepName } from '../hooks/useStep';
import { Modal } from 'ant-design-vue/es';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { delAccessToken, delRefreshToken } from '@/plugins/auth-token';
import { useRouter } from 'vue-router';
import useAppStore from '@/store/modules/app';
import useI18n from '@/hooks/useI18n';
import { onMounted, ref } from 'vue';
import { bootloaderDone } from '@/api/system/quick-start/bootloader';
const { t } = useI18n();
const router = useRouter();

// 信息列表
const infoList = ref<Record<string, any>[]>([]);

/**获取列表 */
function fnGetList() {
  if (!stepState.setupNE) {
    infoList.value.push({
      type: 'error',
      title: t('views.system.quickStart.doneSkipTitle'),
      description: t('views.system.quickStart.doneSkipDesc'),
    });
    return;
  }

  infoList.value.push({
    type: 'warning',
    title: t('views.system.quickStart.doneNETitle'),
    description: t('views.system.quickStart.doneNEDesc'),
  });
}

/**返回上一步 */
function fnStepPrev() {
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.system.quickStart.donePrevTip'),
    onOk() {
      let stepName = 'NeInfoSoftwareLicense';
      if (!stepState.setupNE) {
        stepName = 'SystemConfig';
      }
      fnToStepName(stepName);
    },
  });
}

/**引导完成 */
function fnGuideDone() {
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.system.quickStart.doneOkTip'),
    onOk() {
      bootloaderDone()
        .then(res => {
          if (res.code === RESULT_CODE_SUCCESS) {
            useAppStore().bootloader = false;
            delAccessToken();
          }
        })
        .finally(() => {
          router.push({ name: 'Login' });
        });
    },
  });
}

onMounted(() => {
  fnGetList();
});
</script>

<template>
  <a-result
    status="success"
    :title="t('views.system.quickStart.doneTitle')"
    :sub-title="t('views.system.quickStart.doneTip')"
  >
    <template #extra>
      <a-space direction="vertical" style="width: 40%">
        <a-button block type="primary" @click="fnGuideDone()">
          {{ t('views.system.quickStart.finish') }}
        </a-button>
        <a-button block type="default" @click="fnStepPrev()">
          {{ t('views.system.quickStart.stepPrev') }}
        </a-button>
      </a-space>
    </template>

    <div class="result-content">
      <a-spin
        tip="Loading..."
        style="width: 100%"
        :spinning="infoList.length == 0"
      >
        <a-alert
          v-for="s in infoList"
          :key="s.title"
          :message="s.title"
          :description="s.description"
          :type="s.type"
          show-icon
        />
      </a-spin>
    </div>
  </a-result>
</template>

<style lang="less" scoped>
.result-content {
  max-height: 42vh;
  overflow-y: auto;
  overflow-x: hidden;
}
.ant-alert {
  margin-bottom: 16px;
}
</style>
