<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { rules } from '@/views/financial/budget-saving/saving-goal/utils/columns';
import { FormProps } from '@/views/financial/budget-saving/saving-goal/utils/types';
import { $t } from '@/plugins/i18n';
import LoadingSvg from '@/assets/svg/loading.svg';
import { useAdminUserStore } from '@/store/system/adminUser';
import { savingGoal } from '@/enums/bill/savingGoal';

const props = withDefaults(defineProps<FormProps>(), {
	formInline: () => ({
		// 绑定的用户id
		userId: undefined,
		// 完成状态
		statusType: undefined,
		// 储值目标名称
		savingGoalName: undefined,
		// 目标金额
		amount: undefined,
		// 目标时长
		duration: undefined,
	}),
});

const formRef = ref<FormInstance>();
const form = ref(props.formInline);
// 用户信息列表
const userDataList = ref();
// 搜索用户加载
const loading = ref(false);
const adminUserStore = useAdminUserStore();

/** 搜索 */
const onSearchUserinfo = async (keyword: string) => {
	loading.value = true;
	userDataList.value = await adminUserStore.queryUser({ keyword });
	loading.value = false;
};

onMounted(() => {
	onSearchUserinfo();
});

defineExpose({ formRef });
</script>

<template>
	<el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
		<!-- 绑定的用户id -->
		<el-form-item :label="$t('user')" prop="userId">
			<el-select
				v-model="form.userId"
				:loading="loading"
				:placeholder="$t('user')"
				:remote-method="onSearchUserinfo"
				clearable
				filterable
				remote
				remote-show-suffix
			>
				<el-option v-for="item in userDataList" :key="item.id" :label="item.username" :value="item.id" />
				<template #loading>
					<el-icon class="is-loading">
						<LoadingSvg />
					</el-icon>
				</template>
			</el-select>
		</el-form-item>

		<!-- 完成状态 -->
		<el-form-item :label="$t('statusType')" prop="statusType">
			<el-select v-model="form.statusType" :placeholder="$t('select') + $t('statusType')" clearable filterable remote remote-show-suffix>
				<el-option v-for="item in savingGoal" :key="item.value" :label="item.label" :value="item.value" />
			</el-select>
		</el-form-item>

		<!-- 储值目标名称 -->
		<el-form-item :label="$t('savingGoalName')" prop="savingGoalName">
			<el-input v-model="form.savingGoalName" :placeholder="$t('input') + $t('savingGoalName')" autocomplete="off" type="text" />
		</el-form-item>

		<!-- 目标金额 -->
		<el-form-item :label="$t('amount')" prop="amount">
			<el-input v-model="form.amount" :min="0.01" :placeholder="$t('input') + $t('amount')" :step="0.01" autocomplete="off" type="number" />
		</el-form-item>

		<!-- 目标时长 -->
		<el-form-item :label="$t('duration')" prop="duration">
			<el-date-picker
				v-model="form.duration"
				:end-placeholder="$t('endTime')"
				:start-placeholder="$t('startTime')"
				time-format="YYYY-MM-DD HH:mm:ss"
				type="datetimerange"
				value-format="YYYY-MM-DD HH:mm:ss"
			/>
		</el-form-item>
	</el-form>
</template>
