<template>
	<view class="dialog-overlay" v-if="visible" @click="handleOverlayClick">
		<view class="dialog-container" @click.stop>
			<!-- 弹窗标题 -->
			<view class="dialog-header">
				<text class="dialog-title" :style="{ color: titleColor }">{{ title }}</text>
			</view>
			
			<!-- 弹窗内容 -->
			<view class="dialog-content">
				<slot name="content">
					<view class="default-content" >
						<view v-for="item in formatContent(content)" :key="item">{{item}}</view>
					</view>
				</slot>
			</view>
			
			<!-- 弹窗底部按钮 -->
			<view class="dialog-footer cancelFooter" v-if="showCancel">
				<view 
					class="dialog-btn"
					
					@click="oncancel"
				>
					<text class="btn-text">{{cancelText}}</text>
				</view>
				
			</view>
			<view class="dialog-footer okFooter" v-else-if="showConfirm">
				<view 
					class="dialog-btn okBtn"
					
					@click="onconfirm"
				>
					<text class="btn-text">{{confirmText}}</text>
				</view>
				
			</view>
			<view class="dialog-footer" v-else>
				<view 
					class="dialog-btn cancel"
					
					@click="oncancel"
				>
					<text class="btn-text">{{ cancelText }}</text>
				</view>
				<view
					class="dialog-btn confirm"
					@click="onconfirm"
				>
					<text class="btn-text">{{ confirmText }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { defineProps, defineEmits } from 'vue'
	
	const props = defineProps({
		visible: {
			type: Boolean,
			default: false
		},
		titleColor: {
			type: String,
			default: '#8B2316'
		},
		title: {
			type: String,
			default: '温馨提示'
		},
		content: {
			type: String,
			default: ''
		},
		cancelText:{
			type: String,
			default: '取消'
		},
		confirmText:{
			type: String,
			default: '确定'
		},
		showCancel:{
		   type:Boolean,
		   default:false,   
		},
		showConfirm:{
		   type:Boolean,
		   default:false,   
		},
		closeOnOverlay: {
			type: Boolean,
			default: true
		}
	})
	const formatContent=(val)=>{
		console.log(val)
		if(!val) return [];
		// 将字符串按 \n 切割成数组，并过滤掉空字符串
		const lines = val.split('<br>').filter(line => line.trim() !== '');
		console.log('格式化后的内容数组:', lines);
		return lines;
	}
	const emit = defineEmits(['close','confirm'])
	

	
	const oncancel=()=>{
		emit('close');
	  
	}
	const onconfirm=()=>{
		emit('confirm')
		emit('close');
		
	}
</script>

<style lang="scss" scoped>
	.dialog-overlay {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 9999;
	}
	
	.dialog-container {
		position: relative;
		z-index:100;
		background-color: #ffffff;
		border-radius: 20rpx;
		width: 92%;
		
		overflow: hidden;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.2);
	}
	
	.dialog-header {
		padding: 40rpx 40rpx 20rpx;
		text-align: center;
		.dialog-title {
			font-size: 36rpx;
			color: #8B2316;
			
		}
	}
	
	.dialog-content {
		padding: 40rpx;
		min-height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		
		.default-content {
			white-space:pre-wrap;
			font-size: 32rpx;
			color: #333;
			line-height: 1.5;
			text-align: center;
		}
	}
	
	.dialog-footer {
		display: flex;
		padding:0 40rpx;
		
		.dialog-btn {
			flex: 1;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-top: 1rpx solid #f0f0f0;
			border-right: 1rpx solid #f0f0f0;
			transition: background-color 0.2s;
			
			&:last-child {
				border-right: none;
			}
			
			&:active {
				background-color: #f5f5f5;
			}
			
			&.cancel {
				.btn-text {
					color: #999999;
				}
			}
			
			&.confirm {
				.btn-text {
					color: #8B2316;
					font-weight: 500;
				}
			}
			
			&.default {
				.btn-text {
					color: #333333;
				}
			}
			
			.btn-text {
				font-size: 32rpx;
			}
		}
	}
	.cancelFooter,.okFooter{
		padding: 0;
	}
</style>
