<template>
	<div class="snapshot-invite-code-list-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef"  @checkbox-all="selectAllChangeEvent" @checkbox-change="selectChangeEvent">
				<template #toolbar_buttons>
					<el-button type="primary" @click="onAdd" v-auth="'snapshot:inviteCode:list:add'">
						<SvgIcon name="ele-Plus" class="mr5" />新增邀请码
					</el-button>
					<el-button type="danger" @click="onDelete" v-auth="'snapshot:inviteCode:list:delete'" :disabled="isChecked" :loading="state.loading"
					>	<SvgIcon name="ele-Delete" class="mr5" />删除<span v-if="checkTable.length">({{ checkTable.length }})</span>
					</el-button>
					<el-button type="primary" @click="onExport" v-auth="'snapshot:inviteCode:list:export'">
						<SvgIcon name="iconfont icon-daochu" class="mr5" />导出邀请码
					</el-button>
					<el-button type="primary" @click="onCreate" v-auth="'snapshot:inviteCode:list:create'">
						<SvgIcon name="iconfont icon-daochu" class="mr5" />批量生成
					</el-button>
				</template>
				<template #action="{ row }">
					<el-button link type="primary" @click="onEdit(row)" v-auth="'snapshot:inviteCode:list:edit'" title="编辑"> 编辑 </el-button>
					<el-button link type="primary" @click="download(row)" v-auth="'snapshot:inviteCode:list:download'" title="编辑人员"> 下载 </el-button>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</div>
		<!--  新增 -->
		<code-add ref="codeAddRef" @updateList="queryList" />
		<!--  编辑  -->
		<code-edit ref="codeEditRef" @updateList="queryList" />
		<!--  导出邀请码  -->
		<export-code ref="exportCodeRef" />
	</div>
</template>

<script lang="tsx" setup name="snapshotInviteCodeList">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { getClueList } from '@/api/snapshot/config';
import { ElMessage, ElMessageBox } from 'element-plus';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const CodeAdd = defineAsyncComponent(() => import('@/views/snapshot/inviteCode/list/components/Code-add.vue')); //  新增
const CodeEdit = defineAsyncComponent(() => import('@/views/snapshot/inviteCode/list/components/Code-edit.vue')); // 编辑
const ExportCode = defineAsyncComponent(() => import('@/views/snapshot/inviteCode/list/components/Export-code.vue'));

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		CaseName: null, // 线索名称
		IndustryName: null, // 行业类型
	},
	total: 0, // 总条数
});

const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		slots: {
			buttons: 'toolbar_buttons',
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotInviteCodeList',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ type: 'checkbox', width: 50, align: 'center' },
		{
			field: 'name',
			title: '部门名称',
			minWidth: 200
		},
		{
			field: 'industryName',
			title: '邀请码',
		},
		{
			field: 'citizenReadPackAmountTxt',
			title: '邀请码2',
		},
		{
			field: 'guiderReadPackAmountTxt',
			title: '邀请码地址',
		},
		{ title: '操作', width: 120, fixed: 'right', align: 'center', slots: { default: 'action' } },
	],
	data: [],
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	getClueList(state.queryParams)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result.items ?? [];
			state.total = res.result.total ?? 0;
			gridOptions.loading = false;
		})
		.finally(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
// 新增
const codeAddRef = ref<RefType>();
const onAdd = () => {
	codeAddRef.value.openDialog();
};
// 编辑
const codeEditRef = ref<RefType>();
const onEdit = (row: any) => {
	codeEditRef.value.openDialog(row.id);
};
// 下载
const download = (row:any)=>{

}
const checkTable = ref<EmptyArrayType>([]);
const gridRef = ref<RefType>();
const selectAllChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
// 删除
const onDelete = ()=>{

}
// 导出邀请码
const exportCodeRef = ref<RefType>();
const onExport = ()=>{
	exportCodeRef.value.openDialog();
}
// 批量生成
const onCreate = ()=>{
	ElMessageBox.confirm(`您确定要批量生成邀请码，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
				queryList();
		})
		.catch(() => {});
}
// 页面加载时
onMounted(() => {
	queryList();
});
</script>
