<template>
	<a-modal v-model:visible="openAddModel" :title="title" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
		@cancel="cancelModal" width="50%">
		<a-form :model="createForm" :label-col="{span: 4}" labelAlign="right">
			<a-row gutter="20">
				<a-col span="24">
					<a-form-item required label="角色" name="role">
						<a-select v-model:value="createForm.role" placeholder="请选择角色">
							<a-select-option v-for="(item,index) in role_list" :key="id"
								:value="item.id">{{item.name}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="手机号" name="mobile">
						<a-input v-model:value="createForm.mobile" placeholder="请输入手机号" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="密码" name="password">
						<a-input-password placeholder="密码"
							v-model:value="createForm.password"></a-input-password>
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="邮箱" name="email">
						<a-input v-model:value="createForm.email" placeholder="请输入邮箱" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</a-modal>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, defineProps, watch, defineEmits } from 'vue';
	import { PostCustomerAdd, GetCustomerUpdate,GetCustomerDetail, GetRoleList } from '@/apis/models';
	import { dataForm, otherDataForm, reset } from '@/views/customer/list/add/data.ts';
	import { useCommon } from '@/hooks/useCommon';
	let { store, openAddModel, hideModal, message, richOption } = useCommon();
	const emit = defineEmits();
	let props = defineProps(['edit_record']);
	let title = ref<String>('新增账号');
	let role_list = ref([])
	onMounted(() => {
		getRole()
	})

	const getRole = () => {
		GetRoleList({page: 1, pagesize: 100}).then(res => {
			role_list.value = res.data.roles
		})
	}

	let createForm = ref<CustomerListType.addFormType>(dataForm)


	const sumbitForm = () => {
		if (!createForm.value.id) {
			PostCustomerAdd(createForm.value).then(res => {
				message.success('创建账号成功');
				hideModal();
				resetForm();
				emit('successAdd');
			}).catch(err => {
			})
		} else {
			GetCustomerUpdate(createForm.value).then(res => {
				message.success('修改账号成功');
				hideModal();
				resetForm();
				emit('successAdd');
			}).catch(err => {
			})
		}
	}

	const resetForm = () => {
		createForm.value = reset().dataForm;
	}
	const cancelModal = () => {
		emit('closeAdd');
		resetForm();
		hideModal();
	}


	openAddModel = computed(() => {
		return store.state.openAddModel;
	})

	watch(() => props.edit_record, (newVal) => {
		GetRoleList()
		if (newVal) {
			title.value = '编辑账号';

			createForm.value = {
				id: newVal.id,
				name: newVal.name,
				address: newVal.address,
				telephone:newVal.telephone,
				fax: newVal.fax,
				postal: newVal.postal,
				email: newVal.email,
				content: newVal.content,
			}
		} else {
			title.value = '新增账号';
		}
	})
</script>

<style>

</style>