<template>
	<div class="anchor-edit-dialog">
		<el-dialog :title="(formData.id ? '修改' : '添加') + '主播'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="主播姓名" prop="name">
							<el-input v-model="formData.name" placeholder="请输入主播姓名" clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="联系电话" prop="phone">
							<el-input v-model="formData.phone" placeholder="请输入联系电话" clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="工号" prop="code">
							<el-input v-model="formData.code" placeholder="请输入工号" clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态" prop="status">
							<el-select v-model="formData.status" placeholder="请选择状态" clearable style="width: 100%">
								<el-option label="正常" :value="1" />
								<el-option label="停用" :value="0" />
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="formData.remark" type="textarea" :rows="4" placeholder="请输入备注" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" :loading="loading">
						{{ formData.id ? '修 改' : '新 增' }}
					</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs, nextTick } from 'vue';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import { addAnchor, updateAnchor, getAnchorOne } from '/@/api/trade/operation/setting/anchor';

interface DialogFormData {
	id?: string;
	name: string;
	phone: string;
	code: string;
	status: number;
	remark: string;
}

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

const state = reactive({
	loading: false,
	isShowDialog: false,
	formData: {
		id: '',
		name: '',
		phone: '',
		code: '',
		status: 1,
		remark: '',
	} as DialogFormData,
});

const rules: FormRules = {
	name: [
		{ required: true, message: '主播姓名不能为空', trigger: 'blur' },
		{ min: 2, max: 50, message: '主播姓名长度在 2 到 50 个字符', trigger: 'blur' },
	],
	phone: [
		{ required: true, message: '联系电话不能为空', trigger: 'blur' },
		{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
	],
	code: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
};

const formRef = ref<FormInstance>();
const { loading, isShowDialog, formData } = toRefs(state);

const openDialog = async (row?: DialogFormData) => {
	resetForm();

	if (row && row.id) {
		try {
			state.loading = true;
			const res = await getAnchorOne({ id: String(row.id) });
			if (res.data) {
				state.formData = {
					id: String(res.data.id),
					name: res.data.name || '',
					phone: res.data.phone || '',
					code: res.data.code || '',
					status: res.data.status ?? 1,
					remark: res.data.remark || '',
				};
			}
		} catch (error) {
			// 错误已由全局拦截器处理
		} finally {
			state.loading = false;
		}
	}

	state.isShowDialog = true;
	nextTick(() => {
		formRef.value?.clearValidate();
	});
};

const closeDialog = () => {
	state.isShowDialog = false;
};

const onCancel = () => {
	closeDialog();
};

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

	try {
		const valid = await formRef.value.validate();
		if (!valid) return;

		state.loading = true;

		// 确保id是字符串类型
		const submitData = {
			...state.formData,
			id: state.formData.id ? String(state.formData.id) : undefined,
		};

		if (submitData.id) {
			await updateAnchor(submitData);
			ElMessage.success('修改成功');
		} else {
			await addAnchor(submitData);
			ElMessage.success('添加成功');
		}

		closeDialog();
		emit('refresh');
	} catch (error) {
		// 错误已由全局拦截器处理
	} finally {
		state.loading = false;
	}
};

const resetForm = () => {
	state.formData = {
		id: '',
		name: '',
		phone: '',
		code: '',
		status: 1,
		remark: '',
	};
};

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

<style scoped>
.status-tip {
	font-size: 12px;
	color: #999;
	margin-top: 5px;
}
</style>
