<template>
	<div class="snapshot-invite-code-statistics-detail-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent :disabled="gridOptions.loading">
						<el-form-item label="查询时间段" prop="crTime">
							<el-date-picker
								v-model="state.queryParams.crTime"
								type="datetimerange"
								unlink-panels
								range-separator="至"
								start-placeholder="开始时间"
								end-placeholder="结束时间"
								:shortcuts="shortcuts"
								@change="handleQuery"
								value-format="YYYY-MM-DD[T]HH:mm:ss"
								:default-time="defaultTimeStartEnd"
								:clearable="false"
							/>
						</el-form-item>
						<el-form-item label="姓名" prop="Name">
							<el-input v-model="state.queryParams.Name" placeholder="请填写姓名" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item label="电话" prop="PhoneNumber">
							<el-input v-model="state.queryParams.PhoneNumber" placeholder="请填写电话" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item label="邀请码" prop="InviteCode">
							<el-input
								v-model="state.queryParams.InviteCode"
								placeholder="请填写邀请码"
								clearable
								@keyup.enter="handleQuery"
								class="keyword-input"
							/>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
							<el-button @click="resetQuery(ruleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
						</el-form-item>
					</el-form>
				</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>
		<!--  新增用户  -->
		<user-add ref="userAddRef" @updateList="queryList" />
		<!--  编辑用户  -->
		<user-edit ref="userEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="snapshotInviteCodeStatisticsDetail">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
import { getInviteCodeStatisticsDetail, getInviteCodeStatisticsDetailExport } from '@/api/snapshot/inviteCode';
import { useRoute } from 'vue-router';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const UserAdd = defineAsyncComponent(() => import('@/views/snapshot/config/areaUser/components/User-add.vue')); // 新增用户
const UserEdit = defineAsyncComponent(() => import('@/views/snapshot/config/areaUser/components/User-edit.vue')); // 编辑用户

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		crTime: [], //
		StartTime: null,
		EndTime: null,
		Name: null,
		PhoneNumber: null,
		InviteCode: null,
	},
	total: 0, // 总条数
});

const requestParams = ref<EmptyObjectType>({});
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();
			},
		},
		tools: [{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }],
	},
	params: {
		exportMethod: getInviteCodeStatisticsDetailExport,
		exportParams: requestParams,
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotInviteCodeStatisticsDetail',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'name',
			title: '姓名',
		},
		{
			field: 'phoneNumber',
			title: '电话',
		},
		{
			field: 'openId',
			title: '微信openID',
		},
		{
			field: 'creationTime',
			title: '邀请时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'inviteCode',
			title: '邀请码',
		},
		{
			field: 'orgName',
			title: '部门',
		},
	],
	data: [],
});
const ruleFormRef = ref<any>(null); // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
const route = useRoute();
const routeQueryParams = route.query;
// 获取参数列表
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.OrgId = routeQueryParams.OrgId;
	requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	getInviteCodeStatisticsDetail(requestParams.value)
		.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 resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};

// 页面加载时
onMounted(() => {
	if (routeQueryParams.StartTime && routeQueryParams.EndTime) {
		state.queryParams.crTime = [routeQueryParams.StartTime, routeQueryParams.EndTime];
	}
	queryList();
});
</script>
