<template>
	<div class="auxiliary-message-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<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"
					/>
				</el-form-item>
				<el-form-item label="接收号码" prop="TelNumber">
					<el-input v-model="state.queryParams.TelNumber" placeholder="接收号码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="">
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="drawer = true" class="default-button"> <SvgIcon name="ele-Search" class="mr5" />更多查询</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
				<template #buttons>
					<el-button type="primary" @click="addSms" v-auth="'auxiliary:message:add'" :loading="state.loading">
						<SvgIcon name="ele-Plus" class="mr5" /> 新增短信
					</el-button>
				</template>
			</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 }"
					ref="tableRef"
					height="auto"
					auto-resize
					id="auxiliaryMessage"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="pushBusinessText" title="短信类型" width="120"></vxe-column>
					<vxe-column field="sendStateText" title="发送状态" width="90"></vxe-column>
					<vxe-column field="name" title="接收人" min-width="120"></vxe-column>
					<vxe-column field="telNumber" title="接收号码" min-width="120"></vxe-column>
					<vxe-column field="content" title="短信内容" min-width="300"></vxe-column>
					<vxe-column field="sendOrganize" title="发送部门" width="140"></vxe-column>
					<vxe-column field="sendUser" title="发送人" width="120"></vxe-column>
					<vxe-column field="creationTime" title="添加时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="sendTime" title="发送时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.sendTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="orderNo" title="关联工单编号" width="140"></vxe-column>
					<vxe-column field="remark" title="备注说明" min-width="200"></vxe-column>
					<vxe-column field="reason" title="失败原因" min-width="200"></vxe-column>
				</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>
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px">
				<el-form-item label="接收人" prop="UserName">
					<el-input v-model="state.queryParams.UserName" placeholder="接收人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="发送人" prop="SendName">
					<el-input v-model="state.queryParams.SendName" placeholder="发送人" clearable @keyup.enter="handleQuery" />
				</el-form-item>

				<el-form-item label="发送部门" prop="SendOrg">
					<el-input v-model="state.queryParams.SendOrg" placeholder="发送部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="短信内容" prop="SendContent">
					<el-input v-model="state.queryParams.SendContent" placeholder="短信内容" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="短信类型" prop="PushBusiness">
					<el-select v-model="state.queryParams.PushBusiness" placeholder="请选择短信类型" @change="handleQuery" clearable>
						<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="请选择发送状态" @change="handleQuery" clearable>
						<el-option v-for="item in eSendStateData" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(drawerRuleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</template>
		</el-drawer>
		<message-add ref="messageAddRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="auxiliaryMessage">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { messageBase, messageList } from '@/api/auxiliary/message';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
import { formatDate } from '@/utils/formatTime';

const MessageAdd = defineAsyncComponent(() => import('@/views/auxiliary/message/components/Message-add.vue')); // 新增短信
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		PushBusiness: null, // 短信类型
		Status: null, // 推送状态
		TelNumber: null,
		UserName: null,
		crTime: [],
		StartTime: null,
		EndTime: null,
		SendName: null,
		SendOrg: null,
		SendContent: null,
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
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 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');
	messageList(request)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 手动新增短信
const messageAddRef = ref<RefType>();
const addSms = () => {
	messageAddRef.value.openDialog();
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
// 页面加载时
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
