<template>
	<div class="">
		<el-dialog v-model="state.isShowDialog" draggable title="历史工单" ref="dialogRef" width="60%" append-to-body>
			<div>
				<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent>
					<el-form-item label="关键词" prop="Keyword">
						<el-input v-model="state.queryParams.Keyword" placeholder="工单标题/工单编码" clearable @keyup.enter="handleQuery" />
					</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>
				<el-table :data="state.tableData" @current-change="handleSelectionChange" max-height="500">
					<el-table-column prop="phoneNo" label="重复件" width="70">
						<template #default="{row}">
							<el-radio v-model="state.tableRadio" :label="row.id" @change="handleRowChange(row)">&nbsp;</el-radio>
						</template>
					</el-table-column>
					<el-table-column prop="phoneNo" label="工单标题" show-overflow-tooltip>
						<template #default="{row}">
							<span style="color: var(--el-color-primary)">{{ row.title }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="hotspotName" label="热点分类" show-overflow-tooltip> </el-table-column>
					<el-table-column prop="no" label="工单编码" show-overflow-tooltip> </el-table-column>
					<el-table-column prop="currentStepName" label="当前环节" show-overflow-tooltip></el-table-column>
					<el-table-column prop="statusText" label="状态" width="70" fixed="right" align="center">
						<template #default="{row}">
							<!-- 草稿 -->
							<span style="color: var(--el-color-info)" v-if="row.status === 0">{{ row.statusText }}</span>
							<!-- 待签收 -->
							<span style="color: var(--el-color-success)" v-if="row.status === 10">{{ row.statusText }}</span>
							<!-- 办理中 -->
							<span style="color: var(--el-color-primary)" v-if="row.status === 20">{{ row.statusText }}</span>
							<!-- 会签中 -->
							<span style="color: var(--el-color-primary)" v-if="row.status === 30">{{ row.statusText }}</span>
							<!-- 退回 -->
							<span style="color: var(--el-color-danger)" v-if="row.status === 40">{{ row.statusText }}</span>
							<!-- 办理完成 -->
							<span style="color: var(--el-color-success)" v-if="row.status === 50">{{ row.statusText }}</span>
							<!-- 已归档 -->
							<span style="color: var(--el-color-info)" v-if="row.status === 60">{{ row.statusText }}</span>
						</template>
					</el-table-column>
          <el-table-column label="操作" width="150" fixed="right" align="center">
            <!-- 草稿0 status 可以删除 -->
            <template #default="{ row }">
<!--              <el-button type="primary" link @click="onSupply" :loading="state.loading"  v-auth="'system:workflow:supply'">督办</el-button>-->
              <el-button type="primary" link @click="onSupply(row)" :loading="state.loading"  v-auth="'business:order:supply'">补充</el-button>
              <el-button type="primary" link @click="onRevoke(row)" :loading="state.loading"  v-auth="'business:order:revoke'">撤销</el-button>
            </template>
          </el-table-column>
					<template #empty>
						<Empty />
					</template>
				</el-table>
				<!-- 分页 -->
				<pagination
					:total="state.total"
					v-model:page="state.queryParams.PageIndex"
					v-model:limit="state.queryParams.PageSize"
					@pagination="getList"
				/>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button class="default-button" @click="state.isShowDialog = false">取 消</el-button>
					<el-button type="primary" @click="selectConfirm" :disabled="!state.tableRadio">确 定</el-button>
				</span>
			</template>
		</el-dialog>
    <!-- 工单补充 -->
    <order-supply ref="orderSupplyRef" @onSupplySuccess="onSupplySuccess" />
    <!-- 工单撤销 -->
    <order-revoke ref="orderRevokeRef" @onRevokeSuccess="onRevokeSuccess" />
	</div>
</template>

<script setup lang="ts" name="orderHistory">
import {defineAsyncComponent, reactive, ref} from 'vue';
import type { FormInstance } from 'element-plus';
import { historyOrder } from '/@/api/business/order';
import { useRoute } from 'vue-router';
// import { ElMessage } from 'element-plus';
// 引入组件
const OrderSupply = defineAsyncComponent(() => import('/@/views/business/order/components/Order-supply.vue'));
const OrderRevoke = defineAsyncComponent(() => import('/@/views/business/order/components/Order-revoke.vue'));
const emit = defineEmits(['saveSelect']);
// 定义变量内容
const state = reactive<any>({
	isShowDialog: false,
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		Keyword: '',
	},
	tableData: [],
	total: 0,
	loading: false,
	tableRadio: null as any,
	selectRow: null as any,
	ruleForm: {},
});
const ruleFormRef = ref<RefType>();
const orderSupplyRef = ref<RefType>()
const orderRevokeRef = ref<RefType>()
const route = useRoute();
// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = row;
	state.tableRadio = row.duplicateId ?? '';
	getList();
	state.isShowDialog = true;
};
const dialogRef = ref<RefType>();
// 关闭弹窗
const closeDialog = () => {
	state.isShowDialog = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	getList();
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
};
/** 获取历史工单 */
const getList = () => {
	if (!state.ruleForm.fromPhone) return;
	state.loading = true;
	let request = {
		...state.queryParams,
		PhoneNo: state.ruleForm.fromPhone,
		OrderId: route.params.id ?? '', //传入id 排除重复工单选择自己
	};
	historyOrder(request)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 选择重复件
const handleSelectionChange = (row: any) => {
	if (row) {
		state.tableRadio = row.id;
		state.selectRow = row;
	}
};
const handleRowChange = (row: any) => {
	state.tableRadio = row.id;
	state.selectRow = row;
};
// 补充
const onSupply = (row: any) => {
  // 穿入当前流程id
  orderSupplyRef.value.openDialog(row);
};
// 补充意见提交成功
const onSupplySuccess = () => {
  // 关闭弹窗
  orderSupplyRef.value.closeDialog();
  // 刷新列表
  getList();
};
// 撤销
const onRevoke = (row: any)=>{
  orderRevokeRef.value.openDialog(row);
}
// 撤销提交成功
const onRevokeSuccess = ()=>{
  // 关闭弹窗
  orderRevokeRef.value.closeDialog();
  // 刷新列表
  getList();
}
// 确定选择历史工单
const selectConfirm = () => {
	emit('saveSelect', state.selectRow);
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
<style lang="scss" scoped></style>
