<template>
<MkStickyContainer>
	<template #header><MkPageHeader/></template>
	<div class="mk-group-page">
		<div class="_section">
			<div class="_content" style="display: flex; gap: var(--margin); flex-wrap: wrap;">
				<MkButton inline @click="invite()">{{ i18n.ts.invite }}</MkButton>
				<MkButton inline @click="renameGroup()">{{ i18n.ts.rename }}</MkButton>
				<MkButton inline @click="transfer()">{{ i18n.ts.transfer }}</MkButton>
				<MkButton inline @click="deleteGroup()">{{ i18n.ts.delete }}</MkButton>
			</div>
		</div>
		<div class="_section members _gap">
			<div class="_content">
				<div class="users">
					<div v-for="user in users" :key="user.id" class="user _panel">
						<MkAvatar :user="user" class="avatar" :show-indicator="true"/>
						<div class="body">
							<MkUserName :user="user" class="name"/>
							<MkAcct :user="user" class="acct"/>
						</div>
						<div class="action">
							<button class="_button" @click="removeUser(user)"><i class="fas fa-times"></i></button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</MkStickyContainer>
</template>

<script lang="ts" setup>
import { computed, ref, watch } from 'vue';
import MkButton from '@/components/MkButton.vue';
import { definePageMetadata } from '@/scripts/page-metadata';
import { i18n } from '@/i18n';
import { useRouter } from '@/router';
import * as os from '@/os';

const props = defineProps<{
	groupId: {
			type: string,
			required: true,
		}
}>();

const users = ref<any[]>([]);
const group = ref<any>();

const router = useRouter();

watch(() => props.groupId, () => {
	fetch();
});


async function fetch() {
	os.api('users/groups/show', {
		groupId: props.groupId,
	}).then(gp => {
		group.value = gp;
		os.api('users/show', {
			userIds: group.userIds
		}).then(us => {
			users.value = us;
		});
	});
}

fetch();

function invite() {
	os.selectUser().then(user => {
		os.apiWithDialog('users/groups/invite', {
			groupId: group.id,
			userId: user.id
		});
	});
}

function removeUser(user) {
	os.api('users/groups/pull', {
		groupId: group.id,
		userId: user.id
	}).then(() => {
		users.value = users.filter(x => x.id !== user.id);
	});
}

async function renameGroup() {
	const { canceled, result: name } = await os.inputText({
		title: i18n.ts.groupName,
		default: group.name
	});
	if (canceled) return;

	await os.api('users/groups/update', {
		groupId: group.id,
		name: name
	});

	group.name = name;
}

function transfer() {
	os.selectUser().then(user => {
		os.apiWithDialog('users/groups/transfer', {
			groupId: group.id,
			userId: user.id
		});
	});
}

async function deleteGroup() {
	const { canceled } = await os.confirm({
		type: 'warning',
		text: i18n.t('removeAreYouSure', { x: group.name }),
	});
	if (canceled) return;

	await os.apiWithDialog('users/groups/delete', {
		groupId: group.id,
	});
	router.push('/my/groups');
}

definePageMetadata(computed(() => ({
	title: i18n.ts.members,
	icon: 'fas fa-users',
})));

</script>

<style lang="scss" scoped>
	.mk-group-page {
		> ._section {
			> ._content {
				padding-top: 1rem;
				justify-content: center;
			}
		}
		> .members {
			> ._content {
				> .users {
					> .user {
						display: flex;
						align-items: center;
						padding: 16px;

						> .avatar {
							width: 50px;
							height: 50px;
						}

						> .body {
							flex: 1;
							padding: 8px;

							> .name {
								display: block;
								font-weight: bold;
							}

							> .acct {
								opacity: 0.5;
							}
						}
					}
				}
			}
		}
	}
	</style>
