<template>
		<a-search @searchData="searchData" @clearData="clearData" :search_params="commomParams.search"></a-search>
	<a-biaoge :data="commomParams.table.data" :columns="commomParams.table.columns" :pagination="commomParams.page"
		@page="getPage" :loading="loading">
		<template #default="{ record }">
			<a-row :gutter="20">
				<a-col><a-button type="primary" size="small" primary @click="edit(record)">编辑</a-button></a-col>
				<a-popconfirm title="是否删除该反馈消息?" @confirm="del(record.id)">
					<a-col><a-button  size="small" danger>删除</a-button></a-col>
				</a-popconfirm>
			</a-row>
		</template>
	</a-biaoge>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed,defineEmits } from 'vue';
	import { GetFeedbackList, PostFeedbackDel } from '@/apis/models';
	import { useCommon } from '@/hooks/useCommon';
	import { cols } from '@/components/message/list/columns';
	import ASearch from '@/components/message/list/search.vue';
	import { warnToast, successToast } from '@/utils/toastHelper';
	let { commomParams } = useCommon();
	let loading = ref<Boolean>(true);
	let emit = defineEmits()

	onMounted(() => {
		getData(commomParams.value.search);
	})

	const searchData = (data : object) => {
		commomParams.value.search = data
		getData();
	}

	const clearData = (data : object) => {
		if (data) {
			commomParams.value.search = data
		} else {
			commomParams.value.search = {
				page: 1,
				pagesize: 10,
				sort: 'id',
				sortby: 'asc',
				keyword: ''
			}
		}
		getData();
	}


	const getPage = (data : object) => {
		commomParams.value.search.page = data.current;
		commomParams.value.search.pagesize = data.pageSize;
		getData();
	}
	const getData = async () => {
		try {
			loading.value = true;
			let res = await GetFeedbackList(commomParams.value.search);
			loading.value = false;
			commomParams.value.table.data = res.data.feedbacks;
			commomParams.value.table.columns = cols;
			commomParams.value.page = {
				current: commomParams.value.search.page,
				pageSize: commomParams.value.search.pagesize,
				total: res.data.total,
				pageSizeOptions: ['10', '20', '30', '40'],
				hideOnSinglePage: false,
				showSizeChanger: true
			};
		} catch {
			loading.value = false;
		}
	}
	
	// 修改
	const edit = (record : Object) => {
		emit('toEdit', { record: record })
	}

	// 删除
	const del = (id : number) => {
		commomParams.value.delRecord = { id: id };
		PostFeedbackDel(commomParams.value.delRecord).then(res => {
			message.success('删除成功');
			getData();
		})
	}
</script>

<style scoped lang="less">

</style>