<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 style="height: 80px;" ref="dialogFormRef" :rules="dataRules" :model="state.ruleForm"
			v-loading="loading">
			<div class="custom-style">
				<el-segmented v-model="activeName"
					:options="[t('marketingConfig.grouping'), t('marketingConfig.domain')]" size="default">
					<template #default="scope">
						<div style="min-width: 50px; line-height: 32px;">{{ scope.item }}</div>
					</template>
				</el-segmented>
			</div>
			<el-form-item label="" prop="grouping" v-if="activeName === t('marketingConfig.grouping')">
				<el-select multiple collapse-tags collapse-tags-tooltip :max-collapse-tags="5"
					v-model="state.ruleForm.selectedIds" :placeholder="t('marketingConfig.groupingTip')" clearable>
					<el-option v-for="item in listSelect" :key="item.id" :label="item.groupName" :value="item.id" />
				</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 lang="ts" name="systemMenuDialog">
import { useI18n } from 'vue-i18n';
import { pageListDomain } from '/@/api/marketing/config';
import { useMessage } from '/@/hooks/message';

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

interface DomainItem {
	domains: String[];
	groupName: String;
	id: String;
}

// 定义变量内容
const loading = ref(false);
const type = ref('add'); // 'add' or 'edit'
const activeName = ref(t('marketingConfig.grouping'));
const dialogFormRef = ref();
const listSelect = ref<DomainItem[]>([]);
const selectedObjects = ref<DomainItem[]>([]);

// 定义需要的数据
const state = reactive({
	ruleForm: {
		selectedIds: [] as String[], // 选中域名分组
		domain: [],
	},
});

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 () => {
	selectedObjects.value = [];
	try {
		loading.value = true;
		console.log(state.ruleForm.selectedIds);
		selectedObjects.value = listSelect.value.filter(item=>state.ruleForm.selectedIds.includes(item.id));
		console.log(selectedObjects.value);
		useMessage().success(t('common.addSuccessText'));
		emit('onsuccess', selectedObjects.value);
		onCancel();
	} catch (err) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

const getDomainList = async ()=>{
	const data = await pageListDomain();
	listSelect.value = data.data;
}

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

</script>
<style scoped>
.apps-loadmore.el-select-dropdown .el-scrollbar__wrap {
	height: 330px !important;
}

.custom-style {
	margin-top: 14px;

	.el-segmented__item {
		border-right: 1px solid rgba(230, 230, 230, 1);
		min-width: 72px;
		flex: unset;
	}

	.el-segmented {
		border: 1px solid rgba(230, 230, 230, 1);
		border-bottom: unset;
		--el-segmented-bg-color: #ffffff;
		--el-segmented-item-hover-bg-color: rgba(22, 122, 240, 0.1);
		--el-segmented-item-selected-color: rgba(22, 122, 240, 1);
		--el-segmented-item-hover-color: rgba(22, 122, 240, 1);
		--el-segmented-item-selected-bg-color: rgba(22, 122, 240, 0.1);
		--el-border-radius-base: 4px 4px 0 0;
		--el-segmented-padding: 0;
	}
}
</style>