<script>
	import {Meteor} from "meteor/meteor";
	import {onMount} from "svelte";
	import GridTable from "./../GridTable.svelte";
	import {writable} from "svelte/store";
	import TextField from '@smui/textfield';
	import HelperText from '@smui/textfield/helper-text';
	import {AssetTypes} from "../../api/asset-types";

	onMount(async () => {
		Meteor.subscribe('assetTypes');
	});

	const assetTypesColumns = [
		{
			key: "year",
			title: "Year",
			value: v => v.year,
			minWidth: 100,
			weight: 1,
			cls: "year",
		}, {
			key: "name",
			title: "Name",
			value: v => v.name,
			minWidth: 100,
			weight: 1,
			cls: "name",
		}, {
			key: "description",
			title: "Description",
			value: v => v.description,
			minWidth: 100,
			weight: 1,
			cls: "description",
		},
	];
	const assetTypesActions = {
		title: "Actions",
		headerWidgets: [
			{icon: "add_box", action: () => {editedAssetType.set({});}, tooltip: "Add a new asset type."}
		],
		rowWidgets: [
			{icon: "add_circle", action: (v) => {editedAssetType.set(v)}},
			{icon: "delete", action: (v) => {deleteAssetType(v)}}
		],
	};
	let editedAssetType = writable(null);
	const onAssetTypeSelection = (e) => {
	}
	let dirtyAssetType;
	// Copy the edited value when ever it changes, set some defaults for a new value object (to make the view happy).
	editedAssetType.subscribe(v => {dirtyAssetType = Object.assign({name: "", description: "", year: ""}, v)});
	// Load the sites (reactive).
	let assetTypes = AssetTypes.find({}, {sort: {year: -1}});
	const deleteAssetType = assetType => {
		//TODO:
	};
	const applyAssetTypeChanges = () => {
		if(dirtyAssetType._id)
			Meteor.call("assetTypes.update", dirtyAssetType._id, dirtyAssetType.name, dirtyAssetType.description, dirtyAssetType.year);
		else
			Meteor.call("assetTypes.add", dirtyAssetType.name, dirtyAssetType.description, dirtyAssetType.year);
		editedAssetType.set(null);
		dirtyAssetType = null;
	}
	const rejectAssetTypeChanges = () => {
		editedAssetType.set(null);
	}
</script>

<div class="container">
	<h2>Asset Types</h2>
	<GridTable bind:rows={assetTypes} columns="{assetTypesColumns}" actions="{assetTypesActions}" rowKey="{(v) => {return v._id}}" bind:edited="{editedAssetType}" on:selection={onAssetTypeSelection}>
		{#if dirtyAssetType}
			<div class="editorContainer">
				<div style="grid-column: 1/span 1">
					<TextField type="text" style="width: 100%" bind:value={dirtyAssetType.year} label="Year">
						<HelperText slot="helper">The manufacture or purchase year.  Used for sorting assets so most recent ones appear on top.</HelperText>
					</TextField>
					<TextField type="text" style="width: 100%" bind:value={dirtyAssetType.name} label="Name">
						<HelperText slot="helper">Provide a unique name for the asset type.</HelperText>
					</TextField>
					<TextField type="text" style="width: 100%" bind:value={dirtyAssetType.description} label="Description">
						<HelperText slot="helper">A detailed description.</HelperText>
					</TextField>
				</div>

				<button type="button" style="grid-column: 2/span 1;" class="button accept-button material-icons material-symbols-outlined" on:click={applyAssetTypeChanges}>check</button>
				<button type="button" style="grid-column: 3/span 1;" class="button reject-button material-icons material-symbols-outlined" on:click={rejectAssetTypeChanges}>close</button>
			</div>
		{/if}
	</GridTable>
</div>

<style>
    form {
        margin: 0;
    }
    .editorContainer {
        display: grid;
        grid-template-columns: minmax(10px, 1fr) minmax(3rem, 3rem) minmax(3rem, 3rem);
    }
    .accept-button, .reject-button {
        font-size: .8rem;
        padding: .6rem;
        margin: auto;
        color: white;
        border-radius: 50%;
        border: 0;
        font-weight: 800;
        alignment: center;
        cursor: pointer;
    }
    .accept-button {
        background-color: rgba(61, 148, 61, 0.91);
    }
    .reject-button {
        background-color: rgba(176, 64, 64, 0.61);
    }
    .accept-button:hover {
        background-color: rgb(61, 148, 61);
    }
    .reject-button:hover {
        background-color: rgb(176, 64, 64);
    }
</style>
