<template>
	<view class="lists">
		<!-- <view class="status_bar" :style="navHeight">
		</view> -->
		<scroll-view class="scroll-area view" :scroll-top="scrollTop" show-scrollbar='false' scroll-y="true">
			<view class="main">
				<view class="middle view">
					<view class="text-block">
						<text class="middle-date">发货计划类型</text>
						<text class="middle-date-des">{{orderItem.planTypeTextView}}</text>
						<!-- <view class="middle-date-des uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" @change="bindPickerChange($event,'deliveryPlan')" :value="index"
										range-key='name' mode="selector" :range="deliveryPlanArr">
										<text class="uni-input">{{orderItem.planType}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view> -->

					</view>
					<view class="text-block">
						<text class="middle-date">制票类型</text>
						<text class="middle-date-margin-des">{{orderItem.noticeSheetTypeTextView}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">开票客户</text>
						<text class="middle-date-margin-des">{{orderItem.billCustomer}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date" @tap="selectClient">订单客户</text>
						<text class="middle-date-margin-des">{{orderItem.customerName}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">渠道类型</text>
						<view class="middle-date-address uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" @change="bindPickerChange($event,'channelType')" :value="orderItem.channelsType"
										range-key='name' mode="selector" :range="channelTypeArr">
										<text class="uni-input">{{orderItem.channelTypeName}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
					</view>
					<view class="text-block text-block-last">
						<text class="middle-date">配额年度</text>
						<text class="middle-date-des">{{orderItem.quotasYear}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">紧急程度</text>
						<view class="middle-date-address uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" @change="bindPickerChange($event,'significance')" :value="orderItem.value"
										range-key='name' mode="selector" :range="significanceArr">
										<text class="uni-input">{{orderItem.significanceTextView}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
					</view>
					<view class="text-block">
						<text class="middle-date">是否瓶储</text>
						<view class="middle-date-address uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" @change="bindPickerChange($event,'isPcj')" :value="orderItem.isPcj"
										range-key='name' mode="selector" :range="pcjArr">
										<text class="uni-input">{{orderItem.isPcjName}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
					</view>
					<view class="text-block" v-if="orderItem.isPcj == 1">
						<text class="middle-date">瓶储截止日期</text>
						<view class="middle-date-address uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" mode="date" :value="orderItem.saveEnddate" :start="startDate" @change="bindPickerChange($event,'date')">
										<text class="uni-input">{{orderItem.saveEnddate}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
					</view>
					<view class="text-block" v-if="orderItem.demandType=='ZDZG'">
						<text class="middle-date">要货单类型</text>
						<view class="middle-date-address uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker disabled class="picker-block" @change="bindPickerChange($event,'manifest')" :value="orderItem.demandType"
										range-key='name' mode="selector" :range="manifestArr">
										<text class="uni-input">{{orderItem.demandTypeTextView}}</text>
									</picker>
								</view>
							</view>
							<!-- <image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image> -->
						</view>
					</view>
					<view class="text-block" @click="goTerminal"  v-if="orderItem.demandType=='ZDZG'">
						<text class="middle-date">收货终端</text>
						<view class="middle-date-terminal uni-list picker-year" >
							<!-- <view class="uni-list-cell"> -->
								<!-- <view class="uni-list-cell-db">
									<text class="uni-input">{{orderItem.terminalName}}</text>
								</view> -->
							<!-- </view> -->
							<text class="middle-date-margin-des" :class="{'placeholder-text': !orderItem.terminalName}">
								{{orderItem.terminalName || '请先选择渠道类型'}}
							</text>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
						
						
						<!-- <view class="middle-date-address uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" @change="bindPickerChange($event,'terminalRobot')" :value="orderItem.terminalId"
										range-key='storeName' mode="selector" :range="terminalRobotArrs">
										<text class="uni-input">{{orderItem.terminalName}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view> -->
					</view>
				</view>
					
					<uni-cell class="cell-list" v-for="(ele, index) in orderItem.wantGoodsDetailList" :key="ele.id">
				<view class="middle view">
					<view class="text-block">
						<text class="middle-date">品牌</text>
						<text class="middle-date-des">{{ele.brandBqTextView}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">产品</text>
						<text class="middle-date-pro">{{ele.productDesc}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">产品编号</text>
						<text class="middle-date-des">{{ele.productCode}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">未发货件数</text>
						<text class="middle-date-des">{{ele.undeliveredQuantity}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">未发货瓶数</text>
						<text class="middle-date-des">{{ele.availableBot}}</text>
					</view>
					<!-- <view class="text-block">
						<text class="middle-date">传统当月计划量</text>
						<text class="middle-date-des">{{ele.planQuantity}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">传统当月剩余量</text>
						<text class="middle-date-des">{{ele.monthLeftQty}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">传统当年剩余量</text>
						<text class="middle-date-des">{{ele.annualLeftQty}}</text>
					</view> -->
					<view class="text-block">
						<text class="middle-date">可放行数量</text>
						<text class="middle-date-des">{{releaseNum}}</text>
					</view>
					<view class="text-block text-block-last">
						<text class="middle-date">本次要货件数</text>
						<input class="middle-date-address" type="number" v-model="ele.wantQuantity" placeholder="请输入" />
					</view>
				</view>
					</uni-cell>

				<view class="middle view" v-if="orderItem.channelsType === 'K'">
					<view class="text-block" @click="handleSalePlace">
						<text class="middle-date">卖场全称</text>
						<input class="middle-date-des" type="text" v-model="orderItem.kaStoreFullname" placeholder="请输入" />
					</view>
					<view class="text-block">
						<text class="middle-date">卖场简称</text>
						<input class="middle-date-des" type="text" v-model="orderItem.kaStoreShortname" placeholder="请输入" />
					</view>
					<view class="text-block text-block-last">
						<text class="middle-date">卖场区域</text>
						<input class="middle-date-des" type="text" v-model="orderItem.kaStoreArea" placeholder="请输入" />
					</view>
				</view>
				<view class="bottom">
					<button class="btn" type="default" @tap="nextStep">
						<text class="btn-text">下一步</text>
					</button>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	
	import {
		querySysDictionaryDetailsByCode,
		getProdDeliveryPlan,
		getChannelType,
	} from '@/servers/purchaseList.js'
	
	export default {
		data() {
			return {
				// orderItem: {
				// 	ticketTypeVal: '',
				// 	deliveryPlanCode: '',
				// 	planType: ' ',
				// 	channelTypeCode: '',
				// 	channelTypeName: ' ',
				// 	marketPlaceCode: '',
				// 	kaStoreArea: ' ',
				// 	quantity: ''
				// },
				// significanceName: '正常',
				marketPlaceVal: '',
				index: 0,
				scrollTop: 0,
				deliveryPlanArr : [],
				ticketTypeArr : [],
				significanceArr : [],
				marketPlaceArr : [],
				manifestArr : [],
				pcjArr: [{value:'0',name: '否'}, {value:'1',name: '是'}]
			}
		},
		props: {
			orderItem: {
				type: Object,
				default () {
					return {}
				}
			},
			channelTypeArr: {
				type: Array,
				default: () => []
			},
			terminalRobotArrs: {
				type: Array,
				default: () => []
			},
			releaseNum: {
				type: Number,
				default: 0
			}
		},
		computed: {
			startDate() {
				return this.getDate();
			},
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			...mapState(['sysinfo'])
		},
		mounted() {
			console.log('监听到事件来自 update');
			let _this = this
			uni.$on('selectSalePlaceAccountInfo', function(data) {
				console.log('监听到事件来自 selectSalePlaceAccountInfo ，携带参数 msg 为：', data);
				_this.orderItem.kaStoreFullname = data.selectSalePlaceAccountInfo.fullName
				_this.orderItem.kaStoreShortname = data.selectSalePlaceAccountInfo.shortName
				_this.orderItem.kaStoreArea = data.selectSalePlaceAccountInfo.area
			})
			// 选择终端
			uni.$on('selectTerminal', function(data) {
				console.log('监听到事件来自 selectTerminal ，携带参数 msg 为：', data);
				_this.orderItem.terminalName = data.selectTerminal.storeName
				_this.orderItem.terminalId = data.selectTerminal.storeId
				_this.orderItem.receiveUnitName = data.selectTerminal.storeName
				_this.orderItem.receiveAddress = (data.selectTerminal.provinceName + data.selectTerminal.cityName + data.selectTerminal.districtName + data.selectTerminal.address) || ''
				_this.orderItem.contactNumber = data.selectTerminal.consigneePhone
				_this.orderItem.contacPerson = data.selectTerminal.consignee
				_this.orderItem.province = data.selectTerminal.provinceName
				_this.orderItem.city = data.selectTerminal.cityName
				_this.orderItem.district = data.selectTerminal.districtName
			})
		},
		onUnload() {
			uni.$off('selectSalePlaceAccountInfo')
			uni.$off('selectTerminal')
		},
		beforeDestroy() {
			uni.$off('selectSalePlaceAccountInfo')
			uni.$off('selectTerminal')
		},
		created() {
			// 发货计划类型：A0002
			// 制票类型：A0022
			// 渠道类型：A0054
			// 卖场区域：A0052
			this.getSelectDictDataOrigin('A0002')
			this.getSelectDictDataOrigin('A0022')
			// this.getSelectDictDataOrigin('A0054')
			this.getSelectDictDataOrigin('A0052')
			this.getSelectDictDataOrigin('A0081')
			this.getSelectDictDataOrigin('Significance')
			// this.getProdDeliveryPlan()
		},
		watch:{
			// 'orderItem.planType':function(res,res1){
			// 	res && this.getChannelType()
			// }
		},
		methods: {
			goTerminal() {
				// 检查是否已选择渠道类型
				if (!this.orderItem.channelsType || !this.orderItem.channelTypeName) {
					uni.showToast({
						title: '请先选择渠道类型',
						duration: 2000,
						icon: 'none'
					});
					return;
				}
				
				const params= {
					dealerId: this.orderItem.customerCode,
					inventoryCode: this.orderItem.brand,
					channelType: this.orderItem.channelsType // 添加渠道类型参数
				}
				uni.navigateTo({
					url: `/pages/select-terminal/select-terminal??params=${JSON.stringify(params)}`
				})
			},
			getDate() {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			async getProdDeliveryPlan(){
				const params = {
					'channel': "",
					'customer': this.orderItem.customer,
					'planType': "0",
					'productCode': this.orderItem.productCode,
					'saleCenter': this.orderItem.zzfld000001
				}
				const res = await getProdDeliveryPlan(params)
				console.log(res,'res')
			},
			handleSalePlace() {
				uni.navigateTo({
					url: `/pages/select-sale-place/select-sale-place`
				});
			},
			selectClient() {
				uni.navigateTo({
					url: `/pages/select-client/select-client`
				});
			},
			
			bindPickerChange(e,type) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				const index = e.detail.value || 0
				switch (type) {
					case 'deliveryPlan':
						this.orderItem.deliveryPlanCode =  this.deliveryPlanArr[index].value
						this.orderItem.planType =  this.deliveryPlanArr[index].name
						break;
					case 'channelType':
						this.orderItem.channelsType =  this.channelTypeArr[index].value
						this.orderItem.channelTypeName =  this.channelTypeArr[index].name
						// 渠道类型变化时，如果是终端要货则清空终端信息
						this.onChannelsTypeChange(this.orderItem.channelsType)
						break;
					case 'significance':
						this.orderItem.significance =  this.significanceArr[index].value
						this.orderItem.significanceTextView=  this.significanceArr[index].name
						break;
					case 'isPcj':
						this.orderItem.isPcj =  this.pcjArr[index].value
						this.orderItem.isPcjName=  this.pcjArr[index].name
						break;
					case 'date':
						this.orderItem.saveEnddate = e.detail.value
						break;
					case 'manifest':
						this.orderItem.demandType =  this.manifestArr[index].value
						this.orderItem.demandTypeTextView=  this.manifestArr[index].name
					case 'terminalRobot':
						this.orderItem.terminalId =  this.terminalRobotArrs[index].storeId
						this.orderItem.terminalName=  this.terminalRobotArrs[index].storeName
						this.orderItem.receiveUnitName = this.terminalRobotArrs[index].storeName
						break;
					// case 'marketPlace':
					// 	this.marketPlaceVal =  this.marketPlaceArr[index].value
					// 	this.orderItem.kaStoreArea =  this.marketPlaceArr[index].name
					// 	break;
				}
			},
			nextStep() {
				if (!this.orderItem.channelTypeName) {
					uni.showToast({
						title: '请先选择渠道',
						duration: 2000,
						icon: 'none'
					});
					return
				}
				if(this.orderItem.isPcj == 1 && !this.orderItem.saveEnddate){
					uni.showToast({
						title: '请先选择瓶储截至日期',
						duration: 2000,
						icon: 'none'
					});
					return
				}
				if(this.orderItem.demandType == 'ZDZG' && !this.orderItem.terminalName){
					uni.showToast({
						title: '请先选择收货终端',
						duration: 2000,
						icon: 'none'
					});
					return
				}
				const numFlag = this.orderItem.wantGoodsDetailList.every(function(item){
						return item.wantQuantity 
					});
				if (!numFlag) {
					uni.showToast({
						title: '请先填写要货数量',
						duration: 2000,
						icon: 'none'
					});
					return
				}
				this.$emit('click', 'purchaseReceive')
				// uni.navigateTo({
				// 	url: `/pages/purchase-receive/purchase-receive?order=${JSON.stringify(this.orderItem)}`
				// })
			},
			async getChannelType(){
				const res = await getChannelType({'salePlan': this.orderItem.planType})
				this.channelTypeArr = res.data
				if (!this.channelTypeArr.length) {
					this.channelTypeArr = [{ value: 'S', name: '社会化渠道' }]
				  }
			},
			// 渠道类型变化处理，参考PC端逻辑
			onChannelsTypeChange(value) {
				// 当为终端要货时（demandType='ZDZG'），清空已选择收货终端
				if (this.orderItem.demandType === 'ZDZG') {
					this.orderItem.terminalName = ''
					this.orderItem.terminalId = ''
					this.orderItem.receiveUnitName = ''
					this.orderItem.receiveAddress = ''
					this.orderItem.contactNumber = ''
					this.orderItem.contacPerson = ''
					this.orderItem.province = ''
					this.orderItem.city = ''
					this.orderItem.district = ''
				}
			},
			getSelectDictDataOrigin(dictcode) {
			  querySysDictionaryDetailsByCode({'code': dictcode})
				.then(res => {
					if(res.data.Rows.length>0){
						switch (dictcode){
							case 'A0002':
								this.deliveryPlanArr = res.data.Rows
								break;
							case 'A0022':
								this.ticketTypeArr = res.data.Rows
								this.getTicketType(this.ticketTypeArr)
								break;
							case 'Significance':
								this.significanceArr = res.data.Rows
								break;
							case 'A0081':
								this.manifestArr = res.data.Rows
								break;
							// case 'A0054':
							// 	this.channelTypeArr = res.data.Rows
							// 	break;
							// case 'A0052':
							// 	this.marketPlaceArr = res.data.Rows
							// 	break;
							default:
								break;
						  }
					}
				  
				})
				.catch(() => {})
			},
			getTicketType(arr) {
				arr.forEach(ele => {
					if(ele.value === this.orderItem.infType){
						this.orderItem.ticketTypeVal = ele.name
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.view {
		flex-direction: column;
	}

	/* #ifndef APP-PLUS */
	page {
		width: 100%;
		min-height: 100%;
		display: flex;
		background: #fff;
	}

	.status_bar {
		height: var(--status-bar-height);
		background: #fff;
	}

	/* #endif */

	.lists {
		flex: 1;
		flex-direction: column;
		background: #f0f4f5;
		overflow-y: scroll;
	}

	.scroll-area {
		flex: 1;
	}

	.main {
		margin: 0 16rpx;
		flex-direction: column;
		border-radius: 16rpx;
		margin-top: 16rpx;
	}

	.middle {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 32rpx 64rpx 0 48rpx;
		margin-bottom: 16rpx;
	}

	.text-block {
		overflow: hidden;
		// height: 96rpx;
		// margin-bottom: 14rpx;
		border-bottom: 1px solid #f4f5f6;
	}

	.text-block-last {
		margin-bottom: 0;
		border-bottom: 0;
	}

	.middle-date {
		flex: 2;
		font-size: 28rpx;
		color: #999;
		margin-bottom: 8rpx;
		padding-top: 30rpx;
	}

	.middle-date-des {
		flex: 3;
		font-size: 28rpx;
		height: 96rpx;
		padding-top: 30rpx;
		justify-content: start;
	}
	.middle-date-margin-des{
		flex: 3;
		font-size: 28rpx;
		color: $text-base-color;
		padding-top: 30rpx;
		margin-bottom: 20rpx;
		width: 200%;
		text-align: left;
		justify-content: end;
	}

	.picker-year {
		position: relative;
		align-items: center;
	}

	.uni-list-cell {
		flex: 1;
		height: 100%;
	}

	.uni-list-cell-db {
		flex: 1;
	}

	.picker-block {
		flex: 1;
	}

	.uni-input {
		line-height: 100%;
		height: 100%;
		font-size: 14px;
		color: $text-base-color;
	}

	.icon-arrow {
		position: absolute;
		right: 22rpx;
		top: 40rpx;
		width: 7px;
		height: 10px;
		z-index: 10;
	}

	.bottom {
		
		margin-top: 68rpx;
		padding: 0 16rpx;
		justify-content: space-between;
	}

	.btn {
		flex: 1;
		height: 92rpx;
		margin: 0 auto 48rpx;
		background: $wly-primary-color;
		border-radius: 46rpx;
	}

	.btn-text {
		color: #fff;
		font-size: 34rpx;
	}

	.btn-text-l {
		color: $wly-primary-color;
	}
	.cell-list {
		flex-direction: column;
	}
	.middle-date-pro{
		flex: 3;
		font-size: 28rpx;
		height: 96rpx;
		color: $text-base-color;
		vertical-align: middle;
		margin-top: 10rpx;
	}
	.middle-date-address {
		flex: 3;
		font-size: 28rpx;
		height: 96rpx;
		line-height: 96rpx;
		color: $text-base-color;
	}
	.middle-date-terminal {
		flex: 3;
		font-size: 28rpx;
		height: 96rpx;
		color: $text-base-color;
	}
	
	.placeholder-text {
		color: #999 !important;
	}
</style>
