<template>
	<el-dialog :title="t('marketingApps.addDomain')" 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="['group', 'domain']" size="default">
					<template #default="scope">
						<div style="min-width: 50px; line-height: 32px;">{{ scope.item == 'group' ?
							t('marketingApps.group') : t('marketingApps.domain') }}</div>
					</template>
				</el-segmented>
			</div>
			<el-form-item label="" prop="grouping" v-if="activeName === 'group'">
				<el-select multiple collapse-tags collapse-tags-tooltip :max-collapse-tags="5"
					v-model="state.ruleForm.selectedIds" :placeholder="t('marketingApps.inputGroupTip')" 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('marketingApps.inputDomainTip')"></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="domainEdit">
import { useI18n } from 'vue-i18n';
import { pageListDomain } from '/@/api/marketing/config';
import { useMessage } from '/@/hooks/message';
import { rule } from '/@/utils/validate';
import { DomainItem } from '../types'

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

// 定义变量内容
const loading = ref(false);
const activeName = ref('group');
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,
	},
});

watch(() => props.open, async (val) => {
	if (val) {
		state.ruleForm.selectedIds = [];
		state.ruleForm.domain = [];
		getDomainList();
	}
})

// 表单校验规则
const dataRules = reactive({
	domain: [
		{ required: true, message: '域名不能为空', trigger: 'blur' },
		{ validator: rule.domain, trigger: 'blur' },
		{ validator: rule.overLength, trigger: 'blur' },
	],
});
const onCancel = () => {
	emit('update:open', false);
};
// 保存数据
const onSubmit = async () => {

	const valid = await dialogFormRef.value.validate();
	console.log('valid', valid);

	try {
		loading.value = true;
		if (activeName.value == 'group') {
			selectedObjects.value = [];
			selectedObjects.value = listSelect.value.filter(item => state.ruleForm.selectedIds.includes(item.id as string));
			emit('onsuccess', selectedObjects.value, activeName.value);
		} else if (state.ruleForm.domain.length > 0) {
			emit('onsuccess', [{ domain: state.ruleForm.domain }], activeName.value);
		}
		onCancel();
	} catch (err) {
		console.log(err);
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

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

</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>