<template>
	<el-card class="mb-5 h-full rounded-lg">
		<template #header>
			<div class="flex justify-between items-center">
				<span>{{ t('poster.promptSettings') }}</span>
				<el-select v-model="formData.modelName" class="ml-4 w-60">
					<el-option v-for="model in modelOptions" :key="model.id" :label="model.name" :value="model.id" />
				</el-select>
			</div>
		</template>

		<!-- Prompt input -->
		<el-form @submit.prevent :model="formData" label-position="top" ref="formRef" :rules="rules">
			<!-- QR Code upload -->
			<el-form-item :label="t('poster.qrcode')" prop="qrCode">
				<UploadImage v-model:imageUrl="formData.qrCode" :width="'120px'" :height="'120px'" :border-radius="'4px'" :file-size="2" dir="poster" />
			</el-form-item>

			<el-form-item :label="t('poster.content')" prop="content">
				<ai-editor v-model="formData.content" :placeholder="t('poster.placeholder')" :minHeight="400" />
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="onGeneratePoster" :loading="generating" :disabled="generating">
					{{ t('poster.generate') }}
				</el-button>
				<el-button @click="onResetForm" :disabled="generating">{{ t('common.resetBtn') }}</el-button>
				<el-text v-if="generating" class="ml-2" type="info">{{ t('poster.generatingTip') }}</el-text>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script setup lang="ts" name="PosterForm">
import UploadImage from '/@/components/Upload/Image.vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';
import { list } from '/@/api/knowledge/aiModel';

interface FormData {
	content: string;
	qrCode: string;
	modelName: string;
}

interface ModelOption {
	id: string;
	name: string;
}

const props = defineProps<{
	formData: FormData;
	generating: boolean;
}>();

const emit = defineEmits<{
	(e: 'generate-poster'): void;
	(e: 'reset-form'): void;
	(e: 'update:formData', formData: FormData): void;
}>();

const formRef = ref<FormInstance>();
const modelOptions = ref<ModelOption[]>([]);

const { t } = useI18n();

const rules = ref<FormRules>({
	content: [
		{ required: true, message: t('poster.contentRequired'), trigger: 'blur' },
		{ min: 50, max: 3000, message: t('poster.contentLength'), trigger: 'blur' },
	],
	qrCode: [{ required: true, message: t('poster.qrCodeRequired'), trigger: 'change' }],
	modelName: [{ required: true, message: t('poster.modelRequired'), trigger: 'change' }],
});

const loadModels = async () => {
	try {
		const { data } = await list({ modelType: 'Chat' });
		if (data && Array.isArray(data)) {
			modelOptions.value = data;
			// Set first model as default if available and no model is currently selected
			if (modelOptions.value.length > 0 && !props.formData.modelName) {
				props.formData.modelName = data[0].modelName;
			}
		}
	} catch (error) {
		ElMessage.error(t('poster.loadModelFailed'));
	}
};

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

const onGeneratePoster = () => {
	if (!formRef.value) return;

	formRef.value.validate((valid) => {
		if (valid) {
			ElMessage.info({ message: t('poster.waitingMessage'), duration: 5000 });
			emit('generate-poster');
		}
	});
};

const onResetForm = () => {
	if (formRef.value) {
		formRef.value.resetFields();
	}
	emit('reset-form');
};
</script>
