<script setup lang="ts">
import {Modal} from '@/components';
import {formatIsk} from '@/formaters';
import {MarketType, MarketTypeLabel} from '@/market';
import {Ledger, LedgerSelect, isMain, useLedgersStore} from '@/ledger';
import {computed, ref} from 'vue';
import {fromEveDatetimeInput, toEveDatetimeInput} from './AcquiredType';
import {useAcquiredTypesStore} from './acquisition';

const acquiredTypesStore = useAcquiredTypesStore();
const ledgersStore = useLedgersStore();

const mainLedgers = computed(() => ledgersStore.ledgers.filter(isMain));

const modalOpen = ref<boolean>(false);
const type = ref<MarketType>();
const suggestions = ref<Record<string, number>>({});
const price = ref(1000000);
const count = ref(1);
const date = ref('');
const contextLedgerId = ref<string>();
const ledger = ref<Ledger>();

const ledgerId = computed(() => contextLedgerId.value ?? ledger.value?.ledgerId);

const open = (t: MarketType, s?: Record<string, number> | number, ledgerId?: string) => {
    type.value = t;
    count.value = 1;
    date.value = toEveDatetimeInput(new Date());
    contextLedgerId.value = ledgerId;
    ledger.value = undefined;

    if (typeof s === 'number') {
        suggestions.value = {};
        price.value = s;
    } else if (s) {
        suggestions.value = s;
        price.value = Object.values(s)[0];
    } else {
        suggestions.value = {};
        price.value = 1000000;
    }
    modalOpen.value = true;
}
const add = async () => {
    const id = type.value?.id;

    if (!id) {
        modalOpen.value = false;
        return;
    }

    const lid = ledgerId.value;

    if (!lid) {
        return;
    }

    await acquiredTypesStore.addAcquiredType(id, count.value, price.value, lid, fromEveDatetimeInput(date.value));
    modalOpen.value = false;
}

defineExpose({ open });
</script>

<template>
    <Modal v-model:open="modalOpen">
        <div class="bg-slate-800 rounded">
            <MarketTypeLabel v-if="type" class="m-1" :id="type.id" :name="type.name" hideCopy />
            <hr />
            <div class="flex p-4">
                <div class="flex me-2">
                    <span>Price: </span>
                    <div class="ms-2">
                        <input  type="number" min="0" step="1" v-model="price" @keyup.enter="add" />
                        <div class="px-2 mt-2 bg-slate-600 hover:bg-slate-700 border rounded cursor-pointer" v-for="(p, n) of suggestions" :key="n" @click="price = p">{{ n }}: {{ formatIsk(p) }}</div>
                    </div>
                </div>
                <div class="flex me-2 mb-auto">
                    <span>Count: </span>
                    <input class="ms-2" type="number" min="0" step="1" v-model="count" @keyup.enter="add" />
                </div>
                <div class="flex me-2 mb-auto">
                    <span>Eve Time: </span>
                    <input class="ms-2" type="datetime-local" v-model="date" />
                </div>
                <div v-if="!contextLedgerId" class="flex me-2 mb-auto">
                    <span>Ledger: </span>
                    <LedgerSelect class="ms-2" v-model="ledger" :ledgers="mainLedgers" />
                </div>
                <button class="mb-auto" :disabled="!ledgerId" @click="add">Add</button>
            </div>
        </div>
    </Modal>
</template>