<!--托盘信息查询-->
<template>
	<view class="container">
		<cu-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content"> 整巷道清空</block>
		</cu-custom>
		<view class="example">
			<uni-forms ref="form" labelWidth="80px">
				<uni-forms-item label="货位编号" name="locCode">
					<view class="flex-twice  padding-0 radius">
						<uni-easyinput v-model="locCode" placeholder="请扫描货位编号" @blur="queryLocInfo" />
					</view>
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="handleClear">一键清空</button>
			
			<!-- 货位信息表格 -->
			<view v-if='locList.length>0'>
				<view class="uni-container">
					<uni-table border stripe emptyText="暂无更多数据">
						<uni-tr>
							<uni-th style="flex: 1;" align="center">货位编号</uni-th>
							<uni-th style="flex: 1;" align="center">托盘编号</uni-th>
						</uni-tr>
						<uni-tr v-for="(item, index) in locList" :key="index">
							<uni-td style="flex: 1;" align="center">{{ item.locCode }}</uni-td>
							<uni-td style="flex: 1;" align="center">{{ item.palletCode || '--' }}</uni-td>
						</uni-tr>
					</uni-table>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { queryEmptyLoc, clearEmptyLoc } from "@/api/wms/agvRf.js"
	
	export default {
		data() {
			return {
				locCode: "",       // 输入的货位编号
				locList: [],       // 查询到的货位列表
				whCode: this.$store.state.user.warehouse[0]?.warehouseCode,
				userCode: this.$store.state.user.name,
				rules: {
					locCode: {
						rules: [{
							required: true,
							errorMessage: '货位编号不能为空'
						}]
					}
				}
			};
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			// 查询货位信息
			async queryLocInfo() {
				if (!this.locCode) return
				
				this.$modal.loading("查询中...")
				try {
					const { code, data } = await queryEmptyLoc({ 
						locCode: this.locCode ,
						userCode: this.userCode,
						warehouseCode: this.whCode
						})
					if (code === 200) {
						this.locList = data || []
						if (data.length === 0) {
							this.$modal.msg("该巷道暂无货位信息")
						}
					}
				} catch (e) {
					console.error(e)
				} finally {
					this.$modal.closeLoading()
				}
			}, 
			// 显示清空确认对话框
			showClearConfirm() {
				if (!this.locCode) {
					return this.$modal.msg("请先输入货位编号")
				}
				
				uni.showModal({
					title: '确认清空',
					content: `确定要清空货位【${this.locCode}】下的所有托盘吗？`,
					confirmText: '确定清空',
					confirmColor: '#DD524D',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							this.handleClear()
						}
					}
				})
			},
			
			// 执行清空操作
			async handleClear() {
				this.$modal.loading("清空中...")
				try {
					const { code, msg } = await clearEmptyLoc({ 
						locCode: this.locCode ,
						userCode: this.userCode,
						warehouseCode: this.whCode
					})
					if (code === 200) {
						this.$modal.msgSuccess("清空成功")
						this.locList = []  // 清空列表
						this.locCode = ""  // 清空输入
					} else {
						this.$modal.msgError(msg || "清空失败")
					}
				} catch (e) {
					console.error(e)
					this.$modal.msgError("请求失败")
				} finally {
					this.$modal.closeLoading()
				}
			}
					 
		}
	}
</script>

<style lang="scss">
	/* 原有样式保留 */
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.button {
		margin: 15px 0;
	}
</style>