<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>
						<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,'shunt')" :value="orderItem.shuntName"
										range-key='name' mode="selector" :range="shuntArr">
										<text class="uni-input">{{orderItem.shuntName}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
					</view>
					<view class="text-block" @click="handleShunt">
						<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">
									<text class="uni-input">{{orderItem.shuntDealerName}}</text>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
					</view>
					
					<view class="text-block text-block-last" v-if="orderItem.isShunt - 1 === 0">
						<text class="middle-date" v-if="orderItem.isShunt - 1 === 0">分流渠道</text>
						<view class="middle-date-des uni-list picker-year" v-if="orderItem.isShunt - 1 === 0">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" @change="bindPickerChange($event,'shuntType')" :value="orderItem.shuntTypeName"
										range-key='name' mode="selector" :range="shuntTypeArr">
										<text class="uni-input">{{orderItem.shuntTypeName}}</text>
									</picker>
								</view>
							</view>
							<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode=""></image>
						</view>
					</view>
				</view>
				<view class="middle view">
					<view class="text-block" v-if="orderItem.isShunt - 1 === 0" @click="handleShuntAdress">
						<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">
									<text class="uni-input">{{orderItem.shuntReceiveUnitName}}</text>
								</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-des uni-list picker-year">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<picker class="picker-block" @change="bindPickerChange($event,'receive')" :value="orderItem.receiveUnitName"
										range-key='partner_name' mode="selector" :range="receiveUnitArr">
										<text class="uni-input">{{orderItem.receiveUnitName}}</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-des uni-list picker-year" @click="handleCustomer">
							<view class="uni-list-cell">
								<view class="uni-list-cell-db">
									<text class="uni-input">{{orderItem.spurtCodeUnitName}}</text>
								</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.contacPerson}}</text>
					</view>
				</view>
				<view class="middle view">
					<view class="text-block">
						<text class="middle-date">联系人电话</text>
						<input class="middle-date-des" type="text" v-model="orderItem.contactNumber" placeholder="请输入" />
					</view>
					<view class="text-block">
						<text class="middle-date">收货地址</text>
						<input class="middle-date-des" type="text" v-model="orderItem.receiveAddress" placeholder="请输入" />
					</view>
					<view class="text-block text-block-last">
						<text class="middle-date">备注</text>
					</view>
					<view class="remark">
						<textarea class="remark-text" v-model="orderItem.remark" />
					</view>

					<!-- <view class="text-block title-first">
						<text class="middle-date title-first__text">上传文件</text>
					</view>
					<view class="upload-block">
						<uni-file-picker ref='files' :auto-upload='false' v-model="fileList" file-mediatype="image"
							:imageStyles="imageStyles" @select="select" @progress="progress" @success="success"
							@fail='fail'>
							<text>+</text>
						</uni-file-picker>
					</view> -->
				</view>

				<!-- <text class="receive-record">历史收货信息</text>
				<view class="middle view receive-record-view">
					<view class="text-block text-block-last receive-record-info">
						<text class="record-info-title">唐伟 133****9732</text>
						<text class="record-info-des">广东省云浮市罗定市银霄街294号</text>
					</view>
				</view>-->
				<view class="bottom-area"></view>
				<view class="bottom">
					<button class="btn btn-l" type="default" @tap="preStep">
						<text class="btn-text btn-text-l">上一步</text>
					</button>
					<button class="btn" type="default" @tap="nextStep">
						<text class="btn-text">提交</text>
					</button>
				</view>
			</view>
			<uni-popup ref="popup" background-color="#fff"  :mask-click='false'>
				<view class="popup-content">
					<view class="popup-content-head">
						<image class="popup-content-head__icon" src="@/static/image/order_success@3x.png" mode=""></image>
					</view>
					<view class="popup-content-content">
						<text class="popup-content-content__title">提交成功</text>
						<text class="popup-content-content__title2">订单号：{{ objectId }}</text>
					</view>
					<view class="popup-content-foot">
						<text class="popup-content-foot__btn" @click="fnBackHome()">回到首页</text>
						<text class="popup-content-foot__btn popup-content-foot__sure"
							@click="fnCheckOrderByNo()">查看订单</text>
					</view>
				</view>
			</uni-popup>
		</scroll-view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	
	import {
		querySysDictionaryDetailsByCode,
		getReceipt,
		findByCustomer,
		startProcessInstanceByKey,
		advance
	} from '@/servers/purchaseList.js'
	
	export default {
		data() {
			return {
				receiveUnitArr: [],
				shuntArr: [],
				shuntTypeArr: [],
				array: ['2021', '2020', '2019', '2018'],
				array1: [{'id':0,val:'00'},{'id':1,val:'01'}],
				index: 0,
				quantity: '',
				remark: '',
				fileList: [],
				scrollTop: 0,
				objectId: ''
			}
		},
		props: {
			orderItem: {
				type: Object,
				default () {
					return {}
				}
			},
		},
		watch: {
			'orderItem.isShunt' :function (newName,oldName){
				console.log(newName,'newName')
				if(newName - 0 === 0){
					this.orderItem.shuntReceiveUnitName = ''
					this.orderItem.shuntReceiveUnitCode = ''
					this.orderItem.shuntChannelsType = ''
					this.orderItem.shuntDealer = ''
					this.orderItem.shuntDealerName = ''
				}
			}
		},
		computed: {
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			imageStyles() {
				return {
					width: 64,
					height: 64,
					border: {
						radius: '50%'
					}
				}
			},
			...mapState(['sysinfo', 'userInfo'])
		},
		// onLoad(option) {
		// 	this.orderItem = {
		// 		...this.orderItem,
		// 		...JSON.parse(option.order)
		// 	}
		// },
		// onBackPress(event) {
		// 	uni.switchTab({
		// 		url: `/pages/purchase-main/purchase-main?order=${JSON.stringify(this.orderItem)}`
		// 	})
		// 	return false
		// },
		mounted() {
			console.log('监听到事件来自 update');
			let _this = this
			uni.$on('selectShuntAccountInfo', function(data) {
				console.log('监听到事件来自 selectShuntAccountInfo ，携带参数 msg 为：', data);
				_this.orderItem.shuntDealerName = data.selectShuntAccountInfo.shuntingCustomer
				_this.orderItem.shuntDealer = data.selectShuntAccountInfo.shuntingCustomer
			})
			uni.$on('selectShuntAdressAccountInfo', function(data) {
				console.log('监听到事件来自 selectShuntAdressAccountInfo ，携带参数 msg 为：', data);
				_this.orderItem.shuntReceiveUnitName = data.selectShuntAdressAccountInfo.partner_name
				_this.orderItem.shuntReceiveUnitCode = data.selectShuntAdressAccountInfo.partner
				_this.orderItem.receiveCityCode = data.selectShuntAdressAccountInfo.city_code
				_this.orderItem.receiveCityName = data.selectShuntAdressAccountInfo.city_name
				
			})
			uni.$on('selectShuntCode', function(data) {
				console.log('监听到事件来自 selectShuntCode ，携带参数 msg 为：', data);
				_this.orderItem.spurtCodeUnit = data.selectShuntCode.info.partner
				_this.orderItem.spurtCodeUnitName = data.selectShuntCode.info.name_org1
			})
		
		},
		onUnload() {
			uni.$off('selectShuntAccountInfo')
			uni.$off('selectShuntAdressAccountInfo')
			uni.$off('selectShuntCode')
		},
		beforeDestroy() {
			uni.$off('selectShuntAccountInfo')
			uni.$off('selectShuntAdressAccountInfo')
			uni.$off('selectShuntCode')
		},
		created() {
			// 是否分流：B0099
			// 分流渠道：A0054
			this.getSelectDictDataOrigin('B0099')
			this.getSelectDictDataOrigin('A0054')
			this.getReceipt()
			// this.queryData()
		},
		methods: {
			fnCheckOrderByNo() {
				this.$refs.popup.close()
				uni.redirectTo({
					url: '/pages/my-purchase/my-purchase'
				})
			},
			fnBackHome() {
				this.$refs.popup.close()
				setTimeout(() => {
					uni.switchTab({
						url: `/pages/home/home`
					})
				}, 300)
			},
			handleCustomer() {
				uni.navigateTo({
					url: `/pages/select-customer/select-customer?order=${JSON.stringify(this.orderItem)}`
				})
			},
			handleShunt() {
				uni.navigateTo({
					url: `/pages/select-shunt-list/select-shunt-list?order=${JSON.stringify(this.orderItem)}`
				})
			},
			handleShuntAdress() {
				if(this.orderItem.shuntDealerName){
					uni.navigateTo({
						url: `/pages/select-shunt-adress/select-shunt-adress?order=${JSON.stringify(this.orderItem)}`
					})
				}else{
					uni.showToast({
						title: '请先选择分流经销商！',
						duration: 2000,
						icon: 'none'
					});
				}
			},
			// 查询
			async queryData() {
				const params = {
					'customerCode': this.orderItem.customerCode,
					'type': '2',
				}
				const { data } = await findByCustomer(params)
			},
			async getReceipt(){
				const params = {
					'partner': this.orderItem.customerCode,
					'role': "WLY001"
				}
				const res = await getReceipt(params)
				this.receiveUnitArr = res.data
				if(this.orderItem.receiveUnitName == ' '){
					this.orderItem.receiveUnitName =  this.receiveUnitArr[0].partner_name
					this.orderItem.contactNumber = this.receiveUnitArr[0].tel_number
					this.orderItem.contacPerson = this.receiveUnitArr[0].c_o_name
					this.orderItem.receiveAddress = this.receiveUnitArr[0].street
				}
			},
			getSelectDictDataOrigin(dictcode) {
			  querySysDictionaryDetailsByCode({'code': dictcode})
				.then(res => {
					if(res.data.Rows.length > 0){
						switch (dictcode){
							case 'B0099':
								this.shuntArr = res.data.Rows
								this.orderItem.isShunt =  this.shuntArr[1].value
								this.orderItem.shuntName =  this.shuntArr[1].name
								break;
							case 'A0054':
								this.shuntTypeArr = res.data.Rows
								break;
							default:
								break;
						  }
					}
				  
				})
				.catch(() => {})
			},
			bindPickerChange(e,type) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				const index = e.detail.value || 0
				switch (type) {
					case 'shunt':
						this.orderItem.isShunt =  this.shuntArr[index].value
						this.orderItem.shuntName =  this.shuntArr[index].name
						break;
					case 'shuntType':
						this.orderItem.shuntChannelsType =  this.shuntTypeArr[index].id
						this.orderItem.shuntTypeName =  this.shuntTypeArr[index].name
						break;
					case 'receive':
						this.orderItem.receiveUnitName =  this.receiveUnitArr[index].partner_name
						this.orderItem.contactNumber = this.receiveUnitArr[index].tel_number
						this.orderItem.receiveAddress = this.receiveUnitArr[index].street
						this.orderItem.contacPerson = this.receiveUnitArr[index].c_o_name
						
						
				}
			},
			submit() {
				this.$emit('submit')
			},
			// 获取上传进度
			progress(e) {
				console.log('上传进度：', e)
			},
			// 选择文件
			select(e) {
				console.log('选择文件：', e ,this.fileList)
				// let _this = this
				// const unit16Array = await formatToUnit16Array(e.tempFiles[0].file)
				// let reader = new FileReader();
				// let fileObj = {
				// 	FILENAME: e.tempFiles[0].name,
				// 	MIMETYPE: '',
				// 	DOC_TYPE: e.tempFiles[0].fileType,
				// 	XCONTENT: unit16Array,
				// 	FILELENGTH: e.tempFiles[0].size,
				// 	ZZFLD00002D: this.userInfo.code,
				// 	ZZFLD00002D_DSCR: this.userInfo.name,
				// }
				// reader.readAsDataURL(e.tempFiles[0].file);
				// reader.onload = function(e) {
				// 	// fileObj.XCONTENT = e.target.result;
				// 	_this.imgFileBase64.push(fileObj);
				// 	console.log('2222', e)
				// }
			},
			// 上传成功
			success(e) {
				console.log('上传成功', e)
			},
			// 上传失败
			fail(e) {
				console.log('上传失败：', e)
			},
			preStep() {
				// uni.switchTab({
				// 	url: '/pages/purchase-main/purchase-main'
				// })
				this.$emit('click', 'purchaseMain')
			},
			async nextStep() {
				try {
					const params = JSON.parse(JSON.stringify(this.orderItem))
					params.wantGoodsDetailList = JSON.stringify(params.wantGoodsDetailList)
					const { data, status } = await startProcessInstanceByKey(params)
					if (status == 1) {
						Object.assign(params, data.bizData)
						params.bizId = data.bizId
						params.procInstId = data.procInstId
						params.taskId = data.taskId
						// 提交
						params.wantGoodsDetailList = JSON.stringify(params.wantGoodsDetailList)
						const res = await advance(params)
						if (res.status == 1) {
							this.objectId = res.data.bizData.wantCode
							this.$refs.popup.open('center')
							// uni.showToast({
							// 	title: '要货成功',
							// 	duration: 2000,
							// 	icon: 'none'
							// });
							// uni.switchTab({
							// 	url: '/pages/purchase/purchase'
							// })
						}
					}
				} catch (e) {
				}
			}
		}
	}
</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;
	}

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

	.picker-block {
		flex: 1;
	}

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

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

	.remark-text {
		flex: 1;
		height: 220rpx;
		background: #f5f6f7;
		border-radius: 4px;
		padding: 12rpx 18rpx;
		margin-bottom: 40rpx;
	}

	.title-first {
		border-bottom: 0;
	}

	.title-first__text {
		font-size: 28rpx;
		color: #333333;
		font-weight: 600;
	}

	.upload-block {
		display: flex;
		align-items: center;
		width: 188rpx;
		height: 188rpx;
		background: #f5f6f7;
		border: 1px dashed #dedede;
		border-radius: 4px;
		margin-bottom: 40rpx;
	}

	.uni-file-picker {
		width: 100%;
		height: 100%;
	}

	/deep/ .uni-file-picker__container {
		width: 100%;
		height: 100%;
	}

	.receive-record-view {
		padding: 40rpx 48rpx;
	}

	.receive-record {
		color: #555555;
		font-size: 28rpx;
		margin-bottom: 8rpx;
		margin-top: 32rpx;
	}

	.receive-record-info {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.record-info-title {
		color: #333;
		font-size: 28rpx;
		font-weight: 500;
		margin-bottom: 16rpx;
	}

	.record-info-des {
		color: #999;
		font-size: 24rpx;
	}

	.bottom-area {
		height: 140rpx;
		margin-top: 12rpx;
	}

	.bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 10;
		width: 750rpx;
		height: 140rpx;
		padding: 0 32rpx;
		justify-content: space-between;
		align-items: center;
	}

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

	.btn-l {
		margin-right: 30rpx;
		border: 1px solid $wly-primary-color;
		background-color: #fff;

	}

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

	.btn-text-l {
		color: $wly-primary-color;
	}
	/deep/ .uni-popup__wrapper {
		border-radius: 20rpx;
	}
	
	.popup-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 580rpx;
		height: 408rpx;
		background: #ffffff;
		border-radius: 24rpx;
	}
	
	.popup-content-head {
		margin-top: 56rpx;
		margin-bottom: 16rpx;
	}
	
	.popup-content-head__icon {
		width: 80rpx;
		height: 80rpx;
	}
	
	.popup-content-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 52rpx;
	}
	
	.popup-content-content__title {
		font-size: 32rpx;
		color: #444;
		font-weight: 500;
		margin-bottom: 8rpx;
	}
	
	.popup-content-content__title2 {
		color: #999999;
		font-size: 24rpx;
	}
	
	.popup-content-foot {}
	
	.popup-content-foot__btn {
		width: 152rpx;
		height: 60rpx;
		font-size: 22rpx;
		line-height: 60rpx;
		background: #ffedee;
		text-align: center;
		color: #ff0f00;
		font-weight: 500;
		border-radius: 32rpx;
	}
	
	.popup-content-foot__sure {
		margin-left: 40rpx;
		color: #fff;
		background: #ff1d32;
		border: 1px solid #ff1d32;
	}
</style>
