<template>
	<view class="content">
		<view class="one">
			<view class="one_1" v-if="!focus">
				<input type='text' v-model="name" @input="focusHandler" placeholder='搜索商品'></input>
			</view>
			<view class="one_2" v-if="focus">
				<view class="left">
					<input type="text" v-model="name" @input="focusHandler" placeholder="搜索商品">
				</view>
				<view class="right">
					<button size="mini" class="button" type="default" @click="cancelHandler">取消</button>
				</view>
			</view>
			<!-- 搜索结果显示框 -->
			<scroll-view v-if="focus" class="one_3" scroll-y="true">
				<view class="list" v-for="(item, index) in goodsList" :index="index" :key="index"
					@click="toInput(item)">
					<text class="name"> {{item.name}}</text>
				</view>
			</scroll-view>
		</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">
									<text>
										<text class="iconfont icon-changshangguanli"></text>
										{{item.supplier_name}}
									</text>
									<text class="status">{{item.zhStatus}}</text>
								</view>
								<view class="list_2">
									<view class="left">
										<image v-if="item.spec_file" class="image"
											:src="item.spec_file?item.spec_file.url:''" mode="">
										</image>
										<image v-else class="image" :src="item.good_file?item.good_file.url:''" mode="">
										</image>
									</view>
									<view class="right">
										<view class="right_1">
											<view class="name textOver">
												<text>{{item.good_name||'暂无'}}</text>
											</view>
											<view class="num">
												<text>￥{{item.total_money||'0'}}</text>
												<text>×{{item.num||'0'}}</text>
											</view>
										</view>
										<view class="right_2">
											<view class="spec textOver">
												<text>规格：{{item.spec_name||'暂无'}}</text>
											</view>
										</view>
									</view>
								</view>
								<view class="bottom">
									<button v-if="item.status=='5'" size="mini" type="default"
										@tap.stop="toConfirm(item)">确认发货</button>
									<button v-if="item.status=='-2'||item.status=='-3'" size="mini" type="default"
										@tap.stop="toExam(item)">重新申请购买</button>
									<button v-if="item.status=='8'||item.status=='9'||item.status=='10'" size="mini"
										type="warn" @tap.stop="toKeep(item)">申请维修</button>
									<button v-if="item.status=='4'||item.status=='5'||item.status=='6'" size="mini"
										type="warn" @tap.stop="toRefund(item)">申请退货</button>
								</view>
							</view>
							<view class="is_bottom" v-if="is_bottom">
								<text>{{config.bottom_title}}</text>
							</view>
						</view>
					</scroll-view>
				</view>
			</tabs>
		</view>
	</view>
</template>

<script>
	import moment from 'moment';
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				// 系统设置
				config: {},
				user: {},
				status: '',
				tabs: {
					active: '0',
					menu: []
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 字典表
				statusList: [],
				// 搜索
				focus: false, //控制是否显示带取消按钮的搜索框
				searchInfo: {},
				name: '',
				goodsList: [],
			}
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `status`, e.status || '0');
			that.$set(that.tabs, `active`, e.status || '0');
			that.searchConfig();
		},
		onShow: async function(e) {
			const that = this;
			await that.searchOther();
			that.searchToken();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			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
					});
				}
			},
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						that.$set(that, `user`, res);
						that.$set(that, `name`, '')
						that.$set(that, `searchInfo`, {})
						that.clearPage();
						that.search();
					}
				} 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: user._id,
				}
				if (status != '-1') info.status = status
				let res;
				res = await that.$api(`/OrderDetail/queryInfo`, 'GET', {
					...info,
					...that.searchInfo
				});
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						val.zhStatus = that.searchStatus(val.status)
					}
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 查询状态
			searchStatus(e) {
				const that = this;
				let data = that.statusList.find((i) => i.value == e);
				if (data) return data.label
				else return '暂无'
			},
			// 申请退货
			toRefund(item) {
				const that = this;
				let user = that.user;
				let obj = {
					status: '60'
				}
				uni.showModal({
					title: '提示',
					content: '是否要申请退货?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/OrderDetail/${item._id}`, 'POST', obj);
							if (res.errcode == '0') {
								uni.showToast({
									title: '维护信息成功',
									icon: 'none'
								})
								that.clearPage()
								that.search()
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 确认发货
			async toConfirm(item) {
				const that = this;
				let user = that.user;
				let obj = {
					status: '6'
				}
				uni.showModal({
					title: '提示',
					content: '是否要确认收货?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/OrderDetail/${item._id}`, 'POST', obj);
							if (res.errcode == '0') {
								uni.showToast({
									title: '维护信息成功',
									icon: 'none'
								})
								that.clearPage()
								that.search()
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 重新审批
			async toExam(item) {
				const that = this;
				let user = that.user;
				let obj = {
					status: '0'
				};
				uni.showModal({
					title: '提示',
					content: '确定重新申请该订单吗?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/OrderDetail/${item._id}`, 'POST', obj);
							if (res.errcode == '0') {
								uni.showToast({
									title: '维护信息成功',
									icon: 'none'
								})
								that.clearPage()
								that.search()
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 申请维修
			toKeep(item) {
				const that = this;
				let user = that.user;
				uni.showModal({
					title: '提示',
					content: '确定申请维修该商品吗?',
					success: async function(res) {
						if (res.confirm) {
							let obj = {
								order_id: item._id,
								user: item.user,
								user_name: item.user_name,
								good: item.good,
								good_name: item.good_name,
								good_file: [item.good_file],
								spec: item.spec,
								spec_name: item.spec_name,
								spec_file: [item.spec_file],
								supplier: item.supplier,
								supplier_name: item.supplier_name,
								address: item.address,
								apply_time: moment().format('YYYY-MM-DD HH:mm:ss')
							}
							const res = await that.$api(`/Upkeep`, 'POST', obj);
							if (res.errcode == '0') {
								uni.showToast({
									title: '申请维修成功',
									icon: 'none'
								})
								that.clearPage()
								that.search()
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 查看详情
			toInfo(e) {
				uni.navigateTo({
					url: `/pagesMy/order/detail?id=${e.id||e._id}`
				})
			},
			// 分页
			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);
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (e.name) that.$set(that.searchInfo, `good`, e._id)
				else that.$set(that, `searchInfo`, {})
				that.$set(that, `name`, e.name)
				that.clearPage();
				that.search();
				that.$set(that, `focus`, false)
			},
			// 点击查看商品名称列表
			async focusHandler(e) {
				const that = this;
				if (e.detail.value) {
					let res;
					let info = {
						name: e.detail.value,
						is_use: '0'
					}
					res = await that.$api(`/Good/search`, 'GET', {
						...info,
					})
					if (res.errcode == '0') that.$set(that, `goodsList`, res.data)
					that.$set(that, `focus`, true)
				}
			},
			// 取消搜索
			cancelHandler(e) {
				const that = this;
				that.$set(that, `searchInfo`, {})
				that.$set(that, `name`, '')
				that.$set(that, `focus`, false)
				that.clearPage();
				that.search();
			},
			// 查询其他信息
			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);
					let status = res.data.filter((i) => {
						return i.value == '0' || i.value == '4' || i.value == '5' || i.value == '6';
					});
					var menu = status.map((item) => {
						return {
							title: item.label,
							active: item.value
						}
					})
					menu.unshift({
						title: '全部',
						active: '-1'
					})
					that.$set(that.tabs, `menu`, menu)
				}
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `status`, e.active);
				that.clearPage();
				that.search()
			},
			// 清空列表
			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">
	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

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

			.one_2 {
				display: flex;
				justify-content: center;
				align-items: center;

				.left {
					padding: 0 2vw;
					width: 75vw;

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

				.button {
					background-color: var(--f3CColor);
					color: var(--mainColor);
				}
			}

			.one_3 {
				height: 20vh;

				.list {
					padding: 2vw;
					border-bottom: 1px solid var(--f5Color);

					.name {
						display: inline-block;
						white-space: nowrap;
						width: 100%;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
			}
		}

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

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

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

					.list_1 {
						display: flex;
						justify-content: space-between;
						padding: 2vw 0;
						font-size: var(--font16Size);

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

					.list_2 {
						display: flex;

						.left {
							.image {
								width: 20vw;
								height: 20vw;
								border-radius: 5px;
								border: 1px solid var(--f9Color);
							}
						}

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

							.right_1 {
								display: flex;
								justify-content: space-between;
								margin: 0 0 2vw 0;

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

								.num {
									display: flex;
									flex-direction: column;
									align-items: flex-end;

									text:last-child {
										font-size: var(--font13Size);
										color: var(--f85Color);
									}
								}
							}

							.right_2 {
								font-size: var(--font12Size);
								color: var(--f85Color);
							}
						}
					}

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

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

	.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>