<template>
  <div class="debug-page">
    <PageCanvas title="调试面板">
      <div class="debug-content"
        >
          <section class="debug-card"
            >
              <header class="debug-card-head"
                >
                  <div class="debug-card-title">获取 AccessToken</div>
                </header>

              <div class="debug-form"
                >
                  <div class="debug-field"
                    >
                      <label class="debug-label">API 基础地址</label>
                      <input v-model="form.baseUrl" class="debug-input" placeholder="https://example.com" />
                    </div>

                  <div class="debug-field"
                    >
                      <label class="debug-label">client_id</label>
                      <input v-model="form.clientId" class="debug-input" placeholder="client_id" />
                    </div>

                  <div class="debug-field"
                    >
                      <label class="debug-label">client_secret</label>
                      <input v-model="form.clientSecret" class="debug-input" placeholder="client_secret" />
                    </div>

                  <div class="debug-field"
                    >
                      <label class="debug-label">grant_type</label>
                      <input v-model="form.grantType" class="debug-input" placeholder="client_credentials" />
                    </div>

                  <div class="debug-actions"
                    >
                      <button class="debug-btn" type="button" :disabled="loading" @click="fetchToken"
                        >{{ loading ? "请求中…" : "获取 Token" }}</button>
                    </div>
                </div>

              <div v-if="resultText" class="debug-result"
                >
                  <div class="debug-result-head"
                    >
                      <div class="debug-result-title">请求结果</div>
                      <button class="debug-btn debug-btn-sm" type="button" @click="copyResult"
                        >复制</button>
                    </div>
                  <pre class="debug-pre">{{ resultText }}</pre>
                </div>
            </section>
        </div>
    </PageCanvas>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import PageCanvas from "../../components/layout/PageCanvas.vue";

const STORAGE_KEY = "bim.debug.tokenForm";

function loadForm() {
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    if (raw) return JSON.parse(raw);
  } catch {
    // ignore
  }
  return null;
}

function saveForm(data) {
  try {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
  } catch {
    // ignore
  }
}

const saved = loadForm();
const defaultBaseUrl = String(import.meta.env.VITE_API_BASE_URL || "").trim();

const form = reactive({
  baseUrl: saved?.baseUrl || defaultBaseUrl || "",
  clientId: saved?.clientId || "",
  clientSecret: saved?.clientSecret || "",
  grantType: saved?.grantType || "client_credentials",
});

const loading = ref(false);
const resultText = ref("");

async function fetchToken() {
  if (!form.baseUrl) {
    resultText.value = "请先填写 API 基础地址";
    return;
  }
  saveForm({ baseUrl: form.baseUrl, clientId: form.clientId, clientSecret: form.clientSecret, grantType: form.grantType });

  loading.value = true;
  resultText.value = "";

  try {
    const url = new URL("/oauth2/token", form.baseUrl);
    if (form.clientId) url.searchParams.set("client_id", form.clientId);
    if (form.clientSecret) url.searchParams.set("client_secret", form.clientSecret);
    if (form.grantType) url.searchParams.set("grant_type", form.grantType);

    const res = await fetch(url.toString(), {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({}),
    });

    const data = await res.json().catch(() => ({}));
    resultText.value = JSON.stringify({ status: res.status, statusText: res.statusText, data }, null, 2);
  } catch (err) {
    resultText.value = `请求失败：${err instanceof Error ? err.message : String(err)}`;
  } finally {
    loading.value = false;
  }
}

function copyResult() {
  navigator.clipboard?.writeText?.(resultText.value);
}
</script>

<style scoped>
.debug-page {
  min-height: 100vh;
  font-family: var(--bim-font-cn);
}

.debug-content {
  position: absolute;
  inset: 120px 24px 24px;
  overflow: auto;
}

.debug-card {
  max-width: 720px;
  margin: 0 auto;
  border-radius: 18px;
  border: 1px solid rgba(83, 214, 206, 0.24);
  background:
    radial-gradient(260px 160px at 18% 0%, rgba(83, 214, 206, 0.14), transparent 62%) padding-box,
    linear-gradient(180deg, rgba(18, 26, 31, 0.94), rgba(14, 20, 25, 0.86)) padding-box;
  box-shadow: 0 22px 60px rgba(0, 0, 0, 0.34), 0 0 0 1px rgba(83, 214, 206, 0.12) inset;
  overflow: hidden;
}

.debug-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background:
    radial-gradient(520px 120px at 18% 0%, rgba(83, 214, 206, 0.14), transparent 70%),
    linear-gradient(180deg, rgba(18, 26, 31, 0.96), rgba(14, 20, 25, 0.86));
}

.debug-card-title {
  font-size: 18px;
  font-weight: 950;
  letter-spacing: 0.4px;
  color: rgba(255, 255, 255, 0.92);
}

.debug-form {
  padding: 20px;
  display: grid;
  gap: 14px;
}

.debug-field {
  display: grid;
  gap: 8px;
}

.debug-label {
  font-size: 13px;
  font-weight: 900;
  color: rgba(255, 255, 255, 0.72);
}

.debug-input {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.06);
  padding: 0 12px;
  font-size: 15px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.9);
}

.debug-input::placeholder {
  color: rgba(255, 255, 255, 0.35);
}

.debug-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 6px;
}

.debug-btn {
  appearance: none;
  border: 1px solid rgba(83, 214, 206, 0.35);
  background: rgba(83, 214, 206, 0.14);
  color: rgba(255, 255, 255, 0.92);
  border-radius: 12px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 900;
  cursor: pointer;
}

.debug-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.debug-btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

.debug-result {
  margin: 0 20px 20px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.22);
  overflow: hidden;
}

.debug-result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.04);
}

.debug-result-title {
  font-size: 13px;
  font-weight: 900;
  color: rgba(255, 255, 255, 0.8);
}

.debug-pre {
  margin: 0;
  padding: 12px;
  font-size: 13px;
  line-height: 1.6;
  color: rgba(255, 255, 255, 0.86);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 360px;
  overflow: auto;
}
</style>
