<template>
	<view class="send_box">
		<view class="guide_box" v-if="showGuide" @click="showGuide = false">
			<image src="../../static/imgs/shop/express.png" mode="" class="express_img"></image>
		</view>
		<view class="send_goods">
			<view>
				<view class="label">
					<text>快递单号</text>
					<text>*</text>
				</view>
				<view class="flexB scan_box">
					<input type="text" placeholder="在此输入快递单号" placeholder-class="place" v-model="num" />
					<view class="scan_btn flexCC" @click="scanCode()">快速录入单号</view>
				</view>
			</view>
			<view>
				<view class="label">
					<text>快递公司</text>
					<text>*</text>
				</view>
				<picker @change="bindPickerChange" :value="index" :range="logs" :range-key="'name'">
					<view class="picker flexB">
						<view class="inp_box" :style="{ color: logsName ? '#333' : '#999' }">
							{{ logsName ? logsName : '请选择快递公司' }}
						</view>
						<text class="iconfont iconzhcc_xiangxiajiantou"></text>
					</view>
				</picker>
			</view>
			<view v-if="logsName == '顺丰速运' || logsName == '顺丰快递'">
				<view class="label">
					<text>收货人手机号</text>
					<text>*</text>
				</view>
				<input type="text" placeholder="在此输入收货人手机号" placeholder-class="place" v-model="express_phone" />
			</view>
			<view>
				<view class="label">
					<text>发货日期</text>
					<text>*</text>
				</view>
				<picker @change="bindDateChange" mode="date" :value="date" :start="startDay">
					<view class="picker flexB">
						<view class="inp_box">{{ date }}</view>
						<text class="iconfont iconzhcc_xiangxiajiantou"></text>
					</view>
				</picker>
			</view>
			<view class="sub_btn" @click="submit">提交</view>
		</view>
	</view>
</template>

<script>
	import {
		getLogsCode,
		addLogs,
		getAds,
		location,
		getExpress
	} from '../../apis/shop.js';
	// var jweixin = require('jweixin-module');
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			});
			return {
				num: '', //物流单号
				logs: [], //物流名称选项
				logsName: '', //物流公司名称
				logsCode: '', //物流公司编码
				order_no: '', //订单编号
				index: 0, //选择物流名称
				date: currentDate, //发货日期
				startDay: '2020-01-01',
				user_id: '',
				address: '', //从下个页面选择的地址
				defaultAds: '', //获取的默认地址
				ads: {
					province: '',
					city: '',
					area: '',
					location: ''
				},
				userName: '',
				userPhone: '',
				userAds: '',
				express_phone: '', //发货时候如果是顺丰速运填写用户手机号
				showGuide: true
			};
		},
		onLoad(ops) {
			this.order_no = ops.order_no;
			this.user_id = ops.user_id;
			this.getCode();
		},
		methods: {
			//扫码获取单号
			scanCode() {
				this.showGuide = false;
				uni.scanCode({
					success: res => {
						let code = res.result;
						console.log(res, 2222)
						this.getExpress(code);
					},
					fail: err => {
						console.log(err, 1111)
						uni.showModal({
							content: '扫码失败',
							showCancel: false
						})
					}
				});
			},
			//根据单号获取物流公司
			getExpress(expressCode) {
				uni.showLoading({
					title: '识别中...',
					mask: true
				})
				getExpress({
					expressCode
				}).then(res => {
					if (res.code == 200) {
						if (!res.data.Success) {
							uni.showModal({
								content: '请重新扫码或手动选择输入',
								showCancel: false
							});
							return false;
						}
						this.num = res.data.LogisticCode;
						if (res.data.Shippers) {
							const {
								ShipperName,
								ShipperCode
							} = res.data.Shippers[0];
							this.logsName = ShipperName;
							this.logsCode = ShipperCode;
						} else {
							uni.showModal({
								content: '请重新扫码或手动选择输入',
								showCancel: false
							});
							this.logsName = this.logsCode = '';
						}
					} else {
						uni.showModal({
							content: res.data || '获取数据失败',
							showCancel: false
						});
					}
					uni.hideLoading();
				}).catch(err => {
					uni.hideLoading();
				})
			},
			/*添加地址*/
			addressList() {
				uni.navigateTo({
					url: '../ads-list/ads-list?user_id=' + this.user_id
				});
			},
			/*获取物流的*/
			getCode() {
				getLogsCode().then(res => {
					if (res.code == 200) {
						let list = res.data;
						for (let key in list) {
							this.logs.push({
								code: key,
								name: list[key]
							});
						}
					} else {
						uni.showModal({
							content: res.data || '获取物流公司失败',
							showCancel: false
						});
					}
				});
			},
			/*选择物流公司*/
			bindPickerChange(e) {
				let idx = e.detail.value;
				this.index = idx;
				this.logsName = this.logs[idx].name;
				this.logsCode = this.logs[idx].code;
			},
			/*选择日期*/
			bindDateChange(e) {
				this.date = e.target.value;
				console.log(this.date);
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			submit() {
				// if (!this.address.id && !this.defaultAds.id) {
				// 	uni.showModal({
				// 		content: '发货地址不能为空',
				// 		showCancel: false
				// 	});
				// 	return false;
				// }
				if (!this.num) {
					uni.showModal({
						content: '物流单号不能为空',
						showCancel: false
					});
					return false;
				}
				if (this.logsName == '请选择物流公司') {
					uni.showModal({
						content: '物流名称不能为空',
						showCancel: false
					});
					return false;
				}
				if (this.logsName == '顺丰速运' && !this.express_phone) {
					uni.showModal({
						content: '请输入收货人手机号',
						showCancel: false
					});
					return false;
				}
				if (!this.date) {
					uni.showModal({
						content: '发货日期不能为空',
						showCancel: false
					});
					return false;
				}
				uni.showLoading({
					title: '提交中....'
				});
				addLogs({
						order_no: this.order_no,
						express_no: this.num,
						express_code: this.logsCode,
						express_company: this.logsName,
						express_time: this.date,
						address_id: this.address.id || this.defaultAds.id || '',
						express_phone: this.express_phone
					})
					.then(res => {
						if (res.code == 200) {
							uni.showModal({
								content: '发货成功',
								showCancel: false,
								success: res => {
									if (res.confirm) {
										uni.reLaunch({
											url: '../order-manage/order-manage?status=' + 3
										});
									}
								}
							});
						} else {
							uni.showModal({
								content: res.data || '发货失败',
								showCancel: false
							});
						}
						uni.hideLoading();
					})
					.catch(err => {
						uni.hideLoading();
					});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.guide_box {
		width: 100vw;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		flex-direction: column;
		align-items: center;
		z-index: 999;

		.express_img {
			width: 608rpx;
			height: 731rpx;
			margin-top: 20vh;
		}
	}

	.scan_box {
		width: 690rpx;
		background: #f9f9fb;
		border-radius: 8rpx;

		.scan_btn {
			width: 196rpx;
			height: 60rpx;
			background: #fff4f3;
			border: 2rpx solid #fb231f;
			border-radius: 34rpx;
			color: $base-color;
			margin-right: 10rpx;
			z-index: 999999;
		}

		input {
			width: 70%;
		}
	}

	.send_box {
		width: 100%;
		min-height: 100%;
		background: #fff;

		.send_goods {
			width: 690rpx;
			padding-top: 30rpx;
			margin: 0 auto;
			background-color: #fff;

			.label {
				margin: 30rpx 0;

				text:first-child {
					font-size: 32rpx;
					font-weight: bold;
				}

				text:last-child {
					font-size: 32rpx;
					color: $base-color;
					font-weight: bold;
				}
			}

			.picker {
				background: #f9f9fb;
				height: 88rpx;
				line-height: 88rpx;
				border-radius: 8rpx;
				padding: 0 30rpx;

				.iconfont {
					color: #999;
				}

				.inp_box {
					font-size: 30rpx;
				}
			}

			input {
				height: 88rpx;
				background: #f9f9fb;
				border-radius: 8rpx;
				padding-left: 30rpx;
				box-sizing: border-box;
				font-size: 30rpx;
			}

			.place {
				font-size: 30rpx;
				color: #999;
			}

			.sub_btn {
				margin: 60rpx auto 50rpx;
			}

			.address_box {
				padding: 30rpx;
				width: 100%;
				box-sizing: border-box;
				background-color: #f9f9fb;
				border-radius: 24rpx;

				.icondizhi1 {
					color: $base-color;
					font-size: 50rpx;
					vertical-align: middle;
				}

				.add_box {
					margin-left: 20rpx;
					font-size: 30rpx;
					color: #999;
				}

				.iconiconfontjiantou2 {
					color: #999;
				}

				.ads_info {
					margin: 0 15rpx;

					.shop_info {
						font-size: 32rpx;
						font-weight: bold;

						text:last-child {
							font-size: 28rpx;
							color: #999;
						}
					}

					.ads_del {
						margin-top: 15rpx;
					}
				}
			}

			.scan_box {
				width: 690rpx;
				background: #f9f9fb;
				border-radius: 8rpx;

				.scan_btn {
					width: 196rpx;
					height: 60rpx;
					background: #fff4f3;
					border: 2rpx solid #fb231f;
					border-radius: 34rpx;
					color: $base-color;
					margin-right: 10rpx;
				}

				input {
					width: 70%;
				}
			}
		}
	}
</style>
