<template>
	<view class="content">
		<view class="header-box">
			<UniStatusBar></UniStatusBar>
			<view class="title">
				<u-image src="/static/logo-h.png" width="349" height='40'></u-image>
			</view>
		</view>
		<view class="cbody">
		<view class="body-box bg-blue">
			<!-- 滚动区域 -->
			<view class="scroll-list">
				<scrollBox v-if="list.length" :list="list"></scrollBox>
			</view>
			<!-- 保证金 -->
			<view class="notices flex align_center" v-if="bondRecord" @click="toPayBondAmount">
				<view><u-icon name="volume" color="#f2a557" :size="28" :margin-right='15'></u-icon> 您有一笔保证金待支付 ￥{{bondRecord.bondAmount}}</view> 
				<!-- <text class="text">立即支付>></text> -->
			</view>
			<!-- 待取货提醒 -->
			<view class="notices flex align_center" v-if="!bondRecord&&orderTodo" @click="toOrder">
				<view><u-icon name="volume" color="#f2a557" :size="28" :margin-right='15'></u-icon> 您有待取货的货架订单哦！</view> <text class="text">立即处理>></text>
			</view>
			<!-- 名片 -->
			<view class="userCard flex align_center justify_between" @click="toUser">
				<view class="userCard-info flex align_center justify_between">
					<view>
						<u-image v-if="!hasLogin" src="/static/def_personal_avatar.png" width="90" height="90"></u-image>
						<open-data v-else type="userAvatarUrl" class="user-photo"></open-data>
					</view>
					<view>
						<view v-if="!hasLogin" style="font-size: 40rpx;">
							请点击登录
						</view>
						<view v-else>
							<view>{{userInfo.userNameCN}}</view>
							<view>{{userInfo.orgName}}</view>
						</view>
					</view>
				</view>
				<view>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<view class="body-box">	
			<!-- 扫描按钮 -->
			<view class="scanButton flex align_center" @click="openCamera">
				<u-icon name="scan" size="60"></u-icon><text class="ml10">扫描VIN</text>
			</view>
			<view class="flex align_center justify_center">
				<u-image src="/static/banner1.png" width="645" height="504"></u-image>
			</view>
		</view>
		<!-- 扫描记录 -->
		<view class="list-box">
			<!-- 扫描记录 -->
			<u-cell-group :border="false">
				<u-cell-item title="VIN查询历史" @click="toAllRecord" :title-style="{fontSize:'1em'}" value="全部">
					<u-icon slot="icon" size="38" color="#066cff" style="margin-right: 0.3em;" name="order"></u-icon>
				</u-cell-item>
			</u-cell-group>
			<u-cell-group :border="false" v-if="hasLogin">
				<u-cell-item v-for="item in vinList" @click="toChoosePart(item)" :key="item.id" :title="item.vinCode" :title-style="{fontSize:'1em'}" :label="item.brandName +' '+item.modelName">
					<text slot="icon"></text>
				</u-cell-item>
				<view style="padding: 20upx;">
					<u-empty :src="`/static/nodata.png`" icon-size="180" :text="noDataText" img-width="120" v-if="vinList.length==0 && status!='loading'" mode="list"></u-empty>
					<u-loadmore v-if="status=='loading'" :status="status" />
				</view>
			</u-cell-group>
			<view class="des" v-if="vinList.length&&hasLogin">仅展示最近5条记录，点击“全部”查看更多</view>
			<view class="des" v-if="!hasLogin">请登录后查看历史记录</view>
		</view>
		</view>
	</view>
</template>

<script>
	import {
	    mapState,
	    mapMutations,
	} from 'vuex'
	import scrollBox from '@/components/scrollBox.vue'
	import UniStatusBar from '@/components/uni-status-bar/uni-status-bar.vue'
	import { shelfBondRecordWaitPayRecord, findStoreShelf, shelfGetTotalWaitQty } from '@/api/shelf.js'
	import { listLookUp, getLookUpDatas } from '@/api/data.js';
	import { scanVinLogQueryRoll, getScanVinLogList }from '@/api/car.js'
	export default {
		components: {
			scrollBox,
			UniStatusBar
		},
		data() {
			return {
				status: 'loading',
				noDataText: '暂无数据',
				bondRecord: null, // 保证金
				list:[],
				vinList: [],
				orderTodo: null,
			}
		},
		computed: {
			...mapState(['hasLogin']),
			hasShelf(){
				return this.$store.state.vuex_storeShelf
			},
			userInfo(){
				return this.$store.state.vuex_userInfo
			}
		},
		onReady() {
			// 检测是否登录
			this.$store.dispatch('checkLogin',()=>{
				this.pageInit()
			})
			
			this.getScrollLog()
		},
		onLoad() {
			// 获取数据字典
			this.getListLookUp();
			// 获取支付，收款方式
			this.getLookUpList('PAY_PROCUCT_TYPE', 'vuex_paymentTypeList');
			// 刷新首页数据
			uni.$on("refashHome",()=>{
				this.pageInit()
			})
		},
		onUnload() {
			uni.$off('refashHome')
		},
		// 下拉刷新
		onPullDownRefresh(){
			this.pageInit()
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onShareAppMessage() {
			
		},
		methods: {
			pageInit(){
				if(this.hasLogin){
					// 查询是否有数字货架
					this.getStoreShelf()
					// 获取扫描记录
					this.getVinLog()
				}
			},
			//  获取数据字典
			getListLookUp() {
				const _this = this;
				listLookUp({ pageNo: 1, pageSize: 1000 }).then(res => {
					if (res.status == 200) {
						_this.$store.state.vuex_allLookUp = res.data;
					}
				});
			},
			// 或某一项字典列表，参数code
			getLookUpList(code, vuexKey) {
				getLookUpDatas({ type: code }).then(res => {
					if (res.status == 200) {
						this.$store.state[vuexKey] = res.data || [];
					}
				});
			},
			// 待取货数量合计
			shelfGetTotalWaitQty(){
				shelfGetTotalWaitQty({billState:'WAIT'}).then(res => {
					if(res.status == 200){
						this.orderTodo = res.data
					}
				})
			},
			// 货架订单列表
			toOrder(){
				if(this.hasShelf){
					uni.navigateTo({
						url: "/pages/digitalShelf/orderList"
					})
				}else{
					uni.showToast({
						icon:'none',
						title: '门店没有关联数字货架，无法使用此功能！'
					})
				}
			},
			getScrollLog(){
				scanVinLogQueryRoll({pageNo:1,pageSize:1000}).then(res => {
					this.list = res.data.list || []
				})
			},
			getVinLog(){
				this.vinList  = []
				this.status = "loading"
				getScanVinLogList({pageNo:1,pageSize:5}).then(res => {
					this.vinList = res.data.list || []
					this.vinList = this.vinList.filter(item => item.vinCode)
					this.status = 'nomore'
				})
			},
			// 查询是否支持数字货架
			getStoreShelf(){
				findStoreShelf().then(res => {
						console.log(res)
						this.$store.state.vuex_storeShelf = res.data;
						if(res.data){
							// 保证金查询
							this.getShelfBWPayRecord()
							// 获取订单信息
							this.shelfGetTotalWaitQty()
						}
				})
			},
			// 获取保证金金额
			getShelfBWPayRecord(){
				const data = this.$store.state.vuex_storeShelf
				shelfBondRecordWaitPayRecord({shelfSn: data.shelfSn, storeSn: data.storeSn}).then(res => {
					console.log(res,'获取保证金金额')
					this.bondRecord = res.data
				})
			},
			// 保证金支付
			toPayBondAmount(){
				 
			},
			// 去扫描
			openCamera(){
				uni.navigateTo({
					url: this.hasLogin ? "/pages/scan-frame/scan-frame" : '/pages/login/login'
				})
			},
			// 查看扫描记录
			toAllRecord(){
				uni.navigateTo({
					url: this.hasLogin ? "/pages/vinRecord/vinRecord" : '/pages/login/login'
				})
			},
			toUser(){
				uni.navigateTo({
					url: this.hasLogin ? '/pages/personData/personData' : '/pages/login/login'
				})
			},
			toChoosePart(item){
				if(this.hasShelf){
					item.text = item.modelInfo
					uni.navigateTo({
						url: "/pages/digitalShelf/choosePart?vinNumber="+item.vinCode+"&carList="+encodeURIComponent(JSON.stringify(item))
					})
				}else{
					uni.showToast({
						icon:'none',
						title: '门店没有关联数字货架，无法使用此功能！'
					})
				}
			}
		}
	}
</script>

<style lang="less">
	.content {
		margin: 0;
		padding: 0;
		height: 100vh;
		display: flex;
		flex-direction: column;
		background-image: linear-gradient(#86defa 0%,#cdeff9 25%,#ffffff 40%);
		.header-box{
			z-index: 5;
			padding: 0.3em 1em;
			background: #066cff;
			 .title{
				 padding: 0.3em 0;
			 }
		}
		.cbody{
			flex-grow: 1;
			overflow-y: auto;
			overflow-x: hidden;
		}
		.body-box{
			padding: 0.5em 1em;
			z-index: 4;
			.notices{
				padding: 0.5em 0 0;
				font-size: 1em;
				view{
					color: #ffaf46;
				}
				.text{
					color: #ffffff;
					margin-left: 1em;
				}
			}
			.scanButton{
				margin: 0.8em 0 0.5em;
				padding: 0.5em;
				background: #066cff;
				color: #fff;
				font-size: 1.5em;
				border-radius: 5em;
				justify-content: center;
				&:active{
					opacity: 0.8;
				}
			}
			.ml10{
				margin-left: 0.4em;
			}
		}
		.bg-blue{
			position: relative;
			> view{
				z-index: 100;
				position: relative;
			}
			&::after{
				/* 这个伪类的作用就是一个圆弧的背景色 */
				width: 130%;
				height: 100%;
				position: absolute;
				/*设置水平居中*/
				left: -15%;
				/* 之所以left:20%,是因为width:140%，若width:160%，那么left:30%才能水平居中 */
				top: -20%; 
				/*层叠顺序，最底层显示*/
				content: '';
				border-radius: 0 0 50% 50%;
				/*分别对应 左上 右上 右下 左下 可以修改成其它颜色*/
			    background: linear-gradient(to bottom, #066cff, #1d77ff);  
			}
			.userCard{
				position:relative;
				left:0;
				bottom:-1rem;
				padding:0.8em;
				background: #fff;
				border-radius: 0.5em;
				box-shadow: 0.3em 0.3em 1em #c4e5ee;
				.userCard-info{
					> view{
						&:last-child{
							margin-left: 1em;
							line-height: 2em;
							>view{
								padding: 0.2em;
							}
						}
					}
				}
				.user-photo{
					display: block;
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
					overflow: hidden;
				}
			}
		}
		.list-box{
			padding: 0 1em;
			overflow: auto;
			.des{
				text-align: center;
				padding: 1em 0;
				color: #999;
				font-size: 0.8em;
			}
		}
	}
</style>
