<script>
	import { onMount } from 'svelte';
    import { useTinyRouter } from 'svelte-tiny-router';

	import { api, get } from '../../urls.svelte';
	import { error, yikes } from '../../warn.svelte';
	import { t } from '../../translations.svelte';

	let {module, id} = $props();
	let item = $state(null);
    let router = useTinyRouter();

    function onclick(e){
        e.preventDefault();
        var target = e.target;
        while (target && !target.href) target=target.parentNode;
        let href = target.getAttribute('href');
        if (href) router.navigate(href);
        return false;
    }

	async function loadItem(){
		let url = api(`stock/item/${id}`);
		let res = await get(url);
		if (res.ok) {
			yikes();
			item = await res.json();
		} else error();
	}


	onMount(loadItem);
</script>

{#if item}
<a href="/stock/{id}/view" {onclick} >({item.code}) {item.name}</a>
{/if}