<template>
	<view class="container">
		<image class="cbg" src="../../static/common_bg.png"></image>
		<view class="wrap">
			<view>{{courseInfo.name}}</view>
			
			<view v-if="fee_status==1">
				您已缴费成功。
			</view>
			<view v-else>
				上传缴费图片：
				<u-button type="primary" size="mini" @click="uploadFile">上传</u-button>
				<!-- <u-upload :header='header' 
				@on-success="uploadSuccess"
				:action="action" :file-list="fileList" max-count="1"></u-upload> -->
			</view>
			<view v-if="hasFeeFiles">
				<image :src="info.fee_files[0].url"></image>
			</view>
		</view>
		
		
		<view class="modal">
			<u-popup v-model="showPark" mode="bottom">
				<view class="modal-tip">绑定车牌号</view>
				<view class="modal-content">
					<view>
						车牌1：<u-input type="idcard" v-model="plate1" />
					</view>
					<view>
						车牌2：<u-input type="idcard" v-model="plate2" />
					</view>
				</view>
				
				<view class="modal-btn">
					<u-button type="primary" @click="updatePlate">绑定</u-button>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import {
		ROOTPATH as baseUrl
	} from "@/common/config.js"
	export default {
		components: {
		},
		data() {
			return {
				hasFeeFiles:false,
				course_id:'',
				info:{},
				userInfo:{},
				courseInfo:{},
				header:{},
				action:`${baseUrl}/api/mobile/upload-file`,
				fileList:{},
				showPark:false,
				plate1:'',
				plate2:''
			}
		},
		onLoad(options) {
			this.course_id = options.id
			let token = uni.getStorageSync('stbc_lifeData') ? uni.getStorageSync('stbc_lifeData').vuex_token : ''
			this.header.Authorization = `Bearer ${token}`
			
			this.getCourseDetail(options.id)
		},
		methods:{
			async getCourseDetail(id) {
				
				const res = await this.$u.api.courseGetSign({
					course_id: id
				})
				this.info = res.detail
				this.userInfo = res.detail.user
				this.courseInfo = res.detail.course
				if(res.detail.fee_files.length>0){
					this.hasFeeFiles = true
				}else{
					this.hasFeeFiles = false
				}
			},
			uploadFile(){
				uni.chooseImage({
					success: (chooseImageRes) => {
						console.log("chooseImageRes",chooseImageRes)
						const tempFilePaths = chooseImageRes.tempFilePaths;
						if(chooseImageRes.tempFilePaths && chooseImageRes.tempFilePaths.length>1){
							this.base.toast("缴费图片只能上传1张")
							return
						}
						uni.uploadFile({
							url: this.action, //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							fileType:'image',
							name: 'file',
							header:this.header,
							success: (uploadFileRes) => {
								console.log(uploadFileRes.data);
								
								this.fileList = {}
								this.fileList = JSON.parse(uploadFileRes.data)
								console.log("this.fileList ",this.fileList )
								this.updateSign()
							},
							fail(err){
								console.log("err",err)
								this.base.toast("上传失败")
							},
						});
					}
				});
			},
			updateSign(){
				let files_id = [this.fileList.id]
				this.$u.api.courseUpdateSign({
					id:this.info.id,
					fee_file_ids:files_id
				}).then(res=>{
					this.base.toast("上传成功")
					// 有缴费图片，证明不是第一次上传，不弹出车位绑定
					if(this.hasFeeFiles){
						this.getCourseDetail(this.course_id)
					}else{
						// 第一次 且 没有车牌号  弹出
						if(!this.base.isNull(this.userInfo.plate)){
							this.showPark = true
						}
					}
					
				})
			},
			updatePlate(){
				if(this.base.isNull(this.plate1) && this.base.isNull(this.plate2)){
					this.base.toast('请输入至少一个车牌')
					return
				}
				
				this.$u.api.saveUser({
					id:this.userInfo.id,
					plate:this.plate1+','+this.plate2
				}).then(res=>{
					this.base.toast("绑定车牌成功")
					this.$u.api.user().then(res => {
						this.$u.vuex('vuex_user', res.user)
						this.showPark = false
					})
				})
			}
			
		}

	}
</script>

<style scoped lang="scss">
	.container {
		padding: 30rpx;
		width: 100%;
		height: 100vh;
		overflow: scroll;
		.cbg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
		}

		.wrap {
			position: relative;
			&>view{
				margin:20rpx 0;
			}
		}
	}
</style>