<template>
	<view class="padding page">
		<view class="padding bg-white radius">
			<view class="flex justify-between">
				<view>
					<view class="text-bold text-black">{{ areaData.name || '区域名称' }}</view>
					<view class="margin-top flex align-center">
						<text>颜色：</text>
						<view class="color-dot" :style="{ backgroundColor: areaData.color }"></view>
						<text class="margin-left-xs">{{ areaData.color }}</text>
					</view>
				</view>
				<view>
					<button class="bg-blue cu-btn" @click="openEditPopup" >编辑</button>
					<button class="bg-red cu-btn margin-left">删除</button>
				</view>
			</view>
		</view>
		<button class="cuIcon-add bg-blue round margin-top-xl">新增公司区域</button>
		
		<!-- 编辑区域弹出框 -->
		<u-popup :show="showEditPopup" mode="center" round="20" @close="showEditPopup = false">
			<view class="popup-content">
				<view class="popup-header">
					<view class="popup-title text-bold">编辑区域</view>
					<view class="popup-close" @click="showEditPopup = false">×</view>
				</view>
				<view class="popup-body">
					<!-- 区域名称 -->
					<view class="flex margin-bottom-sm">
						<view>区域名称</view>
						<view class="text-red">*</view>
					</view>
					<up-input v-model="formData.name" placeholder="请输入区域名称"></up-input>
					
					<!-- 区域颜色 -->
					<view class="flex margin-bottom-sm margin-top">
						<view>区域颜色</view>
						<view class="text-red">*</view>
					</view>
					<view class="flex align-center">
						<up-input v-model="formData.color" placeholder="#ef4444" class="flex-sub"></up-input>
						<view class="color-preview" :style="{ backgroundColor: formData.color }"></view>
					</view>
					
					<!-- 预设颜色 -->
					<view class="margin-top margin-bottom-sm text-gray">预设颜色</view>
					<view class="color-grid">
						<view 
							v-for="(color, index) in presetColors" 
							:key="index" 
							class="color-item" 
							:style="{ backgroundColor: color }"
							@click="selectColor(color)"
						></view>
					</view>
				</view>
				<view class="popup-footer">
					<button class="btn-cancel" @click="showEditPopup = false">取消</button>
					<button class="btn-confirm bg-blue" @click="handleEdit">确定</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script setup>
import { ref, reactive } from 'vue';

// 弹出框显示状态
const showEditPopup = ref(false);

// 当前区域数据（用于列表显示）
const areaData = reactive({
	name: '区域名称',
	color: '#ef4444'
});

// 表单数据
const formData = reactive({
	name: '',
	color: '#ef4444'
});

// 预设颜色
const presetColors = [
	'#2563eb', '#ef4444', '#10b981', '#f59e0b', '#6366f1', '#ec4899', '#06b6d4',
	'#84cc16', '#f97316', '#4f46e5', '#dc2626', '#f59e0b', '#d97706', '#8b5cf6',
	'#db2777'
];

// 打开编辑弹出框
const openEditPopup = () => {
	// 初始化表单数据为当前区域数据
	formData.name = areaData.name;
	formData.color = areaData.color;
	showEditPopup.value = true;
};

// 选择预设颜色
const selectColor = (color) => {
	formData.color = color;
};

// 确定编辑
const handleEdit = () => {
	if (!formData.name) {
		uni.showToast({ title: '请输入区域名称', icon: 'none' });
		return;
	}
	if (!formData.color) {
		uni.showToast({ title: '请选择区域颜色', icon: 'none' });
		return;
	}
	
	// 更新区域数据
	areaData.name = formData.name;
	areaData.color = formData.color;
	
	showEditPopup.value = false;
	uni.showToast({
		title: '编辑成功',
		icon: 'success'
	});
};
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
		background: #EBF2FC;
	}
	
	.popup-content {
		width: 600rpx;
		background: #fff;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.popup-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1rpx solid #eee;
	}

	.popup-title {
		font-size: 32rpx;
	}

	.popup-close {
		font-size: 40rpx;
		color: #999;
		cursor: pointer;
	}

	.popup-body {
		padding: 30rpx;
	}

	.popup-footer {
		display: flex;
		padding: 20rpx 30rpx 30rpx;
		
		button {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
			font-size: 30rpx;
			margin: 0 10rpx;
			
			&::after {
				border: none;
			}
		}
		
		.btn-cancel {
			background: #f5f5f5;
			color: #666;
		}
		
		.btn-confirm {
			color: #fff;
		}
	}
	
	// 颜色预览
	.color-preview {
		width: 80rpx;
		height: 80rpx;
		border-radius: 8rpx;
		margin-left: 20rpx;
		flex-shrink: 0;
	}
	
	// 预设颜色网格
	.color-grid {
		display: flex;
		flex-wrap: wrap;
		gap: 16rpx;
	}
	
	.color-item {
		width: 70rpx;
		height: 70rpx;
		border-radius: 12rpx;
		cursor: pointer;
		transition: transform 0.2s;
		
		&:active {
			transform: scale(0.9);
		}
	}
	
	// 颜色圆点
	.color-dot {
		width: 30rpx;
		height: 30rpx;
		border-radius: 6rpx;
		flex-shrink: 0;
		margin-left: 10rpx;
	}
</style>