<template>
	<view style="overflow: hidden;">
		<view class="top-box">
			<view class="top-title ddflex">
				<view :style="'height: '+ systems.navigationHeight+'rpx;padding-top:' + systems.ktxStatusHeight + 'rpx;'" class="ddflex" @click="goback">
					<image style="width: 48rpx;height: 48rpx;margin: auto;margin-right: 20rpx;" src="../../static/pages/images/back.png"></image>
				</view>
				<view v-if="!storeId&&actMerchant" @click="showPop=true" class="ddflex" :style="'height: '+ systems.navigationHeight + 'rpx;padding-top:' + systems.ktxStatusHeight + 'rpx;'+'line-height: '+ systems.navigationHeight + 'rpx;'">
					<view class="tover" style="max-width: 400rpx;">{{actMerchant.name}}</view>
					<image class="dow-ssicon" src="../../static/pages/images/dow-ssicon.png"></image>
				</view>
				<view v-else :style="'height: '+ systems.navigationHeight + 'rpx;padding-top:' + systems.ktxStatusHeight + 'rpx;'+'line-height: '+ systems.navigationHeight + 'rpx;'">订单</view>
			</view>
			<view class="tab-box">
				<view class="fflex ddflex">
					<view :class="'tab-item fflex '+(!queryType?'tab-item-active':'')" @click="queryTypeChange(-1)">
						<view>全部</view>
						<image class="tab-active-icon" src="../../static/images/tab_cur.png"></image>
					</view>
					<view :class="'tab-item fflex '+(queryType==1?'tab-item-active':'')" @click="queryTypeChange(1)">
						<view>未完成</view>
						<image class="tab-active-icon" src="../../static/images/tab_cur.png"></image>
					</view>
					<view :class="'tab-item fflex '+(queryType==2?'tab-item-active':'')" @click="queryTypeChange(2)">
						<view>已完成</view>
						<image class="tab-active-icon" src="../../static/images/tab_cur.png"></image>
					</view>
				</view>
				<view class="search-box ddflex">
					<view class="search-input ddflex fflex">
						<image class="search-input-icon" src="../../static/pages/images/search.png"></image>
						<input class="fflex" v-model="searchValue" confirm-type="search" @confirm="searchFn"  placeholder="请输入编号查询"/>
						<view class="search-btn" @click="searchFn">搜索</view>
					</view>
				</view>
			</view>
		</view>
		
		<view :style="'height: '+ (systems.navigationHeight+systems.ktxStatusHeight+106+105) + 'rpx;'"></view>
		
		<!-- 订单 -->
		<view>
			<view class="order-item" v-for="item,index in groupList" @click="jumpUrl('/pages/orderDetail/orderDetail?id='+item.id)">
				<view class="ddflex order-item-header">
					<view class="order-time">{{item.leaseTime}}</view>
					<block>
						<view v-if="item.status==2||item.status==3"  class="order-state" style="color: #0FCB27;">已完成</view>
						<view v-else class="order-state">未完成</view>
					</block>
				</view>
				<view class="order-item-body">
					<view class="order-item-content ddflex">
						<view class="order-item-content-label">订单编号</view>
						<view class="order-item-content-data fflex tover">{{item.orderNo}}</view>
					</view>
					<view class="order-item-content ddflex">
						<view class="order-item-content-label">门店名称</view>
						<view class="order-item-content-data fflex tover">{{item.leaseStoreName}}</view>
					</view>
					<view class="order-item-content ddflex">
						<view class="order-item-content-label">租借机柜</view>
						<view class="order-item-content-data fflex tover">{{item.leaseDevId}}</view>
					</view>
					<view class="order-item-content ddflex">
						<view class="order-item-content-label">电池ID</view>
						<view class="order-item-content-data fflex tover">{{item.batteryId}}</view>
					</view>
					<!-- <view class="order-item-content ddflex">
						<view class="order-item-content-label">电池型号</view>
						<view class="order-item-content-data fflex tover">60V 30Ah</view>
					</view>	 -->
					<view class="order-item-content ddflex">
						<view class="order-item-content-label">租赁用户</view>
						<view class="order-item-content-data fflex tover">{{item.realName}} {{item.phone}}</view>
					</view>
					<view class="order-item-content ddflex">
						<view class="order-item-content-label">租借时长</view>
						<view class="order-item-content-data fflex tover">{{item.time?(item.time+'分钟'):'-'}}</view>
					</view>
					<view class="order-item-content ddflex">
						<view class="order-item-content-label">应付金额</view>
						<view class="order-item-content-data fflex tover" style="color: #EB5C20;">{{(item.totalMoney||item.totalMoney===0||item.totalMoney==='0')?item.totalMoney+'元':'-'}}</view>
					</view>
				</view>
				<!-- <view v-if="index!=1" class="order-btns ddflex">
					<view class="order-btn" @click.stop="">确认支付</view>
				</view> -->
			</view>
		</view>
		
		<view v-if="groupList.length==0">
			<view class="nodata">
				<image :src="picUrlss+'empty.png'" mode="aspectFit"></image>
				<text>暂无订单~</text>
			</view>
		</view>
		
		<left-pop v-if="showPop" :showPop="showPop" :btnColor="'#11BB8B'" :showBtn='false' title="解除提醒" width="460rpx"
			@closePop="closePop()" @popSubmit="popSubmit()">
			<view class="" style="height: 100%;flex-direction: column;display: flex;">
				<view @click="closePop()" class="pop-title ddflex" :style="'height: '+ systems.navigationHeight + 'rpx;padding-top:' + systems.ktxStatusHeight + 'rpx;'+'line-height: '+ systems.navigationHeight + 'rpx;'">
					<view>切换门店</view>
					<image class="change-ssicon" src="../../static/pages/images/change.png"></image>
				</view>
				<scroll-view :scroll-y="true" @scrolltolower="loadLeftData" class="pop-content fflex">
					<view :class="'pop-content-item '+(actMerchant.id==item.id?'pop-content-active':'')" v-for="(item,index) in merchantList" @click="changePop(item)">
						<view class="pop-content-name tover">{{item.name}}</view>
						<image class="pop-content-icon" src=""></image>
					</view>
				</scroll-view>
			</view>
		</left-pop>
	</view>
</template>

<script>
	const app = getApp();
	const req = require("../../utils/request.js");
	import leftPop from "../../components/left-pop/index";

	export default {
		components: {
			leftPop
		},
		props: {},
		data() {
			return {
				picUrlss: req.public.picUrls,
				systems: {},
				isTop:0,
				
				showPop: false,
				tempData: null,
				
				userInfo:{},
				
				queryType: '',
				
				groupList: [],
				page: 1,
				limit: 10,
				searchValue:'',
				isQuerying:false,//正在查询数据
				
				merchantList: [],
				merchantPage: 1,
				merchantLimit: 10,
				
				actMerchant:null,//当前门店
				
				merchantId:null,
				storeId:null
			}
		},
		onLoad(options) {
			this.merchantId = options.merchantId
			this.storeId = options.storeId
			if(req.isLogins(true)){
				if(!this.storeId){
					this.getMerchantList()
				}else{
					this.actMerchant={
						id:this.storeId
					}
					this.getDataList()
				}
			}else{
				this.getDataList()
			}
		},
		onShow() {

		},
		onReachBottom() {
			this.getDataList(true)
		},
		methods: {
			goback: function() {
				var pages = getCurrentPages();
				if(pages.length==1){
					uni.switchTab({
						url:'/pages/index/index'
					})
				}else{
					wx.navigateBack({
						delta: pages.length - 2
					})
				}
			},
			jumpUrl(url) {
				if (req.isLogins(true)) {
					uni.navigateTo({
						url: url
					})
				}
			},
			queryTypeChange(val) {
				if (this.queryType == val) return false
				this.queryType = val==-1?'':val
				this.getDataList()
			},
			// 弹窗弹出
			openPop(item) {
				this.tempData = JSON.parse(JSON.stringify(item))
				this.showPop = true
			},
			closePop(){
				this.showPop = false
			},
			changePop(item){
				this.actMerchant = item
				this.closePop()
				this.getDataList()
			},
			getDataList(isPage) {
				if(!this.actMerchant||!this.actMerchant.id) return
				if(this.isQuerying) return
				else this.isQuerying = true
				if (!isPage) {
					this.page = 1
				}
				let queryParams = {
					page: this.page,
					limit: this.limit,
				}
				if(this.searchValue){
					queryParams.orderNo=this.searchValue
				}
				queryParams.type = this.queryType
				queryParams.storeId = this.actMerchant.id
				req.getRequest(
					'/admin/v2/order/page',
					queryParams,
					data => {
						if (data != null && data.list.length > 0) {
							if (!isPage) {
								this.groupList = data.list
							} else {
								this.groupList = this.groupList.concat(data.list)
							}
							this.page++
						} else {
							if (!isPage)
								this.groupList = data.list
							else {}
						}
						uni.hideLoading()
						this.isQuerying = false
					}
				)
			},
			loadLeftData(res){
				this.getMerchantList(true)
			},
			getMerchantList(isPage){
				if (!isPage) {
					this.merchantPage = 1
				}
				let queryParams = {
					page: this.merchantPage,
					limit: this.merchantLimit,
				}
				queryParams.merchantId = this.merchantId?this.merchantId:req.getStorage('userInfo').merchantId?req.getStorage('userInfo').merchantId:null
				req.getRequest(
					'/admin/v2/store/page',
					queryParams,
					data => {
						if (data != null && data.list.length > 0) {
							if (!isPage) {
								this.merchantList = data.list
							} else {
								this.merchantList = this.merchantList.concat(data.list)
							}
							this.merchantPage++
						} else {
							if (!isPage)
								this.merchantList = data.list
							else {}
						}
						if(!isPage){
							if(this.actMerchant){
								
							}else{
								this.actMerchant = this.merchantList[0]
							}
							this.getDataList()
						}
					}
				)
			},
			searchFn(){
				this.getDataList()
			},
		},
		mounted() {
			const systemInfo = uni.getSystemInfoSync();
			// px转换到rpx的比例
			let pxToRpxScale = 750 / systemInfo.windowWidth;
			let systems = {
				ktxStatusHeight: systemInfo.statusBarHeight * pxToRpxScale, // 状态栏的高度
				navigationHeight: 44 * pxToRpxScale // 导航栏的高度
			};
			systems.barHeight = systems.ktxStatusHeight + systems.navigationHeight;
			this.systems = systems;
		},
		onPageScroll: function(e) {
			if (e.scrollTop > this.systems.barHeight) {
				this.isTop = 1;
			} else {
				this.isTop = 0;
			}
		}
	}
</script>

<style>
	@import "./orderLook.css";
</style>
