<template>
	<div class="statistics-center-transfer-city-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="state.queryParams.direction" @tab-change="tabChange">
				<el-tab-pane name="out" label="转出列表"></el-tab-pane>
				<el-tab-pane name="in" label="转入列表"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item prop="crTime">
					<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" :disabled="state.loading" ref="statisticalTimeRef"/>
				</el-form-item>
				<el-form-item prop="CiryName" label="互转城市名称">
					<el-input v-model="state.queryParams.CiryName" 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" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery
				}"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="statisticsCenterTransferCity"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="orderCode" title="受理编号" width="140"></vxe-column>
					<template v-if="state.queryParams.direction === 'out'">
						<vxe-column field="transferOutTime" title="转出时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.transferOutTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
						<vxe-column field="sendTimes" title="同步次数" width="100"></vxe-column>
						<vxe-column field="isSuccess" title="同步状态" width="100">
							<template #default="{ row }">
								{{ row.isSuccess ? row.isSuccess ? '成功' : '失败' : '同步中' }}
							</template>
						</vxe-column>
						<vxe-column field="result" title="同步返回" min-width="300"></vxe-column>
						<vxe-column field="cityName" title="转入市州" width="140"></vxe-column>
					</template>
					<template v-else>
						<vxe-column field="transferOutTime" title="转入时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.transferOutTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
						<vxe-column field="result" title="同步转入" min-width="300"></vxe-column>
						<vxe-column field="cityName" title="来源市州" width="140"></vxe-column>
					</template>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsCenterTransferCity">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { formatDate } from '@/utils/formatTime';
import { cityConversionIn, cityConversionOut } from '@/api/statistics/center';
import { defaultDate } from '@/utils/constants';
import { FormInstance } from 'element-plus';
import Other from '@/utils/other';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		CiryName: null, // 城市名称
		crTime: defaultDate,
		StartTime: null,
		EndTime: null,
		direction: 'out',
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const ruleFormRef = ref<RefType>(); // 表单ref
const tabChange = () => {
	statisticalTimeRef.value.reset();
	ruleFormRef.value.resetFields();
	handleQuery();
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	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');
	if (requestParams.value.direction === 'out') {
		cityConversionOut(requestParams.value).then((res) => {
			state.tableData = res.result.items;
			state.total = res.result.total;
			state.loading = false;
		});
	} else {
		cityConversionIn(requestParams.value).then((res) => {
			state.tableData = res.result.items;
			state.total = res.result.total;
			state.loading = false;
		});
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
