<template>
	<el-dialog v-model="state.dialogVisible" draggable title="新增志愿者" destroy-on-close append-to-body @close="close">
		<template #header>
			<el-tabs v-model="state.activeName" @tab-change="handleClick">
				<el-tab-pane :name="item.name" v-for="item in state.tabPaneList" :key="item.name" :label="item.label"></el-tab-pane>
			</el-tabs>
		</template>
		<el-form :model="state.ruleForm" label-width="100px" ref="ruleFormRef" class="show-info-form" v-show="state.activeName === '0'">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="作业类型">{{ state.ruleForm.jobType }}</el-form-item>
				</el-col>
<!--				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="施工人员姓名">{{ state.ruleForm.name }}</el-form-item>
				</el-col>-->
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="联系电话">{{ state.ruleForm.phoneNumber }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="施工地点">{{ state.ruleForm.fullAddress }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="上报时间">{{ formatDate(state.ruleForm.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</el-form-item>
				</el-col>
<!--				<el-col>
					<el-form-item label="作业描述">{{ state.ruleForm.jobType }}</el-form-item>
				</el-col>-->
			</el-row>
		</el-form>
		<div v-show="state.activeName === '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"
				auto-resize
				show-overflow
				:scrollY="{ enabled: true, gt: 100 }"
				max-height="500px"
				id="orderAcceptHistory"
			>
				<vxe-column field="currentStepName" title="当前节点" width="100"></vxe-column>
				<vxe-column field="no" title="工单编码" width="140"></vxe-column>
				<vxe-column field="title" title="工单标题" min-width="150">
					<template #default="{ row }">
						<order-detail :order="row" @updateList="searchHistory">{{ row.title }}</order-detail>
					</template>
				</vxe-column>
			</vxe-table>
			<pagination
				@pagination="searchHistory"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			/>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">关 闭</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { historyOrder } from '@/api/business/order';
import { formatDate } from '@/utils/formatTime';

const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		name: null, // 姓名
		phoneNumber: null, // 联系方式
	},
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
	},
	total: 0,
	activeName: '0',
	tabPaneList: [
		{
			label: '基础信息',
			name: '0',
		},
		{
			label: '关联工单',
			name: '1',
		},
	],
});
// 打开弹窗
const openDialog = async (row: any) => {
	try {
		state.dialogVisible = true;
		state.ruleForm = row;
	} catch (error) {
		console.log(error);
	}
};
// 切换tab 查询列表
const handleClick = (val: string) => {
	switch (val) {
		case '0': //工单详情
			break;
		case '1': // 历史工单
			searchHistory();
			break;
	}
};
//  查询历史工单
const searchHistory = async () => {
	state.loading = true;
	let request = {
		PhoneNo: state.ruleForm.phoneNumber,
	};
	try {
		const response = await historyOrder(request);
		state.tableData = response?.result.items ?? [];
		state.total = response?.result.total;
		state.loading = false;
	} catch (error) {
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	state.activeName = '0';
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
