<template>
	<view class="py-3 mt-4 bg-white u-font-15">
		<PrettyExchange @change="change" />
	</view>
</template>

<script setup>
	import PrettyExchange from '@/components/PrettyExchange/PrettyExchange.vue';
	
	function change(options) {
		if (options instanceof Array) {
			let projectIdList = [];
			let arr = [];
			options.forEach(item => {
				projectIdList.push(item.id);
				arr.push(item.name);
			});
			setProjectSort(projectIdList);
		} else {
			setProjectRelation(options);
		}
	}

	/**
	 * 设置项目顺序
	 * @param { Array } projectIdList 项目id
	 */
	async function setProjectSort(projectIdList) {
		try {
			const params = { projectIdList };
			await uni.$u.api.setProjectSort(params);
			uni.$ui.showToast('排序修改成功');
		} catch (error) {
			console.log('error: ', error);
			uni.$ui.showToast(error.msg || '排序修改失败');
		}
		this.$emit('getProjects');
	}

	/**
	 * 设置项目父子结构
	 * @param { string } id 当前移动的项目的id
	 * @param { string } parentId 父项目的id
	 */
	async function setProjectRelation(options) {
		try {
			const params = options;
			await uni.$u.api.setProjectRelation(params);
			uni.$ui.showToast('排序修改成功');
		} catch (error) {
			console.error('error: ', error);
			uni.$ui.showToast(error.msg || '排序修改失败');
		}
		this.$emit('getProjects');
	}
</script>

<style lang="scss" scoped>
</style>
