<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-des">{{orderItem.noticeSheetTypeTextView}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">开票客户</text>
						<text class="middle-date-des">{{orderItem.billCustomer}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date" @tap="selectClient">订单客户</text>
						<text class="middle-date-des">{{orderItem.customerName}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date">渠道类型</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,'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>
					
					<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.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-block-last">
						<text class="middle-date">本次要货件数</text>
						<input class="middle-date-des" type="text" 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: ''
				// },
				marketPlaceVal: '',
				index: 0,
				scrollTop: 0,
				deliveryPlanArr : [],
				ticketTypeArr : [],
				channelTypeArr : [],
				marketPlaceArr : [],
			}
		},
		props: {
			orderItem: {
				type: Object,
				default () {
					return {}
				}
			},
		},
		computed: {
			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
			})
		},
		onUnload() {
			uni.$off('selectSalePlaceAccountInfo')
		},
		beforeDestroy() {
			uni.$off('selectSalePlaceAccountInfo')
		},
		created() {
			// 发货计划类型：A0002
			// 制票类型：A0022
			// 渠道类型：A0054
			// 卖场区域：A0052
			this.getSelectDictDataOrigin('A0002')
			this.getSelectDictDataOrigin('A0022')
			// this.getSelectDictDataOrigin('A0054')
			this.getSelectDictDataOrigin('A0052')
			// this.getProdDeliveryPlan()
		},
		watch:{
			'orderItem.planType':function(res,res1){
				res && this.getChannelType()
			}
		},
		methods: {
			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
						break;
					// case 'marketPlace':
					// 	this.marketPlaceVal =  this.marketPlaceArr[index].value
					// 	this.orderItem.kaStoreArea =  this.marketPlaceArr[index].name
					// 	break;
				}
			},
			nextStep() {
				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
			},
			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 '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;
		line-height: 96rpx;
		color: #999;
		margin-bottom: 8rpx;
	}

	.middle-date-des {
		flex: 3;
		font-size: 28rpx;
		height: 96rpx;
		line-height: 96rpx;
		color: $text-base-color;
	}

	.picker-year {
		position: relative;
		align-items: center;
		padding-left: 14rpx;
	}

	.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;
	}
</style>
