<template>
	<el-dialog class="api-edit" v-model="showDialog" :title="`${formData.id ? '编辑授权配置' : '新增授权配置'}`" width="600px" :close-on-click-modal="false" :close-on-press-escape="false">
		<el-form ref="formRef" :model="formData" :rules="ruleForm" label-width="auto" @keyup.enter="onSubmit" v-if="showDialog">
			<el-form-item label="名称" prop="name">
				<el-input v-model="formData.name" placeholder="请输入名称" />
			</el-form-item>
			<el-form-item label="APPID" prop="appId">
				<el-input v-model="formData.appId" placeholder="请输入APPID" />
			</el-form-item>
			<el-form-item label="APPSECRET" prop="appSecret">
				<el-input v-model="formData.appSecret" placeholder="请输入APPSECRET" />
			</el-form-item>
			<el-form-item label="回调地址" prop="callbackUrl">
				<el-input v-model="formData.callbackUrl" placeholder="请输入回调地址" />
			</el-form-item>
			<el-form-item label="LOGO" prop="logo">
				<upload-vue :img="formData.logo" @set-img="setImg('logo', $event)"></upload-vue>
			</el-form-item>
			<el-form-item label="应用类型" prop="types">
				<el-radio-group v-model="formData.types">
					<el-radio :label="1">内部独立应用</el-radio>
					<el-radio :label="2">第三方应用</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="onSubmit">确定</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import api from '/@/api/system';
import { ruleRequired } from '/@/utils/validator';
import { ElMessage, UploadProps } from 'element-plus';
import UploadVue from '/@/components/upload/index.vue';

const emit = defineEmits(['getList']);

const showDialog = ref(false);
const formRef = ref();

const baseForm: any = {
	id: undefined,
	name: '',
	logo: '',
	appId: '',
	appSecret: '',
	callbackUrl: '',
	types: 1,
};

const formData = reactive<any>(JSON.parse(JSON.stringify(baseForm)));

const ruleForm = {
	name: [ruleRequired('名称不能为空', 'change')],
	logo: [ruleRequired('LOGO不能为空', 'change')],
	appId: [ruleRequired('APPID不能为空', 'change')],
	appSecret: [ruleRequired('APPSECRET不能为空', 'change')],
	callbackUrl: [ruleRequired('回调地址不能为空', 'change')],
	types: [ruleRequired('应用类型不能为空', 'change')],
};

const onSubmit = async () => {
	await formRef.value.validate();

	const theApi = formData.id ? api.sso.edit : api.sso.add;

	await theApi(formData);

	ElMessage.success('操作成功');
	resetForm();
	showDialog.value = false;
	emit('getList');
};

const resetForm = async () => {
	const types = formData.types
	Object.assign(formData, { ...JSON.parse(JSON.stringify(baseForm)) });
	formRef.value && formRef.value.resetFields(formData);
};
const cancel = async () => {
	resetForm()
	showDialog.value = false
};

const open = async (row: any) => {
	resetForm();
	if (row) Object.assign(formData, row)
	showDialog.value = true;
};

const setImg = (key: string, val: string) => {
	formData[key] = val
}

defineExpose({ open });
</script>
