<template>
	<div class="app-container">
		<el-card shadow="always">
			<!--查询-->
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item label="页面名称" prop="roleName">
					<el-input placeholder="页面名称模糊查询" clearable @keyup.enter="handleQuery" style="width: 240px" v-model="state.queryParams.title" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" plain @click="handleQuery">
						<SvgIcon name="elementSearch" />
						搜索
					</el-button>
					<el-button @click="resetQuery">
						<SvgIcon name="elementRefresh" />
						重置
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="box-card">
			<template #header>
				<div class="card-header">
					<span class="card-header-text">页面管理</span>
					<div>
						<el-button type="primary" plain  @click="handleGoForm">
							<SvgIcon name="elementPlus" />
							新增
						</el-button>
					</div>
				</div>
			</template>
			<!--数据表格-->
			<el-table v-loading="state.loading" :data="state.tableData">
				<el-table-column label="页面编码" prop="page_id" :show-overflow-tooltip="true" />
				<el-table-column label="页面标题" prop="title" :show-overflow-tooltip="true" />
				<el-table-column label="创建用户" prop="proprietor_name" :show-overflow-tooltip="true" />
				<el-table-column label="创建时间" prop="create_time" />
				<el-table-column label="更新时间" prop="update_time" />
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
					<template #default="scope">
						<div style="display: flex; align-items: center; justify-content: center; gap: 10px">
							<div>
								<el-tooltip class="box-item" effect="dark" content="编辑" placement="top-start">
									<el-button
										type="primary"
										size="small"
										circle
										@click="handleGoFormEdit(scope.row)"
									>
										<SvgIcon :size="12" name="elementEdit" />
									</el-button>
								</el-tooltip>
							</div>
							<div>
								<el-tooltip class="box-item" effect="dark" content="删除" placement="top-start">
									<el-button type="danger" size="small" circle @click="onTableRowDel(scope.row)">
										<SvgIcon :size="12" name="elementDelete" />
									</el-button>
								</el-tooltip>
							</div>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页设置-->
			<div v-show="state.count > 0">
				<el-divider></el-divider>
				<el-pagination
					background
					:total="state.count"
					:current-page="state.queryParams.pageNum"
					:page-size="state.queryParams.pageSize"
					layout="total, sizes, prev, pager, next, jumper"
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
				/>
			</div>
		</el-card>
		
		<Preview ref="previewModuleRef" :listId="listId" />
	</div>
</template>

<script lang="ts" setup name="Partner">
import { reactive, onMounted, getCurrentInstance, onUnmounted, ref } from 'vue';
import { list,del} from '@/api/system/page';
import Preview from './component/preview.vue';
import { useRouter } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';

const { proxy } = getCurrentInstance() as any;

const listId = 'hQyTUtB5WjC'; // 页面表id
const previewModuleRef = ref();
const router = useRouter();
const state = reactive({
	// 遮罩层
	loading: true,
	// 选中数组
	ids: [],
	// 非单个禁用
	single: true,
	// 非多个禁用
	multiple: true,
	// 弹出层标题
	title: '',
	// 表格数据
	tableData: [],
	// 总条数
	count: 0,
	curQueryParams: {},
	queryParams: {
		// 页码
		pageNum: 1,
		// 每页大小
		pageSize: 10,
	},
});

/** 查询列表 */
const handleQuery = () => {
	state.loading = true;
	// 如果state.queryParams中有空值，则删除
	const params = state.queryParams
	Object.keys(params).forEach((key) => {
		if (!params[key]) {
			delete params[key];
		}
	});
	list(params).then((response: any) => {
		state.tableData = response.data.result;
		state.count = response.data.count;
		state.loading = false;
	});
};

const handleSizeChange = (val: number) => {
	state.queryParams.pageSize = val;
	handleQuery();
};
const handleCurrentChange = (val: number) => {
	state.queryParams.pageNum = val;
	handleQuery();
};
/** 跳转表单设计页面 */
const handleGoForm = () => {
	localStorage.setItem('isNewPageBtnClick', 'true');
	const currentPath = router.resolve({ name: '/page/edit' }).href;
	// 在新标签页中打开
	window.open(currentPath, '_blank');
};

const handleGoFormEdit = (row: object) => {
	proxy.$router.push({
		path: '/page/edit',
		query: {
			pageId: row.page_id,
		},
	});
};


/** 删除按钮操作 */
const onTableRowDel = (row: any) => {
	ElMessageBox({
		message: '是否确认删除的数据项?',
		title: '警告',
		showCancelButton: true,
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(function () {
		return del(row.page_id).then(() => {
			handleQuery();
			ElMessage.success('删除成功');
		});
	});
};

// 页面加载时
onMounted(() => {
	handleQuery();
 });
// 页面卸载时
onUnmounted(() => {
	proxy.mittBus.off('onEditPartnerModule');
});
</script>
