<template>
	<el-dialog
		:title="type === 'Edit' ? '修改域名' : '添加域名'"
		width="600"
		v-model="props.open"
		:close-on-click-modal="false"
		:destroy-on-close="true"
        @close="onCancel"
		draggable
	>
		<el-form ref="menuDialogFormRef" :rules="dataRules" :model="state.ruleForm" v-loading="loading">
			<el-segmented v-model="activeName" :options="[t('marketingConfig.grouping'), t('marketingConfig.domain')]" size="default" />
			<el-form-item label="" prop="grouping" v-if="activeName === t('marketingConfig.grouping')">
				<el-select v-model="state.ruleForm.groupingTip" :placeholder="t('marketingConfig.groupingTip')" clearable>
					<el-option v-for="item in listSelect" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="" prop="domain" v-else> 
				<el-input v-model="state.ruleForm.domain" type="text" :placeholder="t('marketingConfig.domainTip')"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="onCancel">{{ t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">{{ t('common.confirmButtonText') }}</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup name="systemMenuDialog">
import { useI18n } from 'vue-i18n';
import { info, getAppList, save } from '/@/api/marketing/config';
import { useMessage } from '/@/hooks/message';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['update:open','onsuccess']);
const { t } = useI18n();

// 定义变量内容
const loading = ref(false);
const type = ref('add'); // 'add' or 'edit'
const activeName = ref(t('marketingConfig.grouping'));
const menuDialogFormRef = ref();
const listSelect = ref([
	{
		value: 'group1',
		label: '分组1',
	},
	{
		value: 'group2',
		label: '分组2',
	},
	{
		value: 'group3',
		label: '分组3',
	},
]);
// 定义需要的数据
const state = reactive({
	ruleForm: {
		list: [''],
		https: [''], // 域名集合
		ips: [''], // ip集合
		// {
		//   id: '1',
		//   ip: '美国',
		//   appId: '1',
		//   appUrl: 'www.baidu.com',
		//   appName: 'adsa'
		// },
	},
	appList: [], // 应用下拉框列表
});

const props = defineProps({
	open: {
		type: Boolean,
		default: false,
	},
});
// // 表单校验规则
const dataRules = reactive({
	domain: [
		{ required: true, message: '域名不能为空', trigger: 'blur' }
	],
});
const onCancel = () => {
	emit('update:open', false);
};
// 保存数据
const onSubmit = async () => {
	try {
		loading.value = true;
		// await save(state.ruleForm);
		useMessage().success(t(state.ruleForm.id ? 'common.editSuccessText' : 'common.addSuccessText'));
		emit('onsuccess');
        onCancel();
	} catch (err) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};
</script>
<style>
.apps-loadmore.el-select-dropdown .el-scrollbar__wrap {
	height: 330px !important;
}
</style>