<template>
	<view v-if="flag">
		<view v-if='showLoading'>
			<u-loading-page :loading="showLoading"></u-loading-page>
		</view>
		<view class="" v-else>
			<!-- 小组公司 -->
			<view class="noCar" v-if='company_type==18'>
				<!-- 申请 -->
				<view class="" v-if="step==-2">

					<u-empty text="你还没有所属车辆哦,请向镇公司申请用车或使用自有车辆并绑定系统~" icon="../../static/img/empty/data.png">
					</u-empty>
					<view>
						<mytn text='申请用车' class="upCar" :position="false" @click="applycarFn"></mytn>

					</view>
					<view class="">

						<mytn text='上传自有车辆' @click="show=true"></mytn>
					</view>
					<u-modal :show="show" @close="show=false" :showCancelButton="true" @confirm="upCarFn"
						:closeOnClickOverlay="true" @cancel="show=false" title="车辆上传">
						<view class="slot-content">
							<u--input placeholder="请输入车牌号" v-model="carLicense"></u--input>
						</view>
					</u-modal>
				</view>

				<!-- 等待 -->
				<view class="" v-if='[-3,-1,0,1,2].includes(step)'>

					<view class="loading" v-if='step==-3'>
						<span>你已提交解除申请,等待签约</span>
						<span class="dots">...</span>
					</view>

					<!-- 上传自有车辆 -->
					<view v-if="datas.type==1">
						<view class="loading">
							<span>您已上传自用车辆,系统正在审核中,请耐心等待</span>
							<span class="dots">...</span>
						</view>
					</view>
					<!-- 申请用车 -->
					<view v-else>
						<view class="loading" v-if="datas.type ==0">
							<span>你已提交用车申请，后台正在加快处理中，请耐心等待哦</span>
							<span class="dots">...</span>
						</view>
					</view>
				</view>
				<!-- 审核结束 -->
				<view class="" v-if="step==4">
					<view style="margin-top: 15vh;">
						<view class="">
							你的申请于{{datas.update_time}}被驳回
						</view>
						<view class="">
							驳回理由: {{datas.reject_message}}
						</view>
						<view class="">
							请修改相应内容重新提交哦
						</view>
					</view>
					<view class="">
						<view class="" style="margin: 20rpx 0;">

							<mytn text='上传自有车辆' @click="show=true"></mytn>
						</view>

						<view class="">


							<mytn text='申请用车' :position="false" class="upCar" @click="applycarFn"></mytn>
						</view>
						<u-modal :show="show" @close="show=false" @confirm="upCarFn" :closeOnClickOverlay="true"
							title="车辆上传">
							<view class="slot-content">
								<u--input placeholder="请输入您的车牌号" v-model="carLicense"></u--input>
								<view style="color: red;font-size: 20rpx;" v-if="noInfo">
									车牌号不能为空
								</view>
							</view>
						</u-modal>
					</view>
				</view>

				<!-- 车辆到期 -->
				<!-- 申请 -->
				<view class="" v-if="0">
					<view class="">
						你的车辆已到期,请重新签约
					</view>
					<view class="" style="margin: 20rpx 0;">
						<u-button @tap="applycarFn" type="primary" style="background-color: #0122C7;border: none;"
							class="btn">
							申请用车</u-button>
					</view>
				</view>

			</view>
			<!-- 镇街公司 -->
			<view v-if='company_type==16'>

				<view class="info">
					<text>可监管车辆数量 {{datas.monitor_num}}</text>
					<view @click="showPop=true" class="not_leased">
						<view class="">
							租赁 {{datas.rent_num}}<text v-if="datas.apply_num">(待审核:{{datas.apply_num}})</text>
						</view>
						<view class="triangle-up" v-if="datas.can_rent_num">
						</view>
						<view class="message_bottom" v-if="datas.can_rent_num">
							可再租赁车辆:{{datas.can_rent_num}}辆
						</view>
					</view>
					<text v-if="datas.self_num">自有车辆 {{datas.self_num}}</text>
				</view>
				<!-- 公告栏 -->
				<view class="application_tit" v-if="applicationList.length">
					<!-- <view class="application_tit" v-if="1"> -->
					<view class="head" @click="navgo('/subpkg/property/agencyList')">
						<text>签约申请</text>
						<view style="display: flex;" v-if="applicationList.length>3">
							<text>更多 </text>
							<u-icon name="arrow-right"></u-icon>
						</view>
					</view>
					<view class="content">
						<view class="li" @click="navgo(`/subpkg/property/chosseCompany?id=${item.id}`)"
							v-for="item,index in applicationList">
							<view class="date">
								{{item.create_time.slice(0, 10)}}
							</view>
							<view class="text">
								{{item.company_b_name}}提出租赁车辆一台
							</view>
							<view class="detail">
								<u-icon name="arrow-right"></u-icon>
							</view>
						</view>

					</view>
				</view>
				<view class="personnel_list">
					<view>
						<view class="cards" v-for="item,index in dataList" :key="index">
							<view class="cards_head">
								<text>{{item.car_license}}</text>
							</view>
							<view class="cards_content">
								<view class="right">
									<!-- 已出租 -->
									<view v-if="item.status==2"
										@click="navgo(`/subpkg/property/vehicle_a?id=${item.car_id}`)">
										<view class="li">
											<text>使用人: </text>
											<text> {{item.rent_user.user_name}}</text>
											<view style="float: right;">
												<text style="color: green;" v-if="item.type==1">自有车辆</text>
												<text style="color: orange;" v-else>租赁车辆</text>
											</view>
										</view>
										<view class="li">
											<text>联系方式: </text>
											<text>{{item.rent_user.user_phone}}</text>
										</view>
										<view class="li">
											<text>所属公司: </text>
											<text> {{item.rent_user.company_name}} </text>
										</view>
										<view class="li">
											<text>签约时间: </text>
											<text> {{item.rent_time}} </text>
										</view>
									</view>
									<!-- 未出租 -->
									<view class="" v-else>
										<view class="li">
											<text>车牌号: </text>
											<text> {{item.car_license}}</text>
											<view style="float: right;">
												<text style="color: green;" v-if="item.type==1">自有车辆</text>
												<text style="color: orange;" v-else>租赁车辆</text>
											</view>
										</view>

									</view>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view style="text-align: center;" v-if="!dataList.length">
					<u-empty v-if="!datas.monitor_num" text="你还没有可监管的车辆哦~" icon="../../static/img/empty/data.png">
					</u-empty>
					<u-empty v-else text="你还没有车辆哦，快去向平台申请用车吧" icon="../../static/img/empty/data.png"> </u-empty>
					<!-- 你还没有车辆哦，快去向平台申请用车吧 -->
				</view>
				<u-modal :show="showPop" @close="showPop=false" @confirm="confirm" :closeOnClickOverlay="true"
					:title="`请输入您需要租赁的车辆数量(上限为${datas.can_rent_num})`">
					<view class="slot-content">
						<u--input placeholder="请输入内容" type="number" v-model="newCarNum"></u--input>
						<view style="color: red;font-size: 20rpx;" v-show="novalue">

							<view class="" v-if="newCarNum>datas.can_rent_num">
								数量超过上限
							</view>
							<view class="" v-else>
								数量不能为空
							</view>
						</view>
					</view>
				</u-modal>
				<u-modal :show="showpopp" title="提示" @confirm="confirmFn" @close="showpopp=false"
					:closeOnClickOverlay="true">
					<view class="slot-content">
						<rich-text nodes="你的申请已提交"></rich-text>
					</view>

				</u-modal>
			</view>
		</view>
		<mytn text='合同列表' v-if="datas.rent_num" @click="navgo('/subpkg/property/contractList')"></mytn>
	</view>
</template>

<script>
	import {
		getvehicleListApi,
		getAgencyListApi,
		leaseApi,
		upVehicleApi,
		getAgencyDetailApi,
		getvehicleDetailApi,
		villageCompanyApi
	} from "@/api/property.js"
	import Cache from '@/utils/cache';
	// import mybtn from "@/components/mybtn/mybtn.vue"
	import {
		Toast
	} from "../../libs/uniApi";
	import mytn from "@/components/mybtn/mybtn.vue"
	// Toast("请至少选择一辆车")
	export default {
		components: {
			mytn
		},
		name: "vehicle",
		data() {
			return {
				novalue: false,
				noInfo: false,
				aaa: 0,
				showLoading: true,
				company_type: "",
				// 1上传2审核3审核结束
				step: 1,
				applicationList: [1],
				show: false,
				carLicense: "",
				showPop: false,
				datas: {},
				dataList: [],
				newCarNum: "",
				newcarTit: "",
				flag: false,
				showpopp: false,



			};
		},

		onLoad() {
			let type = (JSON.parse(Cache.get("USER_INFO")).company.company_type)
			this.company_type = type
			// 小组公司
			if (type == 18) {
				// getvehicleDetailApi().then(res => {})
				villageCompanyApi().then(res => {
					this.showLoading = false
					this.datas = res.data
					this.step = res.data.status
					switch (this.step) {
						case 3:
							uni.redirectTo({
								url: `/subpkg/property/vehicle_a?id=${res.data.cars_info.id}`
							})
							// this.step =
							break;
						default:
							break;
					}

				})

			}
			// 镇街公司
			else {
				getvehicleListApi().then(res => {
					this.datas = res.data
					res.data.apply.length >= 2 ? this.applicationList = res.data.apply : this.applicationList = res
						.data.apply.slice(0, 2)
					// this.applicationList = res.data.apply
					this.dataList = res.data.car_list
					this.showLoading = false
				})
				// getAgencyListApi().then(res => {
				// 	this.applicationList = res.data
				// })
			}
			// if (true) {
			// 	uni.navigateTo({
			// 		url: '/subpkg/property/vehicle_a'
			// 	})
			// }
			// getvehicleListApi().then(res => {
			// 	console.log(res)
			// 	// this.getvehicleList = res
			// })
			// getAgencyListApi().then(res => {
			// 	// this.applicationList=res

			// })
			setTimeout(() => {
				this.flag = true
			})
		},



		methods: {
			confirmFn() {
				uni.reLaunch({
					url: '/subpkg/property/index' // 替换为需要刷新的页面路径
				});
			},
			test() {
				this.aaa = 1;
				this.showpopp = true;
				// uni.reLaunch({
				// 	url: '/subpkg/property/index' // 替换为需要刷新的页面路径
				// });
			},
			// 申请车辆
			applycarFn() {
				Toast("操作成功")
				this.step = -1
				return
				upVehicleApi().then(res => {
					Toast("操作成功")
					this.step = -1
				})
			},
			// 自有车辆
			upCarFn() {
				if (!this.carLicense) {
					this.noInfo = true
					return
				}
				upVehicleApi({
					license: this.carLicense
				}).then(res => {
					this.step = 2
					Toast("上传成功")
				})

				this.show = false
				this.noInfo = false

			},
			signFn() {
				this.toDetail("/subpkg/property/chosseCompany")
			},
			confirm() {
				if (!this.newCarNum || this.newCarNum > this.datas.can_rent_num) {
					this.novalue = true
					return
				}
				this.showPop = false
				leaseApi({
					num: Number(this.newCarNum)
				}).then(res => {
					this.newCarNum = ""
					// Toast("操作成功")
					this.showpopp = true
				})
			},

			navgo(url) {
				uni.navigateTo({
					url
				})
			},

		}
	}
</script>

<style lang="scss">
	.dots {
		animation: loadingDots 1s infinite;
	}

	@keyframes loadingDots {
		0% {
			opacity: 0;
		}

		25% {
			opacity: 0;
		}

		50% {
			opacity: 1;
		}

		75% {
			opacity: 1;
		}

		100% {
			opacity: 0;
		}
	}

	.noCar {
		padding: 100rpx;
		margin-top: 10vh;
		// word-wrap: break-word;
		// overflow-wrap: break-word;
		// position: absolute;
		// width: 80vw;
		// height: auto;
		// position: absolute;
		// top: 40vh;
		// left: 50vw;
		// transform: translate(-50%, -50%);


	}

	.upCar {
		position: fixed;
		bottom: 110rpx;
		left: 0;
		// z-index: 9999999;
		z-index: 1;
		width: 100%;
		border-radius: 0;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		background-color: $theme-oa-color;
	}

	.info {
		display: flex;
		justify-content: space-around;
		margin: 10rpx 0 0 0;

		.not_leased {
			display: flex;
			align-items: center;
			// background-color: red;
			flex-direction: column;
		}

		.triangle-up {
			width: 0;
			height: 0;
			border-left: 5px solid transparent;
			border-right: 5px solid transparent;
			border-bottom: 10px solid rgba(0, 0, 0, .5);
		}

		.message_bottom {
			background-color: rgba(0, 0, 0, .5);
			color: white;
			// transform: translateX();
			padding: 20rpx;
			border-radius: 30rpx;
		}
	}

	.application_tit {
		background-color: white;
		padding: 20rpx;
		width: 694rpx;
		border-radius: 20rpx;
		margin: 20rpx auto 0;

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

		.content {
			.li {
				margin: 10rpx 0;
				display: flex;
				justify-content: space-between;

				.date {
					background-color: #FF7C32;
					color: white;
					padding: 0 10rpx;
					border-radius: 15rpx // width: 170rpx;
				}

				.text {
					// background-color: lightyellow;
					max-width: 400rpx;
					white-space: nowrap;

					/* 让文本不换行 */
					overflow: hidden;
					/* 隐藏溢出的文本 */
					text-overflow: ellipsis;
				}

				.detail {
					// background-color: black;
					// width: 50rpx;
				}
			}



		}
	}



	.personnel_list {
		padding: 28rpx 0;
		margin-bottom: 130rpx;

		.empty {
			padding-top: 200rpx;
			text-align: center;

			.btn {
				width: 30vw;
				margin-top: 10rpx;
				margin: 10rpx auto;

			}

			// height: 80vh;
			// background-color: red;
		}

		.cards {
			margin: 0 auto;
			margin-bottom: 28rpx;
			width: 694rpx;
			background: #ffffff;
			border-radius: 30rpx;
			opacity: 1;
			overflow: hidden;
			// box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6);

			.cards_head {
				padding: 28rpx;
				background-color: $theme-oa-color;
				color: white;
				height: 100rpx;
				display: flex;
				justify-content: space-between;
			}

			.cards_content {
				padding: 10rpx 28rpx;
				display: flex;
				justify-content: space-between;

				.left {
					text-align: center;
					// padding: 15rpx 25rpx;
					padding-top: 20rpx;

					width: 160rpx;
					background-color: $theme-oa-color;
					color: white;
					border-radius: 40rpx;

					.cir {
						width: 120rpx;
						height: 120rpx;
						background-color: #ffffff;
						margin-left: 25rpx;
						border-radius: 120rpx;
						position: relative;
					}
				}

				.right {
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.li {
						margin: 10rpx 0;
						width: 85vw;

						.check_box {
							// background-color: #ff0000;
							// color: red;
						}


						text:nth-child(1) {
							margin-right: 20rpx;
						}
					}
				}
			}


		}
	}
</style>