<template>
	<div class="auxiliary-message-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<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="timeStartChangeCr"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
					/>
				</el-form-item>
				<el-form-item label="短信类型" prop="PushBusiness">
					<el-select v-model="state.queryParams.PushBusiness" placeholder="请选择短信类型">
						<el-option v-for="item in ePushBusinessData" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="发送状态" prop="Status">
					<el-select v-model="state.queryParams.Status" placeholder="请选择发送状态">
						<el-option v-for="item in eSendStateData" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<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 #operation="{ row }">
					<el-button link type="primary" @click="onPush(row)" v-auth="'auxiliary:message:push'" v-if="[3].includes(row.sendStateText)">
						重新发送
					</el-button>
				</template>
			</ProTable>
		</el-card>
	</div>
</template>

<script lang="tsx" setup name="auxiliaryMessage">
import { onMounted, reactive, ref } from 'vue';
import { ElButton, ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { messageBase, messageList, pushMessage } from '@/api/auxiliary/message';
import { shortcuts } from '@/utils/constants';
import other from '@/utils/other';
import dayjs from 'dayjs';

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'pushBusinessText', label: '短信类型', width: 130 },
	{ prop: 'sendStateText', label: '发送状态', width: 120 },
	{ prop: 'name', label: '接收人', width: 120 },
	{ prop: 'telNumber', label: '接收号码', width: 120 },
	{ prop: 'content', label: '短信内容', width: 300, showOverflowTooltip: false },
	{
		prop: 'creationTime',
		label: '添加时间',
		width: 170,
		render: (scope: any) => {
			return dayjs(scope.row.creationTime).format('YYYY-MM-DD HH:mm:ss');
		},
	},
	{ prop: 'sendOrganize', label: '发送部门', width: 150 },
	{ prop: 'sendUser', label: '发送人', width: 120 },
	{ prop: 'resendCount', label: '重发次数' },
	{ prop: 'orderNo', label: '关联工单编号', width: 150 },
	{ prop: 'remark', label: '备注说明', width: 200 },
	{ prop: 'reason', label: '失败原因', width: 200 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 120, align: 'center' },
]);
// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		PushBusiness: null, // 短信类型
		Status: null, // 推送状态
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const handleTimeChange = (val: string[], startKey: string, endKey: string) => {
	if (val) {
		state.queryParams[startKey] = val[0];
		state.queryParams[endKey] = val[1];
	} else {
		state.queryParams[startKey] = '';
		state.queryParams[endKey] = '';
	}
};
// 受理时间
const timeStartChangeCr = (val: string[]) => {
	handleTimeChange(val, 'StartTime', 'EndTime');
	queryList();
};
const ruleFormRef = ref<RefType>(null); // 表单ref
const ePushBusinessData = ref<EmptyArrayType>([]); // 短信类型
const eSendStateData = ref<EmptyArrayType>([]); // 发送状态
const getBaseData = async () => {
	try {
		const res = await messageBase();
		ePushBusinessData.value = res.result?.ePushBusinessData ?? [];
		eSendStateData.value = res.result?.eSendStateData ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	const request = other.deepClone(state.queryParams);
	Reflect.deleteProperty(request, 'crTime'); // 删除无用的参数
	messageList(request)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.StartTime = null;
	state.queryParams.EndTime = null;
	queryList();
};
// 重新发送
const onPush = (row: any) => {
	ElMessageBox.confirm(`确定重新发送【${row.content}】短信吗？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
	})
		.then(() => {
			pushMessage(row.id)
				.then(() => {
					ElMessage({
						message: '操作成功',
						type: 'success',
					});
					queryList();
				})
				.catch(() => {});
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getBaseData();
	queryList();
});
</script>
