<template>
	<div class="system-user-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input   v-model="tableData.param.keyword" placeholder="请输入企业名称" style="max-width: 180px"> </el-input>
				<el-button   type="primary" class="ml10" @click="onSearch">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="title" label="协议标题" show-overflow-tooltip></el-table-column>
				<el-table-column prop="content" label="协议内容" show-overflow-tooltip></el-table-column>
				<el-table-column prop="code" label="协议编码" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" type="text" @click="onOpenEdit(scope.row.id)">修改</el-button>
						<!-- <el-button size="small" type="text" @click="onPhotoShow(scope.row)">相册</el-button> -->
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="tableData.param.pageNum"
				background
				v-model:page-size="tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<edit ref="editRef" @finished="initTableData" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import edit from './component/edit.vue';
import Protocol from '/@/api/Protocol';
const tagInfo = '[/src/views/protocol/index.vue]:';
// 定义接口来定义对象的类型
interface TableDataRow {
	name: string;
	size: string;
	register_amount: string;
	establish_time: string;
	detail: string;
	phone: string;
	logo: string;
}

interface TableDataState {
	tableData: {
		data: Array<any>;
		total: number;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
		};
	};
}

export default defineComponent({
	name: 'systemUser',
	components: { edit},
	setup() {
		const editRef = ref();
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
					keyword: '',
				},
			},
			accountShow: false,
		});
		const onSearch = () => {
			state.tableData.param.pageNum = 1;
			initTableData();
		};
		// 初始化表格数据
		const initTableData = async () => {
			const data: Array<any> = [];
			let res = await Protocol.list(state.tableData.param.keyword, state.tableData.param.pageNum, state.tableData.param.pageSize);
			if (res.code != 0) {
				ElMessage.error(res.msg);
				return;
			}
			state.tableData.total = res.data.total;
			state.tableData.data = res.data.data;
		};
		// 打开新增或修改用户弹窗
		const onOpenEdit = (id = 0) => {
			console.log('onOpenEdit', id);
			if (id > 0) {
				editRef.value.openDialog(id);
			} else {
				addRef.value.openDialog(0);
			}
		};
		
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
			initTableData();
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
			initTableData();
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			editRef,
			onOpenEdit,
			onHandleSizeChange,
			onHandleCurrentChange,
			onSearch,
			initTableData,
			...toRefs(state),
		};
	},
});
</script>
