<template>
	<view class="container">
		<uni-navbar title="企业司机"></uni-navbar>
		<view class="searchBox">
			<u-button class="custom-style3" shape="circle" @tap="openSearchInput"
				color="linear-gradient(to top, #F59A02, #FFD334)">
				<u-icon size="40rpx" :bold="true" color="#FFFFFF" name="search"></u-icon>
			</u-button>
		</view>
		<view class="box" style="overflow: hidden;">
			<view class="searchInput commonMt" v-if="searchInputShow">
				<u-input v-model.trim="inputValue" placeholderStyle="color: #7E8EC1" placeholder="请输入姓名或者车牌号"
					confirm-type="search" @confirm="search" :focus="true" shape="circle" class="searchInputStyle"
					@blur="searchInputShow = false">
					<template slot="prefix">
						<u-icon size="40rpx" :bold="true" color="#FFFFFF" name="search"></u-icon>
					</template>
				</u-input>
			</view>
			<u-button v-else class="commonMt custom-style" shape="circle" @click="addCar"
				color="linear-gradient(to top, #F59A02, #FFD334)">
				<!-- <u-icon class="iconStyle" color="#FFFFFF" name="plus-circle-fill"></u-icon>
				新增司机 -->
				<view class="button-class">
					<image style="width: 29rpx;height: 29rpx;" src="../../static/images/transport/plus.png"
						mode="widthFix">
					</image>
					<text style="margin-left: 19rpx;">新增司机</text>
				</view>
			</u-button>
			<view class="box2">
				<view class="commonMb">
					<view @click="lookInfo(item)" class="car-content" v-for="(item,index) in searchData" :key="index">
						<view class="car-content-one commonBj">
							<view class="">
								<view class="content-top">
									<view class="text-part">
										<view class="car"></view>
										<text class="titleStyle">{{ item.driverName }}</text>
										<!-- <text :class="getClass(item.state)+' fontStyle'">{{item .stateName}}</text> -->
									</view>
								</view>
								<view class="centenBox">
									<!-- 	<view class="link">
								</view> -->
									<view class="content-bottom listText" style="margin-top: 20rpx;">
										<view class="icon1 iconStyle2"></view>
										<text>车牌号：
											<text class="bold"
												:class="item.carNumber?'':'colorRed'">{{item.carNumber ?item.carNumber :'未绑定车辆'}}</text>
										</text>
									</view>

								</view>
								<view class="content-bottom listText">
									<view class="icon2 iconStyle2"></view>
									<text>电&emsp;话： {{item.phone}}</text>
								</view>
							</view>
							<view class="btn-right">
								<u-button @tap.native.stop="openIsRest(item)" class="buttonStyle2" shape="circle"
									color="linear-gradient(to top, #F59A02, #FFD334)">
									重置密码
								</u-button>
								<u-button @click.native.stop="changeState(item)" class="buttonStyle2" shape="circle"
									:color="getColor(item.deleteFlag=='0')">
									{{item.deleteFlag=='0'?'解绑司机':'换绑车辆'}}
								</u-button>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="show" mode="center">
			<view class="model">
				<view class="modelTitle">提 示 <view class="close" @click="show=false"></view>
				</view>
				<view class="textBox colorStyle2">
					<text>请确认本企业</text>
					<text>是否要解除<text style="color: #333333;font-weight: bold;">{{list.driverName}}</text>绑定关系？</text>
					<text>解除后将不能参与运输任务</text>
				</view>
				<u-button class="custom-style mt33 buttonLang widthButton" shape="circle" @click="confirm()"
					color="linear-gradient(to top, #F59A02, #FFD334)">
					确定
				</u-button>
			</view>
		</u-popup>
		<tooltipNew @close="driverPasswordDialogClose" :show="restPasswordShow" type="type10" :driverName="driverName"
			:driverPassword="driverPassword" :driverPhone="driverPhone">
		</tooltipNew>
		<u-overlay :show="isRestPassword">
			<view class="dialog">
				<view class="dialog-top tooltip1">
					<view class="dialog-title">
						<text>提示</text>
					</view>
				</view>
				<view class="dialog-bottom">
					<view class="textBox">
						<text>是否要进行</text>
						<text>重置密码操作</text>
					</view>
					<view class="btn-space">
						<u-button class="custom-style mt33 width184" @click="getDriverPassword"
							color="linear-gradient(to top, #F59A02, #FFD334 )" shape="circle" type="warning" text="">
							确 认
						</u-button>
						<u-button class="custom-style mt33 width184 ml40 font-color1" @click="isRestPassword = false"
							color="#ffffff" shape="circle" type="warning" text="">
							取 消
						</u-button>
					</view>
				</view>
			</view>
		</u-overlay>
	</view>
</template>

<script>
	import {
		getUserState,
		driverJb,
		driverPassword
	} from '@/api/company/user.js'
	export default {
		data() {
			return {
				inputValue: null,
				searchInputShow: false,
				userList: [],
				show: false,
				list: {},
				searchData: [],
				driverPassword: null,
				driverName: null,
				driverPhone: null,
				restPasswordShow: false,
				// 是否确认重置对话框
				isRestPassword: false,
				userId: null
			}
		},
		methods: {
			// 保存截图
			downloadFile() {
				let $this = this;
				uni.showLoading({ //加载框
					title: '保存中...',
					mask: true
				})
				var pages = getCurrentPages(); //获取当前页面信息
				var page = pages[pages.length - 1];
				var bitmap = null;
				// $this.$nextTick(()=> {
				var currentWebview = page.$getAppWebview();
				bitmap = new plus.nativeObj.Bitmap('amway_img');
				// 将webview内容绘制到Bitmap对象中
				currentWebview.draw(bitmap, function() {
					// console.log('截屏绘制图片成功');
					//这里我将文件名用四位随机数拼接了，不然会出现当前图片替换上一张图片只能保存一张图片的问题
					let rand = Math.floor(Math.random() * 10000)
					let saveUrl = '_doc/' + rand + 'a.jpg'
					bitmap.save(saveUrl, {}, function(i) {
						console.log('保存图片成功：' + JSON.stringify(i));
						uni.saveImageToPhotosAlbum({
							filePath: i.target,
							success: function() {
								// bitmap.clear(); //销毁Bitmap图片
								uni.showToast({
									title: '截图成功',
									duration: 1500
								});
							},
							complete() {
								uni.hideLoading();
							}
						});
					}, function(e) {
						console.log('保存图片失败：' + JSON.stringify(e));
					});
				}, function(e) {
					console.log('保存图片失败：' + JSON.stringify(e));
				});
				// })
			},
			// 重置密码保存图片按钮
			driverPasswordDialogClose(is) {
				if (!is) {
					this.downloadFile()
				} else {
					this.restPasswordShow = false
				}
			},
			openIsRest(item) {
				this.isRestPassword = true
				this.userId = item.userId
				this.driverName = item.driverName
				this.driverPhone = item.phone
			},
			getDriverPassword() {
				driverPassword(this.userId).then(res => {
					this.isRestPassword = false
					console.log('司机密码', res);
					this.driverPassword = res.msg
					this.restPasswordShow = true
				})
			},
			search() {
				if (this.inputValue) {
					this.searchData = this.userList.filter(item => {
						if (item.carNumber) {
							return item.carNumber.indexOf(this.inputValue) !== -1 || item.driverName.indexOf(this
								.inputValue) !== -1
						} else {
							return item.driverName.indexOf(this.inputValue) !== -1
						}
					})
				} else {
					this.getUserState()
					this.searchInputShow = false
				}
			},
			// 搜索按钮事件
			openSearchInput() {
				this.searchInputShow = true
			},
			lookInfo(item) {
				this.$tab.navigateTo('/pages/userInfo/index?id=' + item.id)
			},
			addCar() {
				this.$tab.navigateTo('/pages/addUser/index')
			},
			confirm() {
				console.log('确定了')
				this.driverJb();
			},
			driverJb() {
				driverJb(this.list.id).then(res => {
					this.show = false;
					this.getUserState();
				})
			},
			changeState(item) {
				if (item.deleteFlag == '0') {
					this.list = item;
					this.show = true;
				} else {
					this.$tab.navigateTo('/pages/userInfo/index?isInfo=true&id=' + item.id)
				}

			},
			getColor(carNumber) {
				if (carNumber) {
					return 'linear-gradient(to top, #FF2D04 0%, #FFA53E 100%)';
				} else {
					return 'linear-gradient(to top, #3FC377 0%, #83D992 100%)';
				}

			},
			getClass(state) {
				switch (state) {
					case '2':
						return 'color2';
						break;
					case '3':
					case '4':
						return 'color3';
						break;
					default:
						return 'color1';
						break;
				}

			},
			getUserState() {
				getUserState({
					pageNum: 1,
					pageSize: 10000
				}).then(res => {
					console.log(res.rows, 'res')
					// deleteFlag 0是綁定，1是解綁
					this.userList = res.rows
					this.searchData = res.rows
				})
			}
		},
		onShow() {
			this.getUserState();
		}
	}
</script>

<style lang="scss" scoped>
	// 忘记密码对话框样式
	.btn-space {
		display: flex;
		margin-bottom: 20rpx;
	}

	.dialog {
		position: fixed;
		width: 80%;
		/* height: 462rpx; */
		padding: 0 0 20rpx 0;
		background: rgba(255, 255, 255, 0.75);
		border: 2rpx solid #77A8FF;
		box-shadow: 0rpx 0rpx 24rpx 6rpx rgba(109, 160, 251, 0.6);
		border-radius: 28rpx;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}

	.dialog-top {
		position: relative;
		height: 299rpx;
		top: -86rpx;
		width: 100%;

	}

	.tooltip1 {
		background: url('@/static/images/tooltip1.png');
		background-size: 100% 100%;
	}

	.dialog-title {
		text-align: center;
		position: absolute;
		bottom: 31rpx;
		left: 50%;
		transform: translate(-50%);
		font-weight: bold;
		color: #2F4173;
		font-size: 34rpx;
	}

	.dialog-bottom {
		/* width: 542rpx; */
		/* height: 200rpx; */
		/* position: absolute; */
		/* bottom: 15rpx; */
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: -70rpx;

	}

	.textBox {
		font-size: 30rpx;
		font-weight: bold;
		color: #485B93;
		line-height: 60rpx;
		text-align: center;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.btn-right {
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.searchInput {
		margin-bottom: 25rpx;

		.searchInputStyle {
			background-color: rgba(255, 255, 255, 0.6);
			border: 2rpx solid #77A8FF !important;
			height: 38rpx !important;
		}
	}

	// 搜索按钮
	.searchBox {
		position: absolute;
		right: 36rpx;
		top: 74rpx;
		z-index: 99999;

		.custom-style3 {
			width: 60rpx;
			height: 60rpx;
		}
	}

	.colorRed {
		color: #F4644B;
	}

	.bold {
		font-weight: bold;
	}

	.iconStyle {
		/* font-size:29rpx ; */
		margin-right: 18rpx;

	}


	.fontStyle {
		font-size: 26rpx;
		font-weight: bold;
	}

	.color1 {
		color: #FBFE01;
	}

	.color2 {
		color: #FC5536;
	}

	.color3 {
		color: #148459;
	}

	.colorStyle1 {
		color: #007C3C;
	}

	.colorStyle2 {
		color: #F4644B;
	}

	.link {
		height: 1px;
		background: #E3E4E8;
		flex: 1;
		// width: 90%;
	}

	.buttonStyle2 {
		width: 140rpx;
		height: 45rpx;
		margin: inherit;
		font-size: 22rpx;
		font-weight: bold;
		color: #FFFFFF;
	}

	.centenBox {
		display: flex;
		justify-content: space-between;
	}


	.car-content-one {
		// width: 678rpx;
		padding: 18rpx 40rpx;
		display: flex;
		justify-content: space-between;
	}

	.car-content {
		display: flex;
		flex-direction: column;
		margin-bottom: 30rpx;
		margin-top: 20rpx;
	}

	.content-top {
		display: flex;
		justify-content: space-between;
	}

	.car {
		width: 60rpx;
		height: 62rpx;
		background-image: url('@/static/images/user/user.png');
		background-size: 100% 100%;
		margin-right: 26rpx;
	}

	.titleStyle {
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
		margin-right: 33rpx;
	}

	.text-part {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.stateStyle {
		text-align: center;
		line-height: 45rpx;
		width: 103rpx;
		height: 45rpx;
		border-radius: 15rpx;
		font-weight: bold;
		color: #FFFFFF;
	}


	.content-bottom {
		display: flex;
		align-items: center;
		margin-bottom: 15rpx;
	}

	.iconStyle2 {
		width: 22rpx;
		height: 22rpx;
		margin-right: 19rpx;
	}

	.icon1 {
		background-image: url('@/static/images/car/lb_01.png');
		background-size: 100% 100%;
	}

	.icon2 {
		background-image: url('@/static/images/user/phone.png');
		background-size: 100% 100%;
	}

	.icon3 {
		background-image: url('@/static/images/car/lb_03.png');
		background-size: 100% 100%;
	}

	// 弹框样式
	/deep/ .u-popup__content {
		background-color: rgba(0, 0, 0, 0);
	}

	.model {
		width: 542rpx;
		// height: 441rpx;
		background: rgba(255, 255, 255, 0.7);
		border: 2rpx solid #77A8FF;
		box-shadow: 0px 0px 24rpx 6rpx rgba(109, 160, 251, 0.6);
		border-radius: 34rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

	}

	.modelTitle {
		width: 542rpx;
		height: 80rpx;
		background: #7FB5FF;
		border-radius: 28rpx 28rpx 0px 0px;
		font-size: 28rpx;
		line-height: 80rpx;
		font-weight: bold;
		text-align: center;
		color: #FFFFFF;
		position: relative;
		margin-bottom: 66rpx;
	}

	.close {
		width: 30rpx;
		height: 30rpx;
		// border: 1px salmon solid;
		position: absolute;
		right: 25rpx;
		top: 50%;
		transform: translateY(-50%);
		background-image: url('@/static/images/close.png');
		background-size: 100% 100%;
	}

	.widthButton {
		width: 150rpx;
		margin-bottom: 53rpx;
	}

	.width184 {
		width: 184rpx;
		margin-bottom: 0;
	}

	.ml40 {
		margin-left: 40rpx;
	}

	.font-color1 {
		color: #F59A02 !important;
		border: 2rpx solid #F59A02 !important;
	}
</style>
