<template>
	<view class="recycling">
		<view class="recycling-hd">
			<image :src="$picUrl+'/static/news/recyling-banner-01.png'"></image>
		</view>
		<view class="recycling-bd">
			<view class="tab">
				<view class="a">
					<view class="item" @click="tabItem(0)"></view>
					<view class="item" @click="tabItem(1)"></view>
					<view class="item" @click="tabItem(2)"></view>
				</view>
				<view class="b">
					<image v-if="tabIndex == 0" :src="$picUrl+'/static/news/icon-recyling-tab-01.png'"></image>
					<image v-if="tabIndex == 1" :src="$picUrl+'/static/news/icon-recyling-tab-02.png'"></image>
					<image v-if="tabIndex == 2" :src="$picUrl+'/static/news/icon-recyling-tab-03.png'"></image>
				</view>
			</view>
			<view class="goods">
				<view class="a">
					<image :src="$picUrl+'/static/news/icon-mall.jpg'"></image>{{storeInfo.shop_name}}
				</view>
				<view class="b">
					<view class="l">
						<image class="pic" :src="storeInfo.logo_url"></image>
						<view class="info">
							<view class="tel">{{storeInfo.linkman}}{{storeInfo.phone}}</view>
							<view class="address">{{storeInfo.address}}</view>
							<view class="time">营业时间:{{storeInfo.shop_hours}}</view>
						</view>
						<view class="do">
							<view class="tel" @click="openPage(1,storeInfo.phone)">
								<image :src="$picUrl+'/static/news/tel.png'"></image>电话
							</view>
							<view class="tel" @click="openPage(2)">
								<image :src="$picUrl+'/static/news/address.png'"></image>导航
							</view>
						</view>
					</view>
					<view class="r">
						<image class="pic" :src="info.recovery_image"></image>
						<view class="info">
							<view class="tel">{{info.recovery_name}}</view>
							<view class="price">免支付预约</view>
						</view>
						<view class="num">
							x1
						</view>
					</view>
				</view>
			</view>
			<view class="form">
				<view class="item">
					<view class="a">品牌</view>
					<view class="b">
						<input class="box" type="text" v-model="obj.brand" placeholder="品牌" />
					</view>
				</view>
				<view class="item">
					<view class="a">型号</view>
					<view class="b">
						<input class="box" type="text" v-model="obj.model" placeholder="型号" />
					</view>
				</view>
				<view class="item">
					<view class="a">服务时间</view>
					<view class="b" @click="openPage(3)">
						<view class="select" :class="obj.server_time?'select-on':''">
							{{obj.server_time?obj.server_time:(obj.server_time?'请选择预约到店时间':'请选择预计寄出商品的时间')}}
						</view>
						<u-icon style="margin-left: 10rpx;"  color="#8B8B8B" name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="item">
					<view class="a">联系人</view>
					<view class="b">
						<input class="box1" v-model="obj.username" type="text" placeholder="联系人姓名" />
						<view class="radio">
							<u-radio-group v-model="sexValue" active-color="red">
								<u-radio @change="radioChange" v-for="(item, index) in sexList" :key="index"
									:name="item.name">
									{{item.name}}
								</u-radio>
							</u-radio-group>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="a">联系电话</view>
					<view class="b">
						<input class="box" type="number" v-model="obj.mobile" placeholder="联系人手机号码" />
					</view>
				</view>
				<view class="item" v-if="tabIndex == 2 ||tabIndex == 1">
					<view class="a">联系人微信</view>
					<view class="b">
						<input class="box" v-model="obj.wx_account" type="text" placeholder="联系人微信号码" />
					</view>
				</view>
				<view class="item" v-if="tabIndex == 2 ||tabIndex == 1">
					<view class="a" @click="addrshow=true">发件地址</view>
					<view class="b" @click="addrshow=true">
						<input type="text" disabled style="font-size: 28rpx;" placeholder="请选择地址" v-model="obj.shipping_address">
						<u-icon name="arrow-right" color="#8B8B8B" size="30"></u-icon>
						<u-picker @confirm="changeRegion" v-model="addrshow" mode="region"></u-picker>
					</view>
				</view>
				<view class="item" v-if="tabIndex == 2 ||tabIndex == 1">
					<view class="a">门牌号</view>
					<view class="b">
						<input class="box" type="text" v-model="obj.house_number" placeholder="例:12幢1单元102室" />
					</view>
				</view>
				<view class="item">
					<view class="a">期待卖价</view>
					<view class="b">
						<input class="box" v-model="obj.expect_price" type="number" placeholder="请输入您期待的售卖价格(元)" />
					</view>
				</view>
				<view class="c" style="margin-top: 20rpx;">
					<u-upload :action="action" :header="header" @on-uploaded="onSuccess" @on-remove="onRemove"
						width="180" height="180" :file-list="fileList" :custom-btn="true" max-count="6">
						<template v-slot:addBtn>
							<view class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
								<image :src="$picUrl+'/static/news/icon-upload.png'"></image>
								<view class="1">上传图片</view>
							</view>
						</template>
					</u-upload>
				</view>
				<view class="remark">
					<view class="name">留言 :</view>
					<view class="xu">
						<view class="li" @click="openPage(5,'可提前来')">可提前来</view>
						<view class="li" @click="openPage(5,'来之前打电话')">来之前打电话</view>
						<view class="li" @click="openPage(5,'请尽快联系我')">请尽快联系我</view>
					</view>
					<view class="content">
						<u-input v-model="obj.remake" height="260" type="textarea" placeholder="请输入您的留言,最多可留言100字"
							:border="true" />
					</view>
				</view>
			</view>
		</view>
		<view class="recycling-fd">
			<view class="a">免支付预购</view>
			<view class="b" @click="openPage(4)">{{source==2?'修改':'提交'}}订单</view>
		</view>
		<!-- 时间选择器 -->
		<u-picker mode="time" :params="params" v-model="show" confirm-color="#FE483B" :show-time-tag="false"
			@confirm="confirm"></u-picker>
		<u-mask :show="isShow" @click="isShow = false">
			<view class="order-dialog">
				<image class="pic" :src="$picUrl+'/static/news/icon-recycling-order-04.png'"></image>
				<view class="a">回收订单提交成功</view>
				<view class="b">感谢您对低碳环保做出的努力!</view>
				<view class="c" @click="onLook">查看订单</view>
			</view>
		</u-mask>
	</view>
</template>
<script>
	import Config from '@/core/config'
	import * as newFunApi from '@/api/newFun'
	import * as UploadApi from '@/api/upload'
	export default {
		data() {
			return {
				info: {},
				isShow: false,
				type: 1,
				tabIndex: 0,
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				addrshow: false,
				show: false,
				storeInfo: {},
				region: [],
				sexValue: '女士',
				sexList: [{
						name: '先生',
						value: 1
					},
					{
						name: '女士',
						value: 2
					},
				],
				obj: {
					recovery_id: '', //回收ID
					shop_id: '', //门店ID
					recovery_type: '', //10上门20门店30邮寄
					username: '', //姓名
					server_time: '', //mobile:'',//手机号
					expect_price: '', //期望价
					sex: 1, //1男2女
					remake: '', //备注
					brand: '', //品牌
					model: '', //型号
					wx_account: '', //微信账号
					house_number: '', //门牌号-邮寄、上门必填
					shipping_address: '', //联系地址-邮寄、上门必填
					express_id: '', //快递ID
					express_no: '', //否快递单号
					image_ids: '', //图片ID(数组或以逗号字符串)
				},
				source: 1,
				action: '',
				header: '',
				fileList: [],
				handleList: [],
			}
		},
		onReady() {
			this.action = (Config.get('apiUrl') + 'upload/image').replace("index.php?s=/", "")
			this.header = {
				'Storeid': uni.getStorageSync('Store').storeInfo.store_id,
				'Access-Token': uni.getStorageSync('AccessToken'),
				'platform': "MP-WEIXIN",
			}
		},
		onLoad(op) {
			this.tabIndex = op.tabIndex;
			this.source = op.source;
			this.order_id = op.order_id || '';
			this.info = uni.getStorageSync("getInfo")
			this.storeInfo = uni.getStorageSync("storeHsInfo")
			if (this.source == 2) {
				this.getOrderDetail(op.order_id)
			}
		},
		onShow() {
			if(uni.getStorageSync("yuyueTime").time){
				this.obj.server_time = uni.getStorageSync("yuyueTime").time
			}else{
				this.obj.server_time = ""
			}
		},
		methods: {
			radioChange(e) {
				this.sexValue = e
				this.obj.sex = e == '先生' ? 1 : 2
			},
			async getOrderDetail(order_id) {
				let {
					status,
					message,
					data
				} = await newFunApi.orderDetail({
					order_id: order_id
				});
				if (status == 200) {
					this.info = data.detail;
					this.storeInfo = data.detail.shop_info
					this.sexValue = data.detail == 1 ? '先生' : '女士'
					this.obj = data.detail;
					if (this.obj.images_list.length > 0) {
						this.obj.images_list.forEach(item => {
							item.url = item.external_url
						});
						this.fileList = this.obj.images_list
					}
				}
			},
			//接受上传返回的数据
			onSuccess(list) {
				let nList = []
				list.forEach(item => {
					if (item.response) {
						nList.push(item)
					}
				});
				list = nList
				if (list.length > 0) {
					const {
						response
					} = list[0];
					if (response.status == 200) {
						uni.showToast({
							title: "上传成功"
						})
						if (this.source == 2) {
							this.handleList = []
							list.forEach(item => {
								this.handleList.push(item.response.data.fileInfo)
							});
						} else {
							this.obj.image_ids = this.handleRemove('', list);
						}

					}
				}
			},
			onRemove(file, fileList) {
				if (this.source == 2) {
					this.handleList = [];
					if (fileList.length > 0) {
						fileList.forEach((item, index) => {
							if (item.response) {
								this.handleList.push(item.response.data.fileInfo)
							} else {
								this.fileList.splice(file, 1)
							}
						});
					} else {
						this.fileList.splice(file, 1)
					}
				} else {
					this.obj.image_ids = this.handleRemove(file, fileList);
				}
			},
			handleRemove: function(file, fileList) {
				let idList = []
				fileList.forEach(item => {
					idList.push(item.response.data.fileInfo.file_id)
				});
				return idList.join(',')

			},
			tabItem(i) {
				this.tabIndex = i
			},
			confirm(e) {
				this.obj.server_time = e.year + "-" + e.month + '-' + e.day + " " + e.hour + ":" + e.minute + ':' +
					e
					.second
			},
			changeRegion(e) {
				this.addrshow = false
				this.obj.shipping_address = e.province.name + e.city.name + e.area.name;
			},
			openPage(i, msg) {
				if (i == 1) { //电话
					uni.makePhoneCall({
						phoneNumber: msg
					})
				}
				if (i == 2) { //导航
					uni.openLocation({
						latitude: Number(this.storeInfo.latitude),
						longitude: Number(this.storeInfo.longitude),
						complete: function(res) {
							console.log(res);
						}
					});
				}
				if (i == 3) { //服务事件
					// this.show = true
					uni.navigateTo({
						url: "/pages/news/recycling/timer?type=1&shop_id="+this.storeInfo.shop_id
					})
				}
				if (i == 4) { //提交
					if (this.source == 2) {
						let trList = []
						this.fileList = this.fileList.concat(this.handleList);
						this.fileList.forEach(item => {
							trList.push(item.file_id)
						});
						this.obj.image_ids = trList.join(',');
					}
					if (this.tabIndex == 0) {
						this.obj.recovery_type = 20
					}
					if (this.tabIndex == 1) {
						this.obj.recovery_type = 10
					}
					if (this.tabIndex == 2) {
						this.obj.recovery_type = 30
					}
					this.obj.shop_id = this.storeInfo.shop_id;
					this.obj.recovery_id = this.info.recovery_id;
					if (!this.obj.brand) {
						uni.showToast({
							title: '请输入品牌',
							icon: 'none',
							duration: 2000
						})
						return
					}
					if (!this.obj.model) {
						uni.showToast({
							title: '请输入型号',
							icon: 'none',
							duration: 2000
						})
						return
					}
					if (!this.obj.server_time) {
						uni.showToast({
							title: '请选择服务时间',
							icon: 'none',
							duration: 2000
						})
						return
					}
					if (!this.obj.username) {
						uni.showToast({
							title: '请输入姓名',
							icon: 'none',
							duration: 2000
						})
						return
					}
					if (!this.obj.mobile) {
						uni.showToast({
							title: '请输入手机号',
							icon: 'none',
							duration: 2000
						})
						return
					}
					if (!this.obj.expect_price) {
						uni.showToast({
							title: '请输入期望价',
							icon: 'none',
							duration: 2000
						})
						return
					}
					if (this.tabIndex == 2) {
						if (!this.obj.wx_account) {
							uni.showToast({
								title: '请输入微信账号',
								icon: 'none',
								duration: 2000
							})
							return
						}
						if (!this.obj.shipping_address) {
							uni.showToast({
								title: '请选择联系地址',
								icon: 'none',
								duration: 2000
							})
							return
						}
						if (!this.obj.house_number) {
							uni.showToast({
								title: '请输入门牌号',
								icon: 'none',
								duration: 2000
							})
							return
						}
					}

					let url = '';
					if (this.source == 2) {
						url = newFunApi.updateOrder(this.obj)
					} else {
						url = newFunApi.addOrder(this.obj)
					}
					url.then(res => {
						if (res.status == 200) {
							this.isShow = true
						} else {
							uni.showToast({
								title: res.message
							})
						}
					})
				}
				if (i == 5) { //备注
					this.obj.remake = msg
				}
			},
			onLook() {
							uni.removeStorageSync("yuyueTime")
				uni.redirectTo({
					url: "/pages/news/recycling/recoveryList"
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.recycling {
		min-height: 100vh;
		background-color: #FFFFFF;
		padding-bottom: 130rpx;
		overflow: hidden;

		.slot-btn {
			width: 80px;
			height: 80px;
			background: #FFFFFF;
			border-radius: 10rpx;
			border: 1px solid #C0C0C0;
			text-align: center;
			font-size: 24rpx;
			font-weight: 500;
			color: #D1D1D1;

			image {
				width: 50rpx;
				height: 50rpx;
				margin-top: 20rpx;
			}
		}

		&-hd {
			width: 686rpx;
			height: 152rpx;
			border-radius: 8rpx;
			overflow: hidden;
			margin: 0 auto;
			margin-top: 30rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		&-bd {
			width: 690rpx;
			min-height: 280rpx;
			overflow: hidden;
			margin: 0 auto;
			margin-top: 30rpx;
			position: relative;

			.tab {
				width: 690rpx;
				height: 280rpx;

				.b {
					width: 100%;
					height: 100%;
					display: block;

					image {
						width: 100%;
						height: 100%;
						display: block;
					}
				}

				.a {
					width: 100%;
					height: 98rpx;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 2;
					display: flex;
					align-items: center;

					.item {
						flex: 1;
						height: 100%;
					}
				}
			}

			.goods {
				padding: 30rpx 35rpx;
				overflow: hidden;
				border-left: 1px solid #F1F1F1;
				border-right: 1px solid #F1F1F1;

				.a {
					display: flex;
					align-items: center;

					image {
						width: 50rpx;
						height: 50rpx;
						margin-right: 10rpx;
					}

					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}

				.b {
					padding: 20rpx 0;
					overflow: hidden;

					.l {
						display: flex;
						align-items: center;
						overflow: hidden;

						.pic {
							width: 182rpx;
							height: 146rpx;
							border-radius: 6rpx;
							margin-right: 10rpx;
						}

						.info {
							width: 305rpx;
							margin-right: 20rpx;

							.title {
								font-size: 24rpx;
								font-weight: 400;
								color: #5C5C5C;
							}

							.address {
								font-size: 24rpx;
								font-weight: 400;
								color: #5C5C5C;
								height: 80rpx;
								line-height: 40rpx;
								overflow: hidden;
							}

							.time {
								font-size: 24rpx;
								font-weight: 400;
								color: #5C5C5C;
							}
						}

						.do {
							flex: 1;
							text-align: center;
							font-size: 20rpx;
							font-weight: 400;
							color: #989898;
							display: flex;
							align-items: center;
							justify-content: space-between;

							image {
								width: 30rpx;
								height: 30rpx;
								display: block;
								margin: 0 auto;
								margin-bottom: 20rpx;
							}
						}
					}

					.r {
						display: flex;
						align-items: flex-start;
						overflow: hidden;
						margin-top: 30rpx;

						.pic {
							width: 182rpx;
							height: 146rpx;
							border-radius: 6rpx;
							margin-right: 10rpx;
						}

						.info {
							width: 305rpx;
							margin-right: 20rpx;

							.title {
								font-size: 28rpx;
								font-weight: 400;
								color: #212121;
							}

							.price {
								font-size: 32rpx;
								font-weight: 500;
								color: #FF584D;
								margin-top: 20rpx;
							}
						}

						.num {
							flex: 1;
							text-align: center;
							font-size: 30rpx;
							font-weight: 400;
							color: #989898;
							text-align: right;
						}
					}
				}
			}

			.form {
				padding: 0 40rpx;
				overflow: hidden;
				border-left: 1px solid #F1F1F1;
				border-right: 1px solid #F1F1F1;
				border-bottom: 1px solid #F1F1F1;

				.remark {
					.name {
						font-size: 28rpx;
						font-weight: 400;
						color: #313131;
						margin-top: 15rpx;
					}

					.xu {
						display: flex;
						flex-wrap: wrap;
						overflow: hidden;

						.li {
							line-height: 52rpx;
							background: #FFFFFF;
							border-radius: 6rpx;
							border: 1px solid #EDEDED;
							padding: 0 20rpx;
							margin-right: 20rpx;
							text-align: center;
							font-size: 24rpx;
							font-weight: 400;
							color: #949494;
							margin-top: 20rpx;
						}
					}

					.content {
						padding: 20rpx 0 50rpx;
						font-size: 28rpx;
						color: #212121;
					}
				}

				.photo {
					display: flex;
					justify-content: flex-start;
					padding: 20rpx 0;
					overflow: hidden;
				}

				.item {
					padding: 20rpx 0;
					border-bottom: 1px solid #F4F4F4;
					display: flex;
					align-items: center;

					.a {
						width: 160rpx;
						font-size: 28rpx;
						font-weight: 400;
						color: #313131;
						line-height: 45rpx;
					}

					.b {
						flex: 1;
						display: flex;
						align-items: center;
						justify-content: space-between;

						.select {
							flex: 1;
							font-size: 28rpx;
							font-weight: 400;
							line-height: 45rpx;
							color: #949494;

							&-on {
								color: #212121;
							}
						}

						.box {
							flex: 1;
							font-size: 28rpx;
							color: #212121;
							line-height: 45rpx;
						}

						.box1 {
							width: 160rpx;
							font-size: 28rpx;
							color: #212121;
							line-height: 45rpx;
						}

						.radio {
							display: flex;
							align-items: center;
							justify-content: flex-end;
							flex: 1;
							border-left: 1px solid #E6E6E6;
						}
					}
				}
			}
		}

		&-fd {
			width: 100%;
			height: 120rpx;
			background-color: #FFFFFF;
			padding: 25rpx 25rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 99;

			.a {
				font-size: 28rpx;
				font-weight: 400;
				color: #727272;
			}

			.b {
				width: 502rpx;
				height: 88rpx;
				line-height: 88rpx;
				background: #FE483B;
				border-radius: 88rpx;
				text-align: center;
				font-size: 28rpx;
				font-weight: 600;
				color: #FFFFFF;
			}
		}

		.order-dialog {
			width: 16.78125rem;
			height: 12.9375rem;
			background: linear-gradient(180deg, #FFE4E4 0%, #FFFFFF 100%);
			border-radius: 0.375rem;
			position: absolute;
			left: 50%;
			top: 50%;
			margin-left: -8.375rem;
			margin-top: -6.46875rem;
			z-index: 2;
			text-align: center;
			padding-top: 3.125rem;
			box-sizing: border-box;

			.pic {
				width: 112rpx;
				height: 128rpx;
				position: absolute;
				left: 50%;
				margin-left: -56rpx;
				top: -60rpx;
				z-index: 2;
			}

			.a {
				font-size: 32rpx;
				font-weight: 500;
				color: #000000;
			}

			.b {
				font-size: 28rpx;
				font-weight: 400;
				color: #727272;
				margin-top: 30rpx;
			}

			.c {
				width: 412rpx;
				line-height: 72rpx;
				background: #F55349;
				border-radius: 8rpx;
				font-size: 28rpx;
				color: #FFFFFF;
				margin: 0 auto;
				margin-top: 60rpx;
			}
		}
	}

	.c {
		overflow: hidden;

		.slot-btn {
			width: 180rpx;
			height: 180rpx;
			background: #F6F6F6;
			border-radius: 10rpx;
			border: 1px solid #C0C0C0;
			text-align: center;
			font-size: 28rpx;
			font-weight: 500;
			color: #6D6D6D;

			.l {
				line-height: 30rpx;
			}

			image {
				width: 50rpx;
				height: 50rpx;
				display: block;
				margin: 0 auto;
				margin-top: 30rpx;
			}
		}
	}

	::v-deep .u-list-item {
		width: 180rpx !important;
		height: 180rpx !important;
	}
</style>