<script setup lang="ts">
import { ref, onBeforeMount } from "vue";
import type { Task } from "@/types";
import { getTasks, createTask, deleteTask } from "@/api/tasks";
import { CirclePlus } from "@lucide/vue";
import TaskItem from "@/components/TaskItem.vue";

const taskInput = ref<string>("");
const tasks = ref<Task[]>([]);
const list = ref<string>("test");
const showLastChecked = ref<boolean>(false);
const showNextReset = ref<boolean>(false);

function fetchTasks() {
    void getTasks(list.value)
        .then((data: Task[]) => {
            tasks.value = data;
            setTimeout(fetchTasks, 10000);
        });
}

function onNewTask() {
    if (taskInput.value.trim()) {
        void createTask(list.value, {
            name: taskInput.value.trim(),
            reset_cron: null,
        })
            .then((data: Task) => {
                tasks.value.push(data);
            })
            .finally(() => {
                taskInput.value = "";
            });
    }
}

function onCloneTask(task: Task) {
    void createTask(list.value, {
        name: task.name,
        reset_cron: task.reset_cron,
    }).then((data: Task) => {
        tasks.value.push(data);
    })
}

function onDeleteTask(task: Task) {
    void deleteTask(task.id);

    tasks.value.splice(tasks.value.indexOf(task), 1);
}

onBeforeMount(fetchTasks);
</script>

<template>
    <ul class="list ">
        <li v-for="(task, i) in tasks" :key="task.id">
            <task-item
                v-if="tasks[i]"
                v-model="tasks[i]"
                :show-last-checked="showLastChecked"
                :show-next-reset="showNextReset"
                @clone="onCloneTask"
                @delete="onDeleteTask"
            />
        </li>
        <li class="list-row">
            <div class="list-col-grow">
                <input
                    v-model="taskInput"
                    type="text"
                    placeholder="New Task"
                    class="input"
                    @keyup.enter="onNewTask"
                />
            </div>
            <div>
                <button
                    class="btn btn-square"
                    :disabled="taskInput.trim().length === 0"
                    @click="onNewTask"
                >
                    <CirclePlus />
                </button>
            </div>
        </li>
    </ul>
    <div class="collapse collapse-arrow  font-light text-sm">
      <input type="checkbox" />
      <div class="collapse-title">Options</div>
      <div class="collapse-content text-sm">
          <fieldset class="fieldset flex flex-col gap-2">
            <label class="label">
              <input v-model="showLastChecked" type="checkbox" class="checkbox" />
              Show last checked time
            </label>
            <label class="label">
              <input v-model="showNextReset" type="checkbox" class="checkbox" />
              Show next reset time
            </label>
          </fieldset>
      </div>
    </div>
</template>
