<template>
	<view class="newselecompany_body">
		<view v-if="typeData.type=='send'" class="newselecompany_tip">请选择分发类型<br />与发起方公司...</view>
		<view v-if="typeData.type=='receive'" class="newselecompany_tip">请选择对应<br />接收方公司...</view>
		<view style=" overflow: hidden;" class="newselecompany_container ">
			<view class="sele_Bar mar">
				<view :style="{transform:`translateX(calc(${barIndex}*100%))`}" class="move_item"></view>
				<view @tap="seleBarFn(0)" :style="{color:barIndex==0? 'white' : ''}" class="sele_Bar_item">柴油</view>
				<view @tap="seleBarFn(1)" :style="{color:barIndex==1? 'white' : ''}" class="sele_Bar_item">天然气</view>
				<view @tap="seleBarFn(2)" :style="{color:barIndex==2? 'white' : ''}" class="sele_Bar_item">尿素</view>
			</view>
			<view class="newselecompany_seach mar"> 
				<view class="newselecompany_seach_title">查找企业</view>
				<view class="newselecompany_seach_input">
					<uni-easyinput v-model="seachValue" placeholder="请输入企业名称或id" prefixIcon="search" confirmType='search' @confirm='seachFn'/>
				</view>
			</view>
			<view style=" overflow: hidden;" class="newselecompany_container_list">
				<view v-if="!scrollY&&typeData.type=='send'" class="newselecompany_container_list_tip mar">最近选择</view>
				<view style="flex: 1; margin-top:26rpx;  overflow: hidden; ">
					<scroll-view v-if="list.length!==0" style="height: 100%;overflow: hidden; " :scroll-y="scrollY" @scrolltolower='scrolltolower'>
						<view v-for="(item,index) in list " :key="index" @click="seleFn(item)"
							class="newselecompany_container_list_item mar">
							<view class="newselecompany_container_list_lable">
								<text>{{item.parentMark==0?'主公司':'分公司'}}</text></view>
							<view class="newselecompany_container_list_name">
								<text>{{item.name}}</text>
								<text
									class="newselecompany_container_list_identification">{{item.companyType|companyType}}</text>
							</view>
							<view class="companyDetails">
								<text class="smallText">{{item.id}}</text>
								<text class="listtext">账户余额 <text
										style="margin-left: 5rpx;font-size: 27rpx;">{{item.balance||'0'}}</text> </text>
							</view>
						</view>
					</scroll-view>
					<view v-if="list.length==0"
						style="width: 100vw; height: 100%; display: flex;align-items: center;justify-content: center;">
						<image src="../../static/qx.png" style="width: 536rpx; height: 399rpx;"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import scroll from '@/components/scroll'
	import driverManagement from '@/api/driverManagement'
	export default {
		components: {
			scroll
		},
		data() {
			return {
				typeData: '',
				scrollY:false,
				timer: null,
				barIndex: 0,
				seachValue: '',
				seleBarData:[
					{
					 lable:'柴油',
					 value:0
					},
					{
					 lable:'天然气',
					 value:1
					},
					{
					 lable:'尿素',
					 value:2
					}
				],
				lately: [],
				list: [],
				selecompany: null,
				page: {
					currentPage: 1,
					pageSize: 10,
					index: "0",
					params: {
						companyType: "",
						nameOrCid: "",
						parentId: "",
						parentMark: ""
					}
				}
			}
		},
		onLoad(e) {
			if (e.jsData) {
				this.typeData = JSON.parse(e.jsData)
				// if(this.typeData.type=='send'){
				// 	this.getflist()
				// }else{
				// 	this.getzlist()
				// }
				if(this.typeData.type!=='send'){
					console.log(this.typeData.data.companyType,'****************')
					this.barIndex = Number(this.typeData.data.companyType)== 0?Number(this.typeData.data.companyType):Number(this.typeData.data.companyType)== 2?1:2 
				}
			}
			this.initFn()
		},
		filters: {
			companyType(e) {
				switch (Number(e)) {
					case 0:
						return 'FUEL'
					case 1:
						return 'OIL'
					case 2:
						return 'LNG'
					case 3:
						return 'SCR'
				}
			}
		},
		methods: {
			async initFn() {
				let lately = uni.getStorageSync(this.typeData.type)
				// if (lately) {
				// 	for (let i = 0; i < lately.length; i++) {
				// 		console.log(lately[i])
				// 		let data = {
				// 			companyType:lately[i].companyType,
				// 			nameOrCid: lately[i].id,
				// 			parentId: lately[i].parentMark == 0 ? null: lately[i].parentId,
				// 			parentMark: lately[i].parentMark == 0 ? null: 1
				// 		}
				// 		this.page.params = Object.assign(this.page.params, data)
				// 		if (this.typeData.type == 'send') {
				// 			 await driverManagement.findAllByField(this.page).then(res => {
				// 				if(res.code!==20000)return
				// 				this.list.push(res.data.list[0])
				// 			})
				// 		} else {
				// 			 await driverManagement.findAllByField(this.page).then(res => {
				// 				if(res.code!==20000)return
				// 				this.list.push(res.data.list[0])
				// 			})
				// 		}
				// 	}
				// }
				if (this.typeData.type == 'send') {
					driverManagement.findThreeDataByIds(lately?lately:[]).then(res=>{
						if(res.code!==20000)return
						this.list = res.data?res.data:[]
					})
				} else { 
					this.getzlist()
				}
				
			},
			setLately(e) {
				let lately = uni.getStorageSync(this.typeData.type) ? uni.getStorageSync(this.typeData.type) : []
				console.log(' %c look ↓', 'color:red;font-size:50px')
				console.log(lately)
				if (lately.length !== 0) {
					let open = false
					lately.forEach(item => {
						if (item.id == e.id) {
							console.log(item.id == e.id)
							open = true
						}
					})
					if(!open){
					   if (lately.length == 3) {
							lately.splice(2, 1)
						}
						lately.unshift(e.id)
						uni.setStorageSync(this.typeData.type, lately)
					}
				} else {
					lately.unshift(e.id)
					uni.setStorageSync(this.typeData.type, lately)
				}

			},
			seleFn(e) {
				clearTimeout(this.timer)
				this.selecompany = e
				this.selecompany.type = this.typeData.type
				this.setLately(e)
				uni.$emit('company', this.selecompany)
				this.timer = setTimeout(() => {
					uni.navigateBack()
				}, 200)
			},
			getflist() {
				this.scrollY = true
				let data = {
					companyType: this.barIndex == 0 ? this.barIndex : this.barIndex == 1 ? 2 : 3,
					nameOrCid: this.seachValue,
					parentId: null,
					parentMark: null
				}
				this.page.params = Object.assign(this.page.params, data)
				driverManagement.findAllByField(this.page).then(res => {
					if (res.code != 20000) return
					if (res.data.list.length == 0) {
						uni.showToast({
							title: '没有数据了哦',
							icon: 'none'
						})
					}
					if (this.page.currentPage !== 1) {
						this.list = this.list.concat(res.data.list);
						return
					}
					this.list = res.data.list
				})
			},
			getzlist() {
				this.scrollY = true
				let data = {
					companyType:this.barIndex == 0 ? this.barIndex : this.barIndex == 1 ? 2 : 3,
					nameOrCid: this.seachValue,
					parentId: this.typeData.data.parentMark == 0 ? this.typeData.data.id : this.typeData.data.parentId,
					parentMark: 1
				}
				this.page.params = Object.assign(this.page.params, data)
				driverManagement.findAllByField(this.page).then(res => {
					if (res.code != 20000) return
					if (res.data.list.length == 0) {
						uni.showToast({
							title: '没有数据了哦',
							icon: 'none'
						})
					}
					if (this.page.currentPage !== 1) {
						this.list = this.list.concat(res.data.list);
						return
					}
					this.list = res.data.list
				})
			},
			scrolltolower() {
				this.page.currentPage += 1
				if (this.typeData.type == 'send') {
					this.getflist()
				} else {
					this.getzlist()
				}
			},
			seachFn() {
				this.scrollY = true
				this.page.currentPage = 1
				if (this.typeData.type == 'send') {
					this.getflist()
				} else {
					this.getzlist()
				}

			},
			seleBarFn(e) {
				this.page.currentPage = 1
				if (this.typeData.type == 'send') {
					this.barIndex = e
					this.getflist()
				} else {
					this.getzlist()
				}
			}
		}
	}
</script>

<style scoped>
	.newselecompany_container_list_identification {
		color: #BBBBBB;
		font-size: 48rpx;
		position: absolute;
		right: 0;
		top: -21rpx;
		font-weight: 600;
		line-height: 56rpx;
	}

	.newselecompany_container_list_name {
		margin-top: 21rpx;
		font-size: 28rpx;
		color: #666666;
		display: flex;
		justify-content: space-between;
		position: relative;
	}

	.listtext {
		font-size: 28rpx;
		color: #666666;
	}

	.smallText {
		font-size: 20rpx;
		color: #BBBBBB;
	}

	.companyDetails {
		display: flex;
		justify-content: space-between;
		margin-top: 36rpx;
	}

	.newselecompany_container_list_lable {
		font-size: 16rpx;
		width: 80rpx;
		height: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #9F43CC;
		color: white;

	}

	.newselecompany_container_list_item {
		background-color: white;
		padding: 0 40rpx 20rpx 40rpx;
		border-radius: 15rpx;
		margin-bottom: 20rpx;
	}

	.newselecompany_container_list_tip {
		margin-top: 60rpx;
		font-size: 28rpx;
		color: #BBBBBB;
	}

	.newselecompany_container_list {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.newselecompany_seach_input {
		background-color: white;
		overflow: hidden;
		border-radius: 20rpx;
		margin-top: 30rpx;
	}

	.newselecompany_seach_title {
		font-size: 35rpx;
	}

	.newselecompany_seach {
		background-color: #2866FF;
		padding: 40rpx;
		border-radius: 10px;
		color: white;
		margin-top: 60rpx;
	}

	.move_item {
		position: absolute;
		left: 10rpx;
		width: calc((100% - 20rpx) / 3);
		height: 60rpx;
		background: #2866FF;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #999999;
		z-index: 0;
		transition: all .3s;
	}

	.mar {
		margin-left: 42rpx;
		margin-right: 42rpx;
	}

	.sele_Bar_item {
		/* width: 287rpx; */
		flex: 1;
		height: 60rpx;
		/* background: #2866FF; */
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #999999;
		z-index: 1;
		transition: color .4s;
	}

	.sele_Bar {
		/* background-color: #18B566; */
		/* margin: 31rpx 40rpx; */
		padding: 10rpx;
		display: flex;
		position: relative;
		background-color: #FFFFFF;
		border-radius: 10px;
	}

	.newselecompany_container {
		flex: 1;
		/* padding: 0 42rpx; */
		display: flex;
		flex-direction: column;
	}

	.newselecompany_tip {
		font-size: 48rpx;
		color: #666666;
		margin-top: 100rpx;
		margin-left: 70rpx;
		margin-bottom: 60rpx;
	}

	.newselecompany_body {
		width: 100%;
		height: calc(100vh - var(--status-bar-height));
		display: flex;
		flex-direction: column;
		position: relative;
		/* overflow: hidden; */
	}
</style>
