<template>
	<div class="tab-container">
		<div class="tab-toolbar">
			<div class="tool-group">
				<el-button-group>
					<slot name="toolGroup"></slot>
				</el-button-group>
			</div>
			<slot name="toolMid"></slot>
			<div style="flex:1"></div>
			<el-icon class="is-loading" v-show="isLoading"><loading/></el-icon>
			<el-pagination
			      v-model:current-page="pagination.currentPage"
			      v-model:page-size="pagination.pageSize"
			      :page-sizes="pageSizeOpts"
			      :small="false"
			      :disabled="false"
			      :background="false"
			      :layout="pageInfoOpts"
			      :total="pagination.totalRows"
			      
			/>
		</div>
		
		<div class="tab-main">
			<el-table :data="tableDatas" style="width: 100%;height:100%;"  border stripe highlight-current-row v-bind="$attrs">
			     <slot name="tabColumns" :index-generate="indexGenerate"></slot>
			</el-table>
		</div>
		
	</div>
</template>

<script setup>
	import {reactive,ref,onMounted,toRaw,watch} from 'vue'
	import request from '../../utils/request'
	import { ElMessage } from "element-plus"
	
	
	
	const props=defineProps({
		pageSize:Number,
		pageSizeOpts:{
			type:Array,
			default:[10,20,50,100]
		},
		pageInfoOpts:String,
		url:String,
		datas:Array,
		initWhere:Object,
		autoLoad:{
			type:Boolean,
			default:true
		}
	})
	
	const pagination=reactive({
		currentPage:1,
		pageSize:props.pageSize||5,
		totalRows:0,
		pageCount:1,
		watchReload:true //通过监听器重载
	})
	
	const isLoading=ref(false)
	const tableDatas=ref([])
	
	
	onMounted(()=>{
		
		if(props.url&&props.autoLoad){
			query()
		}
		else{
			tableDatas.value=props.datas
		}
	})
	
	let _queryParam={}
	
	
	
	//只监听翻页、页size更改
	watch([
		()=>pagination.currentPage,
		()=>pagination.pageSize,
	],
	   ([newcp,newpz],[oldcp,oldpz])=>{
		
		if(pagination.watchReload){
			load(_queryParam)
		}
		
		
	})
	
	let getReqParam=(queryParams)=>{
		
		return Object.assign({},props.initWhere,toRaw(pagination),queryParams)
		
	}
	
	const getPageInfo=()=>{
		return toRaw(pagination)
	}
	
	const query=(queryParams)=>{
		pagination.watchReload=false
		pagination.currentPage=1  //新的查询页码重置为1,禁止通过监听自动查询，手动查询，便于外部接收查询状态
		return load(queryParams)
	} 
	
	const load=async (queryParams)=>{
		//console.log('query...')
		isLoading.value=true
		try{
			let resp=await request({url: props.url,method: 'post',data: getReqParam(queryParams)})
			isLoading.value=false
			console.log(resp)
			pagination.watchReload=true				
			//pagination.currentPage=resp.currentPage
			
			if(resp.code===0){
				tableDatas.value=resp.data.data
				pagination.totalRows=resp.data.totalRow
				pagination.pageCount = resp.data.pageCount
				_queryParam=queryParams||{}
				
				return 0
			}
			else{
				ElMessage.error(resp.msg||"加载数据失败");
				return -1
			}
			
		}
		catch (e) {
			pagination.watchReload=true
			isLoading.value=false
			ElMessage.error("加载数据失败");
		    console.log(e)
			return -1
		}
		
	}
	
	const indexGenerate=(index)=>{
		return (pagination.currentPage-1) * pagination.pageSize + index + 1
	}
		
	
	
	defineExpose({
		query,
		getPageInfo
		
	})
	
	
</script>

<style scoped>
	.tab-container{
		height:100%;
	}
	.tab-toolbar{
		height:50px;
		padding:10px 10px;
		box-sizing: border-box;
		background-color: #f0f9ff;
		display: flex;
		justify-content:space-between;
		border:1px solid #e3f1f8;
		border-bottom:0px;
	}
	
	.tab-toolbar .is-loading{
		margin:8px 20px;
	}
	
	.tab-main{
		height: calc(100% - 50px);
	}
	
	
</style>