<template>
	<view class="main">
		<view class="one">
			<input type="text" v-model="searchInfo.source_name" @input="toInput" placeholder="搜索名称">
		</view>
		<view class="two">
			<tabs :tabs="tabs" @tabsChange="tabsChange">
				<view class="tabsList">
					<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
						<view class="list-scroll-view">
							<view class="list" v-for="(item, index) in list" :key="index" @tap="toInfo(item)">
								<view class="list_1">
									<view class="top">
										<view class="left">
											<image v-if="item.source.file.length>0" class="image"
												:src="item.source.file&&item.source.file.length>0?item.source.file[0].url:''">
											</image>
											<image v-else class="image"
												:src="config.logo_url&&config.logo_url.length>0?config.logo_url[0].url:''">
											</image>
											<text v-if="item.type=='0'">酒店民宿</text>
											<text v-else>景区门票</text>
										</view>
										<view class="right">{{item.zhStatus}}</view>
									</view>
									<view class="center">
										<view class="left">
											<image v-if="item.source.file.length>0" class="image"
												:src="item.source.file&&item.source.file.length>0?item.source.file[0].url:''">
											</image>
											<image v-else class="image"
												:src="config.logo_url&&config.logo_url.length>0?config.logo_url[0].url:''">
											</image>
										</view>
										<view class="right">
											<view class="name textOne">{{item.source_name||'暂无'}}</view>
											<view class="other">
												下单时间：{{item.buy_time||'暂无'}}
											</view>
											<view class="other">
												数量：{{item.num||'0'}}
											</view>
											<view class="other">
												总价：￥{{item.total_money||'0'}}
											</view>
										</view>
									</view>
								</view>
								<view class="bottom">
									<button v-if="item.status=='0'" size="mini" type="default"
										@tap.stop="toPay(item)">支付</button>
									<button v-if="item.status=='0'" size="mini" type="default"
										@tap.stop="toCancel(item)">取消订单</button>
									<button v-if="item.status=='1'" size="mini" type="default"
										@tap.stop="toFile(item)">券码</button>
									<button v-if="item.status=='1'||item.status=='6'" size="mini" type="default"
										@tap.stop="toOut(item)">申请退款</button>
									<button v-if="item.status=='2'" size="mini" type="default"
										@tap.stop="toRate(item)">评价</button>
								</view>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title||'到底了！'}}</text>
						</view>
					</scroll-view>
				</view>
			</tabs>
		</view>
		<uni-popup ref="popup" background-color="#fff" type="center">
			<div class="canvas">
				<canvas canvas-id="qrcode" class="qrcode"></canvas>
			</div>
		</uni-popup>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	import UQrcode from '../../common/uqrcode.js';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				searchInfo: {},
				config: {},
				user: {},
				status: '',
				tabs: {
					active: '0',
					bgColor: '#ffffff',
					menu: []
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 字典表
				statusList: [],
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `status`, e.status || '-1');
			that.$set(that.tabs, `active`, e.status || '-1');
			that.searchToken();
			that.searchConfig();
			await that.searchOther();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) that.$set(that, `user`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				let user = that.user;
				let status = that.status;
				let info = {
					skip: that.skip,
					limit: that.limit,
					user: that.user._id,
				}
				if (status != '-1') info.status = status
				const res = await that.$api(`/order`, 'GET', {
					...info,
					...that.searchInfo
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						const status = that.statusList.find(i => i.value == val.status)
						if (status) val.zhStatus = status.label
					}
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.source_name) that.$set(that.searchInfo, `source_name`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `status`, e.active)
				that.clearPage();
				that.search()
			},
			// 查看详情
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesIndex/order/detail?id=${item._id||item.id}`
				})
			},
			// 支付
			async toPay(item) {
				const that = this;
				console.log(item);
			},
			// 取消支付
			async toCancel(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定取消订单吗?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/order/${item._id}`, 'POST', {
								status: "4"
							})
							if (res.errcode == '0') {
								uni.showToast({
									title: `取消订单成功`,
								});
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: res.errmsg,
								});
							}
						}
					}
				});
			},
			//券码
			async toFile(item) {
				const that = this;
				UQrcode.make({
					canvasId: 'qrcode', //切记canvasId 里边的内容需要跟canvas里边canvas-id="qrcode"的名字一样
					componentInstance: this,
					text: item._id,
					// text: `${that.$config.serverUrl}/oederInfo?id=${item._id}`, //跳转小程序指定页面
					size: 200,
					margin: 0,
					backgroundColor: '#ffffff',
					foregroundColor: '#000000',
					fileType: 'jpg',
					errorCorrectLevel: UQrcode.errorCorrectLevel.H,
					success: res => {}
				})
				that.$refs.popup.open();
			},
			//申请退款
			async toOut(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定申请退款吗?',
					success: async function(res) {
						if (res.confirm) {
							if (item.status == '1') {
								const res = await that.$api(`/order/${item._id}`, 'POST', {
									status: "5"
								})
								if (res.errcode == '0') {
									uni.showToast({
										title: `申请退款成功`,
									});
									that.clearPage();
									that.search();
								} else {
									uni.showToast({
										title: res.errmsg,
									});
								}
							} else {
								uni.navigateTo({
									url: `/pagesIndex/order/after?id=${item._id||item.id}`
								})
							}
						}
					}
				});
			},
			// 评价
			async toRate(item) {
				const that = this;
				uni.navigateTo({
					url: `/pagesIndex/order/rate?id=${item._id||item.id}`
				})
			},
			async searchOther() {
				const that = this;
				let res;
				//状态
				res = await that.$api('/dictData', 'GET', {
					type: 'order_status',
					is_use: '0'
				})
				if (res.errcode == '0') {
					that.$set(that, `statusList`, res.data);
					const menu = res.data.map((item) => {
						return {
							title: item.label,
							active: item.value
						}
					})
					menu.unshift({
						title: '全部',
						active: '-1'
					})
					that.$set(that.tabs, `menu`, menu)
				}
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

			input {
				padding: 2vw;
				background-color: var(--f1Color);
				font-size: var(--font14Size);
				border-radius: 5px;
			}
		}

		.two {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);

			.tabsList {
				position: relative;
				width: 100vw;
				height: 82vh;

				.list:first-child {
					margin: 2vw;
				}

				.list {
					background-color: var(--mainColor);
					border: 1px solid var(--f5Color);
					padding: 2vw;
					margin: 0 2vw 2vw 2vw;
					border-radius: 5px;

					.list_1 {
						padding: 2vw 0;
						font-size: var(--font16Size);

						.top {
							width: 100%;
							display: flex;
							justify-content: space-between;

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

								.image {
									width: 8vw;
									height: 8vw;
									border-radius: 8vw;
								}

								text {
									padding: 0 0 0 2vw;
								}
							}

							.right {
								font-size: var(--font14Size);
								color: var(--fF0Color);
							}
						}

						.center {
							width: 100%;
							display: flex;
							justify-content: space-between;
							margin: 2vw 0 0 0;

							.left {
								width: 20vw;

								.image {
									width: 20vw;
									height: 20vw;
									border-radius: 1vw;
								}
							}

							.right {
								width: 70vw;
								margin: 0 0 0 2vw;

								.name {
									font-size: var(--font14Size);
								}

								.other {
									padding: 2px 0 0 0;
									font-size: var(--font12Size);
									color: var(--f85Color);
								}
							}
						}
					}

					.bottom {
						text-align: center;
						padding: 1vw 0;

						button {
							margin: 0 5px 0 0;
						}
					}
				}
			}
		}

		.canvas {
			padding: 2vw;

			.qrcode {
				width: 200px;
				height: 200px;
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>