<template>
	<view class="container">
		<!-- 主内容区域 -->
		    <scroll-view class="content" scroll-y="true">
				
		      <!-- 基础信息 -->
		      <view class="section">
		        <view class="section-header">
		          <text class="section-title">基础信息</text>
		        </view>
		        <view class="info-card" v-if="pallet">
		          <view class="info-row">
		            <text class="label">工装托盘编号:</text>
		            <text class="value">{{pallet.palletNo || '-' }}</text>
		          </view>
		         <!-- <view class="info-row">
		            <text class="label">外形尺寸:</text>
		            <text class="value">{{pallet.size || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">载重:</text>
		            <text class="value">{{pallet.load || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">使用状态:</text>
		            <text class="value status-used">{{ retType(pallet.palletStatus,3) || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">创建日期:</text>
		            <text class="value">{{pallet.createDate || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">备注:</text>
		            <text class="value">{{pallet.remark || '-' }}</text>
		          </view> -->
		          <view class="info-row">
		            <text class="label">转运状态:</text>
		            <text class="value status-transit">{{ retType(pallet.status,1)|| '-' }}</text>
		          </view>
		         <!-- <view class="info-row">
		            <text class="label">当前转运状态:</text>
		            <text class="value">{{pallet.deliveryId || '-' }}</text>
		          </view> -->
		          <view class="info-row">
		            <text class="label">存放部门:</text>
		            <text class="value">{{ retType(pallet.departCode,2) || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">存放工场:</text>
		            <text class="value">{{pallet.workshop || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">存放区域:</text>
		            <text class="value">{{pallet.location || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">存放地址:</text>
		            <text class="value">{{pallet.address || '-' }}</text>
		          </view>
		           <view class="info-row">
		            <text class="label">确认时间:</text>
		            <text class="value">{{pallet.confirmTime || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">确认人员:</text>
		            <text class="value">{{pallet.confirmName || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">存放天数:</text>
		            <text class="value">{{pallet.stayDays || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">装盘船只:</text>
		            <text class="value">{{pallet.loadProject || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">装盘安装托盘表号:</text>
		            <text class="value">{{pallet.loadInst || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">装盘数量:</text>
		            <text class="value">{{pallet.loadQty || '-' }}</text>
		          </view>
				  <view class="info-row">
				    <text class="label">装盘信息:</text>
				    <text class="value">{{pallet.loadDesc || '-' }}</text>
				  </view>
				  <view class="info-row" v-if="pallet.status=='03'||pallet.status=='04'">
				    <text class="label">去向部门:</text>
				    <text class="value">{{ retType(pallet.toDepartCode,2) || '-' }}</text>
				  </view>
				  <view class="info-row" v-if="pallet.status=='03'||pallet.status=='04'">
				    <text class="label">去向工场:</text>
				    <text class="value">{{pallet.toWorkshop || '-' }}</text>
				  </view>
				  <view class="info-row" v-if="pallet.status=='03'||pallet.status=='04'">
				    <text class="label">去向位置:</text>
				    <text class="value">{{pallet.toLocation || '-' }}</text>
				  </view>
				  <view class="info-row" v-if="pallet.status=='03'||pallet.status=='04'">
				    <text class="label">去向地址:</text>
				    <text class="value">{{pallet.toAddress || '-' }}</text>
				  </view>
				  <view class="info-row">
				    <text class="label">转运时间:</text>
				    <text class="value">{{pallet.fromTime || '-' }}</text>
				  </view>
				  <view class="info-row">
				    <text class="label">转运人员:</text>
				    <text class="value">{{pallet.fromName || '-' }}</text>
				  </view>
		          <!-- <view class="info-row">
		            <text class="label">装盘时间:</text>
		            <text class="value">{{pallet.loadTime || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">装盘人:</text>
		            <text class="value">{{pallet.loadName || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">配送单号:</text>
		            <text class="value">{{pallet.deliveryNo || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">车辆牌号:</text>
		            <text class="value">{{pallet.deliveryCar || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">配送/转运部门:</text>
		            <text class="value">{{ retType(pallet.fromDepartCode,2)|| '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">配送/转运工场:</text>
		            <text class="value">{{pallet.fromWorkshop || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">配送/转运区域:</text>
		            <text class="value">{{pallet.fromLocation || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">配送/转运地址:</text>
		            <text class="value">{{pallet.fromAddress || '-' }}</text>
		          </view>
		           
		          <view class="info-row">
		            <text class="label">配送/转运人员电话:</text>
		            <text class="value">{{pallet.fromPhone || '-' }}</text>
		          </view> -->
		          <!-- <view class="info-row">
		            <text class="label">配送/转运备注:</text>
		            <text class="value">{{pallet.fromRemark || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">去向接收人员:</text>
		            <text class="value">{{pallet.toName || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">去向接收人员电话:</text>
		            <text class="value">{{pallet.toPhone || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收部门:</text>
		            <text class="value">{{ retType(pallet.receiveDepartCode,2) || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收工场:</text>
		            <text class="value">{{pallet.receiveWorkshop || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收区域:</text>
		            <text class="value">{{pallet.receiveLocation || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收地址:</text>
		            <text class="value">{{pallet.receiveAddress || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收时间:</text>
		            <text class="value">{{pallet.receiveTime || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收人员:</text>
		            <text class="value">{{pallet.receiveName || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收人员电话:</text>
		            <text class="value">{{pallet.receivePhone || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">接收备注:</text>
		            <text class="value">{{pallet.receiveRemark || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">卸盘时间:</text>
		            <text class="value">{{pallet.finishTime || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">卸盘人员:</text>
		            <text class="value">{{pallet.finishName || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">卸盘人员电话:</text>
		            <text class="value">{{pallet.finishPhone || '-' }}</text>
		          </view>
		          <view class="info-row">
		            <text class="label">卸盘备注:</text>
		            <text class="value">{{pallet.finishRemark || '-' }}</text>
		          </view> -->
		        </view>
		      </view>
		
		      <!-- 装盘明细 -->
		      <view class="section">
		        <view class="section-header">
		          <text class="section-title">装盘明细（{{pallet&&pallet.instdetailList?pallet.instdetailList.length:0}}）</text>
		        </view>
		        <view class="list-container" v-if="pallet&&pallet.instdetailList">
		          <view v-for="(item, index) in pallet.instdetailList" :key="index" class="list-item">
					  
					<view class="item-header" @click="gotocxDetail(item.instno,item.project)">
						<text class="item-tip">{{ item.groupDesc }} ({{ item.qty?parseInt(item.qty):'0'}})</text>
					</view>
					
					<view v-for="(row, ii) in item.items"> 
		            <view class="item-header" @click="toggleDetail(ii)">
		              <text class="item-title">{{ row.assemcode }} ({{ row.qty?parseInt(row.qty):'0' }})</text>
		              <uni-icons :type="expandedIndex === ii ? 'arrowup' : 'arrowdown'" size="16" color="#999"></uni-icons>
		            </view>
		            <view v-if="expandedIndex === ii" class="item-content">
		              <view class="detail-row">
		                <text class="label">工程编号:</text>
		                <text class="value">{{ row.project }}</text>
		              </view>
					  <view class="detail-row">
					    <text class="label">安装托盘表号:</text>
					    <text class="value">{{ row.instno }}</text>
					  </view>
		              <view class="detail-row">
		                <text class="label">制作图号:</text>
		                <text class="value">{{ row.dwgno }}</text>
		              </view>
		              <!-- <view class="detail-row">
		                <text class="label">分段号:</text>
		                <text class="value">{{ row.section }}</text>
		              </view> -->
		              <view class="detail-row">
		                <text class="label">规格:</text>
		                <text class="value">{{ row.spec }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">材质:</text>
		                <text class="value">{{ row.grd }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">数量:</text>
		                <text class="value">{{ row.qty }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">重量:</text>
		                <text class="value">{{ row.weight }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">表面处理:</text>
		                <text class="value">{{ row.treatment }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">涂装代码:</text>
		                <text class="value">{{ row.paintcode }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">修改类型:</text>
		                <text class="value">{{ row.modifyType }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">修改卡号:</text>
		                <text class="value">{{ row.modifyKh }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">修改通知单号:</text>
		                <text class="value">{{ row.modifyDwgno }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">修改日期:</text>
		                <text class="value">{{ row.modifyDate }}</text>
		              </view>
		              <!-- <view class="detail-row">
		                <text class="label">生产状态:</text>
		                <text class="value">{{ row.status }}</text>
		              </view> -->
		             <!-- <view class="detail-row">
		                <text class="label">状态日期:</text>
		                <text class="value">{{ row.statusDate }}</text>
		              </view> -->
		              <view class="detail-row">
		                <text class="label">制作单位:</text>
		                <text class="value">{{ row.manuDept }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">处理单位:</text>
		                <text class="value">{{ row.treatDept }}</text>
		              </view>
		              <!-- <view class="detail-row">
		                <text class="label">配送单位:</text>
		                <text class="value">{{ row.deliverDept }}</text>
		              </view> -->
		              <view class="detail-row">
		                <text class="label">工装托盘编号:</text>
		                <text class="value">{{ row.palletNo }}</text>
		              </view>
		              <!-- <view class="detail-row">
		                <text class="label">工装托盘转运记录ID:</text>
		                <text class="value">{{ row.deliveryId }}</text>
		              </view> -->
		              <!-- <view class="detail-row">
		                <text class="label">配送单号:</text>
		                <text class="value">{{ row.deliveryNo }}</text>
		              </view> -->
		              <view class="detail-row">
		                <text class="label">配送车辆:</text>
		                <text class="value">{{ row.deliveryCar }}</text>
		              </view>
		              <!-- <view class="detail-row">
		                <text class="label">配送日期:</text>
		                <text class="value">{{ row.deliveryDate }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">配送备注:</text>
		                <text class="value">{{ row.deliveryRemark }}</text>
		              </view> -->
		              <view class="detail-row">
		                <text class="label">需求日期:</text>
		                <text class="value">{{ row.requireRate }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">需求单位:</text>
		                <text class="value">{{ retType(row.requireDepartCode,2) }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">配送地址:</text>
		                <text class="value">{{ row.requireAddress }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">接收人员:</text>
		                <text class="value">{{ row.requireName }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">接收人员电话:</text>
		                <text class="value">{{ row.requirePhone }}</text>
		              </view>
		              <!-- <view class="detail-row">
		                <text class="label">实际接收日期:</text>
		                <text class="value">{{ row.receiveDate }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">实际接收人员:</text>
		                <text class="value">{{ row.receiveName }}</text>
		              </view>
		              <view class="detail-row">
		                <text class="label">实际接收备注:</text>
		                <text class="value">{{ row.receiveRemark }}</text>
		              </view> -->
		            </view>
		            </view>
					
				  </view>
		        </view>
		      </view>
		
		      <!-- 配送、转运图片 -->
		      <view class="section">
		        <view class="section-header">
		          <text class="section-title">配送、转运图片</text>
		        </view>
		        <view class="image-grid" v-if="pallet&&pallet.files">
		          <view class="image-item" v-for="(img, index) in pallet.files" :key="index">
		            <image :src="img" mode="aspectFill" @click="prviewImage(index)" class="image"></image>
		          </view>
		        </view>
		      </view>
		
		      <!-- 零散物资信息 -->
		      <!-- <view class="section">
		        <view class="section-header">
		          <text class="section-title">零散物资信息</text>
		        </view>
		        <view class="info-card" v-if="pallet&&pallet.other">
		         <view class="info-row">
		            <text class="label">物资名称:</text>
		            <text class="value">螺丝钉</text>
		          </view>
		          <view class="info-row">
		            <text class="value">{{pallet.other}}</text>
		          </view>
		        </view>
		      </view> -->
			  
		    </scroll-view>

	</view>
</template>

<script>
	export default {
		props: {
		   palletAll: {
		     type: Object,
		     default: function () {
		       return {
			   };
		     }
		   }
		},
		data() {
			return {
				depts:[],
				dicts:[],
				status:[],
				palletStatus:[],
				pallet:{},
				expandedIndex:null,
				
			}
		},
		mounted() {
		  this._initDetail();
		  this.getTypes();
		},
		watch: {
			palletAll(data) {
			  this._initDetail(data);
			},
			deep: true, // 深度监听
			immediate: true, // 初次监听即执行
		},
		methods: {
			gotocxDetail(instno,project){
				uni.navigateTo({
					url:  `/pages/pallet/tpcxtk?instno=${instno}&project=${project}`
				});
			},
			_initDetail(palletAll = this.palletAll) {
				this.pallet=palletAll;
				// 加载详情
				this.openPalletDetail();
			},
			// 获取托盘详情
			async openPalletDetail() { 
					if(this.pallet.palletNo){
						uni.showLoading({
							title: '数据加载中...'
						});
						const {data: res} = await uni.$http.get('/getInstdetail',{ // /getPalletAll
							palletNo: this.pallet.palletNo
						});
						if(res.success){ 
							if(res.data&&res.data.length>0)
							{
								this.pallet["instdetailList"]=[];
								res.data.forEach(cell=>{
									var findx = -1;
									findx = this.pallet["instdetailList"].findIndex(item => item.groupDesc === cell.groupDesc);
									if(findx<0){
										var add = {
											...cell,
											items:[cell],
										};
										this.pallet["instdetailList"].push(add);
										this.$forceUpdate(); 
									}
									else {
										this.pallet["instdetailList"][findx].items.push(cell);
										this.$forceUpdate(); 
									}
								});
								
								this.pallet["instdetailList"].forEach((cell,idx)=>{
									cell["qty"]=this.getQty(cell.items);
									this.$forceUpdate();
								});
								
								// this.pallet["instdetailList"]=res.data;
								this.$forceUpdate(); 
							} 
						} 
					}
					
				},
			getQty(items){
				var num=0
				if (items&&items.length>0) {
				    items.forEach(cell=>{ 
						num=num+1.0*(cell.qty?cell.qty:0);
					});
					return num.toFixed(2);
				}
				else{
					return 0;
				}
			},
			toggleDetail(index) {
			  if (this.expandedIndex === index) {
			    this.expandedIndex = null;
			  } else {
			    this.expandedIndex = index;
			  }
			},
			getTypes() {
				//获取数据字典
				this.gcs=[];
				this.status=[];
				var commondata = uni.getStorageSync("commondata");
				if(commondata){
					var data = JSON.parse(commondata);
					if(data){ 
						this.dicts=data.dicts;
						this.depts=data.depts; 
						this.dicts.forEach(cell=>{ 
							// console.log(cell.bmlb)
							if("TPZT"==cell.bmlb){
								// 托盘转运状态
								this.status.push(cell);
							}
							if("SYZT"==cell.bmlb){
								 
								// 托盘转运状态
								this.palletStatus.push(cell);
							}
							 
						});
						this.$forceUpdate();
					}
				}
				else{ 
					this.dicts=[];
					this.depts=[];
					this.status=[];
				}
			},
			retType(type,str) {
				var rets = '';
				if(str==1){
					// 状态
					this.status.forEach((cell,idx)=>{
						if(type==cell.bm){
							rets=cell.bmsm;
							return;
						}
					});
				}
				else if(str==3){
					// 使用状态  
					this.palletStatus.forEach((cell,idx)=>{
						if(type==cell.bm){
							rets=cell.bmsm;
							return;
						}
					});
				}
				else if(str==2){
					// 部门
					this.depts.forEach((cell,idx)=>{
						if(type==cell.departCode){
							rets=cell.departName;
							return;
						}
					});
				}
				if(!rets){
					rets=type
				}
				return rets;
			},
			getStatusClass(status) {
			  switch (status) {
			    case '表面接收':
			      return 'status-waiting';
			    case '建立任务':
			      return 'status-delivered';
			    case '内表面接收':
			      return 'status-receivedf';
				case '配盘完工':
				  return 'status-received';
				case '配送出库':
				  return 'status-outbound';
				case '完工转运':
				  return 'status-complete';
			    default:
			      return 'status-default';
			  }
			},
			prviewImage(index) {
				if(this.pallet&&this.pallet.files){
					uni.previewImage({
						urls: this.pallet.files,
						current: index
					});
				}
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.container {
	  height: 100%;
	  background-color: #f5f5f5;
	}
	
	.navbar {
	  display: flex;
	  align-items: center;
	  height: 88rpx;
	  background-color: #fff;
	  padding: 0 30rpx;
	  box-sizing: border-box;
	  position: relative;
	  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}
	
	.nav-left,
	.nav-right {
	  width: 60rpx;
	  display: flex;
	  align-items: center;
	}
	
	.nav-title {
	  flex: 1;
	  text-align: center;
	  font-size: 34rpx;
	  font-weight: bold;
	  color: #333;
	}
	
	.content {
	  height: calc(100%);
	  padding: 0;
	  box-sizing: border-box;
	}
	
	.section {
	  background-color: #fff;
	  border-radius: 16rpx;
	  margin-bottom: 20rpx;
	  overflow: hidden;
	  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}
	
	.section-header {
	  padding: 30rpx;
	  border-bottom: 1rpx solid #eee;
	}
	
	.section-title {
	  font-size: 32rpx;
	  font-weight: bold;
	  color: #333;
	}
	
	.info-card {
	  padding: 30rpx;
	}
	
	.info-row {
	  display: flex;
	  margin-bottom: 20rpx;
	}
	
	.info-row:last-child {
	  margin-bottom: 0;
	}
	
	.label {
	  width: 200rpx;
	  font-size: 28rpx;
	  color: #666;
	  line-height: 1.5;
	}
	
	.value {
	  flex: 1;
	  font-size: 28rpx;
	  color: #333;
	  line-height: 1.5;
	}
	
	.status-used {
	  color: #ff6b35;
	}
	
	.status-transit {
	  color: #4a90e2;
	}
	
	.list-container {
	  padding: 0 30rpx 30rpx;
	}
	
	.list-item {
	  border: 1rpx solid #eee;
	  border-radius: 12rpx;
	  margin-top: 20rpx;
	  overflow: hidden;
	}
	
	.item-header {
	  display: flex;
	  flex-direction: row;
	  align-items: center;
	  padding: 20rpx 30rpx;
	  background-color: #f9f9f9;
	}
	.item-header-tip {
	  display: flex;
	  flex-direction: row;
	  align-items: center;
	  padding: 20rpx 20rpx 0;
	  background-color: #f9f9f9;
	}
	.item-tip{
		display: flex;
		flex: 1;
		font-size: 30rpx;
		font-weight: bold;
		color: #007aff;
		padding: 12rpx;
		border-radius: 10rpx;
		border: 1rpx solid #007aff;
		background: #e2eefa;
	}
	.item-title {
		display: flex;
		flex: 1;
	  font-size: 30rpx;
	  font-weight: bold;
	  color: #333;
	}
	
	.item-content {
	  padding: 20rpx 30rpx;
	}
	
	.detail-row {
	  display: flex;
	  margin-bottom: 15rpx;
	}
	
	.detail-row:last-child {
	  margin-bottom: 0;
	}
	
	.image-grid {
	  display: grid;
	  grid-template-columns: repeat(3, 1fr);
	  gap: 20rpx;
	  padding: 30rpx;
	}
	
	.image-item {
	  border-radius: 12rpx;
	  overflow: hidden;
	  aspect-ratio: 1;
	}
	
	.image {
	  width: 100%;
	  height: 100%;
	}
	
	.status-badge {
	  padding: 6rpx 12rpx;
	  border-radius: 10rpx;
	  font-size: 24rpx;
	  color: #fff;
	  display: flex;
	  align-items: center;
	  margin-right: 10rpx;
	}
	
	.status-waiting {
	  background-color: #faad14;
	}
	
	.status-delivered {
	  background-color: #1890ff;
	}
	
	.status-receivedf {
	  background-color: #52c41a;
	}
	
	.status-received {
	  background-color: #18b566;
	}
	
	.status-outbound {
	  background-color: #fca600;
	}
	 
	.status-complete {
	  background-color: #c191e8;
	}
	 
	.status-default {
	  background-color: #f5f5f5;
	  color: #666666;
	}

</style>
