<template>
	<div class="auxiliary-number-info-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="手机编码" prop="MobileCode">
					<el-input v-model="state.queryParams.MobileCode" placeholder="手机编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="座机编码" prop="TelCode">
					<el-input v-model="state.queryParams.TelCode" placeholder="座机编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</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="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,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
				<template #buttons>
					<el-button type="primary" @click="onAdd" v-auth="'auxiliary:numberInfo: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, height: 30, useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="auxiliaryNumberInfo"
					:custom-config="{ storage: true }"
					showHeaderOverflow
					:params="{ exportMethod: exportNumberInfo, exportParams: requestParams }"
				>
					<vxe-column field="mobileCode" title="手机编码"></vxe-column>
					<vxe-column field="telCode" title="座机编码"></vxe-column>
					<vxe-column field="oFlagText" title="运营商"></vxe-column>
					<vxe-column field="operatorName" title="卡类型"></vxe-column>
					<vxe-column field="mobileAreaName" title="归属地"></vxe-column>
					<vxe-column field="creatorName" title="创建人"></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 title="操作" fixed="right" width="120" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onEdit(row)" title="编辑" v-auth="'auxiliary:numberInfo:edit'"> 编辑 </el-button>
							<el-button link type="danger" @click="onDelete(row)" title="删除" v-auth="'auxiliary:numberInfo:delete'"> 删除 </el-button>
						</template>
					</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" :disabled="state.loading">
				<el-form-item label="归属地" prop="MobileAreaName">
					<el-input v-model.trim="state.queryParams.MobileAreaName" placeholder="归属地" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item prop="OFlag" label="运营商">
					<el-select v-model="state.queryParams.OFlag" placeholder="请选择运营商" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in OFlagOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="卡类型" prop="OperatorName">
					<el-input v-model.trim="state.queryParams.OperatorName" placeholder="卡类型" clearable @keyup.enter="handleQuery" />
				</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>
		<!-- 新增 -->
		<number-info-add ref="numberInfoAddRef" @updateList="queryList" />
		<!-- 编辑 -->
		<number-info-edit ref="numberInfoEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="auxiliaryNumberInfo">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { deleteNumberInfo, getNumberInfoBaseData, getNumberInfoList,exportNumberInfo } from '@/api/auxiliary/numberInfo';
import Other from '@/utils/other';

// 引入组件
const NumberInfoAdd = defineAsyncComponent(() => import('@/views/auxiliary/numberInfo/components/Number-info-add.vue')); // 新增
const NumberInfoEdit = defineAsyncComponent(() => import('@/views/auxiliary/numberInfo/components/Number-info-edit.vue')); // 编辑
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		MobileCode: null, // 手机编码
		TelCode: null, // 座机编码
		OFlag: null, // 运营商
		MobileAreaName: null, // 归属地
		OperatorName: null, // 卡类型
	},
	tableData: [], // 列表数据
	loading: false, // 加载
	total: 0, // 总条数
});
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 班次列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = async () => {
	state.loading = true;
	try {
		requestParams.value = Other.deepClone(state.queryParams);
		const response = await getNumberInfoList(requestParams.value);
		state.tableData = response.result?.items ?? [];
		state.total = response.result?.total ?? 0;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};
/** 重置按钮操作 */
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 numberInfoAddRef = ref<RefType>();
const onAdd = () => {
	numberInfoAddRef.value.openDialog();
};
// 编辑班次
const numberInfoEditRef = ref<RefType>();
const onEdit = (row: any) => {
	numberInfoEditRef.value.openDialog(row.id);
};
// 删除班次
const onDelete = (row: any) => {
	ElMessageBox.confirm(`确定删除该号码信息配置？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
	})
		.then(() => {
			deleteNumberInfo(row.id).then(() => {
				queryList();
				ElMessage.success('删除成功');
			});
		})
		.catch(() => {
			console.log('取消删除');
		});
};
// 查询基础数据
const OFlagOptions = ref<EmptyArrayType>([]); // 运营商
const getBaseData = async () => {
	try {
		const { result } = await getNumberInfoBaseData();
		OFlagOptions.value = result ?? [];
	} catch (e) {
		console.log(e);
	}
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
