<template>
    <section v-if="hardwareBoolean && !addAssetBool"
        :class="['hardware-information', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']" id="hardware">Hardware specifications:</div>
        <div class="asset-data">
            <div class="model-CPU">
                <div class="data-field" id="model">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">Model:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ item.model }}</pre>
                    <input v-if="editable" v-model="item.model" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
                <div class="data-field" id="CPU">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">CPU:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ item.CPU }}</pre>
                    <input v-if="editable" v-model="item.CPU" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
            <div class="serial-number-RAM">
                <div class="data-field" id="serial-number">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">Serial number:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ item.serialnumber }}</pre>
                    <input v-if="editable" v-model="item.serialnumber" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
                <div class="data-field" id="RAM">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">RAM:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ item.RAM }}</pre>
                    <input v-if="editable" v-model="item.RAM" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
        </div>
        <div class="asset-data">
            <div class="additional">
                <div class="storage-configuration">
                    <h3 :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Storage configuration:</h3>
                    <input v-model="item.storageConfiguration" :readonly="!editable" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']"
                        id="storage-configuration">
                </div>
            </div>
            <div class="additional">
                <div class="miscellaneous">
                    <h3 :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Miscellaneous:</h3>
                    <input v-model="item.miscellaneous" :readonly="!editable" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']" id="miscellaneous">
                </div>
            </div>
        </div>
    </section>
    <section v-if="addAssetBool" :class="['hardware-information', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']" id="hardware">Hardware specifications:</div>
        <div class="asset-data">
            <div class="model-CPU">
                <div class="data-field" id="model">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">Model:</pre>
                    <input v-model="newModel" @change="updateAsset()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
                <div class="data-field" id="CPU">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">CPU:</pre>
                    <input v-model="newCPU" @change="updateAsset()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
            <div class="serial-number-RAM">
                <div class="data-field" id="serial-number">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">Serial number:</pre>
                    <input v-model="newSerialnumber" @change="updateAsset()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
                <div class="data-field" id="RAM">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">RAM:</pre>
                    <input v-model="newRAM" @change="updateAsset()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
        </div>
        <div class="asset-data">
            <div class="additional">
                <div class="storage-configuration">
                    <h3 :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Storage configuration:</h3>
                    <input v-model="newStorageConfiguration" @change="updateAsset()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']"
                        id="storage-configuration">
                </div>
            </div>
            <div class="additional">
                <div class="miscellaneous">
                    <h3 :class="['area-title', darkMode ? 'h3-darkmode' : 'h3-lightmode']">Miscellaneous:</h3>
                    <input v-model="newMiscellaneous" @change="updateAsset()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']" id="miscellaneous">
                </div>
            </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.assetEditable);
const chosenAssetId = computed(() => store.state.chosenAssetId);
const addAssetBool = computed(() => store.state.newAsset);

const newHardwareBool = ref(true);
const newModel = ref('');
const newSerialnumber = ref('');
const newCPU = ref('');
const newRAM = ref('');
const newStorageConfiguration = ref('');
const newMiscellaneous = ref('');

const darkMode = ref(true)
const item = ref({});
const hardwareBoolean = 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.hardwareBool == 1) {
            hardwareBoolean.value = true;
        };
    } catch (err) {
        console.log(err.response.statusText);
    }
}

// update hardware fields in the store
const updateAsset = () => {
    if (newModel.value.length === 0 && newSerialnumber.value.length === 0 && newCPU.value.length === 0 && newRAM.value.length === 0 && newStorageConfiguration.value.length === 0 && newMiscellaneous.value.length === 0) {
        newHardwareBool.value = false
    }
    const asset = {
        hardwareBool: newHardwareBool.value,
        model: newModel.value,
        serialnumber: newSerialnumber.value,
        CPU: newCPU.value,
        RAM: newRAM.value,
        storageConfig: newStorageConfiguration.value,
        miscellaneous: newMiscellaneous.value,
    }; 
    store.commit('updateHardwareComponent', asset);
}

//update data
const updateConfigItem = async () => {
    if (item.value.model.length === 0 && item.value.serialnumber.length === 0 && item.value.CPU.length === 0 && item.value.RAM.length === 0 && item.value.storageConfiguration.length === 0 && item.value.miscellaneous.length === 0) {
        item.value.hardwareBool = 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.response.statusText);
    }
}

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

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

      
<style scoped>
.hardware-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;
}


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

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

.div-darkmode,
.h3-darkmode {
    color: #fff;
}

.div-lightmode,
.h3-lightmode {
    color: #000;
}

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

.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;
}

.input {
    border: none;
}

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

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

.data#miscellaneous,
.data#storage-configuration {
    align-self: stretch;
}

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

.model-CPU,
.serial-number-RAM {
    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: 50%;
    padding: 0rem 0.625rem;
    border-radius: 0.3125rem;
}

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


.storage-configuration,
.miscellaneous {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    align-self: stretch;
    padding: 0.625rem 1.875rem 0.625rem 1.25rem;
}
</style>