<template>
	<a-drawer :width="500" title="搜索" placement="right" :open="openSearchModel" @close="onClose">
		<template #extra>
			<a-button style="margin-right: 8px" @click="clearSearch">清空搜索</a-button>
			<a-button type="primary" @click="getData">搜索</a-button>
		</template>
		<a-form :model="commomParams.search">
			<a-row :gutter="[10]">
				<a-col span="24">
					<a-form-item label="选择时间">
							<a-range-picker @Change="getRange" style="width: 100%;"/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</a-drawer>
</template>

<script lang="ts" setup>
	import { ref, onMounted, watch, computed } from 'vue';
	import { useCommon } from '@/hooks/useCommon';
	let { store, commomParams, openSearchModel, hideSearch, dayjs } = useCommon();
	const dayjsRef = ref(dayjs);
	let props = defineProps(['search_params']);
	const emit = defineEmits();
	
	openSearchModel = computed(() => {
		return store.state.openSearchModel;
	})
	
	interface listType {
		start_date:  String,
		end_date: String
	}
	
	commomParams.value.search = commomParams.value.search as listType;
	commomParams.value.search.start_date = '2020-01-01';
	commomParams.value.search.end_date =  dayjs().format('YYYY-MM-DD');
	commomParams.value.search.sort = 'work_experience';
	
	watch(() => props.search_params, (newVal) => {
		emit('searchData', commomParams.value.search)
	}, {
		immediate: true
	})
	
	const getData = () => {
		commomParams.value.search.start_date = dayjsRef.value(val[0]).format('YYYY-MM-DD');
		commomParams.value.search.end_date = dayjsRef.value(val[1]).format('YYYY-MM-DD');
		emit('searchData', commomParams.value.search);
		hideSearch()
	}
	
	// 清空搜索
	const clearSearch = () => {
		commomParams.value.search = {
			page: 1,
			pagesize: 10,
			sort: 'work_experience',
			sortby: 'desc',
			keyword: ''
		}
		commomParams.value.search.start_date = '2020-01-01';
		commomParams.value.search.end_date =  dayjs().format('YYYY-MM-DD');
		emit('clearData', commomParams.value.search);
		hideSearch()
	}
	
	const onClose = () => {
		clearSearch()
		hideSearch()
	}
</script>

<style>
</style>

