//车队管理
<template>
	<view class="center">
		<view class="fleetInfo">
			<view class="fleetInfo_name">队长身份</view>
			<view class="fleetInfo_phone" v-if="fleetInfo.status">{{fleetInfo.status}}</view>
			<view class="fleetInfo_phone" v-else>去认证</view>
		</view>
		<view class="btn_css">
			<view class="btn_item" v-if="!fleetInfo.name">
				<u-button type="primary" text="去认证" @click="operation(1)"></u-button>
			</view>
			<view class="btn_item" v-else>
				<u-button type="primary" text="编辑" @click="operation(2)"></u-button>
			</view>
		</view>
		<u-divider></u-divider>
		<view class="tips_info">
			<span>绑定司机</span>
			<image class='yanjingicon' @click="changeStatus(1)" v-if="isShow == 2" src="../../../static/yioncang.png">
			</image>
			<image class='yanjingicon' @click="changeStatus(2)" v-if="isShow == 1" src="../../../static/xianshi.png">
			</image>
			<!-- <u--image :showLoading="true" src="../../../static/yioncang.png" width="80px" height="80px" @click="click"></u--image> -->
		</view>
		<view class="list_css" v-for="(item,index) in formList">
			<view class="list_left">{{item.driverName}}({{item.accountNumber}})</view>
			<view class="list_right">
				<view class="list_text" v-if="item.status == '待确认'" @click="audit(item,1)">拒绝</view>
				<view class="list_text" v-if="item.status == '待确认'" @click="audit(item,2)">接受</view>
				<view class="list_text"
					v-if="item.status == '已绑定'||item.status == '已解绑'||item.status == '已接受'||item.status == '已拒绝'">
					{{item.status}}</view>
				<view class="list_text"
					v-if="item.status == '已绑定'||item.status == '已解绑'||item.status == '已接受'||item.status == '已拒绝'"
					@click="next(item)">删除</view>
			</view>
		</view>
		<view>
			<u-loadmore :status="loadStatus" />
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-modal :show="tipsShow" :title="alertTitle" :closeOnClickOverlay='true' :showCancelButton='true'
			confirmColor='#2772FB' @confirm="$u.throttle(confirmClick, 5000)" @close="cancelClick"
			@cancel="cancelClick"></u-modal>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				//分页
				pageSize: 20,
				currentPage: 1,
				fleetInfo: {},
				tipsShow: false,
				alertTitle: "",
				formList: [],
				distinguish: "",
				id: "",
				sign: "",
				isShow: 1,
				isLoadMore: false, //是否加载中
				loadStatus: 'loading', //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
			}
		},
		onLoad() {
			this.getInfo()
		},
		onShow() {},
		//下拉刷新
		onPullDownRefresh() {
			this.currentPage = 1
			this.isLoadMore = false
			this.loadStatus = 'loading'
			this.nextGetInfo()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() { //上拉触底函数
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.isLoadMore = true
				this.currentPage += 1
				this.nextGetInfo()
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		onNavigationBarButtonTap(e) {
			if (this.fleetInfo.name) { //判断是否有队长身份
				this.$u.route("/pages/mine/fleet/fleetBill")
			} else {
				this.$refs.uToast.show({
					type: 'error',
					message: "您还没有认证车队长身份！",
					complete() {
						that.getInfo()
					}
				})
			}
		},
		methods: {
			changeStatus(val) {
				this.isShow = val
				if (val == 1) {
					if (this.fleetInfo.name) { //判断是否有队长身份
						this.currentPage = 1
						this.nextGetInfo()
					}
				} else if (val == 2) {
					this.formList = []
				}
			},
			solution() {
				let that = this
				uni.showLoading({
					mask: true,
					title: '加载中'
				})
				this.tipsShow = false
				this.$request.baseRequest('post', '/hyBindCarCaptainInfo/api/deleteBind', {
					id: this.id
				}).then(res => {
					uni.hideLoading()
					if (res.code == 200) {
						this.$refs.uToast.show({
							type: 'success',
							message: "删除成功！",
							complete() {
								that.getInfo()
							}
						})
					} else {
						uni.hideLoading()
						uni.$u.toast(res.message);
					}
				}).catch(res => {
					uni.hideLoading()
					uni.$u.toast(res.message);
				});
			},
			next(obj) {
				this.tipsShow = true
				this.alertTitle = "确定删除" + obj.driverName + "信息？"
				this.id = obj.id
				this.sign = "删除"
			},
			audit(obj, val) {
				if (val == 1) {
					this.alertTitle = "确定拒绝" + obj.driverName + "加入车队？"
					this.distinguish = "2"
				} else if (val == 2) {
					this.alertTitle = "确定接受" + obj.driverName + "加入车队？"
					this.distinguish = "1"
				}
				this.id = obj.id
				this.tipsShow = true
			},
			operation(val) {
				if (val == 1) {
					uni.$u.route('/pages/mine/fleet/carCaptainAuthentication');
				} else {
					uni.$u.route('/pages/mine/fleet/carCaptainAuthentication', {
						data: JSON.stringify(this.fleetInfo),
					});
				}
			},
			leave() {
				this.tipsShow = true
			},
			cancelClick() {
				this.tipsShow = false
			},
			confirmClick() {
				let that = this
				if (this.sign == "删除") { //删除操作
					this.solution()
				} else {
					uni.showLoading({
						mask: true,
						title: '加载中'
					})
					this.tipsShow = false
					this.$request.baseRequest('post', '/hyBindCarCaptainInfo/api/examine', {
							id: this.id,
							examineFlag: this.distinguish
						}).then(res => {
							if (res.code == 200) {
								uni.hideLoading()
								if (this.distinguish == 1) {
									this.$refs.uToast.show({
										type: 'success',
										message: "接受成功！",
										complete() {
											that.getInfo()
										}
									})
								} else {
									this.$refs.uToast.show({
										type: 'success',
										message: "拒绝成功！",
										complete() {
											that.getInfo()
										}
									})
								}
							} else {
								uni.hideLoading()
								uni.$u.toast(res.message);
							}
						})
						.catch(res => {
							uni.hideLoading()
							uni.$u.toast(res.message);
						});
				}

			},
			getInfo() {
				this.$request.baseRequest('get', '/hyCarCaptainInfo/getCarCaptain', {
						commonId: this.userInfo.id
					}).then(res => {
						if (res.code == 200) {
							if (res.data) {
								this.fleetInfo = res.data
								this.nextGetInfo()
							}else{
								this.isLoadMore = true
								this.loadStatus = 'nomore'
							}
						} else {
							uni.$u.toast(res.message);
						}
					})
					.catch(res => {
						uni.$u.toast(res.message);
					});

			},
			nextGetInfo() {
				//车队人员
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				this.$request.baseRequest('get', '/hyBindCarCaptainInfo/api/selectPage', {
						carCaptainCommonId: this.userInfo.id,
						pageSize: this.pageSize,
						currentPage: this.currentPage
					}).then(res => {
						if (res.code == 200) {
							// this.formList = res.data.records
							if (res.data.records.length > 0) {
								this.isLoadMore = false
								this.loadStatus = 'loadmore'
							} else {
								this.isLoadMore = true
								this.loadStatus = 'nomore'
							}
							if (this.currentPage == 1) {
								this.formList = res.data.records
								if (res.data.records.length < 20) {
									this.loadStatus = 'nomore'
								}
							} else {
								this.formList = this.formList.concat(res.data.records)
							}
							uni.hideLoading()
						} else {
							uni.hideLoading()
						}
					})
					.catch(res => {
						uni.hideLoading()
						uni.$u.toast(res.message);
					});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.center {
		padding: 20rpx 30rpx;

	}

	.fleetInfo {
		display: flex;
		width: 100%;
		margin-top: 30rpx;

		.fleetInfo_name {
			width: 50%;
		}

		.fleetInfo_phone {
			width: 50%;
			text-align: right;
		}
	}

	.btn_css {
		display: flex;
		margin-top: 30rpx;

		.btn_item {
			width: 120rpx;
			margin-right: 20rpx;
		}
	}

	.list_css {
		display: flex;
		margin: 30rpx 0;

		.list_left {
			width: 50%;
		}

		.list_right {
			display: flex;
			justify-content: flex-end;
			width: 50%;

			.list_text {
				margin: 0 10rpx;
			}
		}
	}

	.tips_info {
		margin: 20rpx 0;

		.yanjingicon {
			margin: 0 20rpx;
			width: 22px;
			height: 22px;
			top: 10rpx
		}
	}
</style>
