<template>
	<div class="dataShare-orderNoCallLog-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			>
				<template #table-search>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
						<el-form-item label="省本地编码" prop="CaseSerial">
							<el-input v-model="state.queryParams.CaseSerial" placeholder="省本地编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<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-width="0">
							<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>
				</template>
				<template #tableHeader="scope">
					<!--					<el-button type="primary" @click="onEdit(false)" :disabled="!scope.isSelected" :loading="state.loading" title="修改来源渠道">
						修改来源渠道
					</el-button>-->
					<el-button type="primary" @click="onEdit(true)" :disabled="!scope.isSelected" :loading="state.loading" title="修改并推送">
						修改并推送<span v-if="proTableRef?.selectedList?.length">({{ proTableRef?.selectedList?.length }})</span>
					</el-button>
				</template>
				<template #title="{ row }">
					<order-detail :order="{ id: row.orderId }" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
			</ProTable>
		</div>
	</div>
</template>
<script setup lang="tsx" name="dataShareOrderNoCallLog">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { defaultDateTime, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { formatDate } from '@/utils/formatTime';
import { editNoCallSource, getNoCallListNew } from '@/api/dataShare';
import Other from '@/utils/other';

const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', width: 40, align: 'center' },
	{ prop: 'requestData.CASE_TITLE', label: '工单标题', align: 'center', minWidth: 200 },
	{ prop: 'provinceNo', label: '省本地编码', align: 'center', minWidth: 220 },
	{
		prop: 'caseSource',
		label: '来源渠道',
		align: 'center',
	},
	{
		prop: 'startTime',
		label: '受理时间',
		align: 'center',
		width: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'creationTime',
		label: '入库时间',
		align: 'center',
		width: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'firstTime',
		label: '第一次上传省上的时间',
		align: 'center',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.firstTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'lastTime',
		label: '最近一次推送时间',
		align: 'center',
		width: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.lastTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		PageIndex: 1,
		PageSize: 20,
		crTime: defaultDateTime,
		StartTime: null,
		EndTime: null,
		CaseSerial: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	let request = Other.deepClone(state.queryParams);
	request.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	request.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(request, 'crTime');
	getNoCallListNew(request)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 修改
const onEdit = (isPush: boolean) => {
	const title = isPush ? '修改并推送' : '修改来源渠道';
	const ids = proTableRef.value.selectedList.map((item: any) => item.provinceNo);
	ElMessageBox.confirm(`您确定要${title}选择的【${proTableRef.value.selectedList.length}】个工单，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			editNoCallSource({ ids, isPush }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 查看修改记录
const onRecord = (row: any) => {
	console.log(row);
};
onMounted(() => {
	queryList();
});
</script>
