<template>
	<view class="app">
		<view class="app-body">
			<!--关键词搜索-->
			<view class="sreach fx-r fx-bc fx-ac">
				<image class="icon" src="/static/img/tb-seach.png"></image>
				<input type="text" v-model="keyword" placeholder="请输入用户昵称/用户ID" placeholder-style="color: #B3B3B3;" />
				<view class="fx-g1"></view>
				<view class="search-btn" @tap="tapSerach">搜索</view>
			</view>
		</view>

		<view class="search-tm fx-r">
			<view class="item fx-r fx-bc fx-ac" @tap="tapTime">
				<view class="text">{{ selectDate || '开馆时间' }}</view>
				<image src="/static/img/c-ytime.png" class="icon"></image>
			</view>

			<view class="item fx-r fx-bc fx-ac" @tap="tapCg">
				<view class="text">{{ gzItem.id == 0 ? '全部场馆' : gzItem.nickname }}</view>
				<uv-icon name="arrow-down" class="icon"></uv-icon>
			</view>
		</view>

		<scroll-view scroll-y class="scroll" :style="'height: calc(100vh - ' + hFoot + 'px  - 140px);'"
			@scrolltolower="loadMoreData">
			<view class="sc-body">
				<view class="item" v-for="(item,index) in listAr">
					<view class="info fx-r fx-bc">
						<view class="nickname">{{ item.nickname }}</view>
						<view class="fx-r  fx-bc" @tap="tapCall(item.mobile)">
							<view class="mobile">{{ item.mobile }}</view>
							<image src="/static/img/call-tel.png" style="width: 15px;height: 15px;margin-left: 5px;">
							</image>
						</view>
						<view class="fx-g1"></view>
						<view v-if="item.gz_type == 'recycle'" class="status" style="font-size: 12px;color: #FF8034;">
							订单回收</view>
						<view v-if="item.gz_type == 'slim'" class="status" style="font-size: 12px;color: #FF8034;">已瘦身
						</view>
						<view v-if="item.gz_type == 'cf'" class="status" style="font-size: 12px;color: #FF8034;">已拆分
						</view>
					</view>

					<view class="info fx-r">
						<view class="id">ID号：{{ item.uid }}</view>
						<view class="fx-g1"></view>
						<view class="changci">订单号：{{ item.order_id }}</view>
					</view>
					<view class="info fx-r">
						<view class="id">卖家：{{ item.collection_nickname }}</view>
						<view class="fx-g1"></view>
						<view class="changci">电话：{{ item.collection_mobile }}</view>
					</view>

					<view class="info-list" v-for="item2 in item.sub_order">
						<view class="fx-r">
							<view class="iivi">子订单:{{ item2.order_id }}</view>
							<view class="fx-g1"></view>
							<view class="iivi">价格:<text
									style="color: red;font-weight: bold;">￥{{ item2.hanging_price }}</text></view>
						</view>
					</view>
					<view class="infos">
						<view class="info fx-r fx-bc">
							<view class="id">瘦身时间：{{ item.time }}</view>
							<view class="" style="padding-left: 35rpx;font-size: 24rpx;color: red;">
								{{item.is_gs==1?'已挂售':'未挂售'}}</view>
						</view>
					</view>

					<view class="infos fx-r">
						<view class="time">订单价格:￥<text style="color: red;">{{ item.hanging_price }}</text></view>
						<view class="fx-g1"></view>
						<view class="sbtn" v-if="item.gz_type == ''" @tap="tapSs(item,index)">瘦身</view>
						<view class="sbtn ihover" v-if="item.gz_type == ''" @tap="tapHs(item,index)" hidden>回收</view>
						<view class="sbtn" v-if="item.gz_type == ''" @tap="tapCf(item,index,0)">拆分</view>
						<view class="sbtn" v-if="item.gz_type == ''" @tap="tapCf(item,index,1)">拆小单</view>
					</view>
				</view>

				<view v-if="listAr.length > 0">
					<view class="loading fx-r fx-ac fx-bc" v-if="page.isFrite && !page.isFoot">
						<image src="/static/img/xloading.png"></image>
						<text>正在载入更多...</text>
					</view>
					<view class="loading complete" :hidden="!page.isFoot">已加载全部</view>
				</view>
				<view v-if="listAr.length == 0 && isFirst">
					<uv-empty mode="data" icon="/static/img/no-empty.png"></uv-empty>
				</view>
			</view>
		</scroll-view>

		<uv-datetime-picker ref='DateTime' v-model="selectTime" mode="date" @close="closeTime()"
			@confirm="tapCkTime" @cancel="closeTime()"></uv-datetime-picker>
		<uv-popup ref='orderPopup' @close="closePopup">
			<scroll-view style="height: 40vh;background: #fff;">
				<view class="pop-info">
					<view class="item ihover" @tap="tapGzItem(item)" v-for="item in gzData">{{ item.nickname }}</view>
				</view>
			</scroll-view>
		</uv-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		computed: mapState(['user']),
		data() {
			return {
				isFirst: false,
				listAr: [],
				keyword: "",
				id: 0,
				hFoot: 0,
				selectTime: "",
				selectDate: "",
				gzData: [],
				gzItem: {
					name: "",
					id: 0
				},

				page: {
					isFirst: false,
					isLoad: false,
					isFoot: false,
					page: 1
				},
			}
		},
		onLoad(options) {
			this.id = options.id || 0;
			this.initView();
			// #ifdef H5
			this.hFoot = 60;
			// #endif
		},

		methods: {
			...mapMutations(['checkUserLogin']),
			/**
			 * 加载基础配置
			 */
			initView: function() {
				this.getData(true);
				this
					.request
					.get("gzAuction")
					.then(res => {
						if (res.code == 200) {
							this.gzData = res.data;
						}
					});

			},
			/**
			 * 获取数据
			 */
			getData: function(isPull = false) {
				if (this.page.isLoad) return;
				this.page.isLoad = true;
				if (isPull) {
					this.page.page = 1;
					this.page.isLoad = false;
					this.page.isFoot = false;
				}
				uni.showLoading({
					title: '获取数据中..'
				});
				var post = {};
				post.page = this.page.page;

				this
					.request
					.post("gzOrder", {
						keyword: this.keyword,
						page: this.page.page,
						date: this.selectDate,
						auActionId: this.gzItem.id
					})
					.then(res => {
						uni.hideLoading();
						this.page.isFirst = true;
						this.page.isLoad = false;
						this.isFirst = true;
						if (isPull) {
							this.listAr = res.data.list;
						} else {
							this.listAr = this.listAr.concat(res.data.list);
						}
						//是否到底
						if (res.data.list.length != res.data.pageSize) {
							this.page.isFoot = true;
						}
					})
					.catch((res) => {
						console.log(res);
						uni.hideLoading();
						uni.showModal({
							title: '系统提示',
							content: '加载失败，返回在尝试',
							showCancel: false
						});
					});
			},

			tapSerach: function() {
				this.getData(true);
			},

			tapTime: function() {
				this.$refs.DateTime.open();
			},
closeTime: function() {
				this.$refs.DateTime.close();
			},
			tapCkTime: function(ev) {
				console.log(ev);
				this.closeTime();
				this.selectDate = this.utils.date('Y-m-d', ev / 1000);
				this.getData(true);
			},
			//选择场馆
			tapCg: function() {
				this.$refs.orderPopup.open();
			},

			tapGzItem: function(item) {
				this.$refs.orderPopup.close();
				this.gzItem = item;
				this.getData(true);
			},
			/**
			 * 确定要回收吗？
			 * @param {Object} item
			 */
			tapHs: function(item, index) {
				this.utils.showModal("你确定要回" + item.order_id + "订单吗？，回收之后订单所存在将会清理！", () => {
					uni.showLoading({
						title: "操作..."
					});
					this
						.request
						.post("auctionOrderRecycle", {
							id: item.id
						})
						.then(res => {
							uni.hideLoading();
							if (res.code == 200) {
								this.utils.Tip("回收成功");
								this.$set(this.listAr[index], 'gz_type', 'recycle');
							} else {
								this.utils.Tip(res.msg);
							}
						})
						.catch(err => {
							uni.hideLoading();
							this.utils.Tip("加载失败，请重新尝试");
						});
				});
			},
			/**
			 * 订单要瘦身吗？
			 */
			tapSs: function(item, index) {
				this.utils.showModal("你确定要" + item.order_id + "订单瘦身吗？，瘦身之后，订单金额减少！", () => {
					uni.showLoading({
						title: "操作..."
					});
					this
						.request
						.post("auctionOrderSlim", {
							id: item.id
						})
						.then(res => {
							uni.hideLoading();
							if (res.code == 200) {
								this.utils.Tip("瘦身成功");
								this.$set(this.listAr[index], 'price', res.data.price);
								this.$set(this.listAr[index], 'gz_type', 'slim');
							} else {
								this.utils.Tip(res.msg);
							}
						})
						.catch(err => {
							uni.hideLoading();
							this.utils.Tip("加载失败，请重新尝试");
						});
				});
			},
			closePopup() {
				this.$refs.orderPopup.close()
			},
			tapCf: function(item, index, type) {
				this.utils.showModal("你确定要" + item.order_id + "订单要拆分？，拆分之后，原有订单失效，生成新的2个订单。", () => {
					uni.showLoading({
						title: "操作..."
					});
					console.log(type, '88')
					console.log(item.id, '99')
					this.request.post("auctionOrderCf", {
						id: item.id,
						small: type
					}).then(res => {
						uni.hideLoading();
						if (res.code == 200) {
							this.utils.Tip("拆分成功");
							this.$set(this.listAr[index], 'gz_type', 'cf');
							this.$set(this.listAr[index], 'sub_order', res.data);
						} else {
							this.utils.Tip(res.msg);
						}
					}).catch(err => {
						uni.hideLoading();
						this.utils.Tip("加载失败，请重新尝试");
					});
				});
			},




			loadMoreData: function() {
				if (this.page.isFoot || this.page.isLoad) {
					return;
				}
				this.page.page++;
				this.getData();
			},

			tapCall: function(tel) {
				if (tel == null) {
					this.utils.showAlert({
						title: "系统提示",
						content: "暂无联系方式"
					});
					return;
				}
				const info = uni.getSystemInfoSync();
				if (info.platform == 'android') {
					uni.showModal({
						content: "确认拨打" + tel,
						confirmText: "确定",
						cancelText: "取消",
						success: function(res) {
							if (res.confirm) {
								this.$store.dispatch('permission/requestPermissions', 'CALL_PHONE').then(
									res => {
										if (res !== 1) return;
										uni.makePhoneCall({
											phoneNumber: tel,
										});
									});
							}
						}
					});
				} else {
					uni.makePhoneCall({
						phoneNumber: tel
					});
				}
			}
		},
	}
</script>


<style lang="scss">
	.app-body {
		padding: 0px 20rpx;
	}

	.sreach {
		background: #fff;
		margin: 20rpx 0;
		border-radius: 32rpx;
		padding: 16rpx 32rpx;

		.icon {
			width: 46rpx;
			height: 46rpx;
		}

		input {
			width: calc(100% - 46rpx - 16rpx - 50px);
			font-size: 16px;
		}

		.search-btn {
			font-size: 14px;
			color: #FF4C4C;
		}
	}


	.search-tm {
		background: #fff;

		.item {
			height: 40px;
			width: 50%;
			color: #787878;
			font-size: 14px;

			.icon {
				width: 10px;
				height: 10px;
				margin-left: 5px;
			}
		}
	}

	.sc-body {
		padding: 20rpx;

		.item {
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 26rpx 44rpx;
			margin-bottom: 10px;

			.info {
				margin-bottom: 10rpx;

				.nickname {
					font-weight: bold;
					font-size: 32rpx;
					color: #303133;
				}

				.mobile {
					font-weight: 500;
					font-size: 30rpx;
					color: #999999;
					margin-left: 10px;
				}

				.id {
					font-weight: 500;
					font-size: 26rpx;
					color: #666666;
				}

				.changci {
					font-weight: 500;
					font-size: 26rpx;
					color: #666666;

					text {
						color: #333333;
					}
				}

			}

			.infos {
				margin-top: 10px;
				border-top: 1px solid #f1f1f1;
				padding-top: 6px;

				.time {
					font-size: 14px;
					color: #666;
				}
			}
		}
	}

	.pop-info {
		.item {
			padding: 20rpx 0;
			text-align: center;
			border-bottom: 1px solid #f1f1f1;
		}
	}

	.infos {
		.sbtn {
			border-radius: 28rpx;
			border: 1px solid #EE2F72;
			height: 45rpx;
			font-size: 12px;
			padding: 0 10px;
			color: #EE2F72;
			text-align: center;
			line-height: 45rpx;
			margin-left: 6px;
		}
	}

	.info-list {
		padding: 10px 0;
		font-size: 12px;
		border-top: 1px solid #f1f1f1;
	}
</style>