<template>
	<el-dialog v-model="dialogVisible" :title="isEdit ? '修改直播账号' : '新增直播账号'" width="500px">
		<el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
			<el-form-item label="平台" prop="platform">
				<el-select v-model="formData.platform" placeholder="请选择平台" style="width: 100%">
					<el-option label="抖音" value="抖音" />
					<el-option label="快手" value="快手" />
					<el-option label="视频号" value="视频号" />
				</el-select>
			</el-form-item>
			<el-form-item label="账号名称" prop="accountName">
				<el-input v-model="formData.accountName" placeholder="请输入账号名称" />
			</el-form-item>
			<el-form-item label="账号ID" prop="accountId">
				<el-input v-model="formData.accountId" placeholder="请输入账号ID" />
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
					<el-option label="正常" :value="1" />
					<el-option label="停用" :value="0" />
				</el-select>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" rows="3" />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="handleSubmit" :loading="loading">
					{{ isEdit ? '修改' : '新增' }}
				</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import {
	createLiveAccount,
	getLiveAccountDetail,
	updateLiveAccount,
	type LiveAccountSaveParams,
} from '/@/api/trade/operation/setting/live-account';

const emit = defineEmits<{
	(e: 'refresh'): void;
}>();

interface LiveAccountFormData extends LiveAccountSaveParams {}

const dialogVisible = ref(false);
const formRef = ref<FormInstance>();
const loading = ref(false);
const isEdit = ref(false);

const formData = reactive<LiveAccountFormData>({
	id: '',
	platform: '',
	accountName: '',
	accountId: '',
	status: 1,
	remark: '',
});

const rules: FormRules = {
	platform: [{ required: true, message: '请选择平台', trigger: 'change' }],
	accountName: [{ required: true, message: '请输入账号名称', trigger: 'blur' }],
	accountId: [{ required: true, message: '请输入账号ID', trigger: 'blur' }],
	status: [{ required: true, message: '请选择状态', trigger: 'change' }],
};

const resetForm = () => {
	formData.id = '';
	formData.platform = '';
	formData.accountName = '';
	formData.accountId = '';
	formData.status = 1;
	formData.remark = '';
};

const fillForm = (data: Partial<LiveAccountFormData>) => {
	formData.id = data.id ? String(data.id) : '';
	formData.platform = data.platform || '';
	formData.accountName = data.accountName || '';
	formData.accountId = data.accountId || '';
	formData.status = data.status ?? 1;
	formData.remark = data.remark || '';
};

const openDialog = async (row?: { id?: string }) => {
	resetForm();
	isEdit.value = !!row?.id;

	if (!row?.id) {
		dialogVisible.value = true;
		return;
	}

	try {
		loading.value = true;
		const res = await getLiveAccountDetail({ id: String(row.id) });
		if (res?.data) {
			fillForm(res.data);
		}
		dialogVisible.value = true;
	} catch (error) {
		ElMessage.error('获取直播账号详情失败');
	} finally {
		loading.value = false;
	}
};

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

	try {
		await formRef.value.validate();
		loading.value = true;

		const payload: LiveAccountSaveParams = {
			id: formData.id || undefined,
			platform: formData.platform,
			accountName: formData.accountName,
			accountId: formData.accountId,
			status: formData.status,
			remark: formData.remark,
		};

		if (isEdit.value) {
			await updateLiveAccount(payload);
			ElMessage.success('修改成功');
		} else {
			await createLiveAccount(payload);
			ElMessage.success('新增成功');
		}

		dialogVisible.value = false;
		emit('refresh');
	} catch (error) {
		ElMessage.error(isEdit.value ? '修改失败' : '新增失败');
	} finally {
		loading.value = false;
	}
};

defineExpose({
	openDialog,
});
</script>

<style scoped>
.dialog-footer {
	display: flex;
	justify-content: flex-end;
}
</style>
