<template>
  <div class="setup-page">
    <div class="setup-bg"></div>
    <div class="setup-orb setup-orb-a"></div>
    <div class="setup-orb setup-orb-b"></div>

    <div class="setup-card">
      <div class="setup-header">
        <div class="setup-badge">首次设密</div>
        <h1>租户管理员密码设置</h1>
        <p>完成一次性密码配置后即可前往登录页登录。</p>
      </div>

      <div class="tenant-tag">
        <span>租户编码</span>
        <strong>{{ tenantCode }}</strong>
      </div>

      <div v-if="verifying" class="setup-state">
        <el-skeleton :rows="4" animated />
      </div>

      <div v-else-if="!tokenValid" class="setup-state">
        <h2>链接不可用</h2>
        <p>该设置链接可能已过期、已被使用，或与当前租户不匹配。</p>
        <el-button type="primary" @click="goToLogin">返回登录</el-button>
      </div>

      <template v-else>
        <div class="setup-info">
          <div class="info-row">
            <span>管理员</span>
            <strong>{{ verifyData.userName || "-" }}</strong>
          </div>
          <div class="info-row">
            <span>手机号</span>
            <strong>{{ verifyData.phone || "-" }}</strong>
          </div>
          <div class="info-row">
            <span>链接有效期</span>
            <strong>{{ verifyData.expiresAt || "-" }}</strong>
          </div>
        </div>

        <el-form class="setup-form" @submit.prevent>
          <div class="form-item">
            <label>新密码</label>
            <el-input
              v-model="form.newPassword"
              type="password"
              show-password
              placeholder="请输入新密码"
              size="large"
            />
            <PasswordStrengthBar :password="form.newPassword" />
            <p class="password-tip">密码需至少 8 位，并包含大小写字母、数字和特殊字符。</p>
          </div>

          <div class="form-item">
            <label>确认密码</label>
            <el-input
              v-model="form.confirmPassword"
              type="password"
              show-password
              placeholder="请再次输入新密码"
              size="large"
              @keyup.enter="handleSubmit"
            />
          </div>

          <el-button type="primary" size="large" class="submit-btn" :loading="submitting" @click="handleSubmit">
            完成设置
          </el-button>

          <el-button text class="back-btn" @click="goToLogin">
            返回登录
          </el-button>
        </el-form>
      </template>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import PasswordStrengthBar from "../../components/PasswordStrengthBar.vue";
import { completePasswordSetup, verifyPasswordSetupToken } from "../../api/modules";

const route = useRoute();
const router = useRouter();

const tenantCode = computed(() => String(route.params.tenantCode || "").trim());
const token = computed(() => String(route.query.token || "").trim());

const verifying = ref(true);
const tokenValid = ref(false);
const submitting = ref(false);
const verifyData = reactive({
  userName: "",
  phone: "",
  expiresAt: "",
});
const form = reactive({
  newPassword: "",
  confirmPassword: "",
});

const goToLogin = () => {
  router.replace(`/${tenantCode.value}/login`);
};

const loadTokenState = async () => {
  if (!tenantCode.value || !token.value) {
    tokenValid.value = false;
    verifying.value = false;
    return;
  }
  verifying.value = true;
  try {
    const resp = await verifyPasswordSetupToken({
      tenantCode: tenantCode.value,
      token: token.value,
    });
    const data = resp?.data || {};
    verifyData.userName = String(data.userName || "");
    verifyData.phone = String(data.phone || "");
    verifyData.expiresAt = String(data.expiresAt || "");
    tokenValid.value = true;
  } catch (_error) {
    tokenValid.value = false;
  } finally {
    verifying.value = false;
  }
};

const handleSubmit = async () => {
  if (!tokenValid.value || submitting.value) {
    return;
  }
  if (!form.newPassword.trim()) {
    ElMessage.warning("请输入新密码");
    return;
  }
  if (form.newPassword !== form.confirmPassword) {
    ElMessage.warning("两次输入的密码不一致");
    return;
  }

  submitting.value = true;
  try {
    await completePasswordSetup({
      tenantCode: tenantCode.value,
      token: token.value,
      newPassword: form.newPassword,
    });
    ElMessage.success("密码设置成功，请使用新密码登录");
    goToLogin();
  } finally {
    submitting.value = false;
  }
};

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

<style scoped>
.setup-page {
  min-height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 24px;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

.setup-bg {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(circle at top left, rgba(245, 158, 11, 0.24), transparent 36%),
    radial-gradient(circle at bottom right, rgba(14, 165, 233, 0.2), transparent 40%),
    linear-gradient(135deg, #0f172a 0%, #172554 46%, #1f2937 100%);
}

.setup-orb {
  position: fixed;
  border-radius: 999px;
  filter: blur(90px);
  opacity: 0.35;
  pointer-events: none;
}

.setup-orb-a {
  width: 280px;
  height: 280px;
  background: #f59e0b;
  top: -60px;
  right: -40px;
}

.setup-orb-b {
  width: 320px;
  height: 320px;
  background: #0ea5e9;
  left: -120px;
  bottom: -100px;
}

.setup-card {
  position: relative;
  z-index: 1;
  width: min(460px, 100%);
  padding: 36px 32px;
  border-radius: 24px;
  background: rgba(15, 23, 42, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 24px 80px rgba(15, 23, 42, 0.42);
  color: #e2e8f0;
  backdrop-filter: blur(22px);
}

.setup-header h1 {
  margin: 14px 0 10px;
  color: #f8fafc;
  font-size: 28px;
  line-height: 1.2;
}

.setup-header p {
  margin: 0;
  color: rgba(226, 232, 240, 0.72);
  line-height: 1.6;
}

.setup-badge {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.16);
  color: #fbbf24;
  font-size: 12px;
  letter-spacing: 0.08em;
}

.tenant-tag {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 24px 0 18px;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(148, 163, 184, 0.12);
}

.tenant-tag span {
  color: rgba(226, 232, 240, 0.66);
  font-size: 13px;
}

.tenant-tag strong {
  color: #f8fafc;
  font-size: 14px;
}

.setup-info {
  margin-bottom: 22px;
  padding: 14px 16px;
  border-radius: 16px;
  background: rgba(30, 41, 59, 0.72);
  border: 1px solid rgba(148, 163, 184, 0.14);
}

.info-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 6px 0;
  font-size: 14px;
}

.info-row span {
  color: rgba(226, 232, 240, 0.62);
}

.info-row strong {
  color: #f8fafc;
  text-align: right;
}

.setup-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-item label {
  color: rgba(226, 232, 240, 0.88);
  font-size: 14px;
  font-weight: 500;
}

.password-tip {
  margin: 0;
  color: rgba(148, 163, 184, 0.88);
  font-size: 12px;
  line-height: 1.5;
}

.submit-btn {
  width: 100%;
  height: 46px;
  margin-top: 4px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, #f59e0b 0%, #f97316 48%, #0ea5e9 100%);
}

.back-btn {
  width: 100%;
  color: #cbd5e1;
}

.setup-state {
  padding: 10px 0 4px;
}

.setup-state h2 {
  margin: 0 0 8px;
  color: #f8fafc;
}

.setup-state p {
  margin: 0 0 16px;
  line-height: 1.6;
  color: rgba(226, 232, 240, 0.72);
}

.setup-form :deep(.el-input__wrapper) {
  min-height: 48px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.06);
  box-shadow: none;
  border: 1px solid rgba(148, 163, 184, 0.18);
}

.setup-form :deep(.el-input__wrapper.is-focus) {
  border-color: rgba(245, 158, 11, 0.72);
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.14);
}

.setup-form :deep(.el-input__inner) {
  color: #f8fafc;
}

@media (max-width: 640px) {
  .setup-card {
    padding: 28px 20px;
    border-radius: 20px;
  }

  .setup-header h1 {
    font-size: 24px;
  }

  .info-row,
  .tenant-tag {
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>
