<template>
	<view class="moreShelfPart flex flex_column">
		<view class="moreShelfPart-search">
			<u-search
			placeholder="请输入关键字查询" 
			v-model="queryWord" 
			:show-action="isGobleSearch" 
			@focus="isGobleSearch=true" 
			@blur="searchPart"
			@custom="searchPart" 
			@search="searchPart" 
			@clear="searchPart"
			:action-style="{'color': '#fff', 'font-size': '24upx', 'background-color': '#57a3f3', 'border-radius': '6upx', 'padding': '12upx 0'}">
			</u-search>
		</view>
		<view class="moreShelfPart-body">
			<scroll-view
			class="nav-right" 
			scroll-y 
			@scrolltolower="onreachBottom">
				<!-- vin适配配件 -->
				<view>
					<view class="nav-right-title">该车适配</view>
					<view class="nav-right-item" v-for="(item, index) in vinPartList" :key="item.id">
						<view class="uni-col-2">
							<u-image :src="item.images+'?x-oss-process=image/resize,m_fixed,h_120,w_120,color_ffffff'"  @click="viewImg(item)" border-radius="30" width="120" height="120" bg-color="#EBEBEB" ></u-image>
						</view>
						<view class="item-info uni-col-10">
							<view class="item-name">
								<text>{{item.name}}</text>
							</view>
							<view class="item-detail">
								<view class="nav-right-item-btns flex align_center justify_between">
									<view>
										<text class="item-detail-text">{{item.code}}</text>
									</view>
									<view>
										<u-button :throttle-time="100" class="item-btn" v-if="!item.checked" type="primary" :style="{background:'#1283d4'}" shape="circle"  size="mini" @click="choosePart(item)">选择</u-button>
										<u-button :throttle-time="100" class="item-btn" v-else type="success" shape="circle" size="mini" @click="choosePart(item,0)">已选择</u-button>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-if="vinPartList&&vinPartList.length==0">
						<u-empty v-if="vinstatus!='loading'" :src="`/static/nodata.png`" icon-size="120" text="暂无配件" mode="list" :margin-top="30"></u-empty>
					</view>
					<view style="padding: 20upx;" v-if="vinstatus=='loading'" >
						<u-loadmore :status="vinstatus" />
					</view>
				</view>
				<!-- 其它配件 -->
				<view class="nav-right-title" style="margin-top: 20rpx;">其它配件</view>
				<view class="nav-right-item" v-for="(item, index) in partList" :key="item.id">
					<view class="uni-col-2">
						<u-image :src="item.images+'?x-oss-process=image/resize,m_fixed,h_120,w_120,color_ffffff'" @click="viewImg(item)" border-radius="30" width="120" height="120" bg-color="#EBEBEB" ></u-image>
					</view>
					<view class="item-info uni-col-10">
						<view class="item-name">
							<text>{{item.name}}</text>
						</view>
						<view class="item-detail">
							<view class="nav-right-item-btns flex align_center justify_between">
								<view>
									<text class="item-detail-text">{{item.code}}</text>
								</view>
								<view>
									<u-button :throttle-time="100" class="item-btn" v-if="!item.checked" type="primary" :style="{background:'#1283d4'}" shape="circle"  size="mini" @click="choosePart(item)">选择</u-button>
									<u-button :throttle-time="100" class="item-btn" v-else type="success" shape="circle" size="mini" @click="choosePart(item,1)">已选择</u-button>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-if="partList&&partList.length==0">
					<u-empty v-if="status!='loading'" :src="`/static/nodata.png`" icon-size="240" :text="noDataText" mode="list" :margin-top="50"></u-empty>
				</view>
				<view style="padding: 20upx;" v-if="total>pageSize || status=='loading'">
					<u-loadmore :status="status" />
				</view>
			</scroll-view>
		</view>
		<view class="moreShelfPart-footer">
			您可以选择更多数字货架上暂时没有库存的配件进行临配
		</view>
	</view>
</template>

<script>
	import { getShelfProductList, getShelfProductType, queryPartCodeByVin, getShelfQueryList } from '@/api/shelf'
	export default {
		data() {
			return {
				queryWord: '',
				isGobleSearch: false,
				vinPartList: [],
				partList: [],
				pageNo:1,
				pageSize: 10,
				vinCode: [],
				vinNumber: '',
				total: 0, // 列表总数
				noDataText: '暂无配件',
				status: 'loading',
				vinstatus: 'loading'
			}
		},
		onLoad(opts) {
			this.vinNumber = opts.vinNumber
			if(this.vinNumber){
				this.getCarCodeByVin()
			}else{
				this.vinstatus = 'nomore'
				this.getShelfProductList()
			}
		},
		methods: {
			viewImg(item){
				uni.previewImage({
					urls: [item.images]
				})
			},
			clearSearch(){
				this.queryWord = ''
				this.pageNo = 1
				this.partList = []
			},
			// 搜索配件
			searchPart(){
				this.getShelfProductList()
			},
			getCarCodeByVin(){
				queryPartCodeByVin({vinNumber:this.vinNumber}).then(res => {
					console.log(res,'this.vinCode')
					if(res.status == 200){
						this.vinCode = res.data
						this.getVinShelfPartList()
						this.getShelfProductList()
					}
				})
			},
			// 根据客户vin查询该车可用数字货架中的配件
			getVinShelfPartList(){
				let params = {
				      queryWord: this.queryWord,
				      partsClsId: '',
				      vin: this.vinNumber,
					  codeList: this.vinCode,
					  currentInven:0
				  }
				this.vinstatus = 'loading'
				getShelfQueryList(params).then(res => {
					console.log(res,'getVinShelfPartList')
					this.vinPartList = res.data.length ? this.fiterChecked(res.data) : []
					this.vinstatus = 'nomore'
				})
			},
			// 获取数字货架配件列表
			getShelfProductList(){
				const _this = this
				let params = {
				    pageNo: this.pageNo,
				    pageSize: this.pageSize,
					excludeCodeList: this.vinCode,
					queryWord: this.queryWord,
					currentInven:0
				}
				console.log(params)
				_this.status = 'loading'
				getShelfProductList(params).then(res => {
					uni.hideLoading()
					console.log(res,'获取数字货架配件列表')
					if(res.status == 200){
						let list = res.data.list
						console.log(list)
						if (list && list.length>0){
							// 分页 拼接数据
							if (_this.pageNo != 1) {
								_this.partList = _this.partList ? _this.partList.concat(this.fiterChecked(list)) : this.fiterChecked(list)
							} else {
								_this.partList = this.fiterChecked(list)
							}
							this.total = res.data.count
							console.log(_this.status)
							if (_this.partList.length == res.data.count) {
								_this.status = 'nomore'
							} else {
								_this.status = 'loadmore'
							}
							console.log(_this.status)
						} else {
							console.log(_this.status)
							_this.partList = list || []
							this.total = 0
							_this.status = 'nomore'
						}
						console.log(_this.status)
						_this.noDataText = '暂无配件'
					}else{
						_this.status = 'loadmore'
						_this.partList = []
						_this.total = 0
						_this.noDataText = res.message ? res.message : '网络似乎出错了，请稍后再试'
					}
				})
			},
			// 加载更多
			onreachBottom () {
				if(this.partList.length < this.total ){
					if(this.isGobleSearch&&this.queryWord==''){
						return
					}
					this.pageNo++
					this.getShelfProductList()
				}else{
					this.status = "nomore"
				}
			},
			// 过滤已选择的
			fiterChecked (list){
				this.$store.state.vuex_shelfChoosePart.map(item => {
					let p = list.find(k => {
						return k.productSn == item.productSn
					})
					if(p){
						p.checked = true
					}
				})
				return list
			},
			// 对比已选择项目
			hasChecked (row){
				let b = this.$store.state.vuex_shelfChoosePart
				let has = b.findIndex(item => {
					return item.productSn && item.productSn == row.productSn
				})
				return has
			},
			// 选择配件
			choosePart(item,type){
				const hs = this.hasChecked(item)
				const b = this.$store.state.vuex_shelfChoosePart
				// 取消选择
				if(hs>=0){
					item.checked = false
					b.splice(hs,1)
				}else{
					item.checked = true
					item.qty = 1
					b.push(item)
				}
				if(type==0){
					this.vinPartList.splice()
				}else{
					this.partList.splice()
				}
			}
		}
	}
</script>

<style lang="less">
	.moreShelfPart{
		height: 100vh;
		.moreShelfPart-search{
			padding: 20rpx;
		}
		.moreShelfPart-body{
			flex-grow: 1;
			background-color: #fff;
		}
		.moreShelfPart-footer{
			padding: 20rpx;
			color: #191919;
			font-size: 26rpx;
			text-align: center;
		}
		.nav-right{
			padding: 0 30upx;
			height: calc(100vh - 180rpx);
			box-sizing: border-box;
			.nav-right-title{
				font-weight: bold;
				font-size: 32upx;
				padding: 10rpx 0;
			}
			.nav-right-item{
				padding: 10upx;
				border-bottom: 2rpx solid #f5f5f5;
				display: flex;
				.item-info{
					padding-left: 20rpx;
					.item-name{
						font-size: 32upx;
						display: flex;
						align-items: center;
						>image{
							width: 38rpx;
							height: 38rpx;
							margin-right: 10rpx;
						}
						>text{
							flex: 1;
							word-break: break-all;
						}
					}
					.item-detail{
						.item-detail-info{
							padding: 10upx 0 4upx;
						}
						.item-detail-text{
							font-size: 24upx;
							color: #999;
							margin-right: 20upx;
							word-break: break-all;
						}
						.item-price{
							margin-top: 10upx;
							.item-detail-text{
								color: #333;
							}
						}
						 
						.nav-right-item-btns{
							text-align: right;
							margin-top: 10rpx;
							> view{
								&:first-child{
									.item-btn {
										margin-right: 10upx;
										width: auto;
									}
								}
								&:last-child{
									.item-btn {
										margin-left: 10upx;
									}
								}
							}
							.item-btn-icon{
								margin-left: 30upx;
								vertical-align: middle;
							}
							.item-btn {
								width: 112upx;
								&:after{
									border-color: #aaa;
								}
							}
							.item-btn.btn-unselected{
								background-color: #2db7f5!important;;
								border-color: #2db7f5!important;;
							}
							.item-btn.btn-selected{
								background-color: #19be6b!important;;
								border-color: #19be6b!important;
							}
						}
					}
				}
				.item-btn{
					margin: 0;
					width: 112upx;
					color: #fff;
				}
				.item-btn.btn-unselected{
					background-color: #2db7f5!important;;
					border-color: #2db7f5!important;;
				}
				.item-btn.btn-selected{
					background-color: #19be6b!important;;
					border-color: #19be6b!important;
				}
			}
		}
	}
</style>
