<template>
	<view class="info">
		<u-top-tips ref="uTips"></u-top-tips>
		<u-form :model="form" ref="uform" label-width="150">
			<u-form-item label="车辆类型" prop="name" :required="true">
				<u-input type="select" v-model="form.name" @click="show = true" placeholder="请选择车辆类型"></u-input>
				<u-select v-model="show" mode="single-column" :list="list" @confirm="confirm"></u-select>
			</u-form-item>
			<u-form-item label="上传图片" prop="image" :required="true">
				<u-upload
				:file-list="fileList"
				:action="action"
				:header="header"
				@on-success="uploaded"
				@on-error="uploaded"
				@on-remove="remove"
				max-count="1"></u-upload>
			</u-form-item>
		</u-form>
		<view class="btn">
			<u-button hover-class="none" shape="circle" :custom-style="btnStyle" @click="save">保存</u-button>
		</view>
	</view>
</template>

<script>
	import {apiBaseUrl} from '../../../api/request.js'
	import {loading,hidden,toast} from '../../../tips/myTips.js'
	import {getCarList,edit,getUrl} from '../../../api/apis.js'
	export default {
		onLoad(){
			this.init()
			uni.$u.mpShare.title = '页面分享';
		},
		onReady(){
			this.$refs.uform.setRules(this.rules)
		},
		data(){
			return {
				btnStyle:{
					background:"#7D5AFC",
					color:'#FFFFFF'
				},
				show:false,
				carInfo:[],
				form:{
					name:'',
					image:''
				},
				rules:{
					name:[
						{
							required:true,
							message: '请选择车辆类型',
							trigger: 'blur,change'
						}
					],
					image:[
						{
							required:true,
							message: '请上传图片',
							trigger: 'blur,change'
						}
					]
				},
				fileList:[],
				header:{test:11},
				params:{
					IsDeleteEntry:true,
					Model:{
						FID:this.$store.state.userInfo.Id,
						FCarsEntity:[
							{
								FCarsid:{
									FNumber:''
								},
								//证书的fileid
								FAttachmentCars:''
							}
						]
					}
				},
				
			}
		},
		methods:{
			init(){
				let that = this
				let userInfo = this.$store.state.userInfo
				loading()
				if(userInfo.CarsEntity.length !== 0){
					console.log(userInfo.CarsEntity[0])
					this.form.name = userInfo.CarsEntity[0].FCarsid.FDataValue[0].Value
					this.form.image = userInfo.CarsEntity[0].FAttachmentCars
					this.params.Model.FCarsEntity[0].FCarsid.FNumber = userInfo.CarsEntity[0].FCarsid.FNumber
					this.params.Model.FCarsEntity[0].FAttachmentCars = userInfo.CarsEntity[0].FAttachmentCars
					loading()
					getUrl(userInfo.CarsEntity[0].FAttachmentCars).then((res) => {
						hidden()
						if(res.data.code == 0){
							console.log("获取车辆图片:",res)
							let tempObj = {url:''}
							tempObj.url = res.data.result[0].fileurl
							that.fileList.push(tempObj)
						}else{
							console.log("获取车辆图片失败:",res)
						}
					})
				}
				getCarList().then((res) => {
					hidden()
					if(res.data.code == 0){
						console.log("车辆信息:",res)
						that.carInfo = res.data.result
					}else{
						console.log("获取车辆信息失败:",res)
					}
				})
			},
			confirm(res){
				this.params.Model.FCarsEntity[0].FCarsid.FNumber = res[0].value
				this.form.name = res[0].label
			},
			uploaded(data, index, lists, name){
				if(data.code == 100){
					console.log("上传失败:",data)
					this.$refs.uTips.show({
						title:data.message,
						type:'error'
					})
					lists.splice(index,1)
				}
				if(data.code == 0){
					console.log("上传成功:",data)
					this.params.Model.FCarsEntity[0].FAttachmentCars = data.result.fileid
					this.form.image = data.result.fileid
				}
			},
			remove(){
				this.fileList = []
				this.params.Model.FCarsEntity[0].FAttachmentCars = ''
				this.form.image = ''
			},
			save(){
				this.$refs.uform.validate(valid => {
					if(valid){
						console.log('验证通过')
						console.log(this.params)
						loading()
						edit(this.params).then((res) => {
							hidden()
							if(res.data.code == 0){
								toast('修改成功')
								uni.navigateBack()
							}else{
								toast('修改失败')
								uni.navigateBack()
							}
						})
					}else{
						console.log('验证失败')
					}
				})
			},
		},
		computed:{
			list:function(){
				let tempArray = []
				for(let i in this.carInfo){
					let tempObj = {value:'',label:''}
					tempObj.value = this.carInfo[i].FNUMBER
					tempObj.label = this.carInfo[i].fname
					tempArray.push(tempObj)
				}
				return tempArray
			},
			action:function(){
				let url = apiBaseUrl+'/api/UploadAttachments'
				return url
			}
		}
	}
</script>

<style>
	.info{
		padding: 5%;
	}
</style>
