<template>
	<a-modal v-model:visible="openAddModel" :title="title" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
		@cancel="cancelModal" width="50%">
		<a-form :model="createForm" layout="vertical">
			<a-row gutter="20">
				<a-col span="24">
					<a-form-item required label="栏目名称" name="name">
						<a-input v-model:value="createForm.name" placeholder="请输入栏目名称" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item label="封面图" name="name">
						<upload-one upload_txt="上传封面图" @uploadSuccess="uploadSuccess" :success_image="success_img"
							images_length="1"></upload-one>
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item label="上级栏目">
						<search-select placeholder="请选择上级栏目" :list="section_list" :select_value="addOtherForm.name"
							@searchData="sectionSearch" @getSelectValue="getSectionValue"
							:select_disabled="false"></search-select>
					</a-form-item>
				</a-col>

				<a-col span="24">
					<a-form-item required label="描述" name="description">
						<a-input v-model:value="createForm.description" placeholder="请输入描述" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item label="是否显示" name="display">
						<a-radio-group v-model:value="createForm.display" button-style="solid">
							<a-radio-button :value="1">是</a-radio-button>
							<a-radio-button :value="2">否</a-radio-button>
						</a-radio-group>
					</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 { addSection, updateSection, listSection } from '@/apis/models';
	import { dataForm, otherDataForm, reset } from '@/views/information/section/add/data.ts';
	import { useCommon } from '@/hooks/useCommon';
	let { store, openAddModel, hideModal, message, imageprefix } = useCommon();
	const emit = defineEmits();
	let props = defineProps(['edit_record']);
	let title = ref<String>('新增栏目/频道');
	let selectDisabled = ref<Boolean>(false)
	let success_img = ref<String>('')

	let createForm = ref<InformationSectionType.addFormType>(dataForm)
	let addOtherForm = ref<InformationSectionType.addOtherFormType>(otherDataForm)


	onMounted(() => {
		sectionSearch({ page: 1, pagesize: 100 })
	})

	// 上传图片
	const uploadSuccess = (data : Object) => {
		success_img.value = imageprefix + data
		createForm.value.cover_img = data
	}

	// 选择栏目/频道
	let section_list = ref<Object[]>([])
	const sectionSearch = (val : Object) => {
		listSection(val).then((res : object) => {
			const data = res.data.sections.map((item : object) => ({
				label: item.name,
				value: item.id,
			}));
			section_list.value = data;
		})
	}
	const getSectionValue = (val : Object) => {
		createForm.value.parent_id = val.key;
	}


	const sumbitForm = () => {
		if (createForm.value.name && createForm.value.description) {
			if (!createForm.value.id) {
				addSection(createForm.value).then(res => {
					message.success('新增栏目/频道成功');
					hideModal();
					resetForm();
					emit('successAdd');
				}).catch(err => {
				})
			} else {
				updateSection(createForm.value).then(res => {
					message.success('修改栏目/频道成功');
					hideModal();
					resetForm();
					emit('successAdd');
				}).catch(err => {
				})
			}

		} else {
			message.warning('请补充完整信息');
		}

	}

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


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

	watch(() => props.edit_record, (newVal) => {
		sectionSearch({ page: 1, pagesize: 100 })
		if (newVal) {
			title.value = '编辑栏目/频道';
			addOtherForm.value = {
				name: newVal.parent_id == -1 ? '' : newVal.parent_id
			}
			success_img.value = newVal.cover_img ? imageprefix + newVal.cover_img : ''
			createForm.value = {
				id: newVal.id,
				name: newVal.name,
				cover_img: newVal.cover_img,
				parent_id: newVal.parent_id,
				description: newVal.description,
				display: newVal.display
			}
		} else {
			title.value = '新增栏目/频道';
		}
	})
</script>

<style>

</style>