<template>
  <div
    class="flex flex-row justify-between w-full lg:w-auto gap-2 p-2 bg-gray-100 shadow dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-600"
  >
    <dev class="">
      <input
        v-model="displayUrl"
        type="text"
        placeholder="URL источника"
        readonly
        class="flex-1 m-1 h-12 dark:bg-gray-900 border-slate-100 shadow rounded-xl p-3 min-w-0"
      />

      <input
        v-model="displayPromt"
        type="text"
        placeholder="Промт"
        readonly
        class="flex-1 m-1 h-12 dark:bg-gray-900 border-slate-100 shadow rounded-xl p-3 max-w-20"
      />
    </dev>
    <button
      @click="startParsing"
      :disabled="isLoading"
      class="w-25 h-11 m-1 dark:bg-orange-500 hover:dark:bg-orange-600 shadow text-white bg-sky-700 hover:bg-sky-900 rounded-xl px-2 cursor-pointer whitespace-nowrap disabled:opacity-50 disabled:cursor-not-allowed"
    >
      {{ isLoading ? "..." : "Start" }}
    </button>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import axios from "axios";

const props = defineProps({
  source: {
    type: Object,
    required: true,
    default: () => ({ url: "", promt: "" }),
  },
});

const emit = defineEmits(["sourceStarted"]);

const isLoading = ref(false);

const displayUrl = computed({
  get: () => props.source.url || "",
  set: (val) => {
    // Только для чтения
  },
});

const displayPromt = computed({
  get: () => props.source.promt || "",
  set: (val) => {
    // Только для чтения
  },
});

const startParsing = async () => {
  if (!props.source.url) {
    // alert("URL источника не указан");
    return;
  }

  isLoading.value = true;

  try {
      await axios.post("https://allowlgroup.ru/api/8001/parser_all", {
      // await axios.post("http://127.0.0.1:8001/parser_all", {
      url: props.source.url,
      promt: props.source.promt,
    });

    emit("sourceStarted", props.source.url);
    // alert(`Парсинг для ${props.source.url} запущен`);
  } catch (err) {
    console.error("Ошибка запуска парсинга:", err);
    alert("Ошибка при запуске парсинга");
  } finally {
    isLoading.value = false;
  }
};
</script>
