<template>
    <section v-if="softwareBoolean && !addBool"
        :class="['software-information', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['label', darkMode ? 'label-darkmode' : 'label-lightmode']" id="software">Software specifications:
            <div class="asset-data">
                <table class="data-table" id="asset-table-no-customer">
                    <tbody>
                        <tr :class="['table-row', darkMode ? 'tr-head-darkmode' : 'tr-head-lightmode']" id="table-head">
                            <th
                                :class="['Software', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Software-darkmode' : 'Software-lightmode']">
                                Software</th>
                            <th
                                :class="['Version', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Version-darkmode' : 'Version-lightmode']">
                                Version</th>
                            <th :class="['License', darkMode ? 'th-darkmode' : 'th-lightmode']">License</th>
                        </tr>
                        <tr v-if="editable" :class="['table-row', darkMode ? 'tr-darkmode' : 'tr-lightmode']" id="row-1">
                            <td
                                :class="['Software', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Software-darkmode' : 'Software-lightmode']">
                                <input type="text" v-model="item.software" @change="updateConfigItem()"
                                    :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                            </td>
                            <td
                                :class="['Version', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Version-darkmode' : 'Version-lightmode']">
                                <input type="text" v-model="item.version" @change="updateConfigItem()"
                                    :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                            </td>
                            <td :class="['License', darkMode ? 'td-darkmode' : 'td-lightmode']">
                                <input type="text" v-model="item.license" @change="updateConfigItem()"
                                    :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                            </td>
                        </tr>
                        <tr v-if="!editable" :class="['table-row', darkMode ? 'tr-darkmode' : 'tr-lightmode']" id="row-1">
                            <td
                                :class="['Software', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Software-darkmode' : 'Software-lightmode']">
                                {{ item.software }}</td>
                            <td
                                :class="['Version', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Version-darkmode' : 'Version-lightmode']">
                                {{ item.version }}</td>
                            <td :class="['License', darkMode ? 'td-darkmode' : 'td-lightmode']">{{ item.license }}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </section>
    <section v-if="addBool" :class="['software-information', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['label', darkMode ? 'label-darkmode' : 'label-lightmode']" id="software">Software specifications:
            <div class="asset-data">
                <table class="data-table" id="asset-table-no-customer">
                    <tbody>
                        <tr :class="['table-row', darkMode ? 'tr-head-darkmode' : 'tr-head-lightmode']" id="table-head">
                            <th
                                :class="['Software', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Software-darkmode' : 'Software-lightmode']">
                                Software</th>
                            <th
                                :class="['Version', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Version-darkmode' : 'Version-lightmode']">
                                Version</th>
                            <th :class="['License', darkMode ? 'th-darkmode' : 'th-lightmode']">License</th>
                        </tr>
                        <tr :class="['table-row', darkMode ? 'tr-darkmode' : 'tr-lightmode']" id="row-1">
                            <td
                                :class="['Software', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Software-darkmode' : 'Software-lightmode']">
                                <input type="text" v-model="newSoftware" @change="updateAsset()"
                                    :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                            </td>
                            <td
                                :class="['Version', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Version-darkmode' : 'Version-lightmode']">
                                <input type="text" v-model="newVersion" @change="updateAsset()"
                                    :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                            </td>
                            <td :class="['License', darkMode ? 'td-darkmode' : 'td-lightmode']">
                                <input type="text" v-model="newLicense" @change="updateAsset()"
                                    :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </section>
</template>
    
<script setup>
import { ref, onMounted } 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 chosenAssetId = computed(() => store.state.chosenAssetId);
const addBool = computed(() => store.state.new);

const newSoftwareBool = ref(true);
const newSoftware = ref('');
const newVersion = ref('');
const newLicense = ref('');

const darkMode = ref(true)
const item = ref({});
const softwareBoolean = ref(false)

// get config item from id
const getItemById = async () => {
    try {
        const response = await Axios.get(
            `https://${clientsideConfig.url}:${clientsideConfig.port}/api/getConfigItem/${chosenAssetId.value}`
        );
        item.value = response.data;
        if (item.value.softwareBool == 1) {
            softwareBoolean.value = true;
        };
    } catch (err) {
        console.log(err.response.statusText);
    }
}

// update software fields in the store
const updateAsset = () => {
    if (newSoftware.value.length == 0 && newLicense.value.length == 0 && newVersion.value.length == 0) {
        newSoftwareBool.value = false
    }
    const asset = {
        softwareBool: newSoftwareBool.value,
        software: newSoftware.value,
        version: newVersion.value,
        license: newLicense.value,
    };
    store.commit('updateSoftwareComponent', asset);
}

//update data
const updateConfigItem = async () => {
    if (item.value.license.length === 0 && item.value.version.length === 0 && item.value.software.length === 0) {
        item.value.softwareBool = 0;
    }
    try {
        await Axios.put(
            `https://${clientsideConfig.url}:${clientsideConfig.port}/api/updateConfigItem`,
            {
                primaryID: item.value.primaryID,
                assetName: item.value.assetName,
                customerID: item.value.customerID,
                customer: item.value.customer,
                location: item.value.location,
                remoteLocation: item.value.remoteLocation,
                type: item.value.type,
                description: item.value.description,
                notes: item.value.notes,
                state: item.value.state,
                lastView: item.value.lastView,
                user: item.value.user,
                hardwareBool: item.value.hardwareBool,
                model: item.value.model,
                serialnumber: item.value.serialnumber,
                CPU: item.value.CPU,
                RAM: item.value.RAM,
                storageConfiguration: item.value.storageConfiguration,
                miscellaneous: item.value.miscellaneous,
                softwareBool: item.value.softwareBool,
                software: item.value.software,
                version: item.value.version,
                license: item.value.license,
                networkBool: item.value.networkBool,
                IPv4: item.value.IPv4,
                IPv6: item.value.IPv6,
                MAC: item.value.MAC,
                subnetmask: item.value.subnetmask,
            }
        );
        await getItemById();
    } catch (err) {
        console.log(err);
    }
}

onMounted(() => {
    getItemById();
});
</script>

<script>
export default {
    name: "SoftwareSpecifications",
};
</script>      
      
      
<style scoped>
.software-information {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    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;
}

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

.label-darkmode {
    color: #FFFFFF;
}

.label-lightmode {
    color: #000;
}

.label#software {
    padding-top: 1rem;
    padding-bottom: 1rem;
}

.asset-data {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    align-self: stretch;
    padding-left: 0.625rem;
    padding-top: 1rem;
}

.data-table {
    width: 100%;
    padding: 0 0.625rem;
    table-layout: fixed;
    border-collapse: collapse;
}

.table-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    height: 3.125rem;
    padding: 0.625rem;
    gap: 0.625rem;
}

.tr-head-darkmode {
    border-top: none;
    border-bottom: 0.0625rem solid #000000;
}

.tr-head-lightmode {
    border-top: none;
    border-bottom: 0.0625rem solid #8e8e8e;
}

.tr-darkmode {
    border-top: 0.0625rem solid #000000;
}

.tr-lightmode {
    border-top: 0.0625rem solid #8e8e8e;
}

th,
td {
    height: 1.875rem;
    text-align: left;
    padding: 0;
    letter-spacing: 5%;
    font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

.th-darkmode,
.td-darkmode {
    color: #ffffff;
}

.th-lightmode,
.td-lightmode {
    color: #000;
}

th {
    font: 700 0.875rem/1.875rem Overpass, sans-serif;
}


.Software {
    width: 50%;
}

.Software-darkmode {
    border-right: 0.0625rem solid #000000;
}

.Software-lightmode {
    border-right: 0.0625rem solid #8e8e8e;
}

.Version {
    width: 25%;
}

.Version-darkmode {
    border-right: 0.0625rem solid #000000;
}

.Version-lightmode {
    border-right: 0.0625rem solid #8e8e8e;
}

.License {
    width: 25%;
}

.input {
    border: none;
}

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

.data-lightmode {
    background-color: #EBEBEB;
    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;
}
</style>