<template>
	<view class="order-container">
		<scroll-view 
			:scroll-y="!showTypePicker && !showSurfacePicker && !showPackagePicker" 
			class="order-scroll" 
			:show-scrollbar="false"
			:enhanced="true">
			<view class="form-content">
				
				<!-- 模块 1：型材 -->
				<view class="section-card">
					<view class="section-header">
						<view class="blue-bar"></view>
						<text class="section-title">型材</text>
					</view>
					<view class="form-item">
						<text class="label required">型号</text>
						<view class="picker-box" @click="openTypePicker">
							<view class="picker-inner">
								<view class="picker-val" :class="{placeholder: !formData.type}">
									{{ formData.type || '请选择型号' }}
								</view>
								<view class="line-arrow-down"></view>
							</view>
						</view>
					</view>
					<view class="row-flex">
						<view class="half-item">
							<text class="label">名称</text>
							<input class="input-box readonly small-font" v-model="formData.name" disabled placeholder="型号名称" />
						</view>
						<view class="half-item">
							<text class="label">材质</text>
							<input class="input-box readonly small-font" v-model="formData.material" disabled placeholder="材质" />
						</view>
					</view>
				</view>

				<!-- 模块 2：表面处理 -->
				<view class="section-card">
					<view class="section-header">
						<view class="blue-bar"></view>
						<text class="section-title">表面处理</text>
					</view>
					<view class="form-item">
						<text class="label required">表面名称</text>
						<view class="picker-box" @click="openSurfacePicker">
							<view class="picker-inner">
								<view class="picker-val" :class="{placeholder: !formData.surfaceName}">
									{{ formData.surfaceName || '请选择表面名称' }}
								</view>
								<view class="line-arrow-down"></view>
							</view>
						</view>
					</view>
				</view>

				<!-- 模块 3：包装 -->
				<view class="section-card">
					<view class="section-header">
						<view class="blue-bar"></view>
						<text class="section-title">包装</text>
					</view>
					<view class="form-item">
						<text class="label required">包装方式</text>
						<view class="picker-box" @click="openPackagePicker">
							<view class="picker-inner">
								<view class="picker-val" :class="{placeholder: !formData.packageMethod}">
									{{ formData.packageMethod || '请选择包装方式' }}
								</view>
								<view class="line-arrow-down"></view>
							</view>
						</view>
					</view>
				</view>

				<!-- 模块 4：壁厚 -->
				<view class="section-card">
					<view class="section-header">
						<view class="blue-bar"></view>
						<text class="section-title">壁厚</text>
					</view>
					<view class="row-flex">
						<view class="half-item">
							<text class="label required">长度</text>
							<view class="input-wrap">
								<input class="input-box" type="digit" v-model="formData.length" placeholder="请输入" />
								<text class="unit">mm</text>
							</view>
						</view>
						<view class="half-item">
							<text class="label required">壁厚</text>
							<view class="input-wrap">
								<input class="input-box" type="digit" v-model="formData.wallThickness" placeholder="请输入" />
								<text class="unit">mm</text>
							</view>
						</view>
					</view>
				</view>

				<!-- 模块 5：数量 -->
				<view class="section-card">
					<view class="section-header">
						<view class="blue-bar"></view>
						<text class="section-title">数量</text>
					</view>
					<view class="form-item no-margin-bottom">
						<text class="label required">支数</text>
						<input class="input-box full-width" type="number" v-model="formData.count" placeholder="请输入支付支数" />
					</view>
				</view>

				<view class="bottom-placeholder"></view>
			</view>
		</scroll-view>

		<view class="fixed-submit-bar">
			<button class="submit-btn" @click="confirmAddModel">确认并添加</button>
		</view>

		<!-- 型号选择 -->
		<view class="custom-picker-mask" v-if="showTypePicker" @click="closeTypePicker" @touchmove.stop.prevent>
			<view class="picker-popup" @click.stop>
				<view class="popup-header">
					<text class="cancel-text" @click="closeTypePicker">取消</text>
					<text class="popup-title">选择产品型号</text>
					<text class="confirm-text" @click="confirmTypeSelect">确定</text>
				</view>
				<scroll-view scroll-y class="item-list">
					<view class="option-item" v-for="(item, index) in typeList" :key="index" 
						:class="{active: tempSelectedIndex === index}" @click="selectTypeItem(index)">
						<text>{{item}}</text>
						<icon type="success_no_circle" size="16" color="#C1001C" v-if="tempSelectedIndex === index"></icon>
					</view>
				</scroll-view>
			</view>
		</view>

		<!-- 表面处理选择 -->
		<view class="custom-picker-mask" v-if="showSurfacePicker" @click="closeSurfacePicker" @touchmove.stop.prevent>
			<view class="picker-popup" @click.stop>
				<view class="popup-header">
					<text class="cancel-text" @click="closeSurfacePicker">取消</text>
					<text class="popup-title">选择表面处理</text>
					<text class="confirm-text" @click="confirmSurfaceSelect">确定</text>
				</view>
				<scroll-view scroll-y class="item-list">
					<view class="option-item" v-for="(item, index) in surfaceList" :key="index" 
						:class="{active: tempSurfaceIndex === index}" @click="selectSurfaceItem(index)">
						<text>{{item}}</text>
						<icon type="success_no_circle" size="16" color="#C1001C" v-if="tempSurfaceIndex === index"></icon>
					</view>
				</scroll-view>
			</view>
		</view>

		<!-- 包装方式选择 -->
		<view class="custom-picker-mask" v-if="showPackagePicker" @click="closePackagePicker" @touchmove.stop.prevent>
			<view class="picker-popup" @click.stop>
				<view class="popup-header">
					<text class="cancel-text" @click="closePackagePicker">取消</text>
					<text class="popup-title">选择包装方式</text>
					<text class="confirm-text" @click="confirmPackageSelect">确定</text>
				</view>
				<scroll-view scroll-y class="item-list">
					<view class="option-item" v-for="(item, index) in packageList" :key="index" 
						:class="{active: tempPackageIndex === index}" @click="selectPackageItem(index)">
						<text>{{item}}</text>
						<icon type="success_no_circle" size="16" color="#C1001C" v-if="tempPackageIndex === index"></icon>
					</view>
				</scroll-view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				showTypePicker: false, showSurfacePicker: false, showPackagePicker: false,
				tempSelectedIndex: -1, tempSurfaceIndex: -1, tempPackageIndex: -1,
				typeList: ['TY0018', 'TY0019', 'TY0020', 'TY0021', 'TY0022', 'TY0023', 'TY0024', 'TY0025', 'TY0026', 'TY0027', 'TY0028'],
				surfaceList: ['PL坯料', '阳极氧化', '电泳涂漆', '粉末喷涂', '氟碳喷涂', '木纹转印'],
				packageList: ['不贴膜+3点捆扎', '贴膜+纸箱', '气泡膜包装', '简易编织袋', '木托架包装'],
				formData: {
					type: '', name: '', material: '',
					surfaceName: '', packageMethod: '', length: '',
					wallThickness: '', count: ''
				}
			}
		},
		methods: {
			openTypePicker() { if (this.formData.type) this.tempSelectedIndex = this.typeList.indexOf(this.formData.type); this.showTypePicker = true; },
			closeTypePicker() { this.showTypePicker = false; },
			selectTypeItem(index) { this.tempSelectedIndex = index; },
			confirmTypeSelect() { if (this.tempSelectedIndex === -1) return; const val = this.typeList[this.tempSelectedIndex]; this.formData.type = val; this.formData.name = '工业铝材'; this.formData.material = (this.tempSelectedIndex % 2 === 0) ? '6063-T5' : '6061-T6'; this.closeTypePicker(); },
			openSurfacePicker() { if (this.formData.surfaceName) this.tempSurfaceIndex = this.surfaceList.indexOf(this.formData.surfaceName); this.showSurfacePicker = true; },
			closeSurfacePicker() { this.showSurfacePicker = false; },
			selectSurfaceItem(index) { this.tempSurfaceIndex = index; },
			confirmSurfaceSelect() { if (this.tempSurfaceIndex === -1) return; this.formData.surfaceName = this.surfaceList[this.tempSurfaceIndex]; this.closeSurfacePicker(); },
			openPackagePicker() { if (this.formData.packageMethod) this.tempPackageIndex = this.packageList.indexOf(this.formData.packageMethod); this.showPackagePicker = true; },
			closePackagePicker() { this.showPackagePicker = false; },
			selectPackageItem(index) { this.tempPackageIndex = index; },
			confirmPackageSelect() { if (this.tempPackageIndex === -1) return; this.formData.packageMethod = this.packageList[this.tempPackageIndex]; this.closePackagePicker(); },
			confirmAddModel() {
				const fields = [
					{ key: 'type', label: '型号' },
					{ key: 'surfaceName', label: '表面名称' },
					{ key: 'packageMethod', label: '包装方式' },
					{ key: 'length', label: '长度' },
					{ key: 'wallThickness', label: '壁厚' },
					{ key: 'count', label: '支数' }
				];
				for (let item of fields) {
					if (!this.formData[item.key]) {
						uni.showToast({ title: `请完善项目：${item.label}`, icon: 'none' });
						return;
					}
				}
				uni.$emit('add_order_item', JSON.parse(JSON.stringify(this.formData)));
				uni.navigateBack();
			}
		}
	}
</script>

<style scoped>
	/deep/ ::-webkit-scrollbar { display: none !important; width: 0 !important; height: 0 !important; }
	.order-container { width: 100%; height: 100vh; background: #f7f8fa; display: flex; flex-direction: column; overflow: hidden; }
	.order-scroll { flex: 1; height: 0; }
	.form-content { 
		padding: 30rpx; 
		padding-bottom: calc(180rpx + env(safe-area-inset-bottom)); 
	}
	.section-card { background: #fff; border-radius: 24rpx; padding: 30rpx; margin-bottom: 30rpx; box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.02); }
	.section-header { display: flex; align-items: center; margin-bottom: 40rpx; }
	.blue-bar { width: 8rpx; height: 32rpx; background: #C1001C; border-radius: 4rpx; margin-right: 16rpx; }
	.section-title { font-size: 32rpx; font-weight: bold; color: #333; }
	.form-item { margin-bottom: 40rpx; }
	.no-margin-bottom { margin-bottom: 0; }
	.row-flex { display: flex; justify-content: space-between; }
	.half-item { width: 48%; }
	.label { font-size: 28rpx; color: #333; margin-bottom: 20rpx; display: block; font-weight: 500; }
	.label.required::after { content: ' *'; color: #ff4d4f; font-weight: bold; margin-left: 4rpx; }
	.picker-box { background: #f9fafc; border-radius: 12rpx; height: 100rpx; border: 1rpx solid #eee; display: flex; align-items: center; }
	.picker-inner { width: 100%; height: 100%; display: flex; align-items: center; justify-content: space-between; padding: 0 30rpx; box-sizing: border-box; }
	.picker-val { font-size: 30rpx; color: #333; }
	.picker-val.placeholder { color: #ccc; font-size: 28rpx; }
	.line-arrow-down { width: 14rpx; height: 14rpx; border-right: 3rpx solid #bbb; border-bottom: 3rpx solid #bbb; transform: rotate(45deg); margin-top: -8rpx; margin-right: 4rpx; }
	.input-wrap { position: relative; width: 100%; }
	.input-box { font-size: 30rpx; color: #333; width: 100%; height: 100rpx; background: #f9fafc; border-radius: 12rpx; padding: 0 80rpx 0 30rpx; box-sizing: border-box; border: 1rpx solid #eee; }
	.input-box.readonly { background: #f8f9fa; color: #666; border-color: #f0f0f0; }
	.input-box.full-width { padding: 0 30rpx; }
	.unit { position: absolute; right: 24rpx; top: 50%; transform: translateY(-50%); font-size: 26rpx; color: #999; font-weight: 500; }
	.small-font { font-size: 26rpx !important; }
	
	.fixed-submit-bar { 
		position: fixed; 
		bottom: 0; 
		left: 0; 
		width: 100%; 
		background: #fff; 
		padding: 20rpx 30rpx calc(env(safe-area-inset-bottom) + 20rpx); 
		box-sizing: border-box; 
		box-shadow: 0 -10rpx 30rpx rgba(0,0,0,0.05);
		z-index: 100;
	}
	.submit-btn { 
		background: #C1001C; 
		color: #fff; 
		height: 100rpx; 
		border-radius: 50rpx; 
		display: flex; 
		align-items: center; 
		justify-content: center; 
		font-size: 34rpx; 
		font-weight: bold; 
		border: none; 
	}
	.submit-btn:active { opacity: 0.9; transform: scale(0.98); }
	.custom-picker-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.4); z-index: 1000; display: flex; align-items: flex-end; }
	.picker-popup { width: 100%; background: #fff; border-radius: 32rpx 32rpx 0 0; padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx); animation: slideUp 0.15s ease-out; }
	@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
	.popup-header { display: flex; justify-content: space-between; align-items: center; padding: 30rpx 40rpx; border-bottom: 1rpx solid #f0f0f0; }
	.popup-title { font-size: 32rpx; font-weight: bold; color: #333; }
	.cancel-text, .confirm-text { font-size: 30rpx; padding: 10rpx; }
	.confirm-text { color: #C1001C; font-weight: bold; }
	.item-list { max-height: 50vh; padding: 0 40rpx; }
	.option-item { height: 110rpx; display: flex; align-items: center; justify-content: space-between; border-bottom: 1rpx solid #f8f8f8; font-size: 32rpx; color: #333; }
	.option-item.active { color: #C1001C; font-weight: bold; }
	.bottom-placeholder { height: 60rpx; }
</style>
