<template>
	<div class="sys-region-container">
		<el-dialog v-model="state.isShowDialog" draggable overflow destroy-on-close width="500px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<div style="color: red; padding: 10px 10px; background: #faecd8; margin-bottom: 10px">
				<el-icon style="transform: translateY(2px)"><ele-Bell /></el-icon>
				<span> 不建议不指定任何省市名称直接生成全国区划数据，数据超大！ </span>
			</div>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" label-position="top">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="省级名称">
							<el-input v-model="state.ruleForm.province" placeholder="指定省级名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="市级名称">
							<el-input v-model="state.ruleForm.city" placeholder="指定市级名称" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<a href="https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/index.html" target="_blank" style="float: left">查看区划名称：国家统计局</a>
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" :loading="state.loading" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { ElMessage, ElNotification } from 'element-plus';

import { getAPI } from '/@/utils/axios-utils';
import { SysRegionApi } from '/@/api-services/api';

const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	loading: false,
	ruleForm: {} as any,
	isShowDialog: false,
});

// 打开弹窗
const openDialog = () => {
	ruleFormRef.value?.resetFields();
	state.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;

		if (state.ruleForm.city && !state.ruleForm.province) {
			ElMessage.error('不能单独指定市级！');
			return;
		}
		ElNotification({
			title: '提示',
			message: '努力同步中...',
			type: 'success',
			position: 'bottom-right',
		});
		state.loading = true;
		await getAPI(SysRegionApi).apiSysRegionSyncRegionStatsPost(state.ruleForm);
		closeDialog();

		ElMessage.success('生成成功');
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
