<template>
	<a-search @searchData="searchData" @clearData="clearData" :search_params="commomParams.search"></a-search>
	<a-card title="转换验证码">
		<template #extra>
			<a-button @click="getCode" type="primary" size="large">生成验证码</a-button>
		</template>
		<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-dropdown placement="bottomRight">
							<template #overlay>
								<a-menu>
									<a-menu-item key="1">
										<a-button @click="copyCode(record.code)" block>复制</a-button>
									</a-menu-item>
									<a-menu-item key="2">
										<a-button @click="del(record.id)" block>删除</a-button>
									</a-menu-item>
								</a-menu>
							</template>
							<a-button>
								操作
								<DownOutlined />
							</a-button>
						</a-dropdown>
					</a-col>
				</a-row>
			</template>
		</a-biaoge>
	</a-card>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, defineEmits, createVNode } from 'vue';
	import { PostCompanycustomercodeAdd, PostCompanycustomercodeDel, PostCompanycustomercodeUpdate, GetCompanycustomercodeList } from '@/apis/models';
	import ASearch from '@/components/user/conver/search.vue';
	import { useCommon } from '@/hooks/useCommon';
	import { cols } from '@/components/user/conver/columns';
	import { warnToast, successToast } from '@/utils/toastHelper';
	const emit = defineEmits();
	let { commomParams, ExclamationCircleOutlined, Modal } = useCommon();
	let loading = ref<Boolean>(true);

	onMounted(() => {
		getData();
	})

	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 GetCompanycustomercodeList(commomParams.value.search);
			loading.value = false;
			commomParams.value.table.data = res.data.companycustomercodes;
			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 getCode = (id) => {
		Modal.confirm({
			title: '是否要生成验证码',
			icon: createVNode(ExclamationCircleOutlined),
			centered: true,
			onOk() {
				PostCompanycustomercodeAdd().then(res => {
					successToast('已生成验证码')
					getData()
				})
			},
			onCancel() {

			},
		});
	}

	const copyCode = async (code) => {
		try {
			await navigator.clipboard.writeText(code);
			successToast('复制成功');
		} catch (err) {
			warnToast('复制到剪贴板失败，请手动复制');
		}
	}



	const del = (id : number) => {
		commomParams.value.delParam = { id: id };
		PostCompanycustomercodeDel(commomParams.value.delParam).then(res => {
			successToast('删除成功');
			getData();
		})
	}
</script>

<style scoped lang="less">

</style>