<template>
	<div class="pi-dialog" ref="piDialog">
		<el-dialog ref="dialog" v-model="dialogVisible" :fullscreen="isFullscreen" v-bind="attrs" :show-close="false">
			<template #header>
				<slot name="header">
					<span class="el-dialog__title">{{ title }}</span>
				</slot>
				<div class="pi-dialog__headerbtn">
					<button v-if="showFullscreen" aria-label="fullscreen" type="button" @click="setFullscreen">
						<el-icon v-if="isFullscreen" class="el-dialog__close"><el-icon-bottom-left /></el-icon>
						<el-icon v-else class="el-dialog__close"><el-icon-full-screen /></el-icon>
					</button>
					<button v-if="showClose" aria-label="close" type="button" @click="closeDialog">
						<el-icon class="el-dialog__close"><el-icon-close /></el-icon>
					</button>
				</div>
			</template>
			<div v-loading="loading">
				<slot></slot>
			</div>
			<template #footer>
				<slot name="footer"></slot>
			</template>
		</el-dialog>
	</div>
</template>

<script setup>
import {ref, onMounted, watch, useAttrs} from "vue";
const attrs = useAttrs()

const props = defineProps({
	modelValue: { type: Boolean, default: false },
	title: { type: String, default: "" },
	showClose: { type: Boolean, default: true },
	showFullscreen: { type: Boolean, default: false },
	loading: { type: Boolean, default: false }
});

let dialogVisible = ref(false)
let isFullscreen = ref(false)

onMounted(() => {
	dialogVisible.value = props.modelValue.value
})

watch(() => props.modelValue, () => {
	dialogVisible.value = props.modelValue
	if(dialogVisible.value){
		isFullscreen.value = false
	}
})

//关闭
function closeDialog(){
	dialogVisible.value = false
}

// 恢复窗口
function setDefault(){
	isFullscreen.value = !isFullscreen.value
}

// 全屏
function setFullscreen(){
	isFullscreen.value = !isFullscreen.value
}

// 最小化
function setMinimize(){
	isFullscreen.value = !isFullscreen.value
}
</script>

<style lang="scss" scoped>
.pi-dialog__headerbtn {position: absolute;top: var(--el-dialog-padding-primary);right: var(--el-dialog-padding-primary);}
.pi-dialog__headerbtn button {padding: 0;background: transparent;border: none;outline: none;cursor: pointer;font-size: var(--el-message-close-size,16px);margin-left: 15px;color: var(--el-color-info);}
.pi-dialog__headerbtn button:hover .el-dialog__close {color: var(--el-color-primary);}
.pi-dialog:deep(.el-dialog).is-fullscreen {display: flex;flex-direction: column;top:0px !important;left:0px !important;}
.pi-dialog:deep(.el-dialog).is-fullscreen .el-dialog__header {}
.pi-dialog:deep(.el-dialog).is-fullscreen .el-dialog__body {flex:1;overflow: auto;}
.pi-dialog:deep(.el-dialog).is-fullscreen .el-dialog__footer {padding-bottom: 10px;border-top: 1px solid var(--el-border-color-base);}
</style>
