<template>
  <div class="create-service-order">
    <div class="w-full bg-white  border-ff overflow-hidden rounded-[20px]">
      <div class=" h-[48px] w-full flex items-center justify-between bg-[#f0f8f3]">
        <span class="text-[18px] font-medium text-[#171717] text-left ml-[12px]">
          {{ isCallSuccess ? t("home.feedback.created") : t("home.feedback.title") }}
        </span>
        <img class="w-[98px] h-[48px]" src="https://oss.nianxx.cn/mp/static/version_101/home/feedback.png" />
      </div>

      <div v-if="!isCallSuccess" class=" p-[12px]">
        <div class="bg-[#f5f7fa]  flex items-center p-[12px] rounded-[10px] text-[14px] text-[#171717] mb-[12px]">
          <span class="font-medium leading-[22px] mr-[20px]">{{ t("home.feedback.phone") }}</span>
          <input :placeholder="t('home.feedback.phonePlaceholder')" v-model="contactPhone" />
        </div>
        <div class="bg-[#f5f7fa]  p-[12px] rounded-[10px] text-[14px] font-medium text-[#171717] mb-[12px]">
          <div class="font-medium leading-[22px] mb-[12px]">{{ t("home.feedback.content") }}</div>
          <textarea class="h-80" :placeholder="t('home.feedback.contentPlaceholder')"
            placeholder-class="text-[14px] font-normal" maxlength="100" v-model="contactText" />
        </div>

        <div
          class="h-[44px] rounded-full text-white bg-gradient-to-r from-[#0CCD58] to-[#4de4ff] flex items-center justify-center"
          @click="handleCall">
          {{ t("home.feedback.submit") }}
        </div>
      </div>

      <div v-else class=" left flex-1 p-[12px]">
        <div class="text-[12px] text-ink-600 leading-[20px] mb-[4px]">
          {{ t("home.feedback.phonePreview") }} {{ contactPhone }}
        </div>
        <div class="text-[12px] text-ink-600 leading-[20px] ellipsis-2">
          {{ t("home.feedback.contentPreview") }} {{ contactText }}
        </div>
      </div>

      <div class="footer-help flex items-center pl-[12px] mb-[12px]">
        <img class="w-[16px] h-[16px] mr-[4px]" src="./images/icon_volume.png" />
        <span class="text-[12px] font-medium color-FA7319">
          {{ t("home.feedback.successMessage", { appName }) }}
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick, computed } from "vue";
import { useI18n } from "vue-i18n";
import { emitter } from '@/utils/events'
import { SCROLL_TO_BOTTOM } from "@/constants/constant";
import { getCurrentConfig } from "@/constants/base";
import { submitFeedback } from "@/api/home";

const { t } = useI18n();
const contactPhone = ref("");
const contactText = ref("");
const isCallSuccess = ref(false); // 呼叫成功状态
const appName = computed(() => getCurrentConfig().name);

const handleCall = async () => {
  if (!contactPhone.value.trim()) {
    showToast(t("home.feedback.phoneRequired"));
    return;
  }

  if (!contactText.value.trim()) {
    showToast(t("home.feedback.contentRequired"));
    return;
  }

  sendFeedback();
};

/// 提交反馈意见
const sendFeedback = async () => {
  try {
    const res = await submitFeedback({
      userPhone: contactPhone.value,
      content: contactText.value,
    });

    if (res.code === 0) {
      // 设置成功状态
      isCallSuccess.value = true;

      showToast(t("home.feedback.success"));
    } else {
      showToast(res.message || t("home.feedback.failed"));
    }
  } catch (error) {
    console.error("反馈意见失败:", error);
    showToast(t("common.errors.network"));
  }
};

onMounted(() => {
  nextTick(() => {
    setTimeout(() => {
      emitter.emit(SCROLL_TO_BOTTOM, true);
    }, 200);
  });
});
</script>
