<script setup>
import {ref} from "vue";
import {invoke} from "@tauri-apps/api";

const props = defineProps({task_id: String})
const emit = defineEmits(['close'])

let task_id = ref(props.task_id);

let name = "";
let description = "";
let error = ref(" ");

async function create_subtask(){
    if (name.replace(/ /g,'') === ""){
        error.value = "Название подзадачи не может быть пустым!";
    } else{
        let nowDateTime = new Date();
        let date = nowDateTime.getFullYear() + "." + ('0' + (nowDateTime.getMonth()+1)).slice(-2) + '.'
            + ('0' + nowDateTime.getDate()).slice(-2);
        let time = ('0' + nowDateTime.getHours()).slice(-2) + ":" + ('0' + nowDateTime.getMinutes()).slice(-2);

        console.log(props.task_id)
        await invoke('add_subtask', {
            idTask: props.task_id,
            date: date,
            time: time,
            name: name,
            description: description
        });

        emit('close');
    }
}
</script>

<template>
    <div id="create-task-modal" tabindex="-1" aria-hidden="true" class="px-[5vw] md:px-[10vw] lg:px-[15vw] absolute overflow-y-auto overflow-x-hidden w-full top-20 lg:top-24 z-50">
        <div class="relative p-4 max-h-full">
            <!-- Modal content -->
            <form class="relative bg-white rounded-lg shadow dark:bg-[rgb(50,50,50)]" @submit.prevent="create_subtask">
                <!-- Modal header -->
                <div class="flex items-center justify-between px-5 py-2 md:py-3 border-b rounded-t">
                    <h3 class="text-xl font-semibold text-gray-700 dark:text-gray-100">
                        Новая подзадача
                    </h3>
                    <button type="button" @click="$emit('close')" class="transition-colors text-gray-400 dark:text-gray-300 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center" data-modal-hide="default-modal">
                        <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
                            <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
                        </svg>
                    </button>
                </div>
                <!-- Modal body -->
                <div class="p-4 md:p-5 space-y-4">
                    <div class="space-y-6">
                        <div>
                            <label for="name" class="block text-sm font-medium leading-6 text-gray-900 dark:text-gray-200">Задача</label>
                            <div class="mt-2">
                                <input @input="error=' '" maxlength="80" v-model="name" id="name" name="name" type="text" class="px-2 block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-inset sm:text-sm sm:leading-6" />
                            </div>
                        </div>

                        <div>
                            <div class="flex items-center justify-between">
                                <label for="description" class="block text-sm font-medium leading-6 text-gray-900 dark:text-gray-200">Описание</label>
                            </div>
                            <div class="mt-2">
                                <textarea v-model="description" id="description" name="description" class="px-2 py-1.5 block w-full rounded-md border-0 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-inset sm:text-sm sm:leading-6" />
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Modal footer -->
                <div class="flex items-center py-3 px-5 md:p-3 border-t border-gray-200 rounded-b">
                    <input type="submit" class="text-white bg-green-500 hover:bg-green-600 transition-colors font-medium rounded-lg text-sm px-5 py-2.5 text-center" value="Создать">
                    <p class="ml-3 text-red-500">{{error}}</p>
                </div>
            </form>
        </div>
    </div>
</template>

<style scoped>

</style>