<template>
	<view class="page">
		<u-navbar title="驾驶证认证" :border-bottom="false" :background="bgc" back-icon-color="#262B37" title-color='#262B37'
			title-size='36' height='36' id="navbar">
		</u-navbar>
        <view class="box">
			<view class="inp">
				<view class="">
					姓名
				</view>
				<input type="text" v-model="name" placeholder="请输入姓名"/>
			</view>
			<view class="inp">
				<view class="">
					驾驶证号
				</view>
				<input type="text" v-model="no" placeholder="请输入驾驶证号"/>
			</view>
        	<view class="name">
        		请上传叉车驾驶证
        	</view>
			<view hover-class="app-tap-hover" class="one" @click="btn(1)">
				<image :src="zhengpic" mode="aspectFill"></image>
			</view>
			<view hover-class="app-tap-hover" class="one" @click="btn(2)">
				<image :src="fanpic" mode="aspectFill"></image>
			</view>
        </view>
		<view hover-class="app-tap-hover" class="tj" v-if="status == 1" style="background-color: #ccc;" @click="btntj">
			审核中
		</view>
		<view hover-class="app-tap-hover" class="tj" v-else @click="btntj">
			提交
		</view>
		<qiniu-upload-progress />
	</view>
</template>

<script>
	export default {
		data() {
			return {
                bgc: {
                	backgroundColor: "#fff",
                },
				token:'',
				zhengpic:'https://api.ccttiot.com/smartmeter/img/static/u5bmLA43Lcf8YKaiyXXW',
				fanpic:'https://api.ccttiot.com/smartmeter/img/static/ui6c1D4VlthQkYLnZ76F',
				name:'',
				no:'',
				status:3
			}
		},
		onLoad() {
           this.getQiniuToken()
		   this.getcha()
		},
		methods: {
			getcha(){
				this.$u.get(`/app/driver/last`).then(res =>{
					if(res.code == 200){
						if(res.data){
							this.name = res.data.name
							this.no = res.data.no
							this.fanpic = res.data.pictureBack
							this.zhengpic = res.data.pictureFront
							this.status = res.data.status
						}
					}
				})
			},
			btntj(){
				let data = {
					no:this.no,
					name:this.name,
					pictureFront:this.zhengpic,
					pictureBack:this.fanpic
				}
				this.$u.post("/app/driver",data).then((res) => {
					if (res.code == 200) {
						this.token = res.data
						uni.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 2000
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			getQiniuToken() {
				this.$u.get("/common/qiniuToken").then((res) => {
					if (res.code == 200) {
						this.token = res.data
					}
				})
			},
			btn(num) {
				const math = 'static/' + this.$u.guid(20)
				uni.chooseImage({
					count: 9,
					type: 'all',
					success: async (res) => {
						const filePath = res.tempFiles[0].path
						if (!this.token) {
							this.token = await this.$fetchQiniuToken()
						}
						if (!this.token) {
							uni.showToast({ title: '上传失败', icon: 'none' })
							return
						}
						try {
							const url = await this.$uploadQiniuFile({
								filePath,
								token: this.token,
								key: 'smartmeter/img/' + math,
								title: '上传驾驶证照片'
							})
							if (num == 1) {
								this.zhengpic = url
							} else {
								this.fanpic = url
							}
						} catch (e) {
							uni.showToast({ title: '上传失败', icon: 'none' })
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}
    .box{
		width: 672rpx;
		margin: auto;
		padding-bottom: 200rpx;
		box-sizing: border-box;
		.inp{
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;
			input{
				width: 530rpx;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 10rpx;
				border: 1px solid #808080;
				padding-left: 20rpx;
				box-sizing: border-box;
			}
		}
		.name{
			width: 100%;
			font-weight: 600;
			font-size: 32rpx;
			color: #3D3D3D;
			margin-top: 46rpx;
		}
		.one{
			margin-top: 48rpx;
			width: 672rpx;
			height: 478rpx;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			border: 2rpx dashed #808080;
			text-align: center;
			position: relative;
			image{
				position: absolute;
				transform: 50%;
				top: 16rpx;
				transform: translateX(-50%);
				width: 640rpx;
				height: 440rpx;
			}
		}
	}
	.tj{
		width: 680rpx;
		height: 90rpx;
		background: #4297F3;
		border-radius: 54rpx 54rpx 54rpx 54rpx;
		font-weight: 600;
		font-size: 40rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 90rpx;
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		bottom: 50rpx;
	}
</style>