<template>
    <section v-if="!addBool" :class="['information', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <h2 v-if="!editable" :class="['issue-name', darkMode ? 'h2-darkmode' : 'h2-lightmode']">{{ issue.name }}</h2>
        <input v-if="editable" v-model="issue.name" @change="updateIssueItem()"
            :class="['data', 'issue-name-input', darkMode ? 'h2-darkmode' : 'h2-lightmode', darkMode ? 'h2-input-darkmode' : 'h2-input-lightmode', 'input']">
        <div class="issue-data">
            <div class="type">
                <div class="data-field" id="type">
                    <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">Type:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ issue.type }}</pre>
                    <input v-if="editable" v-model="issue.type" @change="updateIssueItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
            <div class="info">
                <div class="state-amount">
                    <div class="data-field" id="state">
                        <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">State:</pre>
                        <pre v-if="!editable"
                            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ issue.state }}</pre>
                        <input v-if="editable" v-model="issue.state" @change="updateIssueItem()"
                            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                    </div>
                    <div class="data-field" id="amount">
                        <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">Amount:</pre>
                        <pre v-if="!editable"
                            :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ issue.amount }}</pre>
                        <input v-if="editable" v-model="issue.amount" @change="updateIssueItem()"
                            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                    </div>
                </div>
            </div>
        </div>
        <div class="issue-data">
            <div class="additional">
                <div class="properties">
                    <h3 :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Properties:</h3>
                    <input v-model="issue.properties" :readonly="!editable" @change="updateIssueItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']" id="properties">
                </div>
            </div>
        </div>
    </section>
    <section v-if="addBool" :class="['information', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <input v-model="newName" @change="updateII()"
            :class="['data', 'issue-name-input', darkMode ? 'h2-darkmode' : 'h2-lightmode', darkMode ? 'h2-input-darkmode' : 'h2-input-lightmode', 'input']">
        <div class="issue-data">
            <div class="type">
                <div class="data-field" id="type">
                    <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">Type:</pre>
                    <input v-model="newType" @change="updateII()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
            <div class="info">
                <div class="state-amount">
                    <div class="data-field" id="state">
                        <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">State:</pre>
                        <input v-model="newState" @change="updateII()"
                            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                    </div>
                    <div class="data-field" id="amount">
                        <pre :class="['label', darkMode ? 'pre-darkmode' : 'pre-lightmode']">Amount:</pre>
                        <input v-model="newAmount" @change="updateII()"
                            :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                    </div>
                </div>
            </div>
        </div>
        <div class="issue-data">
            <div class="additional">
                <div class="properties">
                    <h3 :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Properties:</h3>
                    <input v-model="newProperties" @change="updateII()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']" id="properties">
                </div>
            </div>
        </div>
    </section>
</template>


<script setup>
import { ref, onMounted, watch } from 'vue';
import Axios from '../axios.config.js';
import clientsideConfig from '../../clientsideConfig.js';
import { useStore } from 'vuex';
import { computed } from 'vue';

const store = useStore();
const editable = computed(() => store.state.editable);
const chosenIssueId = computed(() => store.state.chosenIssueId);
const deleteBool = computed(() => store.state.deleteBool);
const addBool = computed(() => store.state.new);

const darkMode = ref(true)
const issue = ref({});
const issues = ref([]);

const newName = ref('');
const newType = ref('');
const newState = ref('');
const newAmount = ref('');
const newProperties = ref('');

//get all issue
const getIssues = async () => {
    try {
        const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getAllIssues`);
        issues.value = response.data;
    } catch (err) {
        console.log(err.response.statusText);
    }
}

const confirmDeleteIssue = async () => {
    if (deleteBool.value === true) {
        if (confirm("Do you really want to delete this issue? It cannot be undone!")) {
            try {
                await Axios.delete(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/deleteIssueItem/${chosenIssueId.value}`);
            } catch (err) {
                console.log(err.response.statusText);
            }
            try {
                await Axios.delete(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/deleteIV/${chosenIssueId.value}`);
            } catch (err) {
                console.log(err.response.statusText);
            }
            store.commit('undoDelete');
            store.commit('resetStore');
            store.commit('changeToIssueItemList');
        } else {
            store.commit('undoDelete');
        }
    }
}

//update data
const updateIssueItem = async () => {
    if (issue.value.name.trim() === "") {
        alert("Please add an issue name!");
        return;
    } else {
        var counter = 0;
        // check if the issue name already exists
        issues.value.forEach(i => {
            if (i.name === issue.value.name) {
                counter += 1;
            }
        });
        if (counter == 2) {
            alert("This issue name already exists. Please choose an unique issue name or modify respectively delete the old one!");
            issue.value.name = '';
            return;
        }
        try {
            await Axios.put(
                `https://${clientsideConfig.url}:${clientsideConfig.port}/api/updateIssueItem`,
                {
                    primaryID: issue.value.primaryID,
                    name: issue.value.name,
                    type: issue.value.type,
                    state: issue.value.state,
                    amount: issue.value.amount,
                    properties: issue.value.properties,
                }
            )
            await getIssueById();
        } catch (err) {
            console.log(err.response.statusText);
        }
    }
}

// get issue from id
const getIssueById = async () => {
    if (!addBool.value) {
        try {
            const response = await Axios.get(
                `https://${clientsideConfig.url}:${clientsideConfig.port}/api/getIssueItem/${chosenIssueId.value}`
            );
            issue.value = response.data;
        } catch (err) {
            console.log(err.response.statusText);
        }
    }
}

// update issue fields in the store
const updateII = () => {
    const ii = {
        name: newName.value,
        type: newType.value,
        state: newState.value,
        amount: newAmount.value,
        properties: newProperties.value,
    };
    store.commit('updateIssueComponent', ii);
}

// include delay to avoid 503 error
const triggerBackendCallsWithDelay = async (fetchDataFunc) => {
    setTimeout(() => {
        fetchDataFunc();
    }, 1500);
}

watch(deleteBool, confirmDeleteIssue);

onMounted(() => {
    getIssueById();
    triggerBackendCallsWithDelay(getIssues);
});
</script>

<script>
export default {
    name: "Issue",
};
</script>



<style scoped>
.information {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    width: 100%;
    padding: 1.25rem 1.875rem;
    border-radius: 0.625rem;
    box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

.section-darkmode {
    background-color: #2c2c2c;
}

.section-lightmode {
    background-color: #fff;
}

.issue-name-input {
    padding: 1rem 0;
    margin: 1rem;
    font-size: initial;
    letter-spacing: 5%;
    text-decoration-line: underline;
    font: italic 400 1rem/187.5% Overpass, -apple-system, Roboto, Helvetica,
        sans-serif;
}

.h2-input-darkmode {
    background-color: #212121;
}

.h2-input-lightmode {
    background-color: #EBEBEB;
}

.input {
    border: none;
}

.issue-name {
    align-self: stretch;
    padding: 1rem 0;
    letter-spacing: 5%;
    text-decoration-line: underline;
    font: italic 400 1rem/187.5% Overpass, -apple-system, Roboto, Helvetica,
        sans-serif;
}

.h2-darkmode {
    color: #fff;
}

.h2-lightmode {
    color: #000;
}

.data-field {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    padding: 0.8rem 1.875rem;
    gap: 1.25rem;
    border-radius: 0.625rem;
}

.data-field#street-name {
    width: 70%;
}

.data-field#street-no {
    width: 30%;
}

.label {
    letter-spacing: 5%;
    font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

.pre-darkmode {
    color: #fff;
}

.pre-lightmode {
    color: #000;
}

.data {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    padding: 0 0.625rem;
    border-radius: 0.3125rem;
    box-shadow: 0.0625rem 0.0625rem 0.25rem 0rem rgba(0, 0, 0, 0.25) inset;
    letter-spacing: 5%;
    font: 400 0.75rem/250% Overpass, sans-serif;
}

.data-darkmode {
    background-color: #212121;
    color: #fff;
}

.data-lightmode {
    background-color: #EBEBEB;
    color: #000;
}

.issue-data {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    align-self: stretch;
}

.type,
.info {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    line-height: normal;
    width: 50%;
    padding: 0 0;
    border-radius: 0.3125rem;
}

.additional {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    line-height: normal;
    width: 100%;
    padding: 0rem 0.625rem;
    border-radius: 0.3125rem;
}

.area-title {
    letter-spacing: 5%;
    font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

.h3-darkmode {
    color: #fff;
}

.h3-lightmode {
    color: #000;
}

.state-amount {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    align-self: stretch;
    padding: 0 1.875rem 0 0;
    gap: 0.625rem;
}

.data#properties {
    align-self: stretch;
}

.properties {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    align-self: stretch;
    padding: 0.625rem 1.875rem 0.625rem 1.25rem;
    padding-top: 0rem;
}
</style>